From 986fc3583da9531d1f40cb12d84c2b1acb357b42 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 2 Aug 2017 21:32:26 -0400 Subject: [PATCH] Move to document-based application --- writeas.xcodeproj/project.pbxproj | 8 ++++++ writeas/AppDelegate.swift | 6 ++-- writeas/Base.lproj/Main.storyboard | 34 +++++++++++++++++++++-- writeas/Document.swift | 44 ++++++++++++++++++++++++++++++ writeas/Info.plist | 21 ++++++++++++++ writeas/WindowController.swift | 26 ++++++++++++++++++ 6 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 writeas/Document.swift create mode 100644 writeas/WindowController.swift diff --git a/writeas.xcodeproj/project.pbxproj b/writeas.xcodeproj/project.pbxproj index 9fde0cf..1fe61f6 100644 --- a/writeas.xcodeproj/project.pbxproj +++ b/writeas.xcodeproj/project.pbxproj @@ -7,7 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 33071005A600B8C542D04637 /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33071BEC2D76387CC2ACE1BD /* WindowController.swift */; }; 330711A70F03BFB84FB889AB /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33071648B80C4188CAC44CA1 /* Constants.swift */; }; + 33071853C7159177EC75175B /* Document.swift in Sources */ = {isa = PBXBuildFile; fileRef = 330715DBB45C2BB18057D4BC /* Document.swift */; }; 33071D332D5799D36FBC1DDD /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3307150C0D2CA1C8A71C5E26 /* Preferences.swift */; }; C98EE9AC1F29532F002F646E /* writeas in Resources */ = {isa = PBXBuildFile; fileRef = C98EE9AB1F29532F002F646E /* writeas */; }; C9953E7D1F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600.ttf in Resources */ = {isa = PBXBuildFile; fileRef = C9953E791F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600.ttf */; }; @@ -30,7 +32,9 @@ /* Begin PBXFileReference section */ 3307150C0D2CA1C8A71C5E26 /* Preferences.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Preferences.swift; path = writeas/Preferences.swift; sourceTree = ""; }; + 330715DBB45C2BB18057D4BC /* Document.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Document.swift; path = writeas/Document.swift; sourceTree = ""; }; 33071648B80C4188CAC44CA1 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; + 33071BEC2D76387CC2ACE1BD /* WindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WindowController.swift; path = writeas/WindowController.swift; sourceTree = ""; }; C98EE9AB1F29532F002F646E /* writeas */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = writeas; sourceTree = ""; }; C9953E791F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600.ttf"; sourceTree = ""; }; C9953E7A1F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600italic.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600italic.ttf"; sourceTree = ""; }; @@ -88,6 +92,8 @@ C99B910B1C33841E00A91990 /* writeas */, C99B910A1C33841E00A91990 /* Products */, 3307150C0D2CA1C8A71C5E26 /* Preferences.swift */, + 330715DBB45C2BB18057D4BC /* Document.swift */, + 33071BEC2D76387CC2ACE1BD /* WindowController.swift */, ); sourceTree = ""; }; @@ -204,6 +210,8 @@ C99B910D1C33841E00A91990 /* AppDelegate.swift in Sources */, 33071D332D5799D36FBC1DDD /* Preferences.swift in Sources */, 330711A70F03BFB84FB889AB /* Constants.swift in Sources */, + 33071853C7159177EC75175B /* Document.swift in Sources */, + 33071005A600B8C542D04637 /* WindowController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/writeas/AppDelegate.swift b/writeas/AppDelegate.swift index 7b5129b..bba6472 100644 --- a/writeas/AppDelegate.swift +++ b/writeas/AppDelegate.swift @@ -24,7 +24,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { // Set night mode state nightModeItem.state = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))! if nightModeItem.state == NSOnState { - vc?.toggle(isNight: true) + let wc = NSApplication.shared().mainWindow!.windowController as? WindowController + wc?.toggle(isNight: true) } // Set font @@ -59,8 +60,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { } @IBAction func toggleNightMode(_ sender: NSMenuItem) { + let wc = NSApplication.shared().mainWindow!.windowController as? WindowController let isOff = sender.state == NSOffState - vc?.toggle(isNight: isOff) + wc?.toggle(isNight: isOff) sender.state = isOff ? NSOnState : NSOffState UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state") } diff --git a/writeas/Base.lproj/Main.storyboard b/writeas/Base.lproj/Main.storyboard index 5a6bc09..d2c5f92 100644 --- a/writeas/Base.lproj/Main.storyboard +++ b/writeas/Base.lproj/Main.storyboard @@ -1,7 +1,6 @@ - + - @@ -60,11 +59,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -382,7 +410,7 @@ DQ - + diff --git a/writeas/Document.swift b/writeas/Document.swift new file mode 100644 index 0000000..21132d9 --- /dev/null +++ b/writeas/Document.swift @@ -0,0 +1,44 @@ +// +// Created by Matt Baer on 8/2/17. +// Copyright (c) 2017 A Bunch Tell. All rights reserved. +// + +import Foundation +import Cocoa + +class Document: NSDocument { + + override init() { + super.init() + // Add your subclass-specific initialization here. + } + + override class func autosavesInPlace() -> Bool { + return false + } + + override func makeWindowControllers() { + // Returns the Storyboard that contains your Document window. + let storyboard = NSStoryboard(name: "Main", bundle: nil) + let windowController = storyboard.instantiateController(withIdentifier: "DocumentWindow") as! NSWindowController + self.addWindowController(windowController) + } + + override func data(ofType typeName: String) throws -> Data { + // Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil. + // You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. + throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil) + } + + override func read(from data: Data, ofType typeName: String) throws { + // Insert code here to read your document from the given data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning false. + // You can also choose to override readFromFileWrapper:ofType:error: or readFromURL:ofType:error: instead. + // If you override either of these, you should also override -isEntireFileLoaded to return false if the contents are lazily loaded. + throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil) + } + + override var isDocumentEdited: Bool { + // Never prompt user to save on exit + return false + } +} diff --git a/writeas/Info.plist b/writeas/Info.plist index 3743876..ffbb0ae 100644 --- a/writeas/Info.plist +++ b/writeas/Info.plist @@ -4,6 +4,27 @@ CFBundleDevelopmentRegion en + CFBundleDocumentTypes + + + CFBundleTypeExtensions + + was + + CFBundleTypeIconFile + + CFBundleTypeName + DocumentType + CFBundleTypeOSTypes + + ???? + + CFBundleTypeRole + Editor + NSDocumentClass + $(PRODUCT_MODULE_NAME).Document + + CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIconFile diff --git a/writeas/WindowController.swift b/writeas/WindowController.swift new file mode 100644 index 0000000..18ec929 --- /dev/null +++ b/writeas/WindowController.swift @@ -0,0 +1,26 @@ +// +// Created by Matt Baer on 8/2/17. +// Copyright (c) 2017 A Bunch Tell. All rights reserved. +// + +import Foundation +import AppKit + +class WindowController: NSWindowController { + var vc: ViewController? + + required init?(coder: NSCoder) { + super.init(coder: coder) + shouldCascadeWindows = true + } + + override func windowDidLoad() { + super.windowDidLoad() + + vc = (self.window?.contentViewController)! as! ViewController + } + + func toggle(isNight: Bool) { + vc?.toggle(isNight: isNight) + } +}