listview: Simplify allocation

With the Tile changes, a lot of stuff does no longer need to be
duplicated between listview and gridview. Move it to ListBase instead.
This commit is contained in:
Benjamin Otte 2023-03-02 21:27:52 +01:00 committed by Benjamin Otte
parent 8aea6fc1b5
commit f3c53ae69d
4 changed files with 45 additions and 71 deletions

View File

@ -624,21 +624,19 @@ gtk_grid_view_size_allocate (GtkWidget *widget,
GtkListTile *tile, *start; GtkListTile *tile, *start;
GArray *heights; GArray *heights;
int min_row_height, unknown_row_height, row_height, col_min, col_nat; int min_row_height, unknown_row_height, row_height, col_min, col_nat;
GtkOrientation orientation, opposite_orientation; GtkOrientation orientation;
GtkScrollablePolicy scroll_policy; GtkScrollablePolicy scroll_policy;
GdkRectangle bounds; int y;
int x, y;
guint i; guint i;
orientation = gtk_list_base_get_orientation (GTK_LIST_BASE (self)); orientation = gtk_list_base_get_orientation (GTK_LIST_BASE (self));
scroll_policy = gtk_list_base_get_scroll_policy (GTK_LIST_BASE (self), orientation); scroll_policy = gtk_list_base_get_scroll_policy (GTK_LIST_BASE (self), orientation);
opposite_orientation = OPPOSITE_ORIENTATION (orientation);
min_row_height = ceil ((double) height / GTK_GRID_VIEW_MAX_VISIBLE_ROWS); min_row_height = ceil ((double) height / GTK_GRID_VIEW_MAX_VISIBLE_ROWS);
/* step 0: exit early if list is empty */ /* step 0: exit early if list is empty */
if (gtk_list_item_manager_get_root (self->item_manager) == NULL) if (gtk_list_item_manager_get_root (self->item_manager) == NULL)
{ {
gtk_list_base_update_adjustments (GTK_LIST_BASE (self), 0, 0, 0, 0, &x, &y); gtk_list_base_allocate (GTK_LIST_BASE (self));
return; return;
} }
@ -756,19 +754,8 @@ gtk_grid_view_size_allocate (GtkWidget *widget,
} }
} }
gtk_list_item_manager_get_tile_bounds (self->item_manager, &bounds); /* step 4: allocate the rest */
gtk_list_base_allocate (GTK_LIST_BASE (self));
/* step 4: update the adjustments */
gtk_list_base_update_adjustments (GTK_LIST_BASE (self),
bounds.width,
bounds.height,
gtk_widget_get_size (widget, opposite_orientation),
gtk_widget_get_size (widget, orientation),
&x, &y);
gtk_list_base_allocate_children (GTK_LIST_BASE (widget));
gtk_list_base_allocate_rubberband (GTK_LIST_BASE (widget));
} }
static void static void

View File

@ -1355,7 +1355,7 @@ update_autoscroll (GtkListBase *self,
remove_autoscroll (self); remove_autoscroll (self);
} }
/** /*
* gtk_list_base_size_allocate_child: * gtk_list_base_size_allocate_child:
* @self: The listbase * @self: The listbase
* @child: The child * @child: The child
@ -1368,7 +1368,7 @@ update_autoscroll (GtkListBase *self,
* but with the coordinates already offset by the scroll * but with the coordinates already offset by the scroll
* offset. * offset.
**/ **/
void static void
gtk_list_base_size_allocate_child (GtkListBase *self, gtk_list_base_size_allocate_child (GtkListBase *self,
GtkWidget *child, GtkWidget *child,
int x, int x,
@ -1432,7 +1432,7 @@ gtk_list_base_size_allocate_child (GtkListBase *self,
gtk_widget_size_allocate (child, &child_allocation, -1); gtk_widget_size_allocate (child, &child_allocation, -1);
} }
void static void
gtk_list_base_allocate_children (GtkListBase *self) gtk_list_base_allocate_children (GtkListBase *self)
{ {
GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self); GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
@ -1537,7 +1537,7 @@ gtk_list_base_get_rubberband_coords (GtkListBase *self,
return TRUE; return TRUE;
} }
void static void
gtk_list_base_allocate_rubberband (GtkListBase *self) gtk_list_base_allocate_rubberband (GtkListBase *self)
{ {
GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self); GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
@ -1912,7 +1912,7 @@ gtk_list_base_init_real (GtkListBase *self,
gtk_widget_add_controller (GTK_WIDGET (self), controller); gtk_widget_add_controller (GTK_WIDGET (self), controller);
} }
static int static void
gtk_list_base_set_adjustment_values (GtkListBase *self, gtk_list_base_set_adjustment_values (GtkListBase *self,
GtkOrientation orientation, GtkOrientation orientation,
int value, int value,
@ -1940,23 +1940,24 @@ gtk_list_base_set_adjustment_values (GtkListBase *self,
g_signal_handlers_unblock_by_func (priv->adjustment[orientation], g_signal_handlers_unblock_by_func (priv->adjustment[orientation],
gtk_list_base_adjustment_value_changed_cb, gtk_list_base_adjustment_value_changed_cb,
self); self);
return value;
} }
void static void
gtk_list_base_update_adjustments (GtkListBase *self, gtk_list_base_update_adjustments (GtkListBase *self)
int total_across,
int total_along,
int page_across,
int page_along,
int *across,
int *along)
{ {
GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self); GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
GdkRectangle bounds;
int value_along, value_across, size; int value_along, value_across, size;
int page_along, page_across;
guint pos; guint pos;
gtk_list_item_manager_get_tile_bounds (priv->item_manager, &bounds);
g_assert (bounds.x == 0);
g_assert (bounds.y == 0);
page_across = gtk_widget_get_size (GTK_WIDGET (self), OPPOSITE_ORIENTATION (priv->orientation));
page_along = gtk_widget_get_size (GTK_WIDGET (self), priv->orientation);
pos = gtk_list_item_tracker_get_position (priv->item_manager, priv->anchor); pos = gtk_list_item_tracker_get_position (priv->item_manager, priv->anchor);
if (pos == GTK_INVALID_LIST_POSITION) if (pos == GTK_INVALID_LIST_POSITION)
{ {
@ -1987,16 +1988,25 @@ gtk_list_base_update_adjustments (GtkListBase *self,
} }
} }
*across = gtk_list_base_set_adjustment_values (self, gtk_list_base_set_adjustment_values (self,
OPPOSITE_ORIENTATION (priv->orientation), OPPOSITE_ORIENTATION (priv->orientation),
value_across, value_across,
total_across, bounds.width,
page_across); page_across);
*along = gtk_list_base_set_adjustment_values (self, gtk_list_base_set_adjustment_values (self,
priv->orientation, priv->orientation,
value_along, value_along,
total_along, bounds.height,
page_along); page_along);
}
void
gtk_list_base_allocate (GtkListBase *self)
{
gtk_list_base_update_adjustments (self);
gtk_list_base_allocate_children (self);
gtk_list_base_allocate_rubberband (self);
} }
GtkScrollablePolicy GtkScrollablePolicy

