diff --git a/Sources/Adwaita/Menu/MenuButton.swift b/Sources/Adwaita/Menu/MenuButton.swift index c3d4351..9b005f5 100644 --- a/Sources/Adwaita/Menu/MenuButton.swift +++ b/Sources/Adwaita/Menu/MenuButton.swift @@ -16,6 +16,8 @@ public struct MenuButton: MenuItem { var handler: () -> Void /// The keyboard shortcut. var shortcut = "" + /// Whether the keyboard shortcut is currently available. + var active = true /// Whether to prefer adding the action to the application window. var preferApplicationWindow: Bool @@ -40,10 +42,10 @@ public struct MenuButton: MenuItem { /// - window: The application window containing the menu. public func addMenuItem(menu: OpaquePointer?, app: GTUIApp, window: GTUIApplicationWindow?) { if let window, preferApplicationWindow { - window.addKeyboardShortcut(shortcut, id: filteredLabel, handler: handler) + window.addKeyboardShortcut(active ? shortcut : "", id: filteredLabel, handler: handler) g_menu_append(menu, label, "win." + filteredLabel) } else { - app.addKeyboardShortcut(shortcut, id: filteredLabel, handler: handler) + app.addKeyboardShortcut(active ? shortcut : "", id: filteredLabel, handler: handler) g_menu_append(menu, label, "app." + filteredLabel) } } @@ -53,10 +55,12 @@ public struct MenuButton: MenuItem { /// Note that the keyboard shortcut is available after the view has been visible for the first time. /// - Parameters: /// - shortcut: The keyboard shortcut. + /// - active: Whether the keyboard shortcut is currently available. /// - Returns: The button. - public func keyboardShortcut(_ shortcut: String) -> Self { + public func keyboardShortcut(_ shortcut: String, active: Bool = true) -> Self { var newSelf = self newSelf.shortcut = shortcut + newSelf.active = active return newSelf } diff --git a/Sources/Adwaita/Model/User Interface/App/GTUIApp.swift b/Sources/Adwaita/Model/User Interface/App/GTUIApp.swift index 853c1c8..3fc9530 100644 --- a/Sources/Adwaita/Model/User Interface/App/GTUIApp.swift +++ b/Sources/Adwaita/Model/User Interface/App/GTUIApp.swift @@ -102,6 +102,23 @@ public class GTUIApp { gtk_application_set_accels_for_action(pointer, (window == nil ? "app." : "win.") + id, [shortcut].cArray) } + /// Remove a keyboard shortcut from the application. + /// - Parameters: + /// - id: The keyboard shortcut's id. + /// - window: Optionally an application window. + public func removeKeyboardShortcut( + id: String, + window: GTUIApplicationWindow? = nil + ) { + if let window { + g_action_map_remove_action(.init(window.pointer), id) + window.fields.removeValue(forKey: id) + } else { + g_action_map_remove_action(.init(pointer), id) + fields.removeValue(forKey: id) + } + } + /// Focus the window with a certain id. Create the window if it doesn't already exist. /// - Parameters: /// - id: The window's id. diff --git a/Sources/Adwaita/Model/User Interface/Window/GTUIApplicationWindow.swift b/Sources/Adwaita/Model/User Interface/Window/GTUIApplicationWindow.swift index ec1863b..0ce7222 100644 --- a/Sources/Adwaita/Model/User Interface/Window/GTUIApplicationWindow.swift +++ b/Sources/Adwaita/Model/User Interface/Window/GTUIApplicationWindow.swift @@ -30,6 +30,13 @@ public class GTUIApplicationWindow: GTUIWindow { app.addKeyboardShortcut(shortcut, id: id, window: self, handler: handler) } + /// Remove a keyboard shortcut. + /// - Parameters: + /// - id: The action's id. + public func removeKeyboardShortcut(id: String) { + app.removeKeyboardShortcut(id: id, window: self) + } + /// Set the window's child. /// - Parameter child: The child. override public func setChild(_ child: OpaquePointer?) { diff --git a/Sources/Adwaita/View/Button+.swift b/Sources/Adwaita/View/Button+.swift index 7cd8d78..29645bd 100644 --- a/Sources/Adwaita/View/Button+.swift +++ b/Sources/Adwaita/View/Button+.swift @@ -43,9 +43,14 @@ extension Button { /// - Parameters: /// - shortcut: The keyboard shortcut. /// - window: The application window. + /// - active: Whether the keyboard shortcut is active. /// - Returns: The button. - public func keyboardShortcut(_ shortcut: String, window: GTUIApplicationWindow) -> Self { - window.addKeyboardShortcut(shortcut, id: shortcut) { self.clicked?() } + public func keyboardShortcut(_ shortcut: String, window: GTUIApplicationWindow, active: Bool = true) -> Self { + if active { + window.addKeyboardShortcut(shortcut, id: shortcut) { self.clicked?() } + } else { + window.removeKeyboardShortcut(id: shortcut) + } return self } @@ -55,9 +60,14 @@ extension Button { /// - Parameters: /// - shortcut: The keyboard shortcut. /// - window: The application. + /// - active: Whether the keyboard shortcut is active. /// - Returns: The button. - public func keyboardShortcut(_ shortcut: String, app: GTUIApp) -> Self { - app.addKeyboardShortcut(shortcut, id: shortcut) { self.clicked?() } + public func keyboardShortcut(_ shortcut: String, app: GTUIApp, active: Bool = true) -> Self { + if active { + app.addKeyboardShortcut(shortcut, id: shortcut) { self.clicked?() } + } else { + app.removeKeyboardShortcut(id: shortcut) + } return self } diff --git a/Sources/Adwaita/View/Modifiers/InspectorWrapper.swift b/Sources/Adwaita/View/Modifiers/InspectorWrapper.swift index 419db36..de2de49 100644 --- a/Sources/Adwaita/View/Modifiers/InspectorWrapper.swift +++ b/Sources/Adwaita/View/Modifiers/InspectorWrapper.swift @@ -114,10 +114,18 @@ extension View { } /// Add a style class to the view. - /// - Parameter style: The style class. + /// - Parameters: + /// - style: The style class. + /// - active: Whether the style is currently applied. /// - Returns: A view. - public func style(_ style: String) -> View { - inspect { gtk_widget_add_css_class($0.pointer?.cast(), style) } + public func style(_ style: String, active: Bool = true) -> View { + inspect { storage in + if active { + gtk_widget_add_css_class(storage.pointer?.cast(), style) + } else { + gtk_widget_remove_css_class(storage.pointer?.cast(), style) + } + } } /// Run a function when the view gets an update. diff --git a/io.github.AparokshaUI.Demo.json b/io.github.AparokshaUI.Demo.json index 9d15a93..4865375 100644 --- a/io.github.AparokshaUI.Demo.json +++ b/io.github.AparokshaUI.Demo.json @@ -42,8 +42,8 @@ } ], "build-commands": [ - "swift build -c release --static-swift-stdlib", - "install -Dm755 .build/release/Demo /app/bin/Demo" + "swift build -c debug --static-swift-stdlib", + "install -Dm755 .build/debug/Demo /app/bin/Demo" ] } ]