[gtksourceview] styleschemechooserwidget: use GtkSourceStyleSchemePreview



commit 72de112f430c8dfee3ddc917760e26dc35d9c654
Author: Christian Hergert <chergert redhat com>
Date:   Sat Oct 16 16:03:32 2021 -0700

    styleschemechooserwidget: use GtkSourceStyleSchemePreview
    
    This reuses the preview widget instead of duplicating that effort. We
    might want to interate on the style a bit, but this at least improves the
    visibility of style schemes.

 gtksourceview/gtksourcestyleschemechooserbutton.c  |   8 +-
 gtksourceview/gtksourcestyleschemechooserwidget.c  | 262 ++++-----------------
 gtksourceview/gtksourcestyleschemechooserwidget.ui |   3 +-
 3 files changed, 55 insertions(+), 218 deletions(-)
---
diff --git a/gtksourceview/gtksourcestyleschemechooserbutton.c 
b/gtksourceview/gtksourcestyleschemechooserbutton.c
index 3e6744f0..6aeaa016 100644
--- a/gtksourceview/gtksourcestyleschemechooserbutton.c
+++ b/gtksourceview/gtksourcestyleschemechooserbutton.c
@@ -195,8 +195,12 @@ ensure_dialog (GtkSourceStyleSchemeChooserButton *button)
        content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
        gtk_box_append (GTK_BOX (content_area), scrolled_window);
 
-       priv->chooser = GTK_SOURCE_STYLE_SCHEME_CHOOSER_WIDGET (gtk_source_style_scheme_chooser_widget_new 
());
-       gtk_widget_show (GTK_WIDGET (priv->chooser));
+       priv->chooser = g_object_new (GTK_SOURCE_TYPE_STYLE_SCHEME_CHOOSER_WIDGET,
+                                     "margin-top", 24,
+                                     "margin-bottom", 24,
+                                     "margin-start", 24,
+                                     "margin-end", 24,
+                                     NULL);
        gtk_source_style_scheme_chooser_set_style_scheme (GTK_SOURCE_STYLE_SCHEME_CHOOSER (priv->chooser),
                                                          priv->scheme);
 
diff --git a/gtksourceview/gtksourcestyleschemechooserwidget.c 
b/gtksourceview/gtksourcestyleschemechooserwidget.c
index c3e9b17f..daffb366 100644
--- a/gtksourceview/gtksourcestyleschemechooserwidget.c
+++ b/gtksourceview/gtksourcestyleschemechooserwidget.c
@@ -20,16 +20,11 @@
 
 #include "config.h"
 
-#if ENABLE_FONT_CONFIG
-# include <fontconfig/fontconfig.h>
-# include <pango/pangocairo.h>
-# include <pango/pangofc-fontmap.h>
-#endif
-
 #include "gtksourcestyleschemechooserwidget.h"
 #include "gtksourcestyleschemechooser.h"
 #include "gtksourcestylescheme-private.h"
 #include "gtksourcestyleschememanager.h"
+#include "gtksourcestyleschemepreview.h"
 #include "gtksourcelanguage.h"
 #include "gtksourcelanguagemanager.h"
 #include "gtksourcebuffer.h"
@@ -55,7 +50,7 @@
 
 typedef struct
 {
-       GtkListBox *list_box;
+       GtkGrid              *grid;
        GtkSourceStyleScheme *scheme;
 } GtkSourceStyleSchemeChooserWidgetPrivate;
 
@@ -74,49 +69,19 @@ enum
        PROP_STYLE_SCHEME
 };
 
-#if ENABLE_FONT_CONFIG
-static FcConfig *map_font_config;
-static GtkCssProvider *css_provider;
-
 static void
-load_override_font (GtkWidget *widget)
+chooser_style_scheme_action_cb (GtkWidget  *widget,
+                               const char *action_name,
+                               GVariant   *param)
 {
-       PangoFontDescription *font_desc;
-       PangoFontMap *font_map;
-
-       if (g_once_init_enter (&map_font_config))
-       {
-               const gchar *font_path = PACKAGE_DATADIR"/fonts/BuilderBlocks.ttf";
-               FcConfig *config = FcInitLoadConfigAndFonts ();
-
-               if (!g_file_test (font_path, G_FILE_TEST_IS_REGULAR))
-                       g_debug ("\"%s\" is missing or inaccessible", font_path);
-
-               FcConfigAppFontAddFile (config, (const FcChar8 *)font_path);
-
-               css_provider = gtk_css_provider_new ();
-               gtk_css_provider_load_from_data (css_provider, "textview, textview text { font-family: 
BuilderBlocks; font-size: 13px; line-height: 17px; }", -1);
-
-               g_once_init_leave (&map_font_config, config);
-       }
-
-       font_map = pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT);
-       pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (font_map), map_font_config);
-       gtk_widget_set_font_map (widget, font_map);
-       font_desc = pango_font_description_from_string ("BuilderBlocks");
-
-       g_assert (map_font_config != NULL);
-       g_assert (font_map != NULL);
-       g_assert (font_desc != NULL);
+       GtkSourceStyleSchemeManager *manager = gtk_source_style_scheme_manager_get_default ();
+       GtkSourceStyleScheme *scheme = gtk_source_style_scheme_manager_get_scheme (manager, 
g_variant_get_string (param, NULL));
 
