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.

158 lines
4.9 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. let draftFile = "Write.as/draft.txt"
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. loadDocument()
  15. writerText.font = NSFont(name: "Lora", 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 viewDidAppear() {
  25. super.viewDidAppear()
  26. configureWindow()
  27. // Style the window
  28. self.view.window!.title = "Write.as"
  29. self.view.window!.titlebarAppearsTransparent = true
  30. self.view.window!.isMovableByWindowBackground = true
  31. self.view.window!.titleVisibility = .hidden
  32. self.view.window!.backgroundColor = NSColor.white
  33. }
  34. func configureWindow() {
  35. // Fit textview to window size
  36. writerText.setFrameSize(NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height))
  37. }
  38. override var representedObject: Any? {
  39. didSet {
  40. // Update the view, if already loaded.
  41. }
  42. }
  43. fileprivate func getDraftPath() -> URL? {
  44. do {
  45. let baseDir = try FileManager.default.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: true)
  46. return baseDir.appendingPathComponent(draftFile)
  47. } catch (let err) {
  48. print("ERROR getting path: \(err)")
  49. return nil
  50. }
  51. }
  52. func saveDocument() {
  53. // Save whatever's written
  54. let path = getDraftPath()
  55. if path == nil {
  56. return
  57. }
  58. //writing
  59. do {
  60. try writerText.textStorage!.string.write(to: path!, atomically: false, encoding: .utf8)// .write(toFile: path.absoluteString, atomically: true, encoding: .utf8)
  61. } catch (let err) {
  62. print("ERROR writing: \(err)")
  63. }
  64. }
  65. func loadDocument() {
  66. let path = getDraftPath()
  67. if path == nil {
  68. return
  69. }
  70. do {
  71. let draftText = try String(contentsOf: path!, encoding: .utf8)
  72. writerText.string = draftText
  73. } catch (let err) {
  74. print("ERROR loading: \(err)")
  75. }
  76. }
  77. func publish() {
  78. saveDocument()
  79. DispatchQueue.global(qos: .background).async {
  80. let task = Process()
  81. task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas"
  82. task.arguments = ["--font", Preferences.getFont().rawValue]
  83. let pipe = Pipe()
  84. task.standardInput = pipe
  85. task.launch()
  86. let fh: FileHandle = pipe.fileHandleForWriting
  87. fh.write(self.writerText.textStorage!.string.data(using: .utf8)!)
  88. fh.closeFile()
  89. task.waitUntilExit()
  90. DispatchQueue.main.async {
  91. var notification = NSUserNotification()
  92. // All these values are optional
  93. notification.title = "Published!"
  94. notification.informativeText = "The link is copied — press ⌘+V to share it."
  95. notification.soundName = NSUserNotificationDefaultSoundName
  96. NSUserNotificationCenter.default.deliver(notification)
  97. }
  98. }
  99. }
  100. func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
  101. return true
  102. }
  103. func toggle(isNight: Bool) {
  104. let darkBG = NSColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
  105. if isNight {
  106. self.view.window!.backgroundColor = darkBG
  107. self.writerText.backgroundColor = darkBG
  108. self.writerText.textColor = NSColor.white
  109. } else {
  110. self.view.window!.backgroundColor = NSColor.white
  111. self.writerText.backgroundColor = NSColor.white
  112. self.writerText.textColor = NSColor.black
  113. }
  114. }
  115. func adjustTextSize(increment: Bool) {
  116. var size = Preferences.getFontSize()
  117. if increment {
  118. size += 2
  119. } else {
  120. size -= 1
  121. }
  122. self.writerText.font = NSFont(name: "Lora", size: size)
  123. UserDefaults.standard.set(String(Int(size)), forKey: "editor_text_size")
  124. configureWindow()
  125. }
  126. func setFont(_ font: Preferences.PostFont) {
  127. self.writerText.font = NSFont(name: font.typeface, size: Preferences.getFontSize())
  128. }
  129. }