[gtk/columnview-reordering: 47/48] columnview: Allow to cancel reorder with Escape



commit c58eb9c2766af3ae04c62cb374b628df07ca0477
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Dec 20 19:24:01 2019 -0500

    columnview: Allow to cancel reorder with Escape
    
    The treeview does this too.

 gtk/gtkcolumnview.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 3 deletions(-)
---
diff --git a/gtk/gtkcolumnview.c b/gtk/gtkcolumnview.c
index 73704328b7..4666e02b93 100644
--- a/gtk/gtkcolumnview.c
+++ b/gtk/gtkcolumnview.c
@@ -39,6 +39,7 @@
 #include "gtkgesturedrag.h"
 #include "gtkeventcontrollermotion.h"
 #include "gtkdragsource.h"
+#include "gtkeventcontrollerkey.h"
 
 /**
  * SECTION:gtkcolumnview
@@ -79,6 +80,8 @@ struct _GtkColumnView
   int drag_x;
   int drag_offset;
   int drag_column_x;
+
+  GtkGesture *drag_gesture;
 };
 
 struct _GtkColumnViewClass
@@ -654,6 +657,8 @@ header_drag_begin (GtkGestureDrag *gesture,
           int size;
 
           gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
+          if (!gtk_widget_has_focus (GTK_WIDGET (self)))
+            gtk_widget_grab_focus (GTK_WIDGET (self));
 
           gtk_column_view_column_get_allocation (column, NULL, &size);
           gtk_column_view_column_set_fixed_width (column, size);
@@ -701,14 +706,24 @@ header_drag_end (GtkGestureDrag *gesture,
     }
   else if (self->in_column_reorder)
     {
+      GdkEventSequence *sequence;
       GtkColumnViewColumn *column;
       GtkWidget *header;
       int i;
 
+      self->in_column_reorder = FALSE;
+
+      if (self->drag_pos == -1)
+        return;
+
       column = g_list_model_get_item (G_LIST_MODEL (self->columns), self->drag_pos);
       header = gtk_column_view_column_get_header (column);
       gtk_style_context_remove_class (gtk_widget_get_style_context (header), "dnd");
 
+      sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
+      if (!gtk_gesture_handles_sequence (GTK_GESTURE (gesture), sequence))
+        return;
+
       for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (self->columns)); i++)
         {
           GtkColumnViewColumn *col = g_list_model_get_item (G_LIST_MODEL (self->columns), i);
@@ -730,8 +745,6 @@ header_drag_end (GtkGestureDrag *gesture,
         }
 
       g_object_unref (column);
-
-      self->in_column_reorder = FALSE;
     }
 }
 
@@ -771,11 +784,15 @@ header_drag_update (GtkGestureDrag *gesture,
                     double          offset_y,
                     GtkColumnView  *self)
 {
+  GdkEventSequence *sequence;
   double start_x, x;
 
-  if (self->drag_pos == -1)
+  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
+  if (!gtk_gesture_handles_sequence (GTK_GESTURE (gesture), sequence))
     return;
 
+  if (self->drag_pos == -1)
+    return;
 
   if (!self->in_column_resize && !self->in_column_reorder)
     {
@@ -791,6 +808,9 @@ header_drag_update (GtkGestureDrag *gesture,
           gtk_style_context_add_class (gtk_widget_get_style_context (header), "dnd");
 
           gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
+          if (!gtk_widget_has_focus (GTK_WIDGET (self)))
+            gtk_widget_grab_focus (GTK_WIDGET (self));
+
           self->in_column_reorder = TRUE;
 
           g_object_unref (column);
@@ -841,6 +861,23 @@ header_motion (GtkEventControllerMotion *controller,
     gtk_widget_set_cursor (self->header, NULL);
 }
 
+static gboolean
+header_key_pressed (GtkEventControllerKey *controller,
+                    guint                  keyval,
+                    guint                  keycode,
+                    GdkModifierType        modifiers,
+                    GtkColumnView         *self)
+{
+  if (self->in_column_reorder)
+    {
+      if (keyval == GDK_KEY_Escape)
+        gtk_gesture_set_state (self->drag_gesture, GTK_EVENT_SEQUENCE_DENIED);
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
 static void
 gtk_column_view_init (GtkColumnView *self)
 {
@@ -859,11 +896,16 @@ gtk_column_view_init (GtkColumnView *self)
   g_signal_connect (controller, "drag-end", G_CALLBACK (header_drag_end), self);
   gtk_event_controller_set_propagation_phase (controller, GTK_PHASE_CAPTURE);
   gtk_widget_add_controller (self->header, controller);
+  self->drag_gesture = GTK_GESTURE (controller);
 
   controller = gtk_event_controller_motion_new ();
   g_signal_connect (controller, "motion", G_CALLBACK (header_motion), self);
   gtk_widget_add_controller (self->header, controller);
 
+  controller = gtk_event_controller_key_new ();
+  g_signal_connect (controller, "key-pressed", G_CALLBACK (header_key_pressed), self);
+  gtk_widget_add_controller (GTK_WIDGET (self), controller);
+
   self->sorter = gtk_column_view_sorter_new ();
   self->factory = gtk_column_list_item_factory_new (self);
   self->listview = GTK_LIST_VIEW (gtk_list_view_new_with_factory (
@@ -877,6 +919,7 @@ gtk_column_view_init (GtkColumnView *self)
                           g_quark_from_static_string (I_("view")));
 
   gtk_widget_set_overflow (GTK_WIDGET (self), GTK_OVERFLOW_HIDDEN);
+  gtk_widget_set_focusable (GTK_WIDGET (self), TRUE);
 
   self->reorderable = TRUE;
 }


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