[shotwell/wip/gtk4: 127/154] Fix keyboard handling, part 1



commit 215dad78930f41e55b5f8a32c1e5ddb8d8c2bfa5
Author: Jens Georg <mail jensge org>
Date:   Tue Apr 19 11:22:32 2022 +0200

    Fix keyboard handling, part 1

 src/CollectionPage.vala         | 31 +++++++--------
 src/MediaPage.vala              | 33 ++++++++--------
 src/Page.vala                   | 83 +++++++++++++++++++----------------------
 src/PageWindow.vala             | 33 +++++++---------
 src/PhotoPage.vala              | 10 ++---
 src/direct/DirectPhotoPage.vala | 13 +++----
 src/events/EventPage.vala       |  8 ++--
 7 files changed, 95 insertions(+), 116 deletions(-)
---
diff --git a/src/CollectionPage.vala b/src/CollectionPage.vala
index e764f1d5..6a150842 100644
--- a/src/CollectionPage.vala
+++ b/src/CollectionPage.vala
@@ -345,10 +345,10 @@ public abstract class CollectionPage : MediaPage {
         }
     }
     
-    #if 0
-    protected override bool on_app_key_pressed(Gdk.EventKey event) {
+    protected override bool on_app_key_pressed(Gtk.EventControllerKey event, uint keyval, uint keycode, 
Gdk.ModifierType modifiers) {
         bool handled = true;
-        switch (Gdk.keyval_name(event.keyval)) {
+        string? format = null;
+        switch (Gdk.keyval_name(keyval)) {
             case "Page_Up":
             case "KP_Page_Up":
             case "Page_Down":
@@ -357,15 +357,15 @@ public abstract class CollectionPage : MediaPage {
             case "KP_Home":
             case "End":
             case "KP_End":
-                key_press_event(event);
+                // TODO: Should be forwarded
+                handled = false;
             break;
-            
             case "bracketright":
-                activate_action("RotateClockwise");
+                activate_action("RotateClockwise", format);
             break;
             
             case "bracketleft":
-                activate_action("RotateCounterclockwise");
+                activate_action("RotateCounterclockwise", format);
             break;
             
             default:
@@ -373,9 +373,8 @@ public abstract class CollectionPage : MediaPage {
             break;
         }
         
-        return handled ? true : base.on_app_key_pressed(event);
+        return handled ? true : base.on_app_key_pressed(event, keyval, keycode, modifiers);
     }
-    #endif
 
     protected override void on_export() {
         if (exporter != null)
@@ -700,29 +699,27 @@ public abstract class CollectionPage : MediaPage {
             photo));
     }
     
-    #if 0
-    protected override bool on_ctrl_pressed(Gdk.EventKey? event) {
-        Gtk.ToolButton? rotate_button = this.builder.get_object ("ToolRotate") as Gtk.ToolButton;
+    protected override bool on_ctrl_pressed() {
+        Gtk.Button? rotate_button = this.builder.get_object ("ToolRotate") as Gtk.Button;
         if (rotate_button != null) {
             rotate_button.set_action_name ("win.RotateCounterclockwise");
             rotate_button.set_icon_name (Resources.COUNTERCLOCKWISE);
             rotate_button.set_tooltip_text (Resources.ROTATE_CCW_TOOLTIP);
         }
 
-        return base.on_ctrl_pressed(event);
+        return base.on_ctrl_pressed();
     }
     
-    protected override bool on_ctrl_released(Gdk.EventKey? event) {
-        Gtk.ToolButton? rotate_button = this.builder.get_object ("ToolRotate") as Gtk.ToolButton;
+    protected override bool on_ctrl_released() {
+        Gtk.Button? rotate_button = this.builder.get_object ("ToolRotate") as Gtk.Button;
         if (rotate_button != null) {
             rotate_button.set_action_name ("win.RotateClockwise");
             rotate_button.set_icon_name (Resources.CLOCKWISE);
             rotate_button.set_tooltip_text (Resources.ROTATE_CW_TOOLTIP);
         }
 
-        return base.on_ctrl_released(event);
+        return base.on_ctrl_released();
     }
-    #endif
     
     public override SearchViewFilter get_search_view_filter() {
         return search_filter;
diff --git a/src/MediaPage.vala b/src/MediaPage.vala
index 74213149..ca6ebc96 100644
--- a/src/MediaPage.vala
+++ b/src/MediaPage.vala
@@ -414,60 +414,60 @@ public abstract class MediaPage : CheckerboardPage {
         }
     }
 
-#if 0
-    protected override bool on_app_key_pressed(Gdk.EventKey event) {
+    protected override bool on_app_key_pressed(Gtk.EventControllerKey event, uint keyval, uint keycode, 
Gdk.ModifierType modifiers) {
         bool handled = true;
-        switch (Gdk.keyval_name(event.keyval)) {
+        string? format = null; // Workaround for missing annotation
+        switch (Gdk.keyval_name(keyval)) {
             case "equal":
             case "plus":
             case "KP_Add":
-                activate_action("IncreaseSize");
+                activate_action("IncreaseSize", format);
             break;
             
             case "minus":
             case "underscore":
             case "KP_Subtract":
-                activate_action("DecreaseSize");
+                activate_action("DecreaseSize", format);
             break;
             
             case "period":
-                activate_action("IncreaseRating");
+                activate_action("IncreaseRating", format);
             break;
             
             case "comma":
-                activate_action("DecreaseRating");
+                activate_action("DecreaseRating", format);
             break;
             
             case "KP_1":
-                activate_action("RateOne");
+                activate_action("RateOne", format);
             break;
             
             case "KP_2":
-                activate_action("RateTwo");
+                activate_action("RateTwo", format);
             break;
             
             case "KP_3":
-                activate_action("RateThree");
+                activate_action("RateThree", format);
             break;
             
             case "KP_4":
-                activate_action("RateFour");
+                activate_action("RateFour", format);
             break;
             
             case "KP_5":
-                activate_action("RateFive");
+                activate_action("RateFive", format);
             break;
             
             case "KP_0":
-                activate_action("RateUnrated");
+                activate_action("RateUnrated", format);
             break;
             
             case "KP_9":
-                activate_action("RateRejected");
+                activate_action("RateRejected", format);
             break;
             
             case "slash":
-                activate_action("Flag");
+                activate_action("Flag", format);
             break;
             
             default:
@@ -475,9 +475,8 @@ public abstract class MediaPage : CheckerboardPage {
             break;
         }
         
-        return handled ? true : base.on_app_key_pressed(event);
+        return handled ? true : base.on_app_key_pressed(event, keyval, keycode, modifiers);
     }
-    #endif
 
     public override void switched_to() {
         base.switched_to();
diff --git a/src/Page.vala b/src/Page.vala
index 12c06eae..11a85f32 100644
--- a/src/Page.vala
+++ b/src/Page.vala
@@ -426,8 +426,7 @@ public abstract class Page : Gtk.Box {
     }
 
     private bool get_modifiers(out bool ctrl, out bool alt, out bool shift, out bool super) {
-    #if 0
-        if (AppWindow.get_instance().get_window() == null) {
+        if (AppWindow.get_instance().get_surface() == null) {
             ctrl = false;
             alt = false;
             shift = false;
@@ -436,19 +435,17 @@ public abstract class Page : Gtk.Box {
             return false;
         }
         
-        int x, y;
+        double x, y;
         Gdk.ModifierType mask;
         var seat = Gdk.Display.get_default().get_default_seat();
-        AppWindow.get_instance().get_window().get_device_position(seat.get_pointer(), out x, out y, out 
mask);
+        AppWindow.get_instance().get_surface().get_device_position(seat.get_pointer(), out x, out y, out 
mask);
 
         ctrl = (mask & Gdk.ModifierType.CONTROL_MASK) != 0;
-        alt = (mask & Gdk.ModifierType.MOD1_MASK) != 0;
+        alt = (mask & Gdk.ModifierType.ALT_MASK) != 0;
         shift = (mask & Gdk.ModifierType.SHIFT_MASK) != 0;
-        super = (mask & Gdk.ModifierType.MOD4_MASK) != 0; // not SUPER_MASK
+        super = (mask & Gdk.ModifierType.SUPER_MASK) != 0; // not SUPER_MASK
         
         return true;
-        #endif
-        return false;
     }
 
     private void update_modifiers() {
@@ -459,27 +456,25 @@ public abstract class Page : Gtk.Box {
             return;
         }
         
-        #if 0
         if (ctrl_pressed && !ctrl_currently_pressed)
-            on_ctrl_released(null);
+            on_ctrl_released();
         else if (!ctrl_pressed && ctrl_currently_pressed)
-            on_ctrl_pressed(null);
+            on_ctrl_pressed();
 
         if (alt_pressed && !alt_currently_pressed)
-            on_alt_released(null);
+            on_alt_released();
         else if (!alt_pressed && alt_currently_pressed)
-            on_alt_pressed(null);
+            on_alt_pressed();
 
         if (shift_pressed && !shift_currently_pressed)
-            on_shift_released(null);
+            on_shift_released();
         else if (!shift_pressed && shift_currently_pressed)
-            on_shift_pressed(null);
+            on_shift_pressed();
 
         if(super_pressed && !super_currently_pressed)
-            on_super_released(null);
+            on_super_released();
         else if (!super_pressed && super_currently_pressed)
-            on_super_pressed(null);
-            #endif
+            on_super_pressed();
         
         ctrl_pressed = ctrl_currently_pressed;
         alt_pressed = alt_currently_pressed;
@@ -777,54 +772,53 @@ public abstract class Page : Gtk.Box {
         }
     }
 
-#if 0
-    protected virtual bool on_ctrl_pressed(Gdk.EventKey? event) {
+    protected virtual bool on_ctrl_pressed() {
         return false;
     }
     
-    protected virtual bool on_ctrl_released(Gdk.EventKey? event) {
+    protected virtual bool on_ctrl_released() {
         return false;
     }
     
-    protected virtual bool on_alt_pressed(Gdk.EventKey? event) {
+    protected virtual bool on_alt_pressed() {
         return false;
     }
     
-    protected virtual bool on_alt_released(Gdk.EventKey? event) {
+    protected virtual bool on_alt_released() {
         return false;
     }
     
-    protected virtual bool on_shift_pressed(Gdk.EventKey? event) {
+    protected virtual bool on_shift_pressed() {
         return false;
     }
     
-    protected virtual bool on_shift_released(Gdk.EventKey? event) {
+    protected virtual bool on_shift_released() {
         return false;
     }
 
-    protected virtual bool on_super_pressed(Gdk.EventKey? event) {
+    protected virtual bool on_super_pressed() {
         return false;
     }
     
-    protected virtual bool on_super_released(Gdk.EventKey? event) {
+    protected virtual bool on_super_released() {
         return false;
     }
     
-    protected virtual bool on_app_key_pressed(Gdk.EventKey event) {
+    protected virtual bool on_app_key_pressed(Gtk.EventControllerKey event, uint keyval, uint keycode, 
Gdk.ModifierType modifiers) {
         return false;
     }
     
-    protected virtual bool on_app_key_released(Gdk.EventKey event) {
+    protected virtual bool on_app_key_released(Gtk.EventControllerKey event, uint keyval, uint keycode, 
Gdk.ModifierType modifiers) {
         return false;
     }
     
-    public bool notify_app_key_pressed(Gdk.EventKey event) {
+    public bool notify_app_key_pressed(Gtk.EventControllerKey event, uint keyval, uint keycode, 
Gdk.ModifierType modifiers) {
         bool ctrl_currently_pressed, alt_currently_pressed, shift_currently_pressed,
             super_currently_pressed;
         get_modifiers(out ctrl_currently_pressed, out alt_currently_pressed,
             out shift_currently_pressed, out super_currently_pressed);
 
-        switch (Gdk.keyval_name(event.keyval)) {
+        switch (Gdk.keyval_name(keyval)) {
             case "Control_L":
             case "Control_R":
                 if (!ctrl_currently_pressed || ctrl_pressed)
@@ -832,7 +826,7 @@ public abstract class Page : Gtk.Box {
 
                 ctrl_pressed = true;
                 
-                return on_ctrl_pressed(event);
+                return on_ctrl_pressed();
 
             case "Meta_L":
             case "Meta_R":
@@ -843,7 +837,7 @@ public abstract class Page : Gtk.Box {
 
                 alt_pressed = true;
                 
-                return on_alt_pressed(event);
+                return on_alt_pressed();
             
             case "Shift_L":
             case "Shift_R":
@@ -852,7 +846,7 @@ public abstract class Page : Gtk.Box {
 
                 shift_pressed = true;
                 
-                return on_shift_pressed(event);
+                return on_shift_pressed();
             
             case "Super_L":
             case "Super_R":
@@ -861,19 +855,19 @@ public abstract class Page : Gtk.Box {
                 
                 super_pressed = true;
                 
-                return on_super_pressed(event);
+                return on_super_pressed();
         }
         
-        return on_app_key_pressed(event);
+        return on_app_key_pressed(event, keycode, keyval, modifiers);
     }
     
-    public bool notify_app_key_released(Gdk.EventKey event) {
+    public bool notify_app_key_released(Gtk.EventControllerKey event, uint keyval, uint keycode, 
Gdk.ModifierType modifiers) {
         bool ctrl_currently_pressed, alt_currently_pressed, shift_currently_pressed,
             super_currently_pressed;
         get_modifiers(out ctrl_currently_pressed, out alt_currently_pressed,
             out shift_currently_pressed, out super_currently_pressed);
 
-        switch (Gdk.keyval_name(event.keyval)) {
+        switch (Gdk.keyval_name(keyval)) {
             case "Control_L":
             case "Control_R":
                 if (ctrl_currently_pressed || !ctrl_pressed)
@@ -881,7 +875,7 @@ public abstract class Page : Gtk.Box {
 
                 ctrl_pressed = false;
                 
-                return on_ctrl_released(event);
+                return on_ctrl_released();
             
             case "Meta_L":
             case "Meta_R":
@@ -892,7 +886,7 @@ public abstract class Page : Gtk.Box {
 
                 alt_pressed = false;
                 
-                return on_alt_released(event);
+                return on_alt_released();
             
             case "Shift_L":
             case "Shift_R":
@@ -901,7 +895,7 @@ public abstract class Page : Gtk.Box {
 
                 shift_pressed = false;
                 
-                return on_shift_released(event);
+                return on_shift_released();
 
             case "Super_L":
             case "Super_R":
@@ -910,12 +904,13 @@ public abstract class Page : Gtk.Box {
 
                 super_pressed = false;
                 
-                return on_super_released(event);
+                return on_super_released();
         }
         
-        return on_app_key_released(event);
+        return on_app_key_released(event, keycode, keyval, modifiers);
     }
-    
+
+    #if 0
     public bool notify_app_focus_in(Gdk.EventFocus event) {
         update_modifiers();
         
diff --git a/src/PageWindow.vala b/src/PageWindow.vala
index 2c587f88..2fb8522e 100644
--- a/src/PageWindow.vala
+++ b/src/PageWindow.vala
@@ -17,16 +17,17 @@ public abstract class PageWindow : Gtk.ApplicationWindow {
         Object(application: Application.get_instance().get_system_app());
 
         // the current page needs to know when modifier keys are pressed
-        #if 0
-        add_events(Gdk.EventMask.KEY_PRESS_MASK | Gdk.EventMask.KEY_RELEASE_MASK
-            | Gdk.EventMask.STRUCTURE_MASK);
-            #endif
         set_show_menubar(true);
 
         notify["maximized"].connect(synthesize_configure_event);
         notify["default-width"].connect(synthesize_configure_event);
         notify["default-height"].connect(synthesize_configure_event);
         notify["fullscreened"].connect(synthesize_configure_event);
+
+        var key_controller = new Gtk.EventControllerKey();
+        key_controller.key_pressed.connect(key_press_event);
+        key_controller.key_released.connect(key_release_event);
+        ((Gtk.Widget)this).add_controller(key_controller);
     }
 
     private void synthesize_configure_event() {
@@ -78,27 +79,19 @@ public abstract class PageWindow : Gtk.ApplicationWindow {
         switched_pages(old_page, null);
     }
 
-    #if 0
-    public override bool key_press_event(Gdk.EventKey event) {
-        if (get_focus() is Gtk.Entry && get_focus().key_press_event(event))
-            return true;
-
-        if (current_page != null && current_page.notify_app_key_pressed(event))
+    public bool key_press_event(Gtk.EventControllerKey event, uint keyval, uint keycode, Gdk.ModifierType 
modifiers) {
+        if (current_page != null && current_page.notify_app_key_pressed(event, keyval, keycode, modifiers))
             return true;
 
-        return (base.key_press_event != null) ? base.key_press_event(event) : false;
+        return false;
     }
 
-    public override bool key_release_event(Gdk.EventKey event) {
-        if (get_focus() is Gtk.Entry && get_focus().key_release_event(event))
-            return true;
-
-        if (current_page != null && current_page.notify_app_key_released(event))
-            return true;
-
-        return (base.key_release_event != null) ? base.key_release_event(event) : false;
+    public void key_release_event(Gtk.EventControllerKey event, uint keyval, uint keycode, Gdk.ModifierType 
modifiers) {
+        if (current_page != null)
+            current_page.notify_app_key_released(event, keyval, keycode, modifiers);
     }
 
+    #if 0
     public override bool focus_in_event(Gdk.EventFocus event) {
         if (current_page != null && current_page.notify_app_focus_in(event))
             return true;
@@ -130,6 +123,6 @@ public abstract class PageWindow : Gtk.ApplicationWindow {
             return;
         }
 
-        set_cursor_from_name("default");
+        set_cursor_from_name(null);
     }
 }
diff --git a/src/PhotoPage.vala b/src/PhotoPage.vala
index 097e4102..e80e638e 100644
--- a/src/PhotoPage.vala
+++ b/src/PhotoPage.vala
@@ -2020,8 +2020,7 @@ public abstract class EditingHostPage : SinglePhotoPage {
         });
     }
 
-#if 0
-    protected override bool on_ctrl_pressed(Gdk.EventKey? event) {
+    protected override bool on_ctrl_pressed() {
         rotate_button.set_icon_name(Resources.COUNTERCLOCKWISE);
         rotate_button.set_label(Resources.ROTATE_CCW_LABEL);
         rotate_button.set_tooltip_text(Resources.ROTATE_CCW_TOOLTIP);
@@ -2031,10 +2030,10 @@ public abstract class EditingHostPage : SinglePhotoPage {
         if (current_tool == null)
             swap_out_original();
 
-        return base.on_ctrl_pressed(event);
+        return base.on_ctrl_pressed();
     }
     
-    protected override bool on_ctrl_released(Gdk.EventKey? event) {
+    protected override bool on_ctrl_released() {
         rotate_button.set_icon_name(Resources.CLOCKWISE);
         rotate_button.set_label(Resources.ROTATE_CW_LABEL);
         rotate_button.set_tooltip_text(Resources.ROTATE_CW_TOOLTIP);
@@ -2044,9 +2043,8 @@ public abstract class EditingHostPage : SinglePhotoPage {
         if (current_tool == null && get_shift_pressed() && !get_alt_pressed())
             swap_in_original();
         
-        return base.on_ctrl_released(event);
+        return base.on_ctrl_released();
     }
-    #endif
     
     protected void on_tool_button_toggled(Gtk.ToggleButton toggle, EditingTools.EditingTool.Factory factory) 
{
         // if the button is an activate, deactivate any current tool running; if the button is
diff --git a/src/direct/DirectPhotoPage.vala b/src/direct/DirectPhotoPage.vala
index 5b794ecd..894ef5bf 100644
--- a/src/direct/DirectPhotoPage.vala
+++ b/src/direct/DirectPhotoPage.vala
@@ -438,17 +438,17 @@ public class DirectPhotoPage : EditingHostPage {
             DesktopIntegration.send_to((Gee.Collection<Photo>) get_view().get_selected_sources());
     }
     
-    #if 0
-    protected override bool on_app_key_pressed(Gdk.EventKey event) {
+    protected override bool on_app_key_pressed(Gtk.EventControllerKey event, uint keyval, uint keycode, 
Gdk.ModifierType modifiers) {
         bool handled = true;
+        string? format = null;
         
-        switch (Gdk.keyval_name(event.keyval)) {
+        switch (Gdk.keyval_name(keyval)) {
             case "bracketright":
-                activate_action("RotateClockwise");
+                activate_action("RotateClockwise", format);
             break;
             
             case "bracketleft":
-                activate_action("RotateCounterclockwise");
+                activate_action("RotateCounterclockwise", format);
             break;
             
             default:
@@ -456,9 +456,8 @@ public class DirectPhotoPage : EditingHostPage {
             break;
         }
         
-        return handled ? true : base.on_app_key_pressed(event);
+        return handled ? true : base.on_app_key_pressed(event, keyval, keycode, modifiers);
     }
-    #endif
     
     private void on_print() {
         if (get_view().get_selected_count() > 0) {
diff --git a/src/events/EventPage.vala b/src/events/EventPage.vala
index 8a2a5e2c..c7692f83 100644
--- a/src/events/EventPage.vala
+++ b/src/events/EventPage.vala
@@ -22,21 +22,19 @@ public class EventPage : CollectionPage {
         return page_event;
     }
     
-    #if 0
-    protected override bool on_app_key_pressed(Gdk.EventKey event) {
+    protected override bool on_app_key_pressed(Gtk.EventControllerKey event, uint keyval, uint keycode, 
Gdk.ModifierType modifiers) {
         // If and only if one image is selected, propagate F2 to the rest of
         // the window, otherwise, consume it here - if we don't do this, it'll
         // either let us re-title multiple images at the same time or
         // spuriously highlight the event name in the sidebar for editing...
-        if (Gdk.keyval_name(event.keyval) == "F2") {
+        if (Gdk.keyval_name(keyval) == "F2") {
             if (get_view().get_selected_count() != 1) {
                 return true; 
             }
         }
          
-        return base.on_app_key_pressed(event);
+        return base.on_app_key_pressed(event, keyval, keycode, modifiers);
     }
-    #endif
     
     ~EventPage() {
         Event.global.items_altered.disconnect(on_events_altered);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]