From aff3ece71c5fca1dfd161c2ba8a3aad509ef6425 Mon Sep 17 00:00:00 2001 From: Francesco Deliro Date: Sun, 21 Oct 2018 13:55:28 +0200 Subject: [PATCH 1/2] Feature/Sidebar use theme background color: added option to use theme color for sidebar background --- Twig/Base.lproj/Main.storyboard | 83 ++++++++++++------- .../PreferencesViewController.swift | 6 ++ Twig/Controllers/SidebarViewController.swift | 7 +- Twig/Models/Preferences.swift | 83 ++++++++++++------- 4 files changed, 117 insertions(+), 62 deletions(-) diff --git a/Twig/Base.lproj/Main.storyboard b/Twig/Base.lproj/Main.storyboard index 2e44254..dcc9f8e 100644 --- a/Twig/Base.lproj/Main.storyboard +++ b/Twig/Base.lproj/Main.storyboard @@ -581,11 +581,11 @@ - + - + @@ -597,13 +597,13 @@ - + - + @@ -612,7 +612,7 @@ - + @@ -621,7 +621,7 @@ - + @@ -630,7 +630,7 @@ - + @@ -672,7 +672,7 @@ - + @@ -692,7 +692,7 @@ - + @@ -712,7 +712,7 @@ - + @@ -743,7 +743,7 @@ - + @@ -763,7 +763,7 @@ - + @@ -772,7 +772,7 @@ - + @@ -792,18 +792,18 @@ - + @@ -811,6 +811,26 @@ + + + + + + + + + + @@ -830,13 +850,14 @@ + - + @@ -947,11 +968,11 @@ - - + diff --git a/Twig/Controllers/PreferencesViewController.swift b/Twig/Controllers/PreferencesViewController.swift index 775ea79..9deb735 100644 --- a/Twig/Controllers/PreferencesViewController.swift +++ b/Twig/Controllers/PreferencesViewController.swift @@ -19,6 +19,7 @@ class PreferencesViewController: NSViewController, NSFontChanging { @IBOutlet weak var useSystemAppearance: NSButton! @IBOutlet weak var showSidebar: NSButton! @IBOutlet weak var enableSpellcheck: NSButton! + @IBOutlet weak var useThemeColorForSidebar: NSButton! override func viewDidLoad() { super.viewDidLoad() @@ -37,6 +38,7 @@ class PreferencesViewController: NSViewController, NSFontChanging { useSystemAppearance.state = getState(preferences.useSystemAppearance) showSidebar.state = getState(preferences.showSidebar) enableSpellcheck.state = getState(preferences.spellcheckEnabled) + useThemeColorForSidebar.state = getState(preferences.useThemeColorForSidebar) } private func getState(_ state: Bool) -> NSControl.StateValue { @@ -90,6 +92,10 @@ class PreferencesViewController: NSViewController, NSFontChanging { preferences.spellcheckEnabled = sender.state.rawValue.bool postNotification() } + @IBAction func useThemeColorForSidebarChanged(_ sender: NSButton) { + preferences.useThemeColorForSidebar = sender.state.rawValue.bool + postNotification() + } func changeFont(_ sender: NSFontManager?) { if let fontManager = sender { diff --git a/Twig/Controllers/SidebarViewController.swift b/Twig/Controllers/SidebarViewController.swift index f740984..7d72e58 100644 --- a/Twig/Controllers/SidebarViewController.swift +++ b/Twig/Controllers/SidebarViewController.swift @@ -16,14 +16,14 @@ class SidebarViewController: NSViewController { var items: [FileSystemItem] = [] override func viewWillAppear() { - updateSidebarVisibility() + updateSidebarAppearance() } override func viewDidLoad() { super.viewDidLoad() // Setup notification observer for preferences change - NotificationCenter.receive(.preferencesChanged, instance: self, selector: #selector(updateSidebarVisibility)) + NotificationCenter.receive(.preferencesChanged, instance: self, selector: #selector(updateSidebarAppearance)) // Setup selector for when row in sidebar is double clicked sidebar.doubleAction = #selector(doubleClicked) @@ -45,8 +45,9 @@ class SidebarViewController: NSViewController { } } - @objc private func updateSidebarVisibility() { + @objc private func updateSidebarAppearance() { (parent as? NSSplitViewController)?.splitViewItems.first?.isCollapsed = !preferences.showSidebar + sidebar.backgroundColor = preferences.useThemeColorForSidebar ? theme.background : .clear } } diff --git a/Twig/Models/Preferences.swift b/Twig/Models/Preferences.swift index 72e3c79..3ce028c 100644 --- a/Twig/Models/Preferences.swift +++ b/Twig/Models/Preferences.swift @@ -11,92 +11,119 @@ import Cocoa class Preferences { static let sharedInstance = Preferences() + enum Keys { + static let showPreviewOnStartup = "showPreviewOnStartup" + static let openNewDocumentOnStartup = "openNewDocumentOnStartup" + static let autosaveDocument = "autosaveDocument" + static let verticalSplitView = "verticalSplitView" + static let modernTitleBar = "modernTitlebar" + static let useSystemAppearance = "useSystemAppearance" + static let showSidebar = "showSidebar" + static let spellcheckEnabled = "spellcheckEnabled" + static let useThemeColorForSidebar = "useThemeColorForSidebar" + static let fontSize = "fontSize" + static let fontName = "fontName" + } + private init() { - if defaults.object(forKey: "showPreviewOnStartup") != nil { - showPreviewOnStartup = defaults.bool(forKey: "showPreviewOnStartup") + if defaults.object(forKey: Keys.showPreviewOnStartup) != nil { + showPreviewOnStartup = defaults.bool(forKey: Keys.showPreviewOnStartup) + } + + if defaults.object(forKey: Keys.openNewDocumentOnStartup) != nil { + openNewDocumentOnStartup = defaults.bool(forKey: Keys.openNewDocumentOnStartup) } - if defaults.object(forKey: "openNewDocumentOnStartup") != nil { - openNewDocumentOnStartup = defaults.bool(forKey: "openNewDocumentOnStartup") + if defaults.object(forKey: Keys.autosaveDocument) != nil { + autosaveDocument = defaults.bool(forKey: Keys.autosaveDocument) } - if defaults.object(forKey: "autosaveDocument") != nil { - autosaveDocument = defaults.bool(forKey: "autosaveDocument") + if defaults.object(forKey: Keys.verticalSplitView) != nil { + verticalSplitView = defaults.bool(forKey: Keys.verticalSplitView) } - if defaults.object(forKey: "verticalSplitView") != nil { - verticalSplitView = defaults.bool(forKey: "verticalSplitView") + if defaults.object(forKey: Keys.modernTitleBar) != nil { + modernTitlebar = defaults.bool(forKey: Keys.modernTitleBar) } - if defaults.object(forKey: "modernTitlebar") != nil { - modernTitlebar = defaults.bool(forKey: "modernTitlebar") + if defaults.object(forKey: Keys.useSystemAppearance) != nil { + useSystemAppearance = defaults.bool(forKey: Keys.useSystemAppearance) } - if defaults.object(forKey: "useSystemAppearance") != nil { - useSystemAppearance = defaults.bool(forKey: "useSystemAppearance") + if defaults.object(forKey: Keys.showSidebar) != nil { + showSidebar = defaults.bool(forKey: Keys.showSidebar) } - if defaults.object(forKey: "showSidebar") != nil { - showSidebar = defaults.bool(forKey: "showSidebar") + if defaults.object(forKey: Keys.spellcheckEnabled) != nil { + spellcheckEnabled = defaults.bool(forKey: Keys.spellcheckEnabled) } - if defaults.object(forKey: "spellcheckEnabled") != nil { - spellcheckEnabled = defaults.bool(forKey: "spellcheckEnabled") + if defaults.object(forKey: Keys.useThemeColorForSidebar) != nil { + useThemeColorForSidebar = defaults.bool(forKey: Keys.useThemeColorForSidebar) } } public var showPreviewOnStartup = true { willSet(newVal) { - setDefaults(key: "showPreviewOnStartup", newVal) + setDefaults(key: Keys.showPreviewOnStartup, newVal) } } public var openNewDocumentOnStartup = true { willSet(newVal) { - setDefaults(key: "openNewDocumentOnStartup", newVal) + setDefaults(key: Keys.openNewDocumentOnStartup, newVal) } } public var autosaveDocument = true { willSet(newVal) { - setDefaults(key: "autosaveDocument", newVal) + setDefaults(key: Keys.autosaveDocument, newVal) } } public var verticalSplitView = true { willSet(newVal) { - setDefaults(key: "verticalSplitView", newVal) + setDefaults(key: Keys.verticalSplitView, newVal) } } public var modernTitlebar = true { willSet(newVal) { - setDefaults(key: "modernTitlebar", newVal) + setDefaults(key: Keys.modernTitleBar, newVal) } } public var useSystemAppearance = false { willSet(newVal) { - setDefaults(key: "useSystemAppearance", newVal) + setDefaults(key: Keys.useSystemAppearance, newVal) } } public var showSidebar = true { willSet(newVal) { - setDefaults(key: "showSidebar", newVal) + setDefaults(key: Keys.showSidebar, newVal) } } public var spellcheckEnabled = true { willSet(newVal) { - setDefaults(key: "spellcheckEnabled", newVal) + setDefaults(key: Keys.spellcheckEnabled, newVal) } } + public var useThemeColorForSidebar = true { + willSet(newVal) { + setDefaults( + key: Keys.useThemeColorForSidebar, + newVal + ) + } + } + public var font: NSFont { get { - let fontSize = defaults.double(forKey: "fontSize") - if let fontName = defaults.string(forKey: "fontName"), + let fontSize = defaults.double(forKey: Keys.fontSize) + if let fontName = defaults.string(forKey: Keys.fontName), let font = NSFont(name: fontName, size: CGFloat(fontSize)) { return font } @@ -111,8 +138,8 @@ class Preferences { return NSFont.monospacedDigitSystemFont(ofSize: CGFloat(18), weight: .regular) } set { - setDefaults(key: "fontName", newValue.fontName) - setDefaults(key: "fontSize", newValue.pointSize) + setDefaults(key: Keys.fontName, newValue.fontName) + setDefaults(key: Keys.fontSize, newValue.pointSize) } } From ccd55fbd25e107b02342b618aa4c98e94428681a Mon Sep 17 00:00:00 2001 From: Francesco Deliro Date: Sun, 28 Oct 2018 19:24:09 +0100 Subject: [PATCH 2/2] Feature/Sidebar use theme background color: fixed row item text color according to sidebar background --- Twig.xcodeproj/project.pbxproj | 4 ++++ Twig/Base.lproj/Main.storyboard | 11 +++++---- Twig/Controllers/SidebarViewController.swift | 25 ++++++++++++++++++-- Twig/Extensions/NSView.swift | 16 +++++++++++++ 4 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 Twig/Extensions/NSView.swift diff --git a/Twig.xcodeproj/project.pbxproj b/Twig.xcodeproj/project.pbxproj index 55154ca..74ef58a 100644 --- a/Twig.xcodeproj/project.pbxproj +++ b/Twig.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 133BC78421861074000E3D45 /* NSView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 133BC78321861074000E3D45 /* NSView.swift */; }; 5EC2EB6C996D63F9FA158160 /* Pods_Twig.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B66B7DAE689A6827E658233B /* Pods_Twig.framework */; }; A104782520E7338800E0930A /* FileSystemItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = A104782420E7338800E0930A /* FileSystemItem.swift */; }; A111013D20E85D9A001EBEDE /* SaveError.swift in Sources */ = {isa = PBXBuildFile; fileRef = A111013C20E85D9A001EBEDE /* SaveError.swift */; }; @@ -62,6 +63,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 133BC78321861074000E3D45 /* NSView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSView.swift; sourceTree = ""; }; A104782320E7273300E0930A /* Twig.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Twig.entitlements; sourceTree = ""; }; A104782420E7338800E0930A /* FileSystemItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileSystemItem.swift; sourceTree = ""; }; A111013C20E85D9A001EBEDE /* SaveError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SaveError.swift; sourceTree = ""; }; @@ -258,6 +260,7 @@ A1682E86209097E700164D78 /* NSMutableAttributedString.swift */, A1150D2120E858E400536336 /* String.swift */, A1A7C05320EA304A003D8DB6 /* NSAppearance.swift */, + 133BC78321861074000E3D45 /* NSView.swift */, ); path = Extensions; sourceTree = ""; @@ -499,6 +502,7 @@ A198047520DB7B2E0066FDA6 /* NotificationCenter.swift in Sources */, A1D5466420D4D9CC00D1221A /* Exporter.swift in Sources */, A156AF852090606C0098BBDD /* Document.swift in Sources */, + 133BC78421861074000E3D45 /* NSView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Twig/Base.lproj/Main.storyboard b/Twig/Base.lproj/Main.storyboard index dcc9f8e..f5e4c32 100644 --- a/Twig/Base.lproj/Main.storyboard +++ b/Twig/Base.lproj/Main.storyboard @@ -968,11 +968,11 @@ -