Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Add view modifier detecting clicks on any widget
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Jan 3, 2024
1 parent 5a1c325 commit 1e6143a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Documentation/Reference/extensions/View.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Run a function when the view appears for the first time.
- Parameter closure: The function.
- Returns: A view.

### `onClick(handler:)`

Run a function when the widget gets clicked.
- Parameter handler: The function.
- Returns: A view.

### `frame(maxSize:)`

Set the view's maximal size.
Expand Down
7 changes: 7 additions & 0 deletions Sources/Adwaita/View/Modifiers/AppearObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ extension View {
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) }
}

}
1 change: 1 addition & 0 deletions Tests/CarouselDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct CarouselDemo: View {
.vexpand()
.hexpand()
.style("card")
.onClick { print(element.id) }
.padding(20)
.frame(minWidth: 300, minHeight: 200)
.frame(maxSize: 500)
Expand Down
1 change: 1 addition & 0 deletions user-manual/Information/Widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
| `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. |

### `Button` Modifiers
| Syntax | Description |
Expand Down

0 comments on commit 1e6143a

Please sign in to comment.