-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
774 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,56 @@ | ||
import Cocoa | ||
import Preferences | ||
|
||
extension PreferencePaneIdentifier { | ||
static let general = PreferencePaneIdentifier("general") | ||
static let advanced = PreferencePaneIdentifier("advanced") | ||
} | ||
|
||
@NSApplicationMain | ||
final class AppDelegate: NSObject, NSApplicationDelegate { | ||
@IBOutlet private var window: NSWindow! | ||
|
||
let preferencesWindowController = PreferencesWindowController( | ||
viewControllers: [ | ||
GeneralPreferenceViewController(), | ||
AdvancedPreferenceViewController() | ||
] | ||
) | ||
var preferencesStyle: PreferencesStyle { | ||
get { | ||
return PreferencesStyle.preferencesStyleFromUserDefaults() | ||
} | ||
set(newPreferencesStyle) { | ||
newPreferencesStyle.storeInUserDefaults() | ||
} | ||
} | ||
|
||
lazy var preferences: [PreferencePane] = [ | ||
GeneralPreferenceViewController(), | ||
AdvancedPreferenceViewController() | ||
] | ||
lazy var preferencesWindowController = PreferencesWindowController(preferencePanes: self.preferences, style: self.preferencesStyle, animated: true) | ||
|
||
func applicationWillFinishLaunching(_ notification: Notification) { | ||
window.orderOut(self) | ||
} | ||
|
||
func applicationDidFinishLaunching(_ notification: Notification) { | ||
preferencesWindowController.showWindow() | ||
preferencesWindowController.show(preferencePane: .advanced) | ||
} | ||
|
||
@IBAction private func preferencesMenuItemActionHandler(_ sender: NSMenuItem) { | ||
preferencesWindowController.showWindow() | ||
preferencesWindowController.show() | ||
} | ||
|
||
@IBAction private func switchStyle(_ sender: Any) { | ||
self.preferencesStyle = (self.preferencesStyle == .segmentedControl) | ||
? .toolbarItems | ||
: .segmentedControl | ||
relaunch() | ||
} | ||
} | ||
|
||
private func relaunch() { | ||
let appBundleIdentifier = Bundle.main.bundleIdentifier! | ||
NSWorkspace.shared.launchApplication( | ||
withBundleIdentifier: appBundleIdentifier, | ||
options: NSWorkspace.LaunchOptions.newInstance, | ||
additionalEventParamDescriptor: nil, | ||
launchIdentifier: nil) | ||
NSApp.terminate(nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
import Preferences | ||
|
||
// Helpers to write styles to and read them from UserDefaults. | ||
|
||
extension PreferencesStyle: RawRepresentable { | ||
public var rawValue: Int { | ||
switch self { | ||
case .toolbarItems: | ||
return 0 | ||
case .segmentedControl: | ||
return 1 | ||
} | ||
} | ||
|
||
public init?(rawValue: Int) { | ||
switch rawValue { | ||
case 0: | ||
self = .toolbarItems | ||
case 1: | ||
self = .segmentedControl | ||
default: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
extension PreferencesStyle { | ||
static var userDefaultsKey: String { return "preferencesStyle" } | ||
|
||
static func preferencesStyleFromUserDefaults(_ userDefaults: UserDefaults = .standard) -> PreferencesStyle { | ||
return PreferencesStyle(rawValue: userDefaults.integer(forKey: PreferencesStyle.userDefaultsKey)) | ||
?? .toolbarItems | ||
} | ||
|
||
func storeInUserDefaults(_ userDefaults: UserDefaults = .standard) { | ||
userDefaults.set(self.rawValue, forKey: PreferencesStyle.userDefaultsKey) | ||
} | ||
} |
Oops, something went wrong.