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.

161 lines
5.0 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. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. loadDocument()
  14. writerText.font = NSFont(name: Preferences.getFont().typeface, size: Preferences.getFontSize())
  15. writerText.isHorizontallyResizable = false
  16. writerText.textContainerInset = NSSize(width: 16, height: 16)
  17. writerText.isAutomaticQuoteSubstitutionEnabled = false
  18. writerText.isAutomaticDashSubstitutionEnabled = false
  19. NSUserNotificationCenter.default.delegate = self
  20. // Do any additional setup after loading the view.
  21. writerText.delegate = self
  22. }
  23. override func viewDidAppear() {
  24. super.viewDidAppear()
  25. configureWindow()
  26. // Style the window
  27. self.view.window!.title = "Write.as"
  28. self.view.window!.titlebarAppearsTransparent = true
  29. self.view.window!.isMovableByWindowBackground = true
  30. self.view.window!.titleVisibility = .hidden
  31. self.view.window!.backgroundColor = NSColor.white
  32. }
  33. func configureWindow() {
  34. // Fit textview to window size
  35. writerText.setFrameSize(NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height))
  36. }
  37. override var representedObject: Any? {
  38. didSet {
  39. // Update the view, if already loaded.
  40. }
  41. }
  42. fileprivate func getDraftPath() -> URL? {
  43. return URL(fileURLWithPath: Constants.draftFile)
  44. }
  45. func saveDocument() {
  46. // Save whatever's written
  47. let path = getDraftPath()
  48. if path == nil {
  49. return
  50. }
  51. //writing
  52. do {
  53. try writerText.textStorage!.string.write(to: path!, atomically: false, encoding: .utf8)
  54. } catch (let err) {
  55. print("ERROR writing: \(err)")
  56. }
  57. }
  58. func loadDocument() {
  59. let path = getDraftPath()
  60. if path == nil {
  61. return
  62. }
  63. do {
  64. let draftText = try String(contentsOf: path!, encoding: .utf8)
  65. writerText.string = draftText
  66. } catch (let err) {
  67. print("ERROR loading: \(err)")
  68. }
  69. }
  70. func publish() {
  71. saveDocument()
  72. DispatchQueue.global(qos: .background).async {
  73. let task = Process()
  74. task.launchPath = Bundle.main.resourcePath! + "/writeas"
  75. task.arguments = ["--font", Preferences.getFont().rawValue]
  76. let pipe = Pipe()
  77. task.standardInput = pipe
  78. task.launch()
  79. let fh: FileHandle = pipe.fileHandleForWriting
  80. fh.write(self.writerText.textStorage!.string.data(using: .utf8)!)
  81. fh.closeFile()
  82. task.waitUntilExit()
  83. DispatchQueue.main.async {
  84. if let ad = NSApplication.shared().delegate as? AppDelegate {
  85. ad.filePublishItem.isEnabled = true
  86. }
  87. var notification = NSUserNotification()
  88. if task.terminationStatus == 0 {
  89. // Successfully published
  90. notification.title = "Published!"
  91. notification.informativeText = "The link is copied — press ⌘+V to share it."
  92. } else {
  93. // Something went wrong
  94. notification.title = "Unable to publish."
  95. notification.informativeText = "There was a problem publishing. Please check your internet and try again."
  96. }
  97. notification.soundName = NSUserNotificationDefaultSoundName
  98. NSUserNotificationCenter.default.deliver(notification)
  99. }
  100. }
  101. }
  102. func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
  103. return true
  104. }
  105. func toggle(isNight: Bool) {
  106. let darkBG = NSColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
  107. if isNight {
  108. self.view.window!.backgroundColor = darkBG
  109. self.writerText.backgroundColor = darkBG
  110. self.writerText.textColor = NSColor.white
  111. } else {
  112. self.view.window!.backgroundColor = NSColor.white
  113. self.writerText.backgroundColor = NSColor.white
  114. self.writerText.textColor = NSColor.black
  115. }
  116. }
  117. func adjustTextSize(increment: Bool) {
  118. var size = Preferences.getFontSize()
  119. if increment {
  120. size += 2
  121. } else {
  122. size -= 1
  123. }
  124. self.writerText.font = NSFont(name: Preferences.getFont().typeface, size: size)
  125. UserDefaults.standard.set(String(Int(size)), forKey: "editor_text_size")
  126. configureWindow()
  127. }
  128. func setFont(_ font: Preferences.PostFont) {
  129. self.writerText.font = NSFont(name: font.typeface, size: Preferences.getFontSize())
  130. }
  131. }