Add view modifier to observe and set focus
This commit is contained in:
parent
ffdb727a00
commit
9e05b2692b
@ -159,6 +159,12 @@ Set the view's visibility.
|
||||
- Parameter visible: Whether the view is visible.
|
||||
- Returns: A view.
|
||||
|
||||
### `focused(_:)`
|
||||
|
||||
Bind to the view's focus.
|
||||
- Parameter focus: Whether the view is focused.
|
||||
- Returns: A view.
|
||||
|
||||
### `stopModifiers()`
|
||||
|
||||
Remove all of the content modifiers for the wrapped views.
|
||||
|
||||
@ -138,4 +138,21 @@ extension View {
|
||||
inspect { gtk_widget_set_visible($0.pointer?.cast(), visible.cBool) }
|
||||
}
|
||||
|
||||
/// Bind to the view's focus.
|
||||
/// - Parameter focus: Whether the view is focused.
|
||||
/// - Returns: A view.
|
||||
public func focused(_ focused: Binding<Bool>) -> View {
|
||||
inspect { storage in
|
||||
storage.notify(name: "has-focus", id: "focused") {
|
||||
let newValue = gtk_widget_has_focus(storage.pointer?.cast()) != 0
|
||||
if focused.wrappedValue != newValue {
|
||||
focused.wrappedValue = newValue
|
||||
}
|
||||
}
|
||||
if focused.wrappedValue != (gtk_widget_has_focus(storage.pointer?.cast()) != 0) {
|
||||
gtk_widget_grab_focus(storage.pointer?.cast())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -47,6 +47,7 @@ There are many more widgets available using auto-generation. Learn [how to use t
|
||||
| `overlay(_:)` | Overlay a view with another view. |
|
||||
| `insensitive(_:)` | Make a view unable to detect actions. This is especially useful for overlays. |
|
||||
| `onClick(handler:)` | Run a function when the user clicks on the widget. |
|
||||
| `focused(_:)` | Set and observe whether a view is focused. |
|
||||
| `verticalCenter()` | Wrap a view in a `VStack` and center vertically. |
|
||||
| `horizontalCenter()` | Wrap a view in an `HStack` and center horizontally. |
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user