-       gtk_style_context_add_provider (gtk_widget_get_style_context (widget),
-                                       GTK_STYLE_PROVIDER (css_provider),
-                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION-1);
+       if (scheme == NULL)
+               return;
 
-       pango_font_description_free (font_desc);
-       g_object_unref (font_map);
+       g_object_set (widget, "style-scheme", scheme, NULL);
 }
-#endif
 
 static void
 gtk_source_style_scheme_chooser_widget_dispose (GObject *object)
@@ -124,7 +89,12 @@ gtk_source_style_scheme_chooser_widget_dispose (GObject *object)
        GtkSourceStyleSchemeChooserWidget *widget = GTK_SOURCE_STYLE_SCHEME_CHOOSER_WIDGET (object);
        GtkSourceStyleSchemeChooserWidgetPrivate *priv = 
gtk_source_style_scheme_chooser_widget_get_instance_private (widget);
 
-       g_clear_pointer ((GtkWidget **)&priv->list_box, gtk_widget_unparent);
+       if (priv->grid != NULL)
+       {
+               gtk_widget_unparent (GTK_WIDGET (priv->grid));
+               priv->grid = NULL;
+       }
+
        g_clear_object (&priv->scheme);
 
        G_OBJECT_CLASS (gtk_source_style_scheme_chooser_widget_parent_class)->dispose (object);
@@ -179,159 +149,40 @@ gtk_source_style_scheme_chooser_widget_class_init (GtkSourceStyleSchemeChooserWi
        gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
        gtk_widget_class_set_template_from_resource (widget_class,
                                                     
"/org/gnome/gtksourceview/ui/gtksourcestyleschemechooserwidget.ui");
-       gtk_widget_class_bind_template_child_private (widget_class, GtkSourceStyleSchemeChooserWidget, 
list_box);
+       gtk_widget_class_bind_template_child_private (widget_class, GtkSourceStyleSchemeChooserWidget, grid);
 
        g_object_class_override_property (object_class, PROP_STYLE_SCHEME, "style-scheme");
-}
 
