gtkiconview: reuse cell layouts across relayouts, avoid O(n) walks per row
The cached cell layout is dropped together with the item size, so changes still have to come with a row-changed, just like for the item size.
This commit is contained in:
parent
32175fcd80
commit
da49e7bb79
1 changed files with 150 additions and 2 deletions
|
|
@ -74,6 +74,15 @@ struct _GtkIconViewItem
|
|||
guint selected : 1;
|
||||
guint selected_before_rubberbanding : 1;
|
||||
|
||||
/* The result of the last gtk_icon_view_calculate_item_size2() and its
|
||||
* inputs: the item geometry before the call, the per-cell maximum
|
||||
* heights of the row and the text direction. Invalidated together with
|
||||
* the item size (see gtk_icon_view_item_invalidate_size()). */
|
||||
guint layout_valid : 1;
|
||||
guint layout_rtl : 1;
|
||||
gint layout_x, layout_y, layout_width;
|
||||
gint layout_out_width, layout_out_height;
|
||||
gint *layout_max_height;
|
||||
};
|
||||
|
||||
typedef struct _GtkIconViewCellInfo GtkIconViewCellInfo;
|
||||
|
|
@ -106,6 +115,14 @@ struct _GtkIconViewPrivate
|
|||
{
|
||||
gint width, height;
|
||||
|
||||
/* The item last looked up by index, so that row-changed for consecutive
|
||||
* rows is O(1) instead of O(n) each. Reset when items are removed or
|
||||
* reordered. */
|
||||
GList *nth_cache_link;
|
||||
|
||||
/* The text cell's wrap width at the last layout (see adjust_wrap_width()) */
|
||||
gint last_wrap_width;
|
||||
|
||||
GtkSelectionMode selection_mode;
|
||||
|
||||
GdkWindow *bin_window;
|
||||
|
|
@ -1425,6 +1442,9 @@ gtk_icon_view_style_set (GtkWidget *widget,
|
|||
gdk_window_set_background (icon_view->priv->bin_window, &widget->style->base[widget->state]);
|
||||
}
|
||||
|
||||
/* Fonts and spacing may have changed: measure the items again. */
|
||||
gtk_icon_view_invalidate_sizes (icon_view);
|
||||
|
||||
gtk_widget_queue_resize (widget);
|
||||
}
|
||||
|
||||
|
|
@ -1590,6 +1610,10 @@ gtk_icon_view_expose (GtkWidget *widget,
|
|||
GtkIconViewItem *item = icons->data;
|
||||
GdkRectangle area;
|
||||
|
||||
/* Rows are laid out top to bottom; nothing after this is exposed. */
|
||||
if (item->y > expose->area.y + expose->area.height)
|
||||
break;
|
||||
|
||||
area.x = item->x;
|
||||
area.y = item->y;
|
||||
area.width = item->width;
|
||||
|
|
@ -2687,6 +2711,69 @@ gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
|
|||
}
|
||||
}
|
||||
|
||||
/* Makes the next layout measure the cells of all items again, as
|
||||
* gtk_icon_view_calculate_item_size2() always did before its results were
|
||||
* kept: for changes to the cells that do not invalidate the item sizes. */
|
||||
static void
|
||||
gtk_icon_view_invalidate_cell_layouts (GtkIconView *icon_view)
|
||||
{
|
||||
GList *l;
|
||||
|
||||
for (l = icon_view->priv->items; l; l = l->next)
|
||||
((GtkIconViewItem *) l->data)->layout_valid = FALSE;
|
||||
}
|
||||
|
||||
/* gtk_icon_view_calculate_item_size2(), but if nothing it depends on
|
||||
* changed except the item position, the cell boxes of the last call are
|
||||
* moved instead of measuring all cells again. Measuring text is by far
|
||||
* the most expensive part of a relayout, and every model change relayouts
|
||||
* all items, so this keeps large views responsive. */
|
||||
static void
|
||||
gtk_icon_view_calculate_item_size2_cached (GtkIconView *icon_view,
|
||||
GtkIconViewItem *item,
|
||||
gint *max_height,
|
||||
gboolean rtl)
|
||||
{
|
||||
gint n_cells = icon_view->priv->n_cells;
|
||||
gint i;
|
||||
|
||||
if (item->layout_valid &&
|
||||
item->n_cells == n_cells &&
|
||||
item->layout_rtl == (rtl != FALSE) &&
|
||||
item->layout_width == item->width &&
|
||||
memcmp (item->layout_max_height, max_height, n_cells * sizeof (gint)) == 0)
|
||||
{
|
||||
gint dx = item->x - item->layout_x;
|
||||
gint dy = item->y - item->layout_y;
|
||||
|
||||
for (i = 0; i < n_cells; i++)
|
||||
{
|
||||
item->box[i].x += dx;
|
||||
item->box[i].y += dy;
|
||||
}
|
||||
|
||||
item->layout_x = item->x;
|
||||
item->layout_y = item->y;
|
||||
item->width = item->layout_out_width;
|
||||
item->height = item->layout_out_height;
|
||||
return;
|
||||
}
|
||||
|
||||
item->layout_x = item->x;
|
||||
item->layout_y = item->y;
|
||||
item->layout_width = item->width;
|
||||
item->layout_rtl = rtl != FALSE;
|
||||
|
||||
gtk_icon_view_calculate_item_size2 (icon_view, item, max_height);
|
||||
|
||||
if (item->layout_max_height == NULL)
|
||||
item->layout_max_height = g_new (gint, n_cells);
|
||||
memcpy (item->layout_max_height, max_height, n_cells * sizeof (gint));
|
||||
item->layout_out_width = item->width;
|
||||
item->layout_out_height = item->height;
|
||||
item->layout_valid = item->n_cells == n_cells;
|
||||
}
|
||||
|
||||
static GList *
|
||||
gtk_icon_view_layout_single_row (GtkIconView *icon_view,
|
||||
GList *first_item,
|
||||
|
|
@ -2771,7 +2858,7 @@ gtk_icon_view_layout_single_row (GtkIconView *icon_view,
|
|||
item->col = col - 1 - item->col;
|
||||
}
|
||||
|
||||
gtk_icon_view_calculate_item_size2 (icon_view, item, max_height);
|
||||
gtk_icon_view_calculate_item_size2_cached (icon_view, item, max_height, rtl);
|
||||
|
||||
/* We may want to readjust the new y coordinate. */
|
||||
if (item->y + item->height + focus_width + icon_view->priv->row_spacing > *y)
|
||||
|
|
@ -2986,6 +3073,12 @@ adjust_wrap_width (GtkIconView *icon_view,
|
|||
|
||||
wrap_width -= icon_view->priv->item_padding * 2;
|
||||
|
||||
if (wrap_width != icon_view->priv->last_wrap_width)
|
||||
{
|
||||
icon_view->priv->last_wrap_width = wrap_width;
|
||||
gtk_icon_view_invalidate_cell_layouts (icon_view);
|
||||
}
|
||||
|
||||
g_object_set (text_info->cell, "wrap-width", wrap_width, NULL);
|
||||
g_object_set (text_info->cell, "width", wrap_width, NULL);
|
||||
}
|
||||
|
|
@ -3011,6 +3104,10 @@ gtk_icon_view_calculate_item_size (GtkIconView *icon_view,
|
|||
item->after = g_new0 (gint, icon_view->priv->n_cells);
|
||||
item->box = g_new0 (GdkRectangle, icon_view->priv->n_cells);
|
||||
|
||||
g_free (item->layout_max_height);
|
||||
item->layout_max_height = NULL;
|
||||
item->layout_valid = FALSE;
|
||||
|
||||
item->n_cells = icon_view->priv->n_cells;
|
||||
}
|
||||
|
||||
|
|
@ -3157,6 +3254,7 @@ gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
|
|||
{
|
||||
item->width = -1;
|
||||
item->height = -1;
|
||||
item->layout_valid = FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -3478,6 +3576,7 @@ gtk_icon_view_item_free (GtkIconViewItem *item)
|
|||
g_free (item->before);
|
||||
g_free (item->after);
|
||||
g_free (item->box);
|
||||
g_free (item->layout_max_height);
|
||||
|
||||
g_free (item);
|
||||
}
|
||||
|
|
@ -3586,6 +3685,8 @@ gtk_icon_view_unselect_item (GtkIconView *icon_view,
|
|||
static void
|
||||
verify_items (GtkIconView *icon_view)
|
||||
{
|
||||
/* O(n) for every inserted or deleted row: debug builds only */
|
||||
#ifdef G_ENABLE_DEBUG
|
||||
GList *items;
|
||||
int i = 0;
|
||||
|
||||
|
|
@ -3599,6 +3700,28 @@ verify_items (GtkIconView *icon_view)
|
|||
|
||||
i++;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* g_list_nth_data() on the items, O(1) when called for consecutive rows */
|
||||
static GtkIconViewItem *
|
||||
gtk_icon_view_nth_item (GtkIconView *icon_view,
|
||||
gint index)
|
||||
{
|
||||
GtkIconViewPrivate *priv = icon_view->priv;
|
||||
GList *link = priv->nth_cache_link;
|
||||
|
||||
/* Inserting rows keeps the cached link valid but shifts indices. */
|
||||
if (link && ((GtkIconViewItem *) link->data)->index == index)
|
||||
;
|
||||
else if (link && ((GtkIconViewItem *) link->data)->index == index - 1 && link->next)
|
||||
link = link->next;
|
||||
else
|
||||
link = g_list_nth (priv->items, index);
|
||||
|
||||
priv->nth_cache_link = link;
|
||||
|
||||
return link ? link->data : NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -3616,7 +3739,7 @@ gtk_icon_view_row_changed (GtkTreeModel *model,
|
|||
gtk_icon_view_stop_editing (icon_view, TRUE);
|
||||
|
||||
index = gtk_tree_path_get_indices(path)[0];
|
||||
item = g_list_nth_data (icon_view->priv->items, index);
|
||||
item = gtk_icon_view_nth_item (icon_view, index);
|
||||
|
||||
gtk_icon_view_item_invalidate_size (item);
|
||||
gtk_icon_view_queue_layout (icon_view);
|
||||
|
|
@ -3689,6 +3812,8 @@ gtk_icon_view_row_deleted (GtkTreeModel *model,
|
|||
|
||||
gtk_icon_view_stop_editing (icon_view, TRUE);
|
||||
|
||||
icon_view->priv->nth_cache_link = NULL;
|
||||
|
||||
if (item == icon_view->priv->anchor_item)
|
||||
icon_view->priv->anchor_item = NULL;
|
||||
|
||||
|
|
@ -3735,6 +3860,8 @@ gtk_icon_view_rows_reordered (GtkTreeModel *model,
|
|||
|
||||
gtk_icon_view_stop_editing (icon_view, TRUE);
|
||||
|
||||
icon_view->priv->nth_cache_link = NULL;
|
||||
|
||||
length = gtk_tree_model_iter_n_children (model, NULL);
|
||||
|
||||
order = g_new (gint, length);
|
||||
|
|
@ -4603,6 +4730,8 @@ gtk_icon_view_cell_layout_pack_start (GtkCellLayout *layout,
|
|||
|
||||
icon_view->priv->cell_list = g_list_append (icon_view->priv->cell_list, info);
|
||||
icon_view->priv->n_cells++;
|
||||
|
||||
gtk_icon_view_invalidate_cell_layouts (icon_view);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -4626,6 +4755,8 @@ gtk_icon_view_cell_layout_pack_end (GtkCellLayout *layout,
|
|||
|
||||
icon_view->priv->cell_list = g_list_append (icon_view->priv->cell_list, info);
|
||||
icon_view->priv->n_cells++;
|
||||
|
||||
gtk_icon_view_invalidate_cell_layouts (icon_view);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -4644,6 +4775,8 @@ gtk_icon_view_cell_layout_add_attribute (GtkCellLayout *layout,
|
|||
GINT_TO_POINTER (column));
|
||||
info->attributes = g_slist_prepend (info->attributes,
|
||||
g_strdup (attribute));
|
||||
|
||||
gtk_icon_view_invalidate_cell_layouts (icon_view);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -4660,6 +4793,8 @@ gtk_icon_view_cell_layout_clear (GtkCellLayout *layout)
|
|||
}
|
||||
|
||||
icon_view->priv->n_cells = 0;
|
||||
|
||||
gtk_icon_view_invalidate_cell_layouts (icon_view);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -4686,6 +4821,8 @@ gtk_icon_view_cell_layout_set_cell_data_func (GtkCellLayout *layout,
|
|||
info->func = func;
|
||||
info->func_data = func_data;
|
||||
info->destroy = destroy;
|
||||
|
||||
gtk_icon_view_invalidate_cell_layouts (icon_view);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -4697,6 +4834,8 @@ gtk_icon_view_cell_layout_clear_attributes (GtkCellLayout *layout,
|
|||
info = gtk_icon_view_get_cell_info (GTK_ICON_VIEW (layout), renderer);
|
||||
if (info != NULL)
|
||||
free_cell_attributes (info);
|
||||
|
||||
gtk_icon_view_invalidate_cell_layouts (GTK_ICON_VIEW (layout));
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -4734,6 +4873,8 @@ gtk_icon_view_cell_layout_reorder (GtkCellLayout *layout,
|
|||
info->position = i;
|
||||
}
|
||||
|
||||
gtk_icon_view_invalidate_cell_layouts (icon_view);
|
||||
|
||||
gtk_widget_queue_draw (GTK_WIDGET (icon_view));
|
||||
}
|
||||
|
||||
|
|
@ -5207,6 +5348,12 @@ gtk_icon_view_get_visible_range (GtkIconView *icon_view,
|
|||
{
|
||||
GtkIconViewItem *item = icons->data;
|
||||
|
||||
/* Items are laid out row by row: once one starts below the visible
|
||||
* area, all following ones do too. */
|
||||
if (start_index != -1 &&
|
||||
item->y > (int) (icon_view->priv->vadjustment->value + icon_view->priv->vadjustment->page_size))
|
||||
break;
|
||||
|
||||
if ((item->x + item->width >= (int)icon_view->priv->hadjustment->value) &&
|
||||
(item->y + item->height >= (int)icon_view->priv->vadjustment->value) &&
|
||||
(item->x <= (int) (icon_view->priv->hadjustment->value + icon_view->priv->hadjustment->page_size)) &&
|
||||
|
|
@ -5383,6 +5530,7 @@ gtk_icon_view_set_model (GtkIconView *icon_view,
|
|||
g_list_foreach (icon_view->priv->items, (GFunc)gtk_icon_view_item_free, NULL);
|
||||
g_list_free (icon_view->priv->items);
|
||||
icon_view->priv->items = NULL;
|
||||
icon_view->priv->nth_cache_link = NULL;
|
||||
icon_view->priv->anchor_item = NULL;
|
||||
icon_view->priv->cursor_item = NULL;
|
||||
icon_view->priv->last_single_clicked = NULL;
|
||||
|
|
|
|||
Loading…
Reference in a new issue