Add view modifier detecting clicks on any widget

This commit is contained in:
david-swift 2024-01-03 17:17:35 +01:00
parent 5a1c325580
commit 1e6143acce
4 changed files with 15 additions and 0 deletions

View File

@ -36,6 +36,12 @@ Run a function when the view appears for the first time.
- Parameter closure: The function. - Parameter closure: The function.
- Returns: A view. - Returns: A view.
### `onClick(handler:)`
Run a function when the widget gets clicked.
- Parameter handler: The function.
- Returns: A view.
### `frame(maxSize:)` ### `frame(maxSize:)`
Set the view's maximal size. Set the view's maximal size.

View File

@ -50,4 +50,11 @@ extension View {
inspectOnAppear { _ in closure() } inspectOnAppear { _ in closure() }
} }
/// Run a function when the widget gets clicked.
/// - Parameter handler: The function.
/// - Returns: A view.
public func onClick(handler: @escaping () -> Void) -> View {
inspectOnAppear { _ = $0.onClick(closure: handler) }
}
} }

View File

@ -33,6 +33,7 @@ struct CarouselDemo: View {
.vexpand() .vexpand()
.hexpand() .hexpand()
.style("card") .style("card")
.onClick { print(element.id) }
.padding(20) .padding(20)
.frame(minWidth: 300, minHeight: 200) .frame(minWidth: 300, minHeight: 200)
.frame(maxSize: 500) .frame(maxSize: 500)

View File

@ -50,6 +50,7 @@ This is an overview of the available widgets and other components in _Adwaita_.
| `toast(_:signal:button:handler:)` | Show a toast with a button on top of the view whenever the signal gets activated. | | `toast(_:signal:button:handler:)` | Show a toast with a button on top of the view whenever the signal gets activated. |
| `overlay(_:)` | Overlay a view with another view. | | `overlay(_:)` | Overlay a view with another view. |
| `insensitive(_:)` | Make a view unable to detect actions. This is especially useful for overlays. | | `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. |
### `Button` Modifiers ### `Button` Modifiers
| Syntax | Description | | Syntax | Description |