-static GtkWidget *
-make_row (GtkSourceStyleScheme *scheme,
-          GtkSourceLanguage    *language)
-{
-       PangoAttrList *attrs;
-       GtkWidget *row;
-       GtkSourceBuffer *buffer;
-       GtkWidget *view;
-       GtkWidget *overlay;
-       GtkWidget *label;
-       gchar *text;
-       const char *name;
-       GdkRGBA fg;
-       gboolean has_fg;
-
-       row = gtk_list_box_row_new ();
-       name = gtk_source_style_scheme_get_name (scheme);
-       has_fg = _gtk_source_style_scheme_get_text_color (scheme, &fg);
-
-       g_object_set_data (G_OBJECT (row), "scheme", scheme);
-
-       buffer = gtk_source_buffer_new_with_language (language);
-       gtk_source_buffer_set_highlight_matching_brackets (buffer, FALSE);
-       gtk_source_buffer_set_style_scheme (buffer, scheme);
-
-       text = g_strdup_printf ("/* %s */\n#include <gtksourceview/gtksource.h>",
-                               gtk_source_style_scheme_get_name (scheme));
-       gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), text, -1);
-       g_free (text);
-
-       overlay = gtk_overlay_new ();
-       gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), overlay);
-
-       view = g_object_new (GTK_SOURCE_TYPE_VIEW,
-                            "buffer", buffer,
-                            "focusable", FALSE,
-                            "can-focus", FALSE,
-                            "cursor-visible", FALSE,
-                            "editable", FALSE,
-                            "right-margin-position", 30,
-                            "show-right-margin", TRUE,
-                            "top-margin", 6,
-                            "bottom-margin", 6,
-                            "left-margin", 6,
-                            "right-margin", 6,
-                            "height-request", 38,
-                            NULL);
-       load_override_font (view);
-       gtk_overlay_set_child (GTK_OVERLAY (overlay), view);
-
-       if (!has_fg)
-               gtk_style_context_get_color (gtk_widget_get_style_context (view), &fg);
-
-       attrs = pango_attr_list_new ();
-       pango_attr_list_insert (attrs, pango_attr_foreground_new (fg.red * 65535, fg.green * 65535, fg.blue * 
65535));
-       pango_attr_list_insert (attrs, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
-       pango_attr_list_insert (attrs, pango_attr_scale_new (0.9));
-
-       label = g_object_new (GTK_TYPE_LABEL,
-                             "attributes", attrs,
-                             "focusable", FALSE,
-                             "can-focus", FALSE,
-                             "hexpand", TRUE,
-                             "vexpand", TRUE,
-                             "yalign", 0.0f,
-                             "xalign", 1.0f,
-                             "label", name,
-                             "selectable", FALSE,
-                             "margin-top", 3,
-                             "margin-end", 6,
-                             NULL);
-       gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
-
-       gtk_widget_show (row);
-       pango_attr_list_unref (attrs);
-
-       return row;
-}
-
-static void
-on_row_selected (GtkListBox                        *list_box,
-                 GtkListBoxRow                     *row,
-                 GtkSourceStyleSchemeChooserWidget *widget)
-{
-       GtkSourceStyleSchemeChooserWidgetPrivate *priv = 
gtk_source_style_scheme_chooser_widget_get_instance_private (widget);
-
-       if (row != NULL)
-       {
-               GtkSourceStyleScheme *scheme;
-
-               scheme = g_object_get_data (G_OBJECT (row), "scheme");
-
-               if (g_set_object (&priv->scheme, scheme))
-               {
-                       g_object_notify (G_OBJECT (widget), "style-scheme");
-               }
-       }
+       gtk_widget_class_install_action (widget_class, "chooser.style-scheme", "s", 
chooser_style_scheme_action_cb);
 }
 
 static void
 gtk_source_style_scheme_chooser_widget_populate (GtkSourceStyleSchemeChooserWidget *widget)
 {
        GtkSourceStyleSchemeChooserWidgetPrivate *priv = 
gtk_source_style_scheme_chooser_widget_get_instance_private (widget);
-       GtkSourceLanguageManager *lm;
-       GtkSourceLanguage *lang;
        GtkSourceStyleSchemeManager *manager;
-       const gchar * const *scheme_ids;
+       const char * const *scheme_ids;
        GtkWidget *child;
-       guint i;
-       gboolean row_selected = FALSE;
 
-       g_signal_handlers_block_by_func (priv->list_box, on_row_selected, widget);
+       g_assert (GTK_SOURCE_IS_STYLE_SCHEME_CHOOSER (widget));
 
-       while ((child = gtk_widget_get_first_child (GTK_WIDGET (priv->list_box))))
+       while ((child = gtk_widget_get_first_child (GTK_WIDGET (priv->grid))))
        {
-               gtk_list_box_remove (priv->list_box, child);
+              gtk_grid_remove (priv->grid, child);
        }
 
        manager = gtk_source_style_scheme_manager_get_default ();
        scheme_ids = gtk_source_style_scheme_manager_get_scheme_ids (manager);
 
-       lm = gtk_source_language_manager_get_default ();
-       lang = gtk_source_language_manager_get_language (lm, "c");
-
-       for (i = 0; scheme_ids [i]; i++)
+       for (guint i = 0; scheme_ids[i]; i++)
        {
-               GtkWidget *row;
-               GtkSourceStyleScheme *scheme;
-
-               scheme = gtk_source_style_scheme_manager_get_scheme (manager, scheme_ids [i]);
-               row = make_row (scheme, lang);
-               gtk_list_box_insert (priv->list_box, GTK_WIDGET (row), -1);
+               GtkSourceStyleScheme *scheme = gtk_source_style_scheme_manager_get_scheme (manager, 
scheme_ids[i]);
+               GtkWidget *preview = gtk_source_style_scheme_preview_new (scheme);
 
-               if (scheme == priv->scheme)
-               {
-                       gtk_list_box_select_row (priv->list_box, GTK_LIST_BOX_ROW (row));
-
-                       row_selected = TRUE;
-               }
-       }
-
-       g_signal_handlers_unblock_by_func (priv->list_box, on_row_selected, widget);
-
-       /* The current scheme may have been removed so select the default one */
-       if (!row_selected)
-       {
-               gtk_source_style_scheme_chooser_set_style_scheme (GTK_SOURCE_STYLE_SCHEME_CHOOSER (widget),
-                                                                 _gtk_source_style_scheme_get_default ());
+               gtk_actionable_set_action_name (GTK_ACTIONABLE (preview), "chooser.style-scheme");
+               gtk_actionable_set_action_target (GTK_ACTIONABLE (preview), "s", scheme_ids[i]);
+               gtk_widget_set_hexpand (preview, TRUE);
+               gtk_grid_attach (priv->grid, preview, i % 2, i / 2, 1, 1);
        }
 }
 
