The Write.as desktop (GUI) app for macOS.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

137 рядки
4.2 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: 16)
  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. // Fit textview to window size
  27. writerText.setFrameSize(NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height))
  28. // Style the window
  29. self.view.window!.title = "Write.as"
  30. self.view.window!.titlebarAppearsTransparent = true
  31. self.view.window!.isMovableByWindowBackground = true
  32. self.view.window!.titleVisibility = .hidden
  33. self.view.window!.backgroundColor = NSColor.white
  34. }
  35. override var representedObject: Any? {
  36. didSet {
  37. // Update the view, if already loaded.
  38. }
  39. }
  40. fileprivate func getDraftPath() -> URL? {
  41. do {
  42. let baseDir = try FileManager.default.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: true)
  43. return baseDir.appendingPathComponent(draftFile)
  44. } catch (let err) {
  45. print("ERROR getting path: \(err)")
  46. return nil
  47. }
  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)// .write(toFile: path.absoluteString, atomically: true, 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 publish() {
  75. saveDocument()
  76. DispatchQueue.global(qos: .background).async {
  77. let task = Process()
  78. task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas"
  79. let pipe = Pipe()
  80. task.standardInput = pipe
  81. task.launch()
  82. let fh: FileHandle = pipe.fileHandleForWriting
  83. fh.write(self.writerText.textStorage!.string.data(using: .utf8)!)
  84. fh.closeFile()
  85. task.waitUntilExit()
  86. DispatchQueue.main.async {
  87. var notification = NSUserNotification()
  88. // All these values are optional
  89. notification.title = "Published!"
  90. notification.informativeText = "The link is copied — press ⌘+V to share it."
  91. notification.soundName = NSUserNotificationDefaultSoundName
  92. NSUserNotificationCenter.default.deliver(notification)
  93. }
  94. }
  95. }
  96. func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
  97. return true
  98. }
  99. func toggle(isNight: Bool) {
  100. let darkBG = NSColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
  101. if isNight {
  102. self.view.window!.backgroundColor = darkBG
  103. self.writerText.backgroundColor = darkBG
  104. self.writerText.textColor = NSColor.white
  105. } else {
  106. self.view.window!.backgroundColor = NSColor.white
  107. self.writerText.backgroundColor = NSColor.white
  108. self.writerText.textColor = NSColor.black
  109. }
  110. }
  111. }