workbenchdev / demos

Demos of GNOME technologies - GTK, libadwaita, CSS, portals, ...
Creative Commons Zero v1.0 Universal
11 stars 15 forks source link

Event controllers demo promotes bad practice for key modifiers #183

Open gjask opened 4 months ago

gjask commented 4 months ago

Event controller demo showcases button click with Ctrl key modifier by creating Gtk.EventControllerKey and tracking key state in global variable. This approach is at least bit quirky (eg. fails to track key state changes made off-focus) and seems like a bad practice (global variable, really?). I know it was meant to find show case for Gtk.EventControllerKey but I believe not teaching beginners (such as myself) bad practices is important. So maybe finding different show case is in order.

Proper technique for handling clicks and other events with key modifier is to use controller.get_current_event_state() in callback. For example like this.

def on_button_release(controller, clicks, x, y):
    if (controller.get_current_event_state() & Gdk.ModifierType.CONTROL_MASK):
        print("Ctrl was pressed")
    else:
        print("Ctrl was not pressed")

click = Gtk.GestureClick()
click.connect("released", on_button_release)
widget.add_controller(click)
gjask commented 4 months ago

I have created this issue, because originally I have only sent PR which didn't get much attention. I don't know if it is normal time span for reaction or because issue was missing. Also if somebody wanted to propose better solution it is good to have issue to track all progress on this.

My proposed solution is: https://github.com/workbenchdev/demos/pull/181