From 0ac4b622a467fc5e84c018b28094d2b654252069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= Date: Thu, 10 Aug 2023 21:10:50 -0600 Subject: [PATCH 1/2] library: Port Event Controllers demo to Vala --- src/Library/demos/Event Controllers/main.vala | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/Library/demos/Event Controllers/main.vala diff --git a/src/Library/demos/Event Controllers/main.vala b/src/Library/demos/Event Controllers/main.vala new file mode 100644 index 000000000..566980b47 --- /dev/null +++ b/src/Library/demos/Event Controllers/main.vala @@ -0,0 +1,90 @@ +#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1 + +private bool ctrl_pressed = false; + +public void main () { + // Gtk.Window hides Gtk.Widget's add_controller method, thus we need to access it as a Widget. + Gtk.Widget window = workbench.window; + + var pic1 = (Gtk.Picture) workbench.builder.get_object ("pic1"); + var pic2 = (Gtk.Picture) workbench.builder.get_object ("pic2"); + + pic1.file = File.new_for_uri (workbench.resolve ("image1.png")); + pic2.file = File.new_for_uri (workbench.resolve ("image2.png")); + + var stack = (Gtk.Stack) workbench.builder.get_object ("stack"); + var primary_button = (Gtk.Button) workbench.builder.get_object ("primary_button"); + var middle_button = (Gtk.Button) workbench.builder.get_object ("middle_button"); + var secondary_button = (Gtk.Button) workbench.builder.get_object ("secondary_button"); + var ctrl_button = (Gtk.Button) workbench.builder.get_object ("ctrl_button"); + + var key_controller = new Gtk.EventControllerKey (); + window.add_controller (key_controller); + + key_controller.key_pressed.connect ((keyval, keycode, state) => { + if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R) { + ctrl_pressed = true; + } + return true; + }); + + key_controller.key_released.connect ((keyval, keycode, state) => { + if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R) { + ctrl_pressed = false; + } + }); + + ctrl_button.clicked.connect (() => { + if (ctrl_pressed) { + ctrl_button.label = "Click to Deactivate"; + ctrl_button.add_css_class ("suggested-action"); + } else { + ctrl_button.label = "Ctrl + Click to Activate"; + ctrl_button.remove_css_class ("suggested-action"); + } + }); + + var gesture_click = new Gtk.GestureClick () { + button = 0 + }; + window.add_controller (gesture_click); + + gesture_click.pressed.connect ((gesture, n_press, x, y) => { + switch (gesture.get_current_button ()) { + case Gdk.BUTTON_PRIMARY: + primary_button.add_css_class ("suggested-action"); + break; + case Gdk.BUTTON_MIDDLE: + middle_button.add_css_class ("suggested-action"); + break; + case Gdk.BUTTON_SECONDARY: + secondary_button.add_css_class ("suggested-action"); + break; + } + }); + + gesture_click.released.connect ((gesture, n_press, x, y) => { + switch (gesture.get_current_button ()) { + case Gdk.BUTTON_PRIMARY: + primary_button.remove_css_class ("suggested-action"); + break; + case Gdk.BUTTON_MIDDLE: + middle_button.remove_css_class ("suggested-action"); + break; + case Gdk.BUTTON_SECONDARY: + secondary_button.remove_css_class ("suggested-action"); + break; + } + }); + + var gesture_swipe = new Gtk.GestureSwipe (); + stack.add_controller (gesture_swipe); + + gesture_swipe.swipe.connect ((vel_x, vel_y) => { + if (vel_x > 0) { + stack.visible_child_name = "pic1"; + } else { + stack.visible_child_name = "pic2"; + } + }); +} From 28c2c8a11b0f06c3c7634ab3ea001980bcdcceb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= Date: Fri, 11 Aug 2023 17:08:12 -0600 Subject: [PATCH 2/2] library: Cast workbench.window when add_controller is called --- src/Library/demos/Event Controllers/main.vala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Library/demos/Event Controllers/main.vala b/src/Library/demos/Event Controllers/main.vala index 566980b47..e2328c497 100644 --- a/src/Library/demos/Event Controllers/main.vala +++ b/src/Library/demos/Event Controllers/main.vala @@ -3,8 +3,7 @@ private bool ctrl_pressed = false; public void main () { - // Gtk.Window hides Gtk.Widget's add_controller method, thus we need to access it as a Widget. - Gtk.Widget window = workbench.window; + Gtk.Window window = workbench.window; var pic1 = (Gtk.Picture) workbench.builder.get_object ("pic1"); var pic2 = (Gtk.Picture) workbench.builder.get_object ("pic2"); @@ -19,7 +18,8 @@ public void main () { var ctrl_button = (Gtk.Button) workbench.builder.get_object ("ctrl_button"); var key_controller = new Gtk.EventControllerKey (); - window.add_controller (key_controller); + // Gtk.Window hides Gtk.Widget's add_controller method, thus we need to cast it + ((Gtk.Widget) window).add_controller (key_controller); key_controller.key_pressed.connect ((keyval, keycode, state) => { if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R) { @@ -47,7 +47,7 @@ public void main () { var gesture_click = new Gtk.GestureClick () { button = 0 }; - window.add_controller (gesture_click); + ((Gtk.Widget) window).add_controller (gesture_click); gesture_click.pressed.connect ((gesture, n_press, x, y) => { switch (gesture.get_current_button ()) {