-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate UserDefaults and MainWindowController to Swift
- Loading branch information
1 parent
107c330
commit 884477d
Showing
9 changed files
with
150 additions
and
116 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
import Cocoa | ||
import Combine | ||
import WebKit | ||
|
||
class MainWindowController: NSWindowController, NSWindowDelegate { | ||
private var subscriptions = Set<AnyCancellable>() | ||
|
||
override func windowDidLoad() { | ||
super.windowDidLoad() | ||
setupSubscribers() | ||
|
||
// Fixes that we can't trust that the main window exists in applicationDidFinishLaunching:. | ||
// Here we always know that this content view controller will be the main web view controller, | ||
// so inform the app delegate | ||
let appDelegate = NSApplication.shared.delegate as? AppDelegate | ||
appDelegate?.mainWebViewController = contentViewController as? WebViewController | ||
} | ||
|
||
func setupSubscribers() { | ||
guard let webViewController = contentViewController as? WebViewController, let window = window else { | ||
return | ||
} | ||
|
||
webViewController.webView.publisher(for: \.title) | ||
.replaceNil(with: "Fastmate") | ||
.assign(to: \.title, on: window) | ||
.store(in: &subscriptions) | ||
|
||
UserDefaults.standard.publisher(for: \.mainWindowFrame) | ||
.filter { $0 != NSRect.zero } | ||
.first() | ||
.sink { window.setFrame($0, display: false) } | ||
.store(in: &subscriptions) | ||
|
||
UserDefaults.standard.publisher(for: \.shouldUseTransparentTitleBar) | ||
.sink { | ||
window.titlebarAppearsTransparent = $0 | ||
window.titleVisibility = $0 ? .hidden : .visible | ||
} | ||
.store(in: &subscriptions) | ||
|
||
NotificationCenter.default.publisher(for: NSWindow.didResizeNotification, object: window) | ||
.debounce(for: .seconds(0.5), scheduler: DispatchQueue.main) | ||
.compactMap { ($0.object as? NSWindow)?.frame } | ||
.assign(to: \.mainWindowFrame, on: UserDefaults.standard) | ||
.store(in: &subscriptions) | ||
|
||
UserDefaults.standard.publisher(for: \.lastUsedWindowColor) | ||
.assign(to: \.backgroundColor, on: window) | ||
.store(in: &subscriptions) | ||
} | ||
|
||
func windowShouldClose(_ sender: NSWindow) -> Bool { | ||
NSApp.hide(sender) | ||
return false | ||
} | ||
} | ||
|
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,58 @@ | ||
import Foundation | ||
import Combine | ||
|
||
extension UserDefaults { | ||
func registerFastmateDefaults() { | ||
register(defaults: [ | ||
#keyPath(arrowNavigatesMessageList): false, | ||
#keyPath(automaticUpdateChecks): true, | ||
#keyPath(shouldShowStatusBarIcon): false, | ||
#keyPath(shouldShowUnreadMailIndicator): true, | ||
#keyPath(shouldShowUnreadMailInDock): true, | ||
#keyPath(shouldShowUnreadMailCountInDock): true, | ||
#keyPath(shouldShowUnreadMailInStatusBar): true, | ||
#keyPath(shouldUseFastmailBeta): false, | ||
#keyPath(shouldUseTransparentTitleBar): true, | ||
#keyPath(watchedFolderType): WatchedFolderType.default.rawValue, | ||
#keyPath(watchedFolders): "", | ||
]) | ||
} | ||
|
||
// These variable names must exactly match the key string for KVO Combine publishers to work. #keyPath sort of verifies this | ||
@objc dynamic var shouldShowStatusBarIcon: Bool { bool(forKey: #keyPath(shouldShowStatusBarIcon)) } | ||
@objc dynamic var shouldShowUnreadMailIndicator: Bool { bool(forKey: #keyPath(shouldShowUnreadMailIndicator)) } | ||
@objc dynamic var shouldShowUnreadMailInDock: Bool { bool(forKey: #keyPath(shouldShowUnreadMailInDock)) } | ||
@objc dynamic var shouldShowUnreadMailCountInDock: Bool { bool(forKey: #keyPath(shouldShowUnreadMailCountInDock)) } | ||
@objc dynamic var shouldShowUnreadMailInStatusBar: Bool { bool(forKey: #keyPath(shouldShowUnreadMailInStatusBar)) } | ||
@objc dynamic var shouldUseFastmailBeta: Bool { bool(forKey: #keyPath(shouldUseFastmailBeta)) } | ||
@objc dynamic var shouldUseTransparentTitleBar: Bool { bool(forKey: #keyPath(shouldUseTransparentTitleBar)) } | ||
@objc dynamic var watchedFolderType: WatchedFolderType { .init(rawValue: UInt(integer(forKey: #keyPath(watchedFolderType))))! } | ||
@objc dynamic var watchedFolders: String { string(forKey: #keyPath(watchedFolders)) ?? "" } | ||
|
||
@objc dynamic var arrowNavigatesMessageList: Bool { | ||
get { bool(forKey: #keyPath(arrowNavigatesMessageList)) } | ||
set { set(newValue, forKey: #keyPath(arrowNavigatesMessageList)) } | ||
} | ||
|
||
@objc dynamic var automaticUpdateChecks: Bool { | ||
get { bool(forKey: #keyPath(automaticUpdateChecks)) } | ||
set { set(newValue, forKey: #keyPath(automaticUpdateChecks)) } | ||
} | ||
|
||
@objc dynamic var mainWindowFrame: NSRect { | ||
get { NSRectFromString(string(forKey: #keyPath(mainWindowFrame)) ?? "") } | ||
set { set(NSStringFromRect(newValue), forKey: #keyPath(mainWindowFrame)) } | ||
} | ||
|
||
@objc dynamic var lastUsedWindowColor: NSColor { | ||
data(forKey: #keyPath(lastUsedWindowColor)) | ||
.flatMap { try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSColor.self, from: $0) } | ||
?? NSColor(red: 0.27, green: 0.34, blue: 0.49, alpha: 1.0) | ||
} | ||
} | ||
|
||
@objc enum WatchedFolderType: UInt { | ||
case `default` | ||
case all | ||
case specific | ||
} |
Oops, something went wrong.