[gnome-text-editor] prefs: tweak style-variant when style-scheme changes



commit e10978180135c53164e74cb7dbb1f3ecddfbf1f0
Author: Christian Hergert <chergert redhat com>
Date:   Thu Aug 19 18:54:16 2021 -0700

    prefs: tweak style-variant when style-scheme changes
    
    This is a minimal fix for #128 to change the style-variant (light/dark)
    so that the style scheme will be changed when it is selected.
    
    What we probably want long term is to change the style of the application
    theme with the style scheme colors. However, that will require a bit more
    work in libadwaita before that is easily doable without the hacks we've
    done in various places in Builder.
    
    Currently, for some style schemes you can change the style-variant
    manually afterwards from the primary menu, but schemes meant for light/dark
    like solarized or Adwaita will still not quite follow this. That is a
    known issue and will require us to handle things like mentioned above.

 src/editor-application-actions.c | 50 ++++++++++++++++++++++++++++++++++++++++
 src/editor-application.c         |  3 ++-
 src/editor-window.c              | 27 +++++++++++++---------
 3 files changed, 68 insertions(+), 12 deletions(-)
---
diff --git a/src/editor-application-actions.c b/src/editor-application-actions.c
index a6a6fa1..a063a5e 100644
--- a/src/editor-application-actions.c
+++ b/src/editor-application-actions.c
@@ -194,6 +194,44 @@ editor_application_actions_quit (GSimpleAction *action,
                              g_object_ref (self));
 }
 
+static void
+editor_application_actions_style_scheme_cb (GSimpleAction *action,
+                                            GVariant      *param,
+                                            gpointer       user_data)
+{
+  EditorApplication *self = user_data;
+  const char *name;
+
+  g_assert (G_IS_SIMPLE_ACTION (action));
+  g_assert (EDITOR_IS_APPLICATION (self));
+  g_assert (g_variant_is_of_type (param, G_VARIANT_TYPE_STRING));
+
+  name = g_variant_get_string (param, NULL);
+
+  g_settings_set_string (self->settings, "style-scheme", name);
+
+  if (g_str_has_suffix (name, "-dark"))
+    g_settings_set_string (self->settings, "style-variant", "dark");
+  else
+    g_settings_set_string (self->settings, "style-variant", "light");
+}
+
+static void
+editor_application_actions_settings_changed_cb (GSettings     *settings,
+                                                const char    *key,
+                                                GSimpleAction *action)
+{
+  g_assert (G_IS_SETTINGS (settings));
+  g_assert (G_IS_SIMPLE_ACTION (action));
+
+  if (g_strcmp0 (key, "style-scheme") == 0 ||
+      g_strcmp0 (key, "style-variant") == 0)
+    {
+      g_autoptr(GVariant) v = g_settings_get_value (settings, "style-scheme");
+      g_simple_action_set_state (action, v);
+    }
+}
+
 void
 _editor_application_actions_init (EditorApplication *self)
 {
@@ -202,10 +240,22 @@ _editor_application_actions_init (EditorApplication *self)
     { "about", editor_application_actions_about_cb },
     { "help", editor_application_actions_help_cb },
     { "quit", editor_application_actions_quit },
+    { "style-scheme", NULL, "s", "''", editor_application_actions_style_scheme_cb },
   };
+  GAction *action;
 
   g_action_map_add_action_entries (G_ACTION_MAP (self),
                                    actions,
                                    G_N_ELEMENTS (actions),
                                    self);
+
+  action = g_action_map_lookup_action (G_ACTION_MAP (self), "style-scheme");
+  g_signal_connect_object (self->settings,
+                           "changed",
+                           G_CALLBACK (editor_application_actions_settings_changed_cb),
+                           action,
+                           0);
+  editor_application_actions_settings_changed_cb (self->settings,
+                                                  "style-scheme",
+                                                  G_SIMPLE_ACTION (action));
 }
diff --git a/src/editor-application.c b/src/editor-application.c
index 3c93560..b0b4939 100644
--- a/src/editor-application.c
+++ b/src/editor-application.c
@@ -155,10 +155,11 @@ editor_application_startup (GApplication *application)
   gtk_application_set_accels_for_action (GTK_APPLICATION (self), "app.quit", quit_accels);
   gtk_application_set_accels_for_action (GTK_APPLICATION (self), "app.help", help_accels);
 
+  self->settings = g_settings_new ("org.gnome.TextEditor");
+
   _editor_application_actions_init (self);
 
   gtk_settings = gtk_settings_get_default ();
-  self->settings = g_settings_new ("org.gnome.TextEditor");
 
   g_settings_bind (self->settings, "auto-save-delay",
                    self->session, "auto-save-delay",
diff --git a/src/editor-window.c b/src/editor-window.c
index d7e842a..288e446 100644
--- a/src/editor-window.c
+++ b/src/editor-window.c
@@ -312,26 +312,31 @@ editor_window_constructed (GObject *object)
   sm = gtk_source_style_scheme_manager_get_default ();
   if ((scheme_ids = gtk_source_style_scheme_manager_get_scheme_ids (sm)))
     {
-      EditorPreferencesRadio *group = NULL;
+      GtkCheckButton *group = NULL;
 
       for (guint i = 0; scheme_ids[i]; i++)
         {
           GtkSourceStyleScheme *scheme = gtk_source_style_scheme_manager_get_scheme (sm, scheme_ids[i]);
           const char *name = gtk_source_style_scheme_get_name (scheme);
-          EditorPreferencesRadio *radio;
-
-          radio = g_object_new (EDITOR_TYPE_PREFERENCES_RADIO,
-                                "schema-id", "org.gnome.TextEditor",
-                                "schema-key", "style-scheme",
-                                "schema-value", scheme_ids[i],
-                                "title", name,
+          GtkWidget *radio;
+          GtkWidget *w;
+
+          radio = g_object_new (GTK_TYPE_CHECK_BUTTON,
+                                "can-focus", FALSE,
+                                "valign", GTK_ALIGN_CENTER,
+                                "action-name", "app.style-scheme",
+                                "action-target", g_variant_new_string (scheme_ids[i]),
                                 NULL);
-          adw_preferences_group_add (self->scheme_group, GTK_WIDGET (radio));
+          w = adw_action_row_new ();
+          adw_preferences_row_set_title (ADW_PREFERENCES_ROW (w), name);
+          adw_action_row_add_prefix (ADW_ACTION_ROW (w), radio);
+          adw_action_row_set_activatable_widget (ADW_ACTION_ROW (w), radio);
+          adw_preferences_group_add (self->scheme_group, w);
 
           if (group == NULL)
-            group = radio;
+            group = GTK_CHECK_BUTTON (radio);
           else
-            editor_preferences_radio_set_group (radio, group);
+            gtk_check_button_set_group (GTK_CHECK_BUTTON (radio), group);
         }
     }
 }


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