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

Commit

Permalink
Add support for detecting clicks on every widget
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Dec 24, 2023
1 parent 5609b22 commit 6044ab6
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions Documentation/Reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
- [filedialog_on_save_cb(ptr_file_userData_)](methods/filedialog_on_save_cb(ptr_file_userData_).md)
- [listbox_on_select_row_cb(ptr_rowPtr_userData_)](methods/listbox_on_select_row_cb(ptr_rowPtr_userData_).md)
- [messagedialog_on_click_cb(ptr_response_userData_)](methods/messagedialog_on_click_cb(ptr_response_userData_).md)
- [on_click_cb(ptr_numberOfPress_xCoordinate_yCoordinate_userData_)](methods/on_click_cb(ptr_numberOfPress_xCoordinate_yCoordinate_userData_).md)
- [tabbutton_on_click_cb(ptr_userData_)](methods/tabbutton_on_click_cb(ptr_userData_).md)
- [taboverview_on_create_tab_cb(ptr_userData_)](methods/taboverview_on_create_tab_cb(ptr_userData_).md)
- [toast_on_click_cb(ptr_userData_)](methods/toast_on_click_cb(ptr_userData_).md)
Expand Down
10 changes: 10 additions & 0 deletions Documentation/Reference/classes/NativeWidgetPeer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ The widget's height.

The widget's width.

### `clickClosure`

The closure that gets executed when the widget is being clicked.

## Methods
### `init()`

Expand Down Expand Up @@ -75,3 +79,9 @@ Add a style class to the widget.
Enable or disable the widget.
- Parameter sensitive: Whether the widget is enabled or disabled.
- Returns: The widget.

### `onClick(closure:)`

Set a closure that is executed when the widget is being clicked.
- Parameter closure: The closure.
- Returns: The widget.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### `on_click_cb(ptr:numberOfPress:xCoordinate:yCoordinate:userData:)`

Run when the widget gets clicked.
- Parameters:
- ptr: The pointer.
- numberOfPress: Number of press that is paired with this release.
- xCoordinate: The X coordinate.
- yCoordinate: The Y coordinate.
- userData: The widget data.
19 changes: 19 additions & 0 deletions Sources/CGTUI/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ filedialog_on_save_cb (void *, void *, void *);
static void
messagedialog_on_click_cb (void *, void *, void *);
static void
on_click_cb (void *, void *, void *, void *, void *);
static void
splitbutton_on_click_cb (void *, void *);
static uint64_t
taboverview_on_create_tab_cb (void *, void *);
Expand Down Expand Up @@ -2414,6 +2416,23 @@ gtui_set_sensitive (uint64_t widget, gboolean sensitive)
gtk_widget_set_sensitive (widget, sensitive);
}

static void
gtui_init_signals (uint64_t wdgt, uint64_t data)
{
GtkWidget *widget;

g_assert_nonnull (wdgt);
g_assert_nonnull (data);
g_assert (GTK_IS_WIDGET (GTK_WIDGET ((void *)wdgt)));

widget = GTK_WIDGET (wdgt);
swift_retain (data);

GtkGestureClick *event_controller = gtk_gesture_click_new ();
gtk_widget_add_controller (widget, event_controller);
g_signal_connect (event_controller, "released", G_CALLBACK (on_click_cb), (void *)data);
}

static void
gtui_label_set_text (uint64_t widget, const char *text)
{
Expand Down
32 changes: 32 additions & 0 deletions Sources/Libadwaita/GTK/NativeWidgetPeer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ open class NativeWidgetPeer: NativePeer {
/// The widget's width.
public var width: Int { .init(gtui_get_width(self.nativePtr)) }

/// The closure that gets executed when the widget is being clicked.
var clickClosure: () -> Void = { }

/// Initialize a native widget peer.
override public init() {
super.init()
Expand Down Expand Up @@ -110,4 +113,33 @@ open class NativeWidgetPeer: NativePeer {
gtui_set_sensitive(self.nativePtr, sensitive.cBool)
return self
}

/// Set a closure that is executed when the widget is being clicked.
/// - Parameter closure: The closure.
/// - Returns: The widget.
public func onClick(closure: @escaping () -> Void) -> NativeWidgetPeer {
clickClosure = closure
let selfAddr = unsafeBitCast(self, to: UInt64.self)
gtui_init_signals(self.nativePtr, selfAddr)
return self
}
}

/// Run when the widget gets clicked.
/// - Parameters:
/// - ptr: The pointer.
/// - numberOfPress: Number of press that is paired with this release.
/// - xCoordinate: The X coordinate.
/// - yCoordinate: The Y coordinate.
/// - userData: The widget data.
@_cdecl("on_click_cb")
func on_click_cb(
ptr: UnsafeMutableRawPointer,
numberOfPress: Int64,
xCoordinate: Double,
yCoordinate: Double,
userData: UnsafeMutableRawPointer
) {
let widget = Unmanaged<NativeWidgetPeer>.fromOpaque(userData).takeUnretainedValue()
widget.clickClosure()
}
4 changes: 3 additions & 1 deletion Tests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public class MyApplication: Application {
func toastOverlay(win: Window) -> ToastOverlay {
var toastOverlay = ToastOverlay(Label(""))
let carousel = Carousel()
.append(StatusPage().title("Hello").description("Page 1").hexpand().frame(minHeight: 300))
.append(StatusPage().title("Hello").description("Page 1").hexpand().frame(minHeight: 300).onClick {
print("Click!")
})
.append(StatusPage().title("World").description("Page 2").hexpand())
.insert(Label("Inserted Page"), at: 0)
let banner = Banner("Okay, so let's close the window.")
Expand Down

0 comments on commit 6044ab6

Please sign in to comment.