color chooser: Don't show too many custom colors

The code was hardcoding a fixed number of custom
colors, but we really want to match the number
of columns in the palette(s). Make it so.
This commit is contained in:
Matthias Clasen 2020-09-11 08:42:03 -04:00
parent 8700e136da
commit fa9608fd02

View File

@ -83,6 +83,8 @@ struct _GtkColorChooserWidget
gboolean has_default_palette; gboolean has_default_palette;
GSettings *settings; GSettings *settings;
int max_custom;
}; };
struct _GtkColorChooserWidgetClass struct _GtkColorChooserWidgetClass
@ -315,7 +317,6 @@ add_palette (GtkColorChooserWidget *cc,
gtk_grid_set_column_spacing (GTK_GRID (grid), 4); gtk_grid_set_column_spacing (GTK_GRID (grid), 4);
gtk_box_append (GTK_BOX (cc->palette), grid); gtk_box_append (GTK_BOX (cc->palette), grid);
left = 0; left = 0;
right = colors_per_line - 1; right = colors_per_line - 1;
if (gtk_widget_get_direction (GTK_WIDGET (cc)) == GTK_TEXT_DIR_RTL) if (gtk_widget_get_direction (GTK_WIDGET (cc)) == GTK_TEXT_DIR_RTL)
@ -353,6 +354,11 @@ add_palette (GtkColorChooserWidget *cc,
gtk_grid_attach (GTK_GRID (grid), p, line, pos, 1, 1); gtk_grid_attach (GTK_GRID (grid), p, line, pos, 1, 1);
} }
} }
if (orientation == GTK_ORIENTATION_HORIZONTAL)
cc->max_custom = MAX (cc->max_custom, colors_per_line);
else
cc->max_custom = MAX (cc->max_custom, n_colors / colors_per_line);
} }
static void static void
@ -363,6 +369,7 @@ remove_default_palette (GtkColorChooserWidget *cc)
remove_palette (cc); remove_palette (cc);
cc->has_default_palette = FALSE; cc->has_default_palette = FALSE;
cc->max_custom = 0;
} }
static void static void
@ -700,16 +707,16 @@ add_custom_color (GtkColorChooserWidget *cc,
const GdkRGBA *color) const GdkRGBA *color)
{ {
GtkWidget *widget; GtkWidget *widget;
GList *children;
GtkWidget *p; GtkWidget *p;
int n;
children = NULL; n = 0;
for (widget = gtk_widget_get_first_child (cc->custom); for (widget = gtk_widget_get_first_child (cc->custom);
widget != NULL; widget != NULL;
widget = gtk_widget_get_next_sibling (widget)) widget = gtk_widget_get_next_sibling (widget))
children = g_list_prepend (children, widget); n++;
if (g_list_length (children) >= 8) while (n >= cc->max_custom)
{ {
GtkWidget *last = gtk_widget_get_last_child (cc->custom); GtkWidget *last = gtk_widget_get_last_child (cc->custom);
@ -717,8 +724,8 @@ add_custom_color (GtkColorChooserWidget *cc,
cc->current = NULL; cc->current = NULL;
gtk_box_remove (GTK_BOX (cc->custom), last); gtk_box_remove (GTK_BOX (cc->custom), last);
n--;
} }
g_list_free (children);
p = gtk_color_swatch_new (); p = gtk_color_swatch_new ();
gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (p), color); gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (p), color);