View File

@ -76,13 +76,6 @@ guint gtk_list_base_get_n_items (GtkListBase
GtkSelectionModel * gtk_list_base_get_model (GtkListBase *self); GtkSelectionModel * gtk_list_base_get_model (GtkListBase *self);
gboolean gtk_list_base_set_model (GtkListBase *self, gboolean gtk_list_base_set_model (GtkListBase *self,
GtkSelectionModel *model); GtkSelectionModel *model);
void gtk_list_base_update_adjustments (GtkListBase *self,
int total_across,
int total_along,
int page_across,
int page_along,
int *across,
int *along);
guint gtk_list_base_get_anchor (GtkListBase *self); guint gtk_list_base_get_anchor (GtkListBase *self);
void gtk_list_base_set_anchor (GtkListBase *self, void gtk_list_base_set_anchor (GtkListBase *self,
@ -107,14 +100,7 @@ gboolean gtk_list_base_grab_focus_on_item (GtkListBase
void gtk_list_base_set_enable_rubberband (GtkListBase *self, void gtk_list_base_set_enable_rubberband (GtkListBase *self,
gboolean enable); gboolean enable);
gboolean gtk_list_base_get_enable_rubberband (GtkListBase *self); gboolean gtk_list_base_get_enable_rubberband (GtkListBase *self);
void gtk_list_base_allocate_rubberband (GtkListBase *self);
void gtk_list_base_allocate_children (GtkListBase *self);
void gtk_list_base_size_allocate_child (GtkListBase *self, void gtk_list_base_allocate (GtkListBase *self);
GtkWidget *child,
int x,
int y,
int width,
int height);
#endif /* __GTK_LIST_BASE_PRIVATE_H__ */ #endif /* __GTK_LIST_BASE_PRIVATE_H__ */

View File

@ -506,8 +506,7 @@ gtk_list_view_size_allocate (GtkWidget *widget,
GtkListView *self = GTK_LIST_VIEW (widget); GtkListView *self = GTK_LIST_VIEW (widget);
GtkListTile *tile; GtkListTile *tile;
GArray *heights; GArray *heights;
int min, nat, row_height; int min, nat, row_height, y;
int x, y;
GtkOrientation orientation, opposite_orientation; GtkOrientation orientation, opposite_orientation;
GtkScrollablePolicy scroll_policy, opposite_scroll_policy; GtkScrollablePolicy scroll_policy, opposite_scroll_policy;
@ -519,7 +518,7 @@ gtk_list_view_size_allocate (GtkWidget *widget,
/* step 0: exit early if list is empty */ /* step 0: exit early if list is empty */
if (gtk_list_item_manager_get_root (self->item_manager) == NULL) if (gtk_list_item_manager_get_root (self->item_manager) == NULL)
{ {
gtk_list_base_update_adjustments (GTK_LIST_BASE (self), 0, 0, 0, 0, &x, &y); gtk_list_base_allocate (GTK_LIST_BASE (self));
return; return;
} }
@ -570,16 +569,8 @@ gtk_list_view_size_allocate (GtkWidget *widget,
y += tile->area.height; y += tile->area.height;
} }
/* step 4: update the adjustments */ /* step 4: allocate the rest */
gtk_list_base_update_adjustments (GTK_LIST_BASE (self), gtk_list_base_allocate (GTK_LIST_BASE (self));
self->list_width,
gtk_list_view_get_list_height (self),
gtk_widget_get_size (widget, opposite_orientation),
gtk_widget_get_size (widget, orientation),
&x, &y);
gtk_list_base_allocate_children (GTK_LIST_BASE (self));
gtk_list_base_allocate_rubberband (GTK_LIST_BASE (self));
} }
static void static void