[dconf-editor] Less code duplication.



commit 29d9c3e1bb17ccc27df57bf67298298824b7dab2
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Oct 2 00:15:00 2015 +0200

    Less code duplication.

 editor/dconf-view.vala         |  213 +++++++++++++++++-----------------------
 editor/dconf-window.vala       |    3 +-
 editor/key-editor-no-schema.ui |    2 +-
 editor/key-editor.ui           |    2 +-
 4 files changed, 92 insertions(+), 128 deletions(-)
---
diff --git a/editor/dconf-view.vala b/editor/dconf-view.vala
index 2e73533..3ac484f 100644
--- a/editor/dconf-view.vala
+++ b/editor/dconf-view.vala
@@ -17,35 +17,20 @@
 
 using Gtk;
 
-[GtkTemplate (ui = "/ca/desrt/dconf-editor/ui/key-editor-no-schema.ui")]
-private class KeyEditorNoSchema : Dialog
+private abstract class KeyEditorDialog : Dialog
 {
-    [GtkChild] private Button button_apply;
-    [GtkChild] private Grid custom_value_grid;
+    protected Key key;
+    protected bool custom_value_is_valid { get; set; default = true; }
 
-    private Key key;
-    private bool custom_value_is_valid = true;
-
-    public KeyEditorNoSchema (Key _key)
-        requires (!_key.has_schema)
+    public KeyEditorDialog ()
     {
         Object (use_header_bar: Gtk.Settings.get_default ().gtk_dialogs_use_header ? 1 : 0);
-
-        this.key = _key;
-
-        // infos
-
-        this.title = key.name;
-        if (this.use_header_bar == 1)        // TODO else..?
-            ((HeaderBar) this.get_header_bar ()).subtitle = key.parent.full_name;       // TODO 
get_header_bar() is [transfer none]
-
-        // widgets creation
-        custom_value_grid.add (create_child ());
-
-        this.response.connect (response_cb);
+        this.response.connect ((dialog, response_id) => { if (response_id == ResponseType.APPLY) 
response_apply_cb (); this.destroy (); });
     }
 
-    private KeyEditorChild create_child ()
+    protected abstract void response_apply_cb ();
+
+    protected KeyEditorChild create_child ()
     {
         switch (key.type_string)
         {
@@ -66,25 +51,83 @@ private class KeyEditorNoSchema : Dialog
                 return new KeyEditorChildNumber (key);
             default:
                 KeyEditorChildDefault key_editor_child_default = new KeyEditorChildDefault (key.type_string, 
key.value);
-                key_editor_child_default.is_valid.connect ((is_valid) => { custom_value_is_valid = is_valid; 
button_apply.set_sensitive (is_valid); });
+                key_editor_child_default.is_valid.connect ((is_valid) => { custom_value_is_valid = is_valid; 
});
                 return key_editor_child_default;
         }
     }
 
-    private void response_cb (Dialog dialog, int response_id)
+    protected string key_to_description ()
     {
-        if (response_id == ResponseType.APPLY)
+        switch (key.type_string)
         {
-            Variant variant = ((KeyEditorChild) custom_value_grid.get_child_at (0, 0)).get_variant ();
-            if (key.is_default || key.value != variant)
-                key.value = variant;
+            case "y":
+            case "n":
+            case "q":
+            case "i":
+            case "u":
+            case "x":
+            case "t":
+                string min, max;
+                get_min_and_max (out min, out max);
+                return _("Integer [%s..%s]").printf (min, max);
+            case "d":
+                string min, max;
+                get_min_and_max (out min, out max);
+                return _("Double [%s..%s]").printf (min, max);
+            case "b":
+                return _("Boolean");
+            case "s":
+                return _("String");
+            case "<enum>":
+                return _("Enumeration");
+            default:
+                return key.type_string;
+        }
+    }
+
+    private void get_min_and_max (out string min, out string max)
+    {
+        if (key.has_schema && key.schema.range_type == "range")     // TODO test more; and what happen if 
only min/max is in range?
+        {
+            min = key.schema.range_content.get_child_value (0).print (false);
+            max = key.schema.range_content.get_child_value (1).print (false);
+        }
+        else
+        {
+            string variant_type = key.type_string;
+            min = Key.get_min (variant_type).print (false);
+            max = Key.get_max (variant_type).print (false);
         }
-        this.destroy ();
+    }
+}
+
+[GtkTemplate (ui = "/ca/desrt/dconf-editor/ui/key-editor-no-schema.ui")]
+private class KeyEditorNoSchema : KeyEditorDialog       // TODO add type information, or integrate type 
information in KeyEditorChilds
+{
+    [GtkChild] private Grid custom_value_grid;
+
+    public KeyEditorNoSchema (Key _key)
+        requires (!_key.has_schema)
+    {
+        this.key = _key;
+
+        this.title = key.name;
+        if (this.use_header_bar == 1)        // TODO else..?
+            ((HeaderBar) this.get_header_bar ()).subtitle = key.parent.full_name;       // TODO 
get_header_bar() is [transfer none]
+
+        custom_value_grid.add (create_child ());
+    }
+
+    protected override void response_apply_cb ()
+    {
+        Variant variant = ((KeyEditorChild) custom_value_grid.get_child_at (0, 0)).get_variant ();
+        if (key.is_default || key.value != variant)
+            key.value = variant;
     }
 }
 
 [GtkTemplate (ui = "/ca/desrt/dconf-editor/ui/key-editor.ui")]
-private class KeyEditor : Dialog
+private class KeyEditor : KeyEditorDialog
 {
     [GtkChild] private Button button_apply;
     [GtkChild] private Grid custom_value_grid;
@@ -97,22 +140,19 @@ private class KeyEditor : Dialog
 
     [GtkChild] private Switch custom_value_switch;
 
-    private Key key;
-    private bool custom_value_is_valid = true;
-
     public KeyEditor (Key _key)
         requires (_key.has_schema)
     {
-        Object (use_header_bar: Gtk.Settings.get_default ().gtk_dialogs_use_header ? 1 : 0);
-
         this.key = _key;
 
-        // infos
-
         this.title = key.name;
         if (this.use_header_bar == 1)        // TODO else..?
             ((HeaderBar) this.get_header_bar ()).subtitle = key.parent.full_name;       // TODO 
get_header_bar() is [transfer none]
 
+        custom_value_grid.add (create_child ());
+
+        // infos
+
         string summary = key.schema.summary ?? "";
         string description = key.schema.description ?? "";
 
@@ -122,100 +162,23 @@ private class KeyEditor : Dialog
         type_label.set_text (key_to_description ());
         default_label.set_text (key.schema.default_value.print (false));
 
-        // widgets creation
+        // switch
 
         custom_value_switch.set_active (key.is_default);
         custom_value_switch.notify["active"].connect (() => { button_apply.set_sensitive 
(custom_value_switch.get_active () ? true : custom_value_is_valid); });
-
-        custom_value_grid.add (create_child ());
-
-        this.response.connect (response_cb);
+        notify["custom-value-is-valid"].connect (() => { button_apply.set_sensitive (custom_value_is_valid); 
});
     }
 
-    private KeyEditorChild create_child ()
+    protected override void response_apply_cb ()
     {
-        switch (key.type_string)
+        if (!custom_value_switch.active)
         {
-            case "<enum>":
-                return new KeyEditorChildEnum (key);
-            case "b":
-                return new KeyEditorChildBool (key.value.get_boolean ());
-            case "s":
-                return new KeyEditorChildString (key.value.get_string ());
-            case "y":
-            case "n":
-            case "q":
-            case "i":
-            case "u":
-            case "x":
-            case "t":
-            case "d":
-                return new KeyEditorChildNumber (key);
-            default:
-                KeyEditorChildDefault key_editor_child_default = new KeyEditorChildDefault (key.type_string, 
key.value);
-                key_editor_child_default.is_valid.connect ((is_valid) => { custom_value_is_valid = is_valid; 
button_apply.set_sensitive (is_valid); });
-                return key_editor_child_default;
-        }
-    }
-
-    private string key_to_description ()
-    {
-        switch (key.type_string)
-        {
-            case "y":
-            case "n":
-            case "q":
-            case "i":
-            case "u":
-            case "x":
-            case "t":
-                string min, max;
-                get_min_and_max (out min, out max);
-                return _("Integer [%s..%s]").printf (min, max);
-            case "d":
-                string min, max;
-                get_min_and_max (out min, out max);
-                return _("Double [%s..%s]").printf (min, max);
-            case "b":
-                return _("Boolean");
-            case "s":
-                return _("String");
-            case "<enum>":
-                return _("Enumeration");
-            default:
-                return key.schema.type;
-        }
-    }
-
-    private void get_min_and_max (out string min, out string max)
-    {
-        if (key.schema.range_type == "range")       // TODO test more; and what happen if only min/max is in 
range?
-        {
-            min = key.schema.range_content.get_child_value (0).print (false);
-            max = key.schema.range_content.get_child_value (1).print (false);
-        }
-        else
-        {
-            string variant_type = key.value.get_type_string ();
-            min = Key.get_min (variant_type).print (false);
-            max = Key.get_max (variant_type).print (false);
-        }
-    }
-
-    private void response_cb (Dialog dialog, int response_id)
-    {
-        if (response_id == ResponseType.APPLY)
-        {
-            if (!custom_value_switch.active)
-            {
-                Variant variant = ((KeyEditorChild) custom_value_grid.get_child_at (0, 0)).get_variant ();
-                if (key.is_default || key.value != variant)
-                    key.value = variant;
-            }
-            else if (!key.is_default)
-                key.set_to_default ();
+            Variant variant = ((KeyEditorChild) custom_value_grid.get_child_at (0, 0)).get_variant ();
+            if (key.is_default || key.value != variant)
+                key.value = variant;
         }
-        this.destroy ();
+        else if (!key.is_default)
+            key.set_to_default ();
     }
 }
 
