The Write.as desktop (GUI) app for macOS.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.4 KiB

  1. //
  2. // ViewController.swift
  3. // writeas
  4. //
  5. // Created by Matt Baer on 12/29/15.
  6. // Copyright © 2015 A Bunch Tell. All rights reserved.
  7. //
  8. import Cocoa
  9. class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCenterDelegate {
  10. @IBOutlet var writerText: NSTextView!
  11. var isNight = false
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. loadDocument()
  15. writerText.font = NSFont(name: Preferences.getFont().typeface, size: Preferences.getFontSize())
  16. writerText.isHorizontallyResizable = false
  17. writerText.textContainerInset = NSSize(width: 16, height: 16)
  18. writerText.isAutomaticQuoteSubstitutionEnabled = false
  19. writerText.isAutomaticDashSubstitutionEnabled = false
  20. NSUserNotificationCenter.default.delegate = self
  21. // Do any additional setup after loading the view.
  22. writerText.delegate = self
  23. }
  24. override func viewWillAppear() {
  25. super.viewWillAppear()
  26. self.view.window!.titlebarAppearsTransparent = true
  27. self.view.window!.isMovableByWindowBackground = true
  28. self.view.window!.titleVisibility = .hidden
  29. toggle(isNight: isNight)
  30. }
  31. override func viewDidAppear() {
  32. super.viewDidAppear()
  33. configureWindow()
  34. // Style the window
  35. self.view.window!.title = "Write.as"
  36. }
  37. func configureWindow() {
  38. // Fit textview to window size
  39. writerText.setFrameSize(NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height))
  40. }
  41. override var representedObject: Any? {
  42. didSet {
  43. // Update the view, if already loaded.
  44. }
  45. }
  46. fileprivate func getDraftPath() -> URL? {
  47. return URL(fileURLWithPath: Constants.draftFile)
  48. }
  49. func saveDocument() {
  50. // Save whatever's written
  51. let path = getDraftPath()
  52. if path == nil {
  53. return
  54. }
  55. //writing
  56. do {
  57. try writerText.textStorage!.string.write(to: path!, atomically: false, encoding: .utf8)
  58. } catch (let err) {
  59. print("ERROR writing: \(err)")
  60. }
  61. }
  62. func loadDocument() {
  63. let path = getDraftPath()
  64. if path == nil {
  65. return
  66. }
  67. do {
  68. let draftText = try String(contentsOf: path!, encoding: .utf8)
  69. writerText.string = draftText
  70. } catch (let err) {
  71. print("ERROR loading: \(err)")
  72. }
  73. }
  74. func embolden() {
  75. formatText(with: "**\(getSelectedText())**", offset: 2)
  76. }
  77. func emphasize() {
  78. formatText(with: "_\(getSelectedText())_", offset: 1)
  79. }
  80. func addLink() {
  81. let text = getSelectedText()
  82. formatText(with: "[\(text)](http://)", offset: text == "" ? 1 : (text.characters.count + 10), alwaysOffset: true)
  83. }
  84. fileprivate func getSelectedText() -> String {
  85. let selectedRange = writerText.selectedRange()
  86. return (self.writerText.textStorage?.string as! NSString).substring(with: selectedRange)
  87. }
  88. fileprivate func formatText(with: String, offset: Int) {
  89. formatText(with: with, offset: offset, alwaysOffset: false)
  90. }
  91. fileprivate func formatText(with: String, offset: Int, alwaysOffset: Bool) {
  92. let selectedRange = writerText.selectedRange()
  93. let selString = getSelectedText()
  94. if self.writerText.shouldChangeText(in: selectedRange, replacementString: with) {
  95. self.writerText.replaceCharacters(in: NSRange(location: selectedRange.location, length: selectedRange.length), with: with)
  96. self.writerText.didChangeText()
  97. if selString == "" || alwaysOffset {
  98. writerText.setSelectedRange(NSRange(location: selectedRange.location + offset, length: 0))
  99. }
  100. }
  101. }
  102. func publish() {
  103. saveDocument()
  104. DispatchQueue.global(qos: .background).async {
  105. let task = Process()
  106. task.launchPath = Bundle.main.resourcePath! + "/writeas"
  107. task.arguments = ["--font", Preferences.getFont().rawValue]
  108. let pipe = Pipe()
  109. task.standardInput = pipe
  110. task.launch()
  111. let fh: FileHandle = pipe.fileHandleForWriting
  112. fh.write(self.writerText.textStorage!.string.data(using: .utf8)!)
  113. fh.closeFile()
  114. task.waitUntilExit()
  115. DispatchQueue.main.async {
  116. if let ad = NSApplication.shared().delegate as? AppDelegate {
  117. ad.filePublishItem.isEnabled = true
  118. }
  119. var notification = NSUserNotification()
  120. if task.terminationStatus == 0 {
  121. // Successfully published
  122. notification.title = "Published!"
  123. notification.informativeText = "The link is copied — press ⌘+V to share it."
  124. } else {
  125. // Something went wrong
  126. notification.title = "Unable to publish."
  127. notification.informativeText = "There was a problem publishing. Please check your internet and try again."
  128. }
  129. notification.soundName = NSUserNotificationDefaultSoundName
  130. NSUserNotificationCenter.default.deliver(notification)
  131. }
  132. }
  133. }
  134. func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
  135. return true
  136. }
  137. func toggle(isNight: Bool) {
  138. let darkBG = NSColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
  139. if isNight {
  140. self.view.window!.backgroundColor = darkBG
  141. self.writerText.backgroundColor = darkBG
  142. self.writerText.textColor = NSColor.white
  143. } else {
  144. self.view.window!.backgroundColor = NSColor.white
  145. self.writerText.backgroundColor = NSColor.white
  146. self.writerText.textColor = NSColor.black
  147. }
  148. self.isNight = isNight
  149. }
  150. func adjustTextSize(increment: Bool) {
  151. var size = Preferences.getFontSize()
  152. if increment {
  153. size += 2
  154. } else {
  155. size -= 1
  156. }
  157. self.writerText.font = NSFont(name: Preferences.getFont().typeface, size: size)
  158. UserDefaults.standard.set(String(Int(size)), forKey: "editor_text_size")
  159. configureWindow()
  160. }
  161. func setFont(_ font: Preferences.PostFont) {
  162. self.writerText.font = NSFont(name: font.typeface, size: Preferences.getFontSize())
  163. }
  164. }