From bfdede7accc8378b617e858c8d350d9779ceb14b Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Sun, 21 Jun 2020 03:56:53 +0200 Subject: [PATCH] Revert "gtk-demo: Make the colors demo populate incrementally" This eases adding a bunch of follow-up features. After those features have landed, this functionality will be readded. This reverts commit a10340859df6aec405d9b6fc6e25c4790137af85. --- demos/gtk-demo/listview_colors.c | 152 +++++-------------------------- 1 file changed, 22 insertions(+), 130 deletions(-) diff --git a/demos/gtk-demo/listview_colors.c b/demos/gtk-demo/listview_colors.c index dc623c65c8..8a0a6c3650 100644 --- a/demos/gtk-demo/listview_colors.c +++ b/demos/gtk-demo/listview_colors.c @@ -241,54 +241,30 @@ gtk_color_new (const char *name, return result; } -typedef struct +static GListModel * +create_colors_model (void) { - GtkWidget *widget; - GBytes *data; - GListStore *store; - char **lines; - guint n; - guint i; -} ColorData; - -static void -free_color_data (gpointer data) -{ - ColorData *cd = data; - - if (cd->widget) - gtk_widget_set_sensitive (cd->widget, TRUE); - g_bytes_unref (cd->data); - g_object_unref (cd->store); - g_strfreev (cd->lines); - g_free (cd); -} - -static gboolean -add_color (GtkWidget *widget, - GdkFrameClock *clock, - gpointer data) -{ - ColorData *cd = data; - const char *name; - char **fields; - int red, green, blue; - int h, s, v; + GListStore *result; GtkColor *color; - guint64 start, now; + GBytes *data; + char **lines; + guint i; - start = g_get_monotonic_time (); + result = g_list_store_new (GTK_TYPE_COLOR); + data = g_resources_lookup_data ("/listview_colors/color.names.txt", 0, NULL); + lines = g_strsplit (g_bytes_get_data (data, NULL), "\n", 0); - while (cd->i < cd->n) + for (i = 0; lines[i]; i++) { - const char *line = cd->lines[cd->i]; + const char *name; + char **fields; + int red, green, blue; + int h, s, v; - cd->i++; - - if (line[0] == '#' || line[0] == '\0') + if (lines[i][0] == '#' || lines[i][0] == '\0') continue; - fields = g_strsplit (line, " ", 0); + fields = g_strsplit (lines[i], " ", 0); name = fields[1]; red = atoi (fields[3]); green = atoi (fields[4]); @@ -298,67 +274,16 @@ add_color (GtkWidget *widget, v = atoi (fields[11]); color = gtk_color_new (name, red / 255., green / 255., blue / 255., h, s, v); - g_list_store_append (cd->store, color); + g_list_store_append (result, color); g_object_unref (color); g_strfreev (fields); - - now = g_get_monotonic_time (); - if (now > start + 4000) - return G_SOURCE_CONTINUE; } + g_strfreev (lines); - return G_SOURCE_REMOVE; -} + g_bytes_unref (data); -static void -populate_colors_model (GtkWidget *widget, - GListStore *store) -{ - ColorData *cd; - - cd = g_new (ColorData, 1); - - gtk_widget_set_sensitive (widget, FALSE); - - cd->widget = widget; - cd->store = g_object_ref (store); - cd->data = g_resources_lookup_data ("/listview_colors/color.names.txt", 0, NULL); - cd->lines = g_strsplit (g_bytes_get_data (cd->data, NULL), "\n", 0); - cd->n = g_strv_length (cd->lines); - cd->i = 0; - - gtk_widget_add_tick_callback (widget, add_color, cd, free_color_data); -} - -static void -fill (GtkWidget *view, - GListStore *store) -{ - ColorData *cd; - gboolean res; - - cd = g_new (ColorData, 1); - - cd->widget = NULL; - cd->store = g_object_ref (store); - cd->data = g_resources_lookup_data ("/listview_colors/color.names.txt", 0, NULL); - cd->lines = g_strsplit (g_bytes_get_data (cd->data, NULL), "\n", 0); - cd->n = g_strv_length (cd->lines); - cd->i = 0; - - do { - res = add_color (view, NULL, cd); - } while (res == G_SOURCE_CONTINUE); - free_color_data (cd); -} - -static void -refill (GtkWidget *button, - GListStore *store) -{ - g_list_store_remove_all (store); - populate_colors_model (button, store); + return G_LIST_MODEL (result); } static char * @@ -476,7 +401,6 @@ create_color_grid (void) GtkWidget *gridview; GtkListItemFactory *factory; GListModel *model, *selection; - GListStore *store; gridview = gtk_grid_view_new (); gtk_scrollable_set_hscroll_policy (GTK_SCROLLABLE (gridview), GTK_SCROLL_NATURAL); @@ -490,34 +414,15 @@ create_color_grid (void) gtk_grid_view_set_max_columns (GTK_GRID_VIEW (gridview), 24); gtk_grid_view_set_enable_rubberband (GTK_GRID_VIEW (gridview), TRUE); - store = g_list_store_new (GTK_TYPE_COLOR); - fill (gridview, store); - - model = G_LIST_MODEL (gtk_sort_list_model_new (G_LIST_MODEL (store), NULL)); + model = G_LIST_MODEL (gtk_sort_list_model_new (create_colors_model (), NULL)); selection = G_LIST_MODEL (gtk_property_selection_new (model, "selected")); gtk_grid_view_set_model (GTK_GRID_VIEW (gridview), selection); g_object_unref (selection); g_object_unref (model); - g_object_unref (store); return gridview; } -static void -items_changed_cb (GListModel *model, - guint position, - guint removed, - guint added, - GtkWidget *label) -{ - guint n = g_list_model_get_n_items (model); - char *text; - - text = g_strdup_printf ("%u items", n); - gtk_label_set_label (GTK_LABEL (label), text); - g_free (text); -} - static GtkWidget *window = NULL; GtkWidget * @@ -529,12 +434,11 @@ do_listview_colors (GtkWidget *do_widget) GtkListItemFactory *factory; GListStore *factories; GListModel *model; + GtkSorter *sorter; GtkSorter *multi_sorter; GListStore *sorters; GtkExpression *expression; - GtkWidget *button; - GtkWidget *label; window = gtk_window_new (); gtk_window_set_title (GTK_WINDOW (window), "Colors"); @@ -555,18 +459,6 @@ do_listview_colors (GtkWidget *do_widget) model = gtk_grid_view_get_model (GTK_GRID_VIEW (gridview)); g_object_get (model, "model", &model, NULL); - button = gtk_button_new_with_mnemonic ("_Refill"); - g_signal_connect (button, "clicked", - G_CALLBACK (refill), - gtk_sort_list_model_get_model (GTK_SORT_LIST_MODEL (model))); - - gtk_header_bar_pack_start (GTK_HEADER_BAR (header), button); - - label = gtk_label_new ("0 items"); - g_signal_connect (gtk_grid_view_get_model (GTK_GRID_VIEW (gridview)), - "items-changed", G_CALLBACK (items_changed_cb), label); - gtk_header_bar_pack_start (GTK_HEADER_BAR (header), label); - sorters = g_list_store_new (GTK_TYPE_SORTER); sorter = gtk_string_sorter_new (gtk_property_expression_new (GTK_TYPE_COLOR, NULL, "name"));