From d6543f8570e32421b0859b3c5920e6341aa6745d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0krle?= Date: Mon, 24 Jun 2024 18:45:30 +0200 Subject: [PATCH] Improve Event Controllers --- src/Event Controllers/main.blp | 2 +- src/Event Controllers/main.js | 32 +++++++++++++---------------- src/Event Controllers/main.py | 36 +++++++++++++-------------------- src/Event Controllers/main.vala | 27 ++++++++++--------------- 4 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/Event Controllers/main.blp b/src/Event Controllers/main.blp index 99bde6d8..86439229 100644 --- a/src/Event Controllers/main.blp +++ b/src/Event Controllers/main.blp @@ -23,7 +23,7 @@ Adw.StatusPage { } Gtk.Button ctrl_button { - label: _("Ctrl + Click to Activate"); + label: _("Ctrl Key"); width-request: 200; halign: center; } diff --git a/src/Event Controllers/main.js b/src/Event Controllers/main.js index 935233d0..47e77811 100644 --- a/src/Event Controllers/main.js +++ b/src/Event Controllers/main.js @@ -15,29 +15,17 @@ const secondary_button = workbench.builder.get_object("secondary_button"); stack_picture_1.file = Gio.File.new_for_uri(workbench.resolve("image1.png")); stack_picture_2.file = Gio.File.new_for_uri(workbench.resolve("image2.png")); -let ctrl_pressed = false; - // Key controller to detect when the Ctrl key is pressed and released const key_controller = new Gtk.EventControllerKey(); window.add_controller(key_controller); key_controller.connect("key-pressed", (_self, keyval, _keycode, _state) => { if (keyval === Gdk.KEY_Control_L || keyval === Gdk.KEY_Control_R) { - ctrl_pressed = true; + ctrl_button.add_css_class("suggested-action"); } }); key_controller.connect("key-released", (_self, keyval, _keycode, _state) => { if (keyval === Gdk.KEY_Control_L || keyval === Gdk.KEY_Control_R) { - ctrl_pressed = false; - } -}); - -ctrl_button.connect("clicked", () => { - 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"); } }); @@ -48,33 +36,41 @@ const gesture_click = new Gtk.GestureClick({ button: 0 }); window.add_controller(gesture_click); gesture_click.connect("pressed", (_self, _n_press, _x, _y) => { - switch (gesture_click.get_current_button()) { + let css_class = "suggested-action"; + if (_self.get_current_event_state() & Gdk.ModifierType.CONTROL_MASK) { + css_class = "destructive-action"; + } + + switch (_self.get_current_button()) { case Gdk.BUTTON_PRIMARY: - primary_button.add_css_class("suggested-action"); + primary_button.add_css_class(css_class); break; case Gdk.BUTTON_MIDDLE: - middle_button.add_css_class("suggested-action"); + middle_button.add_css_class(css_class); break; case Gdk.BUTTON_SECONDARY: - secondary_button.add_css_class("suggested-action"); + secondary_button.add_css_class(css_class); break; } }); gesture_click.connect("released", (_self, _n_press, _x, _y) => { - switch (gesture_click.get_current_button()) { + switch (_self.get_current_button()) { case Gdk.BUTTON_PRIMARY: primary_button.remove_css_class("suggested-action"); + primary_button.remove_css_class("destructive-action"); break; case Gdk.BUTTON_MIDDLE: middle_button.remove_css_class("suggested-action"); + middle_button.remove_css_class("destructive-action"); break; case Gdk.BUTTON_SECONDARY: secondary_button.remove_css_class("suggested-action"); + secondary_button.remove_css_class("destructive-action"); break; } }); diff --git a/src/Event Controllers/main.py b/src/Event Controllers/main.py index a614bd50..00dc75ca 100644 --- a/src/Event Controllers/main.py +++ b/src/Event Controllers/main.py @@ -4,7 +4,6 @@ gi.require_version("Gdk", "4.0") from gi.repository import Gdk, Gio, Gtk -from gettext import gettext as _ import workbench window = workbench.window @@ -19,52 +18,47 @@ stack_picture_1.set_file(Gio.File.new_for_uri(workbench.resolve("image1.png"))) stack_picture_2.set_file(Gio.File.new_for_uri(workbench.resolve("image2.png"))) -ctrl_pressed = False - def on_key_pressed(_self, keyval, _keycode, _state): if keyval == Gdk.KEY_Control_L or keyval == Gdk.KEY_Control_R: - global ctrl_pressed - ctrl_pressed = True + ctrl_button.add_css_class("suggested-action") def on_key_released(_self, keyval, _keycode, _state): if keyval == Gdk.KEY_Control_L or keyval == Gdk.KEY_Control_R: - global ctrl_pressed - ctrl_pressed = False - - -def on_clicked(_self): - if ctrl_pressed: - ctrl_button.set_label(_("Click to Deactivate")) - ctrl_button.add_css_class("suggested-action") - else: - ctrl_button.set_label(_("Ctrl + Click to Activate")) ctrl_button.remove_css_class("suggested-action") def on_pressed(_self, _n_press, _x, _y): - match gesture_click.get_current_button(): + if _self.get_current_event_state() & Gdk.ModifierType.CONTROL_MASK: + css_class = "destructive-action" + else: + css_class = "suggested-action" + + match _self.get_current_button(): case Gdk.BUTTON_PRIMARY: - primary_button.add_css_class("suggested-action") + primary_button.add_css_class(css_class) case Gdk.BUTTON_MIDDLE: - middle_button.add_css_class("suggested-action") + middle_button.add_css_class(css_class) case Gdk.BUTTON_SECONDARY: - secondary_button.add_css_class("suggested-action") + secondary_button.add_css_class(css_class) def on_released(_self, _n_press, _x, _y): - match gesture_click.get_current_button(): + match _self.get_current_button(): case Gdk.BUTTON_PRIMARY: primary_button.remove_css_class("suggested-action") + primary_button.remove_css_class("destructive-action") case Gdk.BUTTON_MIDDLE: middle_button.remove_css_class("suggested-action") + middle_button.remove_css_class("destructive-action") case Gdk.BUTTON_SECONDARY: secondary_button.remove_css_class("suggested-action") + secondary_button.remove_css_class("destructive-action") def on_swipe(_self, vel_x, _vel_y): @@ -80,8 +74,6 @@ def on_swipe(_self, vel_x, _vel_y): key_controller.connect("key-pressed", on_key_pressed) key_controller.connect("key-released", on_key_released) -ctrl_button.connect("clicked", on_clicked) - # Detect pointer button press and release events gesture_click = Gtk.GestureClick(button=0) window.add_controller(gesture_click) diff --git a/src/Event Controllers/main.vala b/src/Event Controllers/main.vala index 0423a122..d2e7ee8f 100644 --- a/src/Event Controllers/main.vala +++ b/src/Event Controllers/main.vala @@ -1,7 +1,5 @@ #! /usr/bin/env -S vala workbench.vala --pkg libadwaita-1 -private bool ctrl_pressed = false; - public void main () { Gtk.Window window = workbench.window; @@ -23,23 +21,13 @@ public void main () { key_controller.key_pressed.connect ((keyval, keycode, state) => { if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R) { - ctrl_pressed = true; + ctrl_button.add_css_class ("suggested-action"); } 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"); } }); @@ -50,15 +38,19 @@ public void main () { ((Gtk.Widget) window).add_controller (gesture_click); gesture_click.pressed.connect ((gesture, n_press, x, y) => { + var css_class = "suggested-action"; + if ((gesture.get_current_event_state () & Gdk.ModifierType.CONTROL_MASK) > 0) { + css_class = "destructive-action"; + } switch (gesture.get_current_button ()) { case Gdk.BUTTON_PRIMARY: - primary_button.add_css_class ("suggested-action"); + primary_button.add_css_class (css_class); break; case Gdk.BUTTON_MIDDLE: - middle_button.add_css_class ("suggested-action"); + middle_button.add_css_class (css_class); break; case Gdk.BUTTON_SECONDARY: - secondary_button.add_css_class ("suggested-action"); + secondary_button.add_css_class (css_class); break; } }); @@ -67,12 +59,15 @@ public void main () { switch (gesture.get_current_button ()) { case Gdk.BUTTON_PRIMARY: primary_button.remove_css_class ("suggested-action"); + primary_button.remove_css_class ("destructive-action"); break; case Gdk.BUTTON_MIDDLE: middle_button.remove_css_class ("suggested-action"); + middle_button.remove_css_class ("destructive-action"); break; case Gdk.BUTTON_SECONDARY: secondary_button.remove_css_class ("suggested-action"); + secondary_button.remove_css_class ("destructive-action"); break; } });