columnviewtitle: Clean up interactions

Separate the apis we use for updating title,
menu and sort indicator.
This commit is contained in:
Matthias Clasen 2022-10-20 13:37:26 -04:00
parent e040d3663e
commit 9ed1505073
3 changed files with 37 additions and 21 deletions

View File

@ -706,7 +706,7 @@ gtk_column_view_column_set_title (GtkColumnViewColumn *self,
self->title = g_strdup (title); self->title = g_strdup (title);
if (self->header) if (self->header)
gtk_column_view_title_update (GTK_COLUMN_VIEW_TITLE (self->header)); gtk_column_view_title_set_title (GTK_COLUMN_VIEW_TITLE (self->header), title);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TITLE]); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TITLE]);
} }
@ -765,7 +765,7 @@ gtk_column_view_column_set_sorter (GtkColumnViewColumn *self,
gtk_column_view_column_remove_from_sorter (self); gtk_column_view_column_remove_from_sorter (self);
if (self->header) if (self->header)
gtk_column_view_title_update (GTK_COLUMN_VIEW_TITLE (self->header)); gtk_column_view_title_update_sort (GTK_COLUMN_VIEW_TITLE (self->header));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SORTER]); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SORTER]);
} }
@ -790,7 +790,7 @@ void
gtk_column_view_column_notify_sort (GtkColumnViewColumn *self) gtk_column_view_column_notify_sort (GtkColumnViewColumn *self)
{ {
if (self->header) if (self->header)
gtk_column_view_title_update (GTK_COLUMN_VIEW_TITLE (self->header)); gtk_column_view_title_update_sort (GTK_COLUMN_VIEW_TITLE (self->header));
} }
/** /**
@ -862,7 +862,7 @@ gtk_column_view_column_set_header_menu (GtkColumnViewColumn *self,
return; return;
if (self->header) if (self->header)
gtk_column_view_title_update (GTK_COLUMN_VIEW_TITLE (self->header)); gtk_column_view_title_set_menu (GTK_COLUMN_VIEW_TITLE (self->header), menu);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_HEADER_MENU]); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_HEADER_MENU]);
} }

View File

@ -283,45 +283,56 @@ gtk_column_view_title_new (GtkColumnViewColumn *column)
title = g_object_new (GTK_TYPE_COLUMN_VIEW_TITLE, NULL); title = g_object_new (GTK_TYPE_COLUMN_VIEW_TITLE, NULL);
title->column = g_object_ref (column); title->column = g_object_ref (column);
gtk_column_view_title_update (title); gtk_column_view_title_update_sort (title);
gtk_column_view_title_set_title (title, gtk_column_view_column_get_title (column));
gtk_column_view_title_set_menu (title, gtk_column_view_column_get_header_menu (column));
return GTK_WIDGET (title); return GTK_WIDGET (title);
} }
void void
gtk_column_view_title_update (GtkColumnViewTitle *self) gtk_column_view_title_set_title (GtkColumnViewTitle *self,
const char *title)
{ {
GtkSorter *sorter; gtk_label_set_label (GTK_LABEL (self->title), title);
GtkColumnView *view; }
GtkColumnViewSorter *view_sorter;
gboolean inverted;
GtkColumnViewColumn *active;
gtk_label_set_label (GTK_LABEL (self->title), gtk_column_view_column_get_title (self->column)); void
gtk_column_view_title_set_menu (GtkColumnViewTitle *self,
GMenuModel *model)
{
g_clear_pointer (&self->popup_menu, gtk_widget_unparent);
}
sorter = gtk_column_view_column_get_sorter (self->column); void
gtk_column_view_title_update_sort (GtkColumnViewTitle *self)
if (sorter) {
if (gtk_column_view_column_get_sorter (self->column))
{ {
GtkColumnView *view;
GtkColumnViewSorter *view_sorter;
GtkColumnViewColumn *primary;
GtkSortType sort_order;
view = gtk_column_view_column_get_column_view (self->column); view = gtk_column_view_column_get_column_view (self->column);
view_sorter = GTK_COLUMN_VIEW_SORTER (gtk_column_view_get_sorter (view)); view_sorter = GTK_COLUMN_VIEW_SORTER (gtk_column_view_get_sorter (view));
active = gtk_column_view_sorter_get_sort_column (view_sorter, &inverted); primary = gtk_column_view_sorter_get_primary_sort_column (view_sorter);
sort_order = gtk_column_view_sorter_get_primary_sort_order (view_sorter);
gtk_widget_show (self->sort); gtk_widget_show (self->sort);
gtk_widget_remove_css_class (self->sort, "ascending"); gtk_widget_remove_css_class (self->sort, "ascending");
gtk_widget_remove_css_class (self->sort, "descending"); gtk_widget_remove_css_class (self->sort, "descending");
gtk_widget_remove_css_class (self->sort, "unsorted"); gtk_widget_remove_css_class (self->sort, "unsorted");
if (self->column != active)
if (self->column != primary)
gtk_widget_add_css_class (self->sort, "unsorted"); gtk_widget_add_css_class (self->sort, "unsorted");
else if (inverted) else if (sort_order == GTK_SORT_DESCENDING)
gtk_widget_add_css_class (self->sort, "descending"); gtk_widget_add_css_class (self->sort, "descending");
else else
gtk_widget_add_css_class (self->sort, "ascending"); gtk_widget_add_css_class (self->sort, "ascending");
} }
else else
gtk_widget_hide (self->sort); gtk_widget_hide (self->sort);
g_clear_pointer (&self->popup_menu, gtk_widget_unparent);
} }
GtkColumnViewColumn * GtkColumnViewColumn *

View File

@ -38,7 +38,12 @@ GType gtk_column_view_title_get_type (void) G_GNUC_CO
GtkWidget * gtk_column_view_title_new (GtkColumnViewColumn *column); GtkWidget * gtk_column_view_title_new (GtkColumnViewColumn *column);
void gtk_column_view_title_update (GtkColumnViewTitle *self); void gtk_column_view_title_set_title (GtkColumnViewTitle *self,
const char *title);
void gtk_column_view_title_set_menu (GtkColumnViewTitle *self,
GMenuModel *model);
void gtk_column_view_title_update_sort (GtkColumnViewTitle *self);
GtkColumnViewColumn * gtk_column_view_title_get_column (GtkColumnViewTitle *self); GtkColumnViewColumn * gtk_column_view_title_get_column (GtkColumnViewTitle *self);