[gtk/wip/otte/listview: 60/152] listview: Add list.scroll_to_item action



commit e50f2d6c991b4b08314f5f93a75c35b319ff7b26
Author: Benjamin Otte <otte redhat com>
Date:   Fri Oct 4 06:50:47 2019 +0200

    listview: Add list.scroll_to_item action
    
    The action scrolls the given item into view.
    
    Listitems activate this action when they gain focus.

 gtk/gtklistitem.c | 18 ++++++++++++++++++
 gtk/gtklistview.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
---
diff --git a/gtk/gtklistitem.c b/gtk/gtklistitem.c
index f5a00ac036..842f04b70d 100644
--- a/gtk/gtklistitem.c
+++ b/gtk/gtklistitem.c
@@ -23,6 +23,7 @@
 
 #include "gtkbinlayout.h"
 #include "gtkcssnodeprivate.h"
+#include "gtkeventcontrollerfocus.h"
 #include "gtkgestureclick.h"
 #include "gtkintl.h"
 #include "gtkmain.h"
@@ -268,6 +269,18 @@ gtk_list_item_click_gesture_pressed (GtkGestureClick *gesture,
     gtk_widget_grab_focus (widget);
 }
 
+static void
+gtk_list_item_enter_cb (GtkEventControllerFocus *controller,
+                        GtkListItem             *self)
+{
+  GtkWidget *widget = GTK_WIDGET (self);
+
+  gtk_widget_activate_action (widget,
+                              "list.scroll-to-item",
+                              "u",
+                              self->position);
+}
+
 static void
 gtk_list_item_click_gesture_released (GtkGestureClick *gesture,
                                       int              n_press,
@@ -289,6 +302,7 @@ gtk_list_item_click_gesture_canceled (GtkGestureClick  *gesture,
 static void
 gtk_list_item_init (GtkListItem *self)
 {
+  GtkEventController *controller;
   GtkGesture *gesture;
 
   self->selectable = TRUE;
@@ -308,6 +322,10 @@ gtk_list_item_init (GtkListItem *self)
   g_signal_connect (gesture, "cancel",
                     G_CALLBACK (gtk_list_item_click_gesture_canceled), self);
   gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (gesture));
+
+  controller = gtk_event_controller_focus_new ();
+  g_signal_connect (controller, "enter", G_CALLBACK (gtk_list_item_enter_cb), self);
+  gtk_widget_add_controller (GTK_WIDGET (self), controller);
 }
 
 GtkListItem *
diff --git a/gtk/gtklistview.c b/gtk/gtklistview.c
index 63f34f455a..6d0283139d 100644
--- a/gtk/gtklistview.c
+++ b/gtk/gtklistview.c
@@ -731,6 +731,48 @@ gtk_list_view_select_item (GtkWidget  *widget,
     }
 }
 
+static void
+gtk_list_view_scroll_to_item (GtkWidget  *widget,
+                              const char *action_name,
+                              GVariant   *parameter)
+{
+  GtkListView *self = GTK_LIST_VIEW (widget);
+  ListRow *row;
+  guint pos;
+
+  if (!g_variant_check_format_string (parameter, "u", FALSE))
+    return;
+
+  g_variant_get (parameter, "u", &pos);
+  row = gtk_list_item_manager_get_nth (self->item_manager, pos, NULL);
+  if (row == NULL)
+    return;
+
+  if (row->parent.widget)
+    {
+      int y = list_row_get_y (self, row);
+      int start = gtk_adjustment_get_value (self->adjustment[GTK_ORIENTATION_VERTICAL]);
+      int height = gtk_widget_get_height (GTK_WIDGET (self));
+      double align;
+
+      if (y < start)
+        align = 0.0;
+      else if (y + row->height > start + height)
+        align = 1.0;
+      else
+        align = (double) (y - start) / (height - row->height);
+
+      gtk_list_view_set_anchor (self, pos, align);
+    }
+  else
+    {
+      if (pos < gtk_list_item_tracker_get_position (self->item_manager, self->anchor))
+        gtk_list_view_set_anchor (self, pos, 0.0);
+      else
+        gtk_list_view_set_anchor (self, pos, 1.0);
+    }
+}
+
 static void
 gtk_list_view_class_init (GtkListViewClass *klass)
 {
@@ -810,6 +852,18 @@ gtk_list_view_class_init (GtkListViewClass *klass)
                                    "(ubb)",
                                    gtk_list_view_select_item);
 
+  /**
+   * GtkListView|list.scroll-to-item:
+   * @position: position of item to scroll to
+   *
+   * Scrolls to the item given in @position with the minimum amount
+   * of scrolling required. If the item is already visible, nothing happens.
+   */
+  gtk_widget_class_install_action (widget_class,
+                                   "list.scroll-to-item",
+                                   "u",
+                                   gtk_list_view_scroll_to_item);
+
   gtk_widget_class_set_css_name (widget_class, I_("list"));
 }
 


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