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.

152 lines
4.6 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 = "\(NSHomeDirectory())/.writeas/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. return URL(fileURLWithPath: draftFile)
  45. }
  46. func saveDocument() {
  47. // Save whatever's written
  48. let path = getDraftPath()
  49. if path == nil {
  50. return
  51. }
  52. //writing
  53. do {
  54. try writerText.textStorage!.string.write(to: path!, atomically: false, encoding: .utf8)// .write(toFile: path.absoluteString, atomically: true, encoding: .utf8)
  55. } catch (let err) {
  56. print("ERROR writing: \(err)")
  57. }
  58. }
  59. func loadDocument() {
  60. let path = getDraftPath()
  61. if path == nil {
  62. return
  63. }
  64. do {
  65. let draftText = try String(contentsOf: path!, encoding: .utf8)
  66. writerText.string = draftText
  67. } catch (let err) {
  68. print("ERROR loading: \(err)")
  69. }
  70. }
  71. func publish() {
  72. saveDocument()
  73. DispatchQueue.global(qos: .background).async {
  74. let task = Process()
  75. task.launchPath = Bundle.main.resourcePath! + "/writeas"
  76. task.arguments = ["--font", Preferences.getFont().rawValue]
  77. let pipe = Pipe()
  78. task.standardInput = pipe
  79. task.launch()
  80. let fh: FileHandle = pipe.fileHandleForWriting
  81. fh.write(self.writerText.textStorage!.string.data(using: .utf8)!)
  82. fh.closeFile()
  83. task.waitUntilExit()
  84. DispatchQueue.main.async {
  85. var notification = NSUserNotification()
  86. // All these values are optional
  87. notification.title = "Published!"
  88. notification.informativeText = "The link is copied — press ⌘+V to share it."
  89. notification.soundName = NSUserNotificationDefaultSoundName
  90. NSUserNotificationCenter.default.deliver(notification)
  91. }
  92. }
  93. }
  94. func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
  95. return true
  96. }
  97. func toggle(isNight: Bool) {
  98. let darkBG = NSColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
  99. if isNight {
  100. self.view.window!.backgroundColor = darkBG
  101. self.writerText.backgroundColor = darkBG
  102. self.writerText.textColor = NSColor.white
  103. } else {
  104. self.view.window!.backgroundColor = NSColor.white
  105. self.writerText.backgroundColor = NSColor.white
  106. self.writerText.textColor = NSColor.black
  107. }
  108. }
  109. func adjustTextSize(increment: Bool) {
  110. var size = Preferences.getFontSize()
  111. if increment {
  112. size += 2
  113. } else {
  114. size -= 1
  115. }
  116. self.writerText.font = NSFont(name: "Lora", size: size)
  117. UserDefaults.standard.set(String(Int(size)), forKey: "editor_text_size")
  118. configureWindow()
  119. }
  120. func setFont(_ font: Preferences.PostFont) {
  121. self.writerText.font = NSFont(name: font.typeface, size: Preferences.getFontSize())
  122. }
  123. }