From c66049091d6e74256bf8e798fd2d1bc63f430320 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 26 Jul 2017 18:41:51 -0400 Subject: [PATCH] Make editor UI good + support saving/loading draft doc --- writeas/AppDelegate.swift | 14 +++++-- writeas/Base.lproj/Main.storyboard | 26 +----------- writeas/ViewController.swift | 67 +++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/writeas/AppDelegate.swift b/writeas/AppDelegate.swift index 296b6c3..7410c55 100644 --- a/writeas/AppDelegate.swift +++ b/writeas/AppDelegate.swift @@ -11,16 +11,24 @@ import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { - + var vc: ViewController? func applicationDidFinishLaunching(aNotification: NSNotification) { // Insert code here to initialize your application } - func applicationWillTerminate(aNotification: NSNotification) { + func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application } + + func applicationDidBecomeActive(_ notification: Notification) { + if vc == nil { + vc = NSApplication.shared().mainWindow?.contentViewController as! ViewController + } + } - + @IBAction func saveDocument(_ sender: AnyObject) { + vc?.saveDocument() + } } diff --git a/writeas/Base.lproj/Main.storyboard b/writeas/Base.lproj/Main.storyboard index 53267b8..daba7e1 100644 --- a/writeas/Base.lproj/Main.storyboard +++ b/writeas/Base.lproj/Main.storyboard @@ -82,14 +82,9 @@ - + - - - - - - + @@ -97,12 +92,6 @@ - - - - - - @@ -148,12 +137,6 @@ - - - - - - @@ -191,11 +174,6 @@ - - - - - diff --git a/writeas/ViewController.swift b/writeas/ViewController.swift index eea8e96..8ce22a3 100644 --- a/writeas/ViewController.swift +++ b/writeas/ViewController.swift @@ -8,20 +8,83 @@ import Cocoa -class ViewController: NSViewController { +class ViewController: NSViewController, NSTextViewDelegate { + + @IBOutlet var writerText: NSTextView! + + let draftFile = "Write.as/draft.txt" override func viewDidLoad() { super.viewDidLoad() + loadDocument() + + writerText.font = NSFont(name: "Lora", size: 16) + writerText.isHorizontallyResizable = false + writerText.textContainerInset = NSSize(width: 16, height: 16) + writerText.isAutomaticQuoteSubstitutionEnabled = false + writerText.isAutomaticDashSubstitutionEnabled = false + // Do any additional setup after loading the view. + writerText.delegate = self } - override var representedObject: AnyObject? { + override func viewDidAppear() { + super.viewDidAppear() + + // Fit textview to window size + writerText.setFrameSize(NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height)) + + // Style the window + self.view.window!.title = "Write.as" + self.view.window!.titlebarAppearsTransparent = true + self.view.window!.isMovableByWindowBackground = true + self.view.window!.titleVisibility = .hidden + self.view.window!.backgroundColor = NSColor.white + } + + override var representedObject: Any? { didSet { // Update the view, if already loaded. } } + fileprivate func getDraftPath() -> URL? { + do { + let baseDir = try FileManager.default.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: true) + return baseDir.appendingPathComponent(draftFile) + } catch (let err) { + print("ERROR getting path: \(err)") + return nil + } + } + func saveDocument() { + // Save whatever's written + let path = getDraftPath() + if path == nil { + return + } + + //writing + do { + try writerText.textStorage!.string.write(to: path!, atomically: false, encoding: .utf8)// .write(toFile: path.absoluteString, atomically: true, encoding: .utf8) + } catch (let err) { + print("ERROR writing: \(err)") + } + } + + func loadDocument() { + let path = getDraftPath() + if path == nil { + return + } + do { + let draftText = try String(contentsOf: path!, encoding: .utf8) + writerText.string = draftText + } catch (let err) { + print("ERROR loading: \(err)") + } + } }