forked from aparoksha/adwaita-swift
Allow deactivating style or keyboard shortcut
This commit is contained in:
parent
e44f3d78d2
commit
c7eb2120a4
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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?) {
|
||||
|
||||
@ -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 {
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user