From b13fbcbcf18b3a4b24c2a1232ec9a5b25f8a7b91 Mon Sep 17 00:00:00 2001 From: david-swift Date: Wed, 14 Feb 2024 20:31:40 +0100 Subject: [PATCH] Add view modifier to set focus only --- Documentation/Reference/extensions/View.md | 6 ++++++ Sources/Adwaita/View/Modifiers/InspectorWrapper.swift | 11 +++++++++++ user-manual/Information/Widgets.md | 1 + 3 files changed, 18 insertions(+) diff --git a/Documentation/Reference/extensions/View.md b/Documentation/Reference/extensions/View.md index c509e5b..90ac9de 100644 --- a/Documentation/Reference/extensions/View.md +++ b/Documentation/Reference/extensions/View.md @@ -165,6 +165,12 @@ Bind to the view's focus. - Parameter focus: Whether the view is focused. - Returns: A view. +### `focus(_:)` + +Bind a signal that focuses the view. +- Parameter focus: Whether the view is focused. +- Returns: A view. + ### `stopModifiers()` Remove all of the content modifiers for the wrapped views. diff --git a/Sources/Adwaita/View/Modifiers/InspectorWrapper.swift b/Sources/Adwaita/View/Modifiers/InspectorWrapper.swift index 438c993..ad28180 100644 --- a/Sources/Adwaita/View/Modifiers/InspectorWrapper.swift +++ b/Sources/Adwaita/View/Modifiers/InspectorWrapper.swift @@ -155,4 +155,15 @@ extension View { } } + /// Bind a signal that focuses the view. + /// - Parameter focus: Whether the view is focused. + /// - Returns: A view. + public func focus(_ signal: Signal) -> View { + inspect { storage in + if signal.update { + gtk_widget_grab_focus(storage.pointer?.cast()) + } + } + } + } diff --git a/user-manual/Information/Widgets.md b/user-manual/Information/Widgets.md index d2b66fb..d629018 100644 --- a/user-manual/Information/Widgets.md +++ b/user-manual/Information/Widgets.md @@ -48,6 +48,7 @@ There are many more widgets available using auto-generation. Learn [how to use t | `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. | +| `focus(_:)` | Bind a signal that focuses the view. | | `verticalCenter()` | Wrap a view in a `VStack` and center vertically. | | `horizontalCenter()` | Wrap a view in an `HStack` and center horizontally. |