[libhandy/wip/haecker-felix/flap-widget] Make modal actually handle esc



commit e530054233adb3ab71d7fe5e1cb977da0a544020
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Wed Nov 25 15:11:46 2020 +0500

    Make modal actually handle esc

 src/hdy-flap.c | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)
---
diff --git a/src/hdy-flap.c b/src/hdy-flap.c
index 58b4d062..0bcad038 100644
--- a/src/hdy-flap.c
+++ b/src/hdy-flap.c
@@ -94,6 +94,7 @@ struct _HdyFlap
 
   gboolean modal;
   GtkGesture *click_gesture;
+  GtkEventController *key_controller;
 };
 
 static void hdy_flap_buildable_init (GtkBuildableIface *iface);
@@ -417,6 +418,24 @@ pressed_cb (GtkGestureMultiPress *gesture,
   hdy_flap_set_reveal_flap (self, FALSE);
 }
 
+static gboolean
+key_pressed_cb (GtkEventControllerKey *controller,
+                guint                  keyval,
+                guint                  keycode,
+                GdkModifierType        modifiers,
+                HdyFlap               *self)
+{
+  if (keyval == GDK_KEY_Escape &&
+      self->reveal_progress > 0 &&
+      self->fold_progress > 0) {
+    hdy_flap_set_reveal_flap (self, FALSE);
+
+    return GDK_EVENT_STOP;
+  }
+
+  return GDK_EVENT_PROPAGATE;
+}
+
 static void
 register_window (HdyFlap   *self,
                  ChildInfo *info)
@@ -1355,6 +1374,7 @@ hdy_flap_dispose (GObject *object)
   g_clear_object (&self->shadow_helper);
   g_clear_object (&self->tracker);
   g_clear_object (&self->click_gesture);
+  g_clear_object (&self->key_controller);
 
   G_OBJECT_CLASS (hdy_flap_parent_class)->dispose (object);
 }
@@ -1600,6 +1620,8 @@ hdy_flap_class_init (HdyFlapClass *klass)
 static void
 hdy_flap_init (HdyFlap *self)
 {
+  gtk_widget_add_events (GTK_WIDGET (self), GDK_KEY_PRESS_MASK);
+
   self->orientation = GTK_ORIENTATION_HORIZONTAL;
   self->flap_position = GTK_PACK_START;
   self->fold_policy = HDY_FLAP_FOLD_POLICY_AUTO;
@@ -1631,6 +1653,11 @@ hdy_flap_init (HdyFlap *self)
   gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (self->click_gesture),
                                               GTK_PHASE_CAPTURE);
   g_signal_connect_object (self->click_gesture, "pressed", G_CALLBACK (pressed_cb), self, 0);
+
+  self->key_controller = gtk_event_controller_key_new (GTK_WIDGET (self));
+  gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (self->key_controller),
+                                              GTK_PHASE_BUBBLE);
+  g_signal_connect_object (self->key_controller, "key-pressed", G_CALLBACK (key_pressed_cb), self, 0);
 }
 
 static void
@@ -2331,8 +2358,6 @@ void
 hdy_flap_set_modal (HdyFlap  *self,
                     gboolean  modal)
 {
-  GtkPropagationPhase phase = GTK_PHASE_NONE;
-
   g_return_if_fail (HDY_IS_FLAP (self));
 
   modal = !!modal;
@@ -2342,11 +2367,10 @@ hdy_flap_set_modal (HdyFlap  *self,
 
   self->modal = modal;
 
-  if (modal)
-    phase = GTK_PHASE_CAPTURE;
-
   gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (self->click_gesture),
-                                              phase);
+                                              modal ? GTK_PHASE_CAPTURE : GTK_PHASE_NONE);
+  gtk_event_controller_set_propagation_phase (self->key_controller,
+                                              modal ? GTK_PHASE_BUBBLE : GTK_PHASE_NONE);
 
   gtk_widget_queue_allocate (GTK_WIDGET (self));
 


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