@@ -346,26 +197,17 @@ on_scheme_ids_changed (GtkSourceStyleSchemeManager       *manager,
 static void
 gtk_source_style_scheme_chooser_widget_init (GtkSourceStyleSchemeChooserWidget *widget)
 {
-       GtkSourceStyleSchemeChooserWidgetPrivate *priv = 
gtk_source_style_scheme_chooser_widget_get_instance_private (widget);
-       GtkSourceStyleSchemeManager *manager;
-
        gtk_widget_init_template (GTK_WIDGET (widget));
 
-       manager = gtk_source_style_scheme_manager_get_default ();
-       g_signal_connect (manager,
-                         "notify::scheme-ids",
-                         G_CALLBACK (on_scheme_ids_changed),
-                         widget);
+       g_signal_connect_object (gtk_source_style_scheme_manager_get_default (),
+                                "notify::scheme-ids",
+                                G_CALLBACK (on_scheme_ids_changed),
+                                widget,
+                                0);
 
        gtk_source_style_scheme_chooser_widget_populate (widget);
-
        gtk_source_style_scheme_chooser_set_style_scheme (GTK_SOURCE_STYLE_SCHEME_CHOOSER (widget),
                                                          _gtk_source_style_scheme_get_default ());
-
-       g_signal_connect (priv->list_box,
-                         "row-selected",
-                         G_CALLBACK (on_row_selected),
-                         widget);
 }
 
 static GtkSourceStyleScheme *
@@ -384,30 +226,22 @@ gtk_source_style_scheme_chooser_widget_set_style_scheme (GtkSourceStyleSchemeCho
        GtkSourceStyleSchemeChooserWidget *widget = GTK_SOURCE_STYLE_SCHEME_CHOOSER_WIDGET (chooser);
        GtkSourceStyleSchemeChooserWidgetPrivate *priv = 
gtk_source_style_scheme_chooser_widget_get_instance_private (widget);
 
-       if (g_set_object (&priv->scheme, scheme))
+       g_assert (GTK_SOURCE_IS_STYLE_SCHEME_CHOOSER_WIDGET (widget));
+
+       if (!g_set_object (&priv->scheme, scheme))
+               return;
+
+       for (GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (priv->grid));
+            child;
+            child = gtk_widget_get_next_sibling (child))
        {
-               GtkWidget *child;
-
-               for (child = gtk_widget_get_first_child (GTK_WIDGET (priv->list_box));
-                    child != NULL;
-                    child = gtk_widget_get_next_sibling (child))
-               {
-                       GtkListBoxRow *row = GTK_LIST_BOX_ROW (child);
-                       GtkSourceStyleScheme *cur;
-
-                       cur = g_object_get_data (G_OBJECT (row), "scheme");
-
-                       if (cur == scheme)
-                       {
-                               g_signal_handlers_block_by_func (priv->list_box, on_row_selected, widget);
-                               gtk_list_box_select_row (priv->list_box, row);
-                               g_signal_handlers_unblock_by_func (priv->list_box, on_row_selected, widget);
-                               break;
-                       }
-               }
-
-               g_object_notify (G_OBJECT (chooser), "style-scheme");
+               GtkSourceStyleSchemePreview *preview = GTK_SOURCE_STYLE_SCHEME_PREVIEW (child);
+               GtkSourceStyleScheme *child_scheme = gtk_source_style_scheme_preview_get_scheme (preview);
+
+               gtk_source_style_scheme_preview_set_selected (preview, scheme == child_scheme);
        }
+
+       g_object_notify (G_OBJECT (chooser), "style-scheme");
 }
 
 static void
diff --git a/gtksourceview/gtksourcestyleschemechooserwidget.ui 
b/gtksourceview/gtksourcestyleschemechooserwidget.ui
index a818e2c3..1c3d998e 100644
--- a/gtksourceview/gtksourcestyleschemechooserwidget.ui
+++ b/gtksourceview/gtksourcestyleschemechooserwidget.ui
@@ -3,8 +3,7 @@
   <requires lib="gtk+" version="3.98"/>
   <template class="GtkSourceStyleSchemeChooserWidget" parent="GtkWidget">
     <child>
-      <object class="GtkListBox" id="list_box">
-        <property name="selection-mode">browse</property>
+      <object class="GtkGrid" id="grid">
       </object>
     </child>
   </template>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]