Allow deactivating style or keyboard shortcut

This commit is contained in:
david-swift 2024-05-18 10:31:41 +02:00
parent e44f3d78d2
commit c7eb2120a4
6 changed files with 58 additions and 12 deletions

View File

@ -16,6 +16,8 @@ public struct MenuButton: MenuItem {
var handler: () -> Void var handler: () -> Void
/// The keyboard shortcut. /// The keyboard shortcut.
var shortcut = "" var shortcut = ""
/// Whether the keyboard shortcut is currently available.
var active = true
/// Whether to prefer adding the action to the application window. /// Whether to prefer adding the action to the application window.
var preferApplicationWindow: Bool var preferApplicationWindow: Bool
@ -40,10 +42,10 @@ public struct MenuButton: MenuItem {
/// - window: The application window containing the menu. /// - window: The application window containing the menu.
public func addMenuItem(menu: OpaquePointer?, app: GTUIApp, window: GTUIApplicationWindow?) { public func addMenuItem(menu: OpaquePointer?, app: GTUIApp, window: GTUIApplicationWindow?) {
if let window, preferApplicationWindow { 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) g_menu_append(menu, label, "win." + filteredLabel)
} else { } else {
app.addKeyboardShortcut(shortcut, id: filteredLabel, handler: handler) app.addKeyboardShortcut(active ? shortcut : "", id: filteredLabel, handler: handler)
g_menu_append(menu, label, "app." + filteredLabel) 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. /// Note that the keyboard shortcut is available after the view has been visible for the first time.
/// - Parameters: /// - Parameters:
/// - shortcut: The keyboard shortcut. /// - shortcut: The keyboard shortcut.
/// - active: Whether the keyboard shortcut is currently available.
/// - Returns: The button. /// - Returns: The button.
public func keyboardShortcut(_ shortcut: String) -> Self { public func keyboardShortcut(_ shortcut: String, active: Bool = true) -> Self {
var newSelf = self var newSelf = self
newSelf.shortcut = shortcut newSelf.shortcut = shortcut
newSelf.active = active
return newSelf return newSelf
} }

View File

@ -102,6 +102,23 @@ public class GTUIApp {
gtk_application_set_accels_for_action(pointer, (window == nil ? "app." : "win.") + id, [shortcut].cArray) 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. /// Focus the window with a certain id. Create the window if it doesn't already exist.
/// - Parameters: /// - Parameters:
/// - id: The window's id. /// - id: The window's id.

View File

@ -30,6 +30,13 @@ public class GTUIApplicationWindow: GTUIWindow {
app.addKeyboardShortcut(shortcut, id: id, window: self, handler: handler) 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. /// Set the window's child.
/// - Parameter child: The child. /// - Parameter child: The child.
override public func setChild(_ child: OpaquePointer?) { override public func setChild(_ child: OpaquePointer?) {

View File

@ -43,9 +43,14 @@ extension Button {
/// - Parameters: /// - Parameters:
/// - shortcut: The keyboard shortcut. /// - shortcut: The keyboard shortcut.
/// - window: The application window. /// - window: The application window.
/// - active: Whether the keyboard shortcut is active.
/// - Returns: The button. /// - Returns: The button.
public func keyboardShortcut(_ shortcut: String, window: GTUIApplicationWindow) -> Self { public func keyboardShortcut(_ shortcut: String, window: GTUIApplicationWindow, active: Bool = true) -> Self {
if active {
window.addKeyboardShortcut(shortcut, id: shortcut) { self.clicked?() } window.addKeyboardShortcut(shortcut, id: shortcut) { self.clicked?() }
} else {
window.removeKeyboardShortcut(id: shortcut)
}
return self return self
} }
@ -55,9 +60,14 @@ extension Button {
/// - Parameters: /// - Parameters:
/// - shortcut: The keyboard shortcut. /// - shortcut: The keyboard shortcut.
/// - window: The application. /// - window: The application.
/// - active: Whether the keyboard shortcut is active.
/// - Returns: The button. /// - Returns: The button.
public func keyboardShortcut(_ shortcut: String, app: GTUIApp) -> Self { public func keyboardShortcut(_ shortcut: String, app: GTUIApp, active: Bool = true) -> Self {
if active {
app.addKeyboardShortcut(shortcut, id: shortcut) { self.clicked?() } app.addKeyboardShortcut(shortcut, id: shortcut) { self.clicked?() }
} else {
app.removeKeyboardShortcut(id: shortcut)
}
return self return self
} }

View File

@ -114,10 +114,18 @@ extension View {
} }
/// Add a style class to the 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. /// - Returns: A view.
public func style(_ style: String) -> View { public func style(_ style: String, active: Bool = true) -> View {
inspect { gtk_widget_add_css_class($0.pointer?.cast(), style) } 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. /// Run a function when the view gets an update.

View File

@ -42,8 +42,8 @@
} }
], ],
"build-commands": [ "build-commands": [
"swift build -c release --static-swift-stdlib", "swift build -c debug --static-swift-stdlib",
"install -Dm755 .build/release/Demo /app/bin/Demo" "install -Dm755 .build/debug/Demo /app/bin/Demo"
] ]
} }
] ]