Narrow shortcut settings notifications
This commit is contained in:
parent
b1d4db1bdb
commit
5da7da127a
4 changed files with 25 additions and 4 deletions
|
|
@ -7810,7 +7810,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
|
|||
private func installShortcutDefaultsObserver() {
|
||||
guard shortcutDefaultsObserver == nil else { return }
|
||||
shortcutDefaultsObserver = NotificationCenter.default.addObserver(
|
||||
forName: UserDefaults.didChangeNotification,
|
||||
forName: KeyboardShortcutSettings.didChangeNotification,
|
||||
object: nil,
|
||||
queue: .main
|
||||
) { [weak self] _ in
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ import SwiftUI
|
|||
|
||||
/// Stores customizable keyboard shortcuts (definitions + persistence).
|
||||
enum KeyboardShortcutSettings {
|
||||
static let didChangeNotification = Notification.Name("cmux.keyboardShortcutSettingsDidChange")
|
||||
static let actionUserInfoKey = "action"
|
||||
|
||||
enum Action: String, CaseIterable, Identifiable {
|
||||
// Titlebar / primary UI
|
||||
case toggleSidebar
|
||||
|
|
@ -198,16 +201,34 @@ enum KeyboardShortcutSettings {
|
|||
if let data = try? JSONEncoder().encode(shortcut) {
|
||||
UserDefaults.standard.set(data, forKey: action.defaultsKey)
|
||||
}
|
||||
postDidChangeNotification(action: action)
|
||||
}
|
||||
|
||||
static func resetShortcut(for action: Action) {
|
||||
UserDefaults.standard.removeObject(forKey: action.defaultsKey)
|
||||
postDidChangeNotification(action: action)
|
||||
}
|
||||
|
||||
static func resetAll() {
|
||||
for action in Action.allCases {
|
||||
resetShortcut(for: action)
|
||||
UserDefaults.standard.removeObject(forKey: action.defaultsKey)
|
||||
}
|
||||
postDidChangeNotification()
|
||||
}
|
||||
|
||||
private static func postDidChangeNotification(
|
||||
action: Action? = nil,
|
||||
center: NotificationCenter = .default
|
||||
) {
|
||||
var userInfo: [AnyHashable: Any] = [:]
|
||||
if let action {
|
||||
userInfo[actionUserInfoKey] = action.rawValue
|
||||
}
|
||||
center.post(
|
||||
name: didChangeNotification,
|
||||
object: nil,
|
||||
userInfo: userInfo.isEmpty ? nil : userInfo
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Backwards-Compatible API (call-sites can migrate gradually)
|
||||
|
|
|
|||
|
|
@ -332,7 +332,7 @@ struct TitlebarControlsView: View {
|
|||
.onHover { hovering in
|
||||
isHoveringControls = hovering
|
||||
}
|
||||
.onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in
|
||||
.onReceive(NotificationCenter.default.publisher(for: KeyboardShortcutSettings.didChangeNotification)) { _ in
|
||||
shortcutRefreshTick &+= 1
|
||||
}
|
||||
.onAppear {
|
||||
|
|
|
|||
|
|
@ -5419,7 +5419,7 @@ private struct ShortcutSettingRow: View {
|
|||
.onChange(of: shortcut) { newValue in
|
||||
KeyboardShortcutSettings.setShortcut(newValue, for: action)
|
||||
}
|
||||
.onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in
|
||||
.onReceive(NotificationCenter.default.publisher(for: KeyboardShortcutSettings.didChangeNotification)) { _ in
|
||||
let latest = KeyboardShortcutSettings.shortcut(for: action)
|
||||
if latest != shortcut {
|
||||
shortcut = latest
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue