[gnome-builder] libide/tweaks: port IdeTweaksSpin to IdeTweaksBinding



commit e28225ba38aea35bb64f8dc77f01d1ff424d8264
Author: Christian Hergert <chergert redhat com>
Date:   Fri Aug 26 12:37:05 2022 -0700

    libide/tweaks: port IdeTweaksSpin to IdeTweaksBinding
    
    This uses the new API in IdeTweaksBinding to create adjustments and wires
    that up to the spin button. Now, the number of digits is controlled in the
    ui file rather than trying to (poorly) guess what it would be.

 src/libide/tweaks/ide-tweaks-spin.c     | 197 +++++---------------------------
 src/libide/tweaks/ide-tweaks-spin.h     |  11 +-
 src/plugins/editorui/tweaks-language.ui |  39 ++++++-
 src/plugins/editorui/tweaks.ui          |  33 ++++--
 src/plugins/terminal/tweaks.ui          |   8 +-
 5 files changed, 96 insertions(+), 192 deletions(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-spin.c b/src/libide/tweaks/ide-tweaks-spin.c
index 5cac691a4..c42a7edb2 100644
--- a/src/libide/tweaks/ide-tweaks-spin.c
+++ b/src/libide/tweaks/ide-tweaks-spin.c
@@ -32,16 +32,14 @@
 struct _IdeTweaksSpin
 {
   IdeTweaksWidget parent_instance;
-  IdeTweaksSettings *settings;
-  char *key;
   char *title;
   char *subtitle;
+  guint digits;
 };
 
 enum {
   PROP_0,
-  PROP_KEY,
-  PROP_SETTINGS,
+  PROP_DIGITS,
   PROP_SUBTITLE,
   PROP_TITLE,
   N_PROPS
@@ -51,120 +49,25 @@ G_DEFINE_FINAL_TYPE (IdeTweaksSpin, ide_tweaks_spin, IDE_TYPE_TWEAKS_WIDGET)
 
 static GParamSpec *properties [N_PROPS];
 
-static void
-set_double_property (gpointer    instance,
-                     const char *property,
-                     GVariant   *value)
-{
-  GValue val = { 0 };
-  double v = 0;
-
-  g_assert (instance != NULL);
-  g_assert (property != NULL);
-  g_assert (value != NULL);
-
-  if (g_variant_is_of_type (value, G_VARIANT_TYPE_DOUBLE))
-    v = g_variant_get_double (value);
-
-  else if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT16))
-    v = g_variant_get_int16 (value);
-  else if (g_variant_is_of_type (value, G_VARIANT_TYPE_UINT16))
-    v = g_variant_get_uint16 (value);
-
-  else if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT32))
-    v = g_variant_get_int32 (value);
-  else if (g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
-    v = g_variant_get_uint32 (value);
-
-  else if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT64))
-    v = g_variant_get_int64 (value);
-  else if (g_variant_is_of_type (value, G_VARIANT_TYPE_UINT64))
-    v = g_variant_get_uint64 (value);
-
-  else
-    g_warning ("Unknown variant type: %s\n", (gchar *)g_variant_get_type (value));
-
-  g_value_init (&val, G_TYPE_DOUBLE);
-  g_value_set_double (&val, v);
-  g_object_set_property (instance, property, &val);
-  g_value_unset (&val);
-}
-
-static GtkAdjustment *
-create_adjustment (const char *schema_id,
-                   const char *key,
-                   guint      *digits)
-{
-  GSettingsSchemaSource *source;
-  GSettingsSchemaKey *schema_key = NULL;
-  GSettingsSchema *schema = NULL;
-  GtkAdjustment *ret = NULL;
-  GVariant *range = NULL;
-  GVariant *values = NULL;
-  GVariant *lower = NULL;
-  GVariant *upper = NULL;
-  GVariantIter iter;
-  char *type = NULL;
-
-  g_assert (schema_id != NULL);
-
-  source = g_settings_schema_source_get_default ();
-  schema = g_settings_schema_source_lookup (source, schema_id, TRUE);
-  schema_key = g_settings_schema_get_key (schema, key);
-  range = g_settings_schema_key_get_range (schema_key);
-  g_variant_get (range, "(sv)", &type, &values);
-
-  if (!ide_str_equal0 (type, "range") ||
-      (2 != g_variant_iter_init (&iter, values)))
-    goto cleanup;
-
-  lower = g_variant_iter_next_value (&iter);
-  upper = g_variant_iter_next_value (&iter);
-
-  ret = gtk_adjustment_new (0, 0, 0, 1, 10, 0);
-  set_double_property (ret, "lower", lower);
-  set_double_property (ret, "upper", upper);
-
-  if (g_variant_is_of_type (lower, G_VARIANT_TYPE_DOUBLE) ||
-      g_variant_is_of_type (upper, G_VARIANT_TYPE_DOUBLE))
-    {
-      gtk_adjustment_set_step_increment (ret, 0.1);
-      *digits = 2;
-    }
-
-cleanup:
-  g_clear_pointer (&schema, g_settings_schema_unref);
-  g_clear_pointer (&schema_key, g_settings_schema_key_unref);
-  g_clear_pointer (&range, g_variant_unref);
-  g_clear_pointer (&lower, g_variant_unref);
-  g_clear_pointer (&upper, g_variant_unref);
-  g_clear_pointer (&values, g_variant_unref);
-  g_clear_pointer (&type, g_free);
-
-  return ret;
-}
-
 static GtkWidget *
 ide_tweaks_spin_create_for_item (IdeTweaksWidget *instance,
                                  IdeTweaksItem   *widget)
 {
   IdeTweaksSpin *self = (IdeTweaksSpin *)widget;
-  GtkAdjustment *adjustment;
+  IdeTweaksBinding *binding;
+  GtkAdjustment *adjustment = NULL;
   GtkSpinButton *button;
-  const char *schema_id;
   AdwActionRow *row;
-  guint digits = 0;
 
   g_assert (IDE_IS_TWEAKS_SPIN (self));
 
-  if (self->settings == NULL || self->key == NULL)
-    return NULL;
+  if ((binding = ide_tweaks_widget_get_binding (IDE_TWEAKS_WIDGET (self))) &&
+      (adjustment = ide_tweaks_binding_create_adjustment (binding)))
+    ide_tweaks_binding_bind (binding, adjustment, "value");
 
-  schema_id = ide_tweaks_settings_get_schema_id (self->settings);
-  adjustment = create_adjustment (schema_id, self->key, &digits);
   button = g_object_new (GTK_TYPE_SPIN_BUTTON,
                          "adjustment", adjustment,
-                         "digits", digits,
+                         "digits", self->digits,
                          "valign", GTK_ALIGN_CENTER,
                          NULL);
   row = g_object_new (ADW_TYPE_ACTION_ROW,
@@ -174,10 +77,6 @@ ide_tweaks_spin_create_for_item (IdeTweaksWidget *instance,
                       NULL);
   adw_action_row_add_suffix (row, GTK_WIDGET (button));
 
-  ide_tweaks_settings_bind (self->settings, self->key,
-                            adjustment, "value",
-                            G_SETTINGS_BIND_DEFAULT);
-
   return GTK_WIDGET (row);
 }
 
@@ -186,10 +85,8 @@ ide_tweaks_spin_dispose (GObject *object)
 {
   IdeTweaksSpin *self = (IdeTweaksSpin *)object;
 
-  g_clear_pointer (&self->key, g_free);
   g_clear_pointer (&self->title, g_free);
   g_clear_pointer (&self->subtitle, g_free);
-  g_clear_object (&self->settings);
 
   G_OBJECT_CLASS (ide_tweaks_spin_parent_class)->dispose (object);
 }
@@ -204,12 +101,8 @@ ide_tweaks_spin_get_property (GObject    *object,
 
   switch (prop_id)
     {
-    case PROP_KEY:
-      g_value_set_string (value, ide_tweaks_spin_get_key (self));
-      break;
-
-    case PROP_SETTINGS:
-      g_value_set_object (value, ide_tweaks_spin_get_settings (self));
+    case PROP_DIGITS:
+      g_value_set_uint (value, ide_tweaks_spin_get_digits (self));
       break;
 
     case PROP_SUBTITLE:
@@ -235,12 +128,8 @@ ide_tweaks_spin_set_property (GObject      *object,
 
   switch (prop_id)
     {
-    case PROP_KEY:
-      ide_tweaks_spin_set_key (self, g_value_get_string (value));
-      break;
-
-    case PROP_SETTINGS:
-      ide_tweaks_spin_set_settings (self, g_value_get_object (value));
+    case PROP_DIGITS:
+      ide_tweaks_spin_set_digits (self, g_value_get_uint (value));
       break;
 
     case PROP_SUBTITLE:
@@ -268,15 +157,10 @@ ide_tweaks_spin_class_init (IdeTweaksSpinClass *klass)
 
   widget_class->create_for_item = ide_tweaks_spin_create_for_item;
 
-  properties[PROP_KEY] =
-    g_param_spec_string ("key", NULL, NULL,
-                         NULL,
-                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
-
-  properties[PROP_SETTINGS] =
-    g_param_spec_object ("settings", NULL, NULL,
-                         IDE_TYPE_TWEAKS_SETTINGS,
-                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+  properties[PROP_DIGITS] =
+    g_param_spec_uint ("digits", NULL, NULL,
+                       0, 6, 0,
+                       (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
 
   properties[PROP_SUBTITLE] =
     g_param_spec_string ("subtitle", NULL, NULL,
@@ -296,28 +180,25 @@ ide_tweaks_spin_init (IdeTweaksSpin *self)
 {
 }
 
-/**
- * ide_tweaks_spin_get_settings:
- * @self: a #IdeTweaksSpin
- *
- * Gets the settings containing #IdeTweaksSpin:key.
- *
- * Returns: (transfer none) (nullable): an #IdeTweaksSettings or %NULL
- */
-IdeTweaksSettings *
-ide_tweaks_spin_get_settings (IdeTweaksSpin *self)
+guint
+ide_tweaks_spin_get_digits (IdeTweaksSpin *self)
 {
-  g_return_val_if_fail (IDE_IS_TWEAKS_SPIN (self), NULL);
+  g_return_val_if_fail (IDE_IS_TWEAKS_SPIN (self), 0);
 
-  return self->settings;
+  return self->digits;
 }
 
-const char *
-ide_tweaks_spin_get_key (IdeTweaksSpin *self)
+void
+ide_tweaks_spin_set_digits (IdeTweaksSpin *self,
+                            guint          digits)
 {
-  g_return_val_if_fail (IDE_IS_TWEAKS_SPIN (self), NULL);
+  g_return_if_fail (IDE_IS_TWEAKS_SPIN (self));
 
-  return self->key;
+  if (digits != self->digits)
+    {
+      self->digits = digits;
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_DIGITS]);
+    }
 }
 
 const char *
@@ -336,26 +217,6 @@ ide_tweaks_spin_get_title (IdeTweaksSpin *self)
   return self->title;
 }
 
-void
-ide_tweaks_spin_set_settings (IdeTweaksSpin     *self,
-                              IdeTweaksSettings *settings)
-{
-  g_return_if_fail (IDE_IS_TWEAKS_SPIN (self));
-
-  if (g_set_object (&self->settings, settings))
-    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SETTINGS]);
-}
-
-void
-ide_tweaks_spin_set_key (IdeTweaksSpin *self,
-                           const char    *key)
-{
-  g_return_if_fail (IDE_IS_TWEAKS_SPIN (self));
-
-  if (ide_set_string (&self->key, key))
-    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_KEY]);
-}
-
 void
 ide_tweaks_spin_set_subtitle (IdeTweaksSpin *self,
                               const char    *subtitle)
diff --git a/src/libide/tweaks/ide-tweaks-spin.h b/src/libide/tweaks/ide-tweaks-spin.h
index da9332ac1..4ae2db183 100644
--- a/src/libide/tweaks/ide-tweaks-spin.h
+++ b/src/libide/tweaks/ide-tweaks-spin.h
@@ -47,14 +47,9 @@ IDE_AVAILABLE_IN_ALL
 void               ide_tweaks_spin_set_subtitle (IdeTweaksSpin     *self,
                                                  const char        *subtitle);
 IDE_AVAILABLE_IN_ALL
-const char        *ide_tweaks_spin_get_key      (IdeTweaksSpin     *self);
+guint              ide_tweaks_spin_get_digits   (IdeTweaksSpin     *self);
 IDE_AVAILABLE_IN_ALL
-void               ide_tweaks_spin_set_key      (IdeTweaksSpin     *self,
-                                                 const char        *key);
-IDE_AVAILABLE_IN_ALL
-IdeTweaksSettings *ide_tweaks_spin_get_settings (IdeTweaksSpin     *self);
-IDE_AVAILABLE_IN_ALL
-void               ide_tweaks_spin_set_settings (IdeTweaksSpin     *self,
-                                                 IdeTweaksSettings *settings);
+void               ide_tweaks_spin_set_digits   (IdeTweaksSpin     *self,
+                                                 guint              digits);
 
 G_END_DECLS
diff --git a/src/plugins/editorui/tweaks-language.ui b/src/plugins/editorui/tweaks-language.ui
index adf4e332d..99a922177 100644
--- a/src/plugins/editorui/tweaks-language.ui
+++ b/src/plugins/editorui/tweaks-language.ui
@@ -110,16 +110,34 @@
                               <object class="IdeTweaksSpin" id="tab_width">
                                 <property name="title" translatable="yes">Tab Size</property>
                                 <property name="subtitle" translatable="yes">The number of characters to 
indent for Tab</property>
-                                <property name="settings">language_page_settings</property>
-                                <property name="key">tab-width</property>
+                                <property name="binding">
+                                  <object class="IdeTweaksSetting">
+                                    <property name="schema-id">org.gnome.builder.editor.language</property>
+                                    <property name="schema-key">tab-width</property>
+                                    <binding name="path-suffix">
+                                      <lookup name="id" type="GtkSourceLanguage">
+                                        <lookup name="item">language_page_factory</lookup>
+                                      </lookup>
+                                    </binding>
+                                  </object>
+                                </property>
                               </object>
                             </child>
                             <child>
                               <object class="IdeTweaksSpin" id="indent_width">
                                 <property name="title" translatable="yes">Indentation Size</property>
                                 <property name="subtitle" translatable="yes">The number of characters to 
indent, or -1 to use tab size</property>
-                                <property name="settings">language_page_settings</property>
-                                <property name="key">indent-width</property>
+                                <property name="binding">
+                                  <object class="IdeTweaksSetting">
+                                    <property name="schema-id">org.gnome.builder.editor.language</property>
+                                    <property name="schema-key">indent-width</property>
+                                    <binding name="path-suffix">
+                                      <lookup name="id" type="GtkSourceLanguage">
+                                        <lookup name="item">language_page_factory</lookup>
+                                      </lookup>
+                                    </binding>
+                                  </object>
+                                </property>
                               </object>
                             </child>
                           </object>
@@ -138,8 +156,17 @@
                               <object class="IdeTweaksSpin" id="right_margin_position">
                                 <property name="title" translatable="yes">Right Margin Position</property>
                                 <property name="subtitle" translatable="yes">The desired maximum line 
length</property>
-                                <property name="settings">language_page_settings</property>
-                                <property name="key">right-margin-position</property>
+                                <property name="binding">
+                                  <object class="IdeTweaksSetting">
+                                    <property name="schema-id">org.gnome.builder.editor.language</property>
+                                    <property name="schema-key">right-margin-position</property>
+                                    <binding name="path-suffix">
+                                      <lookup name="id" type="GtkSourceLanguage">
+                                        <lookup name="item">language_page_factory</lookup>
+                                      </lookup>
+                                    </binding>
+                                  </object>
+                                </property>
                               </object>
                             </child>
                           </object>
diff --git a/src/plugins/editorui/tweaks.ui b/src/plugins/editorui/tweaks.ui
index f22406a2b..0fb9bbb54 100644
--- a/src/plugins/editorui/tweaks.ui
+++ b/src/plugins/editorui/tweaks.ui
@@ -77,8 +77,13 @@
                           <object class="IdeTweaksSpin" id="editor_line_height">
                             <property name="title" translatable="yes">Line Height</property>
                             <property name="subtitle" translatable="yes">Adjust spacing between 
lines</property>
-                            <property name="settings">settings_org_gnome_builder_editor</property>
-                            <property name="key">line-height</property>
+                            <property name="digits">2</property>
+                            <property name="binding">
+                              <object class="IdeTweaksSetting">
+                                <property name="schema-id">org.gnome.builder.editor</property>
+                                <property name="schema-key">line-height</property>
+                              </object>
+                            </property>
                           </object>
                         </child>
                       </object>
@@ -257,8 +262,12 @@
                           <object class="IdeTweaksSpin">
                             <property name="title" translatable="yes">Minimum Characters to 
Highlight</property>
                             <property name="subtitle" translatable="yes">The minimum selection length before 
highlighting matches</property>
-                            <property name="settings">settings_org_gnome_builder_editor</property>
-                            <property name="key">min-char-selected</property>
+                            <property name="binding">
+                              <object class="IdeTweaksSetting">
+                                <property name="schema-id">org.gnome.builder.editor</property>
+                                <property name="schema-key">min-char-selected</property>
+                              </object>
+                            </property>
                           </object>
                         </child>
                       </object>
@@ -306,8 +315,12 @@
                           <object class="IdeTweaksSpin" id="max_completion_rows">
                             <property name="title" translatable="yes">Maximum Completion Proposals</property>
                             <property name="subtitle" translatable="yes">The maximum number of completion 
rows that will be displayed at once</property>
-                            <property name="settings">settings_org_gnome_builder_editor</property>
-                            <property name="key">completion-n-rows</property>
+                            <property name="binding">
+                              <object class="IdeTweaksSetting">
+                                <property name="schema-id">org.gnome.builder.editor</property>
+                                <property name="schema-key">completion-n-rows</property>
+                              </object>
+                            </property>
                           </object>
                         </child>
                       </object>
@@ -409,8 +422,12 @@
                           <object class="IdeTweaksSpin">
                             <property name="title" translatable="yes">Auto-Save Delay</property>
                             <property name="subtitle" translatable="yes">The delay in seconds to wait before 
auto-saving the document</property>
-                            <property name="settings">settings_org_gnome_builder_editor</property>
-                            <property name="key">auto-save-timeout</property>
+                            <property name="binding">
+                              <object class="IdeTweaksSetting">
+                                <property name="schema-id">org.gnome.builder.editor</property>
+                                <property name="schema-key">auto-save-timeout</property>
+                              </object>
+                            </property>
                           </object>
                         </child>
                       </object>
diff --git a/src/plugins/terminal/tweaks.ui b/src/plugins/terminal/tweaks.ui
index 5c3ac30ff..f3e3a4399 100644
--- a/src/plugins/terminal/tweaks.ui
+++ b/src/plugins/terminal/tweaks.ui
@@ -79,8 +79,12 @@
                   <object class="IdeTweaksSpin">
                     <property name="title" translatable="yes">Maximum Lines in Scrollback</property>
                     <property name="subtitle" translatable="yes">The maximum number of lines stored in 
history when limiting scrollback</property>
-                    <property name="settings">settings_org_gnome_builder_terminal</property>
-                    <property name="key">scrollback-lines</property>
+                    <property name="binding">
+                      <object class="IdeTweaksSetting">
+                        <property name="schema-id">org.gnome.builder.editor.terminal</property>
+                        <property name="schema-key">scrollback-lines</property>
+                      </object>
+                    </property>
                   </object>
                 </child>
               </object>


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