listview: Move bounds check into base class
authorBenjamin Otte <otte@redhat.com>
Thu, 9 Mar 2023 03:02:22 +0000 (04:02 +0100)
committerBenjamin Otte <otte@redhat.com>
Fri, 10 Mar 2023 04:26:28 +0000 (05:26 +0100)
This way, listview and gridview don't need to check if the rect is out
of bounds and nothing is selected, a quick rectangle_intersect() does
the job for them.

gtk/gtkgridview.c
gtk/gtklistbase.c
gtk/gtklistview.c

index 6abbfc55dadb03fa9bf672062f24c5df908bedc2..dcfd1edd689ec44599d0ed8bd66f3858d11d068a 100644 (file)
@@ -378,22 +378,16 @@ gtk_grid_view_get_items_in_rect (GtkListBase        *base,
 {
   GtkGridView *self = GTK_GRID_VIEW (base);
   guint first_row, last_row, first_column, last_column;
-  GdkRectangle bounds;
   GtkBitset *result;
 
   result = gtk_bitset_new_empty ();
 
-  /* limit rect to the region that actually overlaps items */
-  gtk_list_item_manager_get_tile_bounds (self->item_manager, &bounds);
-  if (!gdk_rectangle_intersect (&bounds, rect, &bounds))
-    return result;
-
-  first_column = fmax (floor (bounds.x / self->column_width), 0);
-  last_column = fmin (floor ((bounds.x + bounds.width) / self->column_width), self->n_columns - 1);
+  first_column = fmax (floor (rect->x / self->column_width), 0);
+  last_column = fmin (floor ((rect->x + rect->width) / self->column_width), self->n_columns - 1);
   /* match y = 0 here because we care about the rows, not the cells */
-  if (!gtk_grid_view_get_position_from_allocation (base, 0, bounds.y, &first_row, NULL))
+  if (!gtk_grid_view_get_position_from_allocation (base, 0, rect->y, &first_row, NULL))
     g_return_val_if_reached (result);
-  if (!gtk_grid_view_get_position_from_allocation (base, 0, bounds.y + bounds.height - 1, &last_row, NULL))
+  if (!gtk_grid_view_get_position_from_allocation (base, 0, rect->y + rect->height - 1, &last_row, NULL))
     g_return_val_if_reached (result);
 
   gtk_bitset_add_rectangle (result,
index 1e4ee499a3cf62d1ee671183fb9818cb9a01d52e..7b781bf3b3c412b5746cad40518347995231369e 100644 (file)
@@ -1454,7 +1454,14 @@ static GtkBitset *
 gtk_list_base_get_items_in_rect (GtkListBase        *self,
                                  const GdkRectangle *rect)
 {
-  return GTK_LIST_BASE_GET_CLASS (self)->get_items_in_rect (self, rect);
+  GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
+  GdkRectangle bounds;
+
+  gtk_list_item_manager_get_tile_bounds (priv->item_manager, &bounds);
+  if (!gdk_rectangle_intersect (&bounds, rect, &bounds))
+    return gtk_bitset_new_empty ();
+
+  return GTK_LIST_BASE_GET_CLASS (self)->get_items_in_rect (self, &bounds);
 }
 
 static gboolean
index 6765165791d7eb939d21f411217d55e471fa42cd..f210ac26cef4b87af7eea1ae94e0fa1a6fe7948c 100644 (file)
@@ -186,20 +186,6 @@ dump (GtkListView *self)
   g_print ("  => %u widgets in %u list rows\n", n_widgets, n_list_rows);
 }
 
-static int
-gtk_list_view_get_list_height (GtkListView *self)
-{
-  GtkListTile *tile;
-  GtkListTileAugment *aug;
-
-  tile = gtk_list_item_manager_get_root (self->item_manager);
-  if (tile == NULL)
-    return 0;
-
-  aug = gtk_list_tile_get_augment (self->item_manager, tile);
-  return aug->area.height;
-}
-
 static GtkListTile *
 gtk_list_view_split (GtkListBase *base,
                      GtkListTile *tile,
@@ -297,9 +283,6 @@ gtk_list_view_get_items_in_rect (GtkListBase                 *base,
 
   result = gtk_bitset_new_empty ();
 
-  if (rect->y >= gtk_list_view_get_list_height (self))
-    return result;
-
   n_items = gtk_list_base_get_n_items (base);
   if (n_items == 0)
     return result;