gridview: Use gtk_list_item_manager_get_nearest_tile()
authorBenjamin Otte <otte@redhat.com>
Thu, 9 Mar 2023 04:30:25 +0000 (05:30 +0100)
committerBenjamin Otte <otte@redhat.com>
Fri, 10 Mar 2023 16:03:33 +0000 (17:03 +0100)
Simplifies gtk_grid_view_get_position_from_allocation() a bit because we
can omit the bounds shenanigan.

gtk/gtkgridview.c

index dcfd1edd689ec44599d0ed8bd66f3858d11d068a..6b245d8dfed73f3a8bdd364ca3110c0b8e6b8886 100644 (file)
@@ -302,15 +302,8 @@ gtk_grid_view_get_position_from_allocation (GtkListBase           *base,
   GtkGridView *self = GTK_GRID_VIEW (base);
   GtkListTile *tile;
   guint pos;
-  GdkRectangle bounds;
 
-  gtk_list_item_manager_get_tile_bounds (self->item_manager, &bounds);
-  if (bounds.width <= 0 || bounds.height <= 0)
-    return FALSE;
-  x = CLAMP (x, bounds.x, bounds.x + bounds.width - 1);
-  y = CLAMP (y, bounds.y, bounds.y + bounds.height - 1);
-
-  tile = gtk_list_item_manager_get_tile_at (self->item_manager, x, y);
+  tile = gtk_list_item_manager_get_nearest_tile (self->item_manager, x, y);
   if (tile == NULL)
     return FALSE;
 
@@ -329,7 +322,7 @@ gtk_grid_view_get_position_from_allocation (GtkListBase           *base,
   if (tile->n_items > 1)
     {
       /* offset in x direction */
-      pos += (x - tile->area.x) / self->column_width;
+      pos += MAX (tile->area.width - 1, x - tile->area.x) / self->column_width;
       if (area)
         {
           guint col = MIN (x / self->column_width, self->n_columns - 1);
@@ -342,7 +335,7 @@ gtk_grid_view_get_position_from_allocation (GtkListBase           *base,
         {
           guint rows_in_tile = tile->n_items / self->n_columns;
           guint row_height = tile->area.height / rows_in_tile;
-          guint row_index = (y - tile->area.y) / row_height;
+          guint row_index = MAX (tile->area.height - 1, y - tile->area.y) / row_height;
           pos += self->n_columns * row_index;
 
           if (area)