@@ -330,7 +293,7 @@ private class KeyEditorChildNumber : Grid, KeyEditorChild
         this.attach (label, 0, 0, 1, 1);
 
         double min, max;
-        if (key.schema.range_type == "range")       // TODO test more; and what happen if only min/max is in 
range?
+        if (key.has_schema && key.schema.range_type == "range")       // TODO test more; and what happen if 
only min/max is in range?
         {
             min = get_variant_as_double (key.schema.range_content.get_child_value (0));
             max = get_variant_as_double (key.schema.range_content.get_child_value (1));
diff --git a/editor/dconf-window.vala b/editor/dconf-window.vala
index d348f97..6c09064 100644
--- a/editor/dconf-window.vala
+++ b/editor/dconf-window.vala
@@ -293,7 +293,7 @@ private class KeyListBoxRowEditableNoSchema : KeyListBoxRow
         this.key = _key;
 
         Pango.AttrList attr_list = new Pango.AttrList ();
-        attr_list.change (Pango.attr_weight_new (Pango.Weight.BOLD));    // TODO good?
+        attr_list.insert (Pango.attr_weight_new (Pango.Weight.BOLD));   // TODO good?
         key_name_label.set_attributes (attr_list);
         key_value_label.set_attributes (attr_list);
 
@@ -334,6 +334,7 @@ private class KeyListBoxRowEditable : KeyListBoxRow
     public KeyListBoxRowEditable (Key _key)
     {
         this.key = _key;
+
         key_value_label.set_attributes (attr_list);
         update ();      // sets key_name_label attributes and key_value_label label
         key_name_label.label = key.name;
diff --git a/editor/key-editor-no-schema.ui b/editor/key-editor-no-schema.ui
index b91c968..ac69e45 100644
--- a/editor/key-editor-no-schema.ui
+++ b/editor/key-editor-no-schema.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
-  <template class="KeyEditorNoSchema" parent="GtkDialog">
+  <template class="KeyEditorNoSchema" parent="KeyEditorDialog">
     <property name="visible">False</property>
     <property name="modal">True</property>
     <property name="default-height">1</property><!-- TODO bug of GtkDialog, that reserves space for bottom 
buttons -->
diff --git a/editor/key-editor.ui b/editor/key-editor.ui
index 3401669..9150c3b 100644
--- a/editor/key-editor.ui
+++ b/editor/key-editor.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
-  <template class="KeyEditor" parent="GtkDialog">
+  <template class="KeyEditor" parent="KeyEditorDialog">
     <property name="visible">False</property>
     <property name="modal">True</property>
     <property name="default-height">1</property><!-- TODO bug of GtkDialog, that reserves space for bottom 
buttons -->


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