[dconf-editor] Clean KeyEditorChild* reload functions.



commit 7deed7f1896c6b2080c2af052d37ff5b4c5aa0ae
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue Jul 26 23:22:04 2016 +0200

    Clean KeyEditorChild* reload functions.

 editor/dconf-view.vala             |  124 ++++++++++++++++++++++++++++--------
 editor/modifications-revealer.vala |    9 +--
 editor/registry-info.vala          |   17 ++---
 editor/registry-view.vala          |    6 +-
 4 files changed, 109 insertions(+), 47 deletions(-)
---
diff --git a/editor/dconf-view.vala b/editor/dconf-view.vala
index e5873e0..925c40c 100644
--- a/editor/dconf-view.vala
+++ b/editor/dconf-view.vala
@@ -19,7 +19,7 @@ using Gtk;
 
 public interface KeyEditorChild : Widget
 {
-    public signal void value_has_changed (bool enable_revealer, bool is_valid = false);
+    public signal void value_has_changed (bool is_valid = true);
 
     public abstract Variant get_variant ();
     public signal void child_activated ();
@@ -49,7 +49,7 @@ private class KeyEditorChildEnum : MenuButton, KeyEditorChild
                 reload (gvariant);
                 popover.closed ();
 
-                value_has_changed (true, true);
+                value_has_changed ();
             });
         reload (key.planned_change && (key.planned_value != null) ? key.planned_value : key.value);
         this.set_popover ((Popover) popover);
@@ -99,7 +99,7 @@ private class KeyEditorChildFlags : Grid, KeyEditorChild
         popover.set_relative_to (button);
         popover.value_changed.connect ((gvariant) => {
                 reload (gvariant);
-                value_has_changed (true, true);
+                value_has_changed ();
             });
         reload (key.planned_change && (key.planned_value != null) ? key.planned_value : key.value);
         button.set_popover ((Popover) popover);
@@ -114,7 +114,6 @@ private class KeyEditorChildFlags : Grid, KeyEditorChild
     {
         this.variant = gvariant;
         label.label = gvariant.print (false);
-        value_has_changed (false);
     }
 }
 
@@ -141,7 +140,7 @@ private class KeyEditorChildNullableBool : MenuButton, KeyEditorChild
                 reload (gvariant);
                 popover.closed ();
 
-                value_has_changed (true, true);
+                value_has_changed ();
             });
         reload (key.planned_change && (key.planned_value != null) ? key.planned_value : key.value);
         this.set_popover ((Popover) popover);
@@ -193,7 +192,7 @@ private class KeyEditorChildBool : Grid, KeyEditorChild // might be managed by a
         button_true.active = initial_value;
         button_true.bind_property ("active", button_false, "active", 
BindingFlags.INVERT_BOOLEAN|BindingFlags.SYNC_CREATE|BindingFlags.BIDIRECTIONAL);
 
-        button_true.toggled.connect (() => { value_has_changed (true, true); });
+        button_true.toggled.connect (() => { value_has_changed (); });
     }
 
     public Variant get_variant ()
@@ -204,13 +203,13 @@ private class KeyEditorChildBool : Grid, KeyEditorChild // might be managed by a
     public void reload (Variant gvariant)
     {
         button_true.active = gvariant.get_boolean ();
-        value_has_changed (false);
     }
 }
 
 private class KeyEditorChildNumberDouble : SpinButton, KeyEditorChild
 {
-    private uint locked = 0;
+    private ulong deleted_text_handler = 0;
+    private ulong inserted_text_handler = 0;
 
     public KeyEditorChildNumberDouble (Key key)
         requires (key.type_string == "d")
@@ -239,28 +238,52 @@ private class KeyEditorChildNumberDouble : SpinButton, KeyEditorChild
         this.input_purpose = InputPurpose.NUMBER;
         this.width_chars = 30;
 
-        this.buffer.deleted_text.connect (() => { if (locked > 0) locked -= 1; else value_has_changed (true, 
true); });     // TODO test value for
-        this.buffer.inserted_text.connect (() => { if (locked > 0) locked -= 1; else value_has_changed 
(true, true); });    //   non-numeric chars
-        this.activate.connect (() => { update (); child_activated (); });
+        EntryBuffer ref_buffer = buffer;    // an EntryBuffer doesn't emit a "destroy" signal
+        deleted_text_handler = ref_buffer.deleted_text.connect (() => { value_has_changed (); });     // 
TODO test value for
+        inserted_text_handler = ref_buffer.inserted_text.connect (() => { value_has_changed (); });   //   
non-numeric chars
+        ulong entry_activate_handler = activate.connect (() => { update (); child_activated (); });
+
+        destroy.connect (() => {
+                ref_buffer.disconnect (deleted_text_handler);
+                ref_buffer.disconnect (inserted_text_handler);
+                disconnect (entry_activate_handler);
+            });
     }
 
-    public Variant get_variant ()
+    public Variant get_variant ()   // TODO test_value against range
     {
         return new Variant.double (this.get_value ());  // TODO parse the text instead of getting the value, 
or updates when editing manually are buggy
     }
 
+    private void set_lock (bool state)
+        requires (deleted_text_handler != 0 && inserted_text_handler != 0)
+    {
+        if (state)
+        {
+            SignalHandler.block (buffer, deleted_text_handler);
+            SignalHandler.block (buffer, inserted_text_handler);
+        }
+        else
+        {
+            SignalHandler.unblock (buffer, deleted_text_handler);
+            SignalHandler.unblock (buffer, inserted_text_handler);
+        }
+    }
+
     public void reload (Variant gvariant)
     {
-        locked = 2;
+        set_lock (true);
         this.set_value (gvariant.get_double ());
-        value_has_changed (false);  // set disable_revealer_for_value to false, might be useful for corner 
cases
+        set_lock (false);
     }
 }
 
 private class KeyEditorChildNumberInt : SpinButton, KeyEditorChild
 {
     private string key_type;
-    private uint locked = 0;
+
+    private ulong deleted_text_handler = 0;
+    private ulong inserted_text_handler = 0;
 
     public KeyEditorChildNumberInt (Key key)
         requires (key.type_string == "y" || key.type_string == "n" || key.type_string == "q" || 
key.type_string == "i" || key.type_string == "u" || key.type_string == "h")     // TODO key.type_string == 
"x" || key.type_string == "t" ||
@@ -289,9 +312,16 @@ private class KeyEditorChildNumberInt : SpinButton, KeyEditorChild
         this.input_purpose = InputPurpose.NUMBER;   // TODO could be DIGITS for UnsignedInt
         this.width_chars = 30;
 
-        this.buffer.deleted_text.connect (() => { if (locked > 0) locked -= 1; else value_has_changed (true, 
true); });     // TODO test value for
-        this.buffer.inserted_text.connect (() => { if (locked > 0) locked -= 1; else value_has_changed 
(true, true); });    //   non-numeric chars
-        this.activate.connect (() => { update (); child_activated (); });
+        EntryBuffer ref_buffer = buffer;    // an EntryBuffer doesn't emit a "destroy" signal
+        deleted_text_handler = ref_buffer.deleted_text.connect (() => { value_has_changed (); });
+        inserted_text_handler = ref_buffer.inserted_text.connect (() => { value_has_changed (); });
+        ulong entry_activate_handler = activate.connect (() => { update (); child_activated (); });
+
+        destroy.connect (() => {
+                ref_buffer.disconnect (deleted_text_handler);
+                ref_buffer.disconnect (inserted_text_handler);
+                disconnect (entry_activate_handler);
+            });
     }
 
     private static void get_min_and_max_double (out double min, out double max, string variant_type)
@@ -322,7 +352,7 @@ private class KeyEditorChildNumberInt : SpinButton, KeyEditorChild
         }
     }
 
-    public Variant get_variant ()
+    public Variant get_variant ()   // TODO test_value against range
     {
         switch (key_type)
         {
@@ -340,11 +370,26 @@ private class KeyEditorChildNumberInt : SpinButton, KeyEditorChild
         return int64.parse (this.get_text ());
     }
 
+    private void set_lock (bool state)
+        requires (deleted_text_handler != 0 && inserted_text_handler != 0)
+    {
+        if (state)
+        {
+            SignalHandler.block (buffer, deleted_text_handler);
+            SignalHandler.block (buffer, inserted_text_handler);
+        }
+        else
+        {
+            SignalHandler.unblock (buffer, deleted_text_handler);
+            SignalHandler.unblock (buffer, inserted_text_handler);
+        }
+    }
+
     public void reload (Variant gvariant)       // TODO "key_editor_child_number_int_real_reload: assertion 
'gvariant != NULL' failed" two times when ghosting a key
     {
-        locked = 2;
+        set_lock (true);
         this.set_value (get_variant_as_double (gvariant));
-        value_has_changed (false);  // set disable_revealer_for_value to false, might be useful for corner 
cases
+        set_lock (false);
     }
 }
 
@@ -353,7 +398,9 @@ private class KeyEditorChildDefault : Entry, KeyEditorChild
     private string variant_type;
     private Variant variant;
     private bool is_string;
-    private bool locked = false;
+
+    private ulong deleted_text_handler = 0;
+    private ulong inserted_text_handler = 0;
 
     public KeyEditorChildDefault (string type, Variant initial_value)
     {
@@ -368,10 +415,16 @@ private class KeyEditorChildDefault : Entry, KeyEditorChild
         this.is_string = type == "s" || type == "o" || type == "g";
         this.text = is_string ? initial_value.get_string () : initial_value.print (false);
 
-        this.buffer.deleted_text.connect (() => { if (!locked) value_has_changed (true, test_value ()); });
-        this.buffer.inserted_text.connect (() => { if (!locked) value_has_changed (true, test_value ()); });
-        this.activate.connect (() => { if (test_value ()) child_activated (); });
-        value_has_changed (true, test_value ());
+        EntryBuffer ref_buffer = buffer;    // an EntryBuffer doesn't emit a "destroy" signal
+        deleted_text_handler = ref_buffer.deleted_text.connect (() => { value_has_changed (test_value ()); 
});
+        inserted_text_handler = ref_buffer.inserted_text.connect (() => { value_has_changed (test_value ()); 
});
+        ulong entry_activate_handler = activate.connect (() => { if (test_value ()) child_activated (); });
+
+        destroy.connect (() => {
+                ref_buffer.disconnect (deleted_text_handler);
+                ref_buffer.disconnect (inserted_text_handler);
+                disconnect (entry_activate_handler);
+            });
     }
 
     private bool test_value ()
@@ -410,12 +463,27 @@ private class KeyEditorChildDefault : Entry, KeyEditorChild
         return variant;
     }
 
+    private void set_lock (bool state)
+        requires (deleted_text_handler != 0 && inserted_text_handler != 0)
+    {
+        if (state)
+        {
+            SignalHandler.block (buffer, deleted_text_handler);
+            SignalHandler.block (buffer, inserted_text_handler);
+        }
+        else
+        {
+            SignalHandler.unblock (buffer, deleted_text_handler);
+            SignalHandler.unblock (buffer, inserted_text_handler);
+        }
+    }
+
     public void reload (Variant gvariant)
     {
-        locked = true;
+        set_lock (true);
         this.text = is_string ? gvariant.get_string () : gvariant.print (false);
         if (!test_value ())
             assert_not_reached ();
-        locked = false;
+        set_lock (false);
     }
 }
diff --git a/editor/modifications-revealer.vala b/editor/modifications-revealer.vala
index 7516af3..8379a1a 100644
--- a/editor/modifications-revealer.vala
+++ b/editor/modifications-revealer.vala
@@ -38,7 +38,6 @@ class ModificationsRevealer : Revealer
     private HashTable<string, GSettingsKey> gsettings_keys_awaiting_hashtable = new HashTable<string, 
GSettingsKey> (str_hash, str_equal);
 
     public signal void reload ();
-    public signal void reload_menu ();
 
     public Behaviour behaviour { get; set; }
 
@@ -110,10 +109,7 @@ class ModificationsRevealer : Revealer
     public void dismiss_change (Key key)
     {
         if (mode == Mode.NONE)
-        {
-            dismiss_delayed_settings ();
-            return;
-        }
+            mode = behaviour == Behaviour.ALWAYS_DELAY ? Mode.DELAYED : Mode.TEMPORARY;
 
         key.planned_change = false;
         key.planned_value = null;
@@ -199,7 +195,7 @@ class ModificationsRevealer : Revealer
 
         /* reload the hamburger menu */ /* FIXME should go back to keys list if the key is erased */
 
-        reload_menu ();
+        reload ();
     }
 
     [GtkCallback]
@@ -225,7 +221,6 @@ class ModificationsRevealer : Revealer
         /* reload notably key_editor_child */
 
         reload ();
-        reload_menu ();
     }
 
     /*\
diff --git a/editor/registry-info.vala b/editor/registry-info.vala
index 8bb7b35..79181f8 100644
--- a/editor/registry-info.vala
+++ b/editor/registry-info.vala
@@ -110,19 +110,16 @@ class RegistryInfo : Grid
 
         KeyEditorChild key_editor_child = create_child (key);
 
-        ulong value_has_changed_handler = key_editor_child.value_has_changed.connect ((enable_revealer, 
is_valid) => {
-                if (enable_revealer)
+        ulong value_has_changed_handler = key_editor_child.value_has_changed.connect ((is_valid) => {
+                if (revealer.should_delay_apply (tmp_string))
                 {
-                    if (revealer.should_delay_apply (tmp_string))
-                    {
-                        if (is_valid)
-                            revealer.add_delayed_setting (key, key_editor_child.get_variant ());
-                        else
-                            revealer.dismiss_change (key);
-                    }
+                    if (is_valid)
+                        revealer.add_delayed_setting (key, key_editor_child.get_variant ());
                     else
-                        key.value = key_editor_child.get_variant ();
+                        revealer.dismiss_change (key);
                 }
+                else
+                    key.value = key_editor_child.get_variant ();
             });
 
         if (has_schema)
diff --git a/editor/registry-view.vala b/editor/registry-view.vala
index 85d4c93..ae5a8f0 100644
--- a/editor/registry-view.vala
+++ b/editor/registry-view.vala
@@ -44,7 +44,7 @@ class RegistryView : Grid
 
     construct
     {
-        ulong revealer_reload_menu_handler = revealer.reload_menu.connect (invalidate_popovers);
+        ulong revealer_reload_handler = revealer.reload.connect (invalidate_popovers);
 
         EntryBuffer buffer = search_entry.get_buffer ();
         ulong search_entry_buffer_deleted_text_handler = buffer.deleted_text.connect (() => { 
search_next_button.set_sensitive (true); });
@@ -53,7 +53,7 @@ class RegistryView : Grid
         bind_property ("behaviour", revealer, "behaviour", 
BindingFlags.BIDIRECTIONAL|BindingFlags.SYNC_CREATE);
 
         destroy.connect (() => {
-                revealer.disconnect (revealer_reload_menu_handler);
+                revealer.disconnect (revealer_reload_handler);
                 buffer.disconnect (search_entry_buffer_deleted_text_handler);
 
                 base.destroy ();
@@ -245,6 +245,7 @@ class RegistryView : Grid
             ulong change_dismissed_handler = key_row.change_dismissed.connect (() => { 
revealer.dismiss_change (key); });
 
             ulong key_planned_change_handler = key.notify ["planned-change"].connect (() => { 
set_delayed_icon (row, key); });
+            ulong key_planned_value_handler = key.notify ["planned-value"].connect (() => { set_delayed_icon 
(row, key); });
             set_delayed_icon (row, key);
 
             on_row_clicked_handler = row.on_row_clicked.connect (() => {
@@ -260,6 +261,7 @@ class RegistryView : Grid
                     key_row.disconnect (set_key_value_handler);
                     key_row.disconnect (change_dismissed_handler);
                     key.disconnect (key_planned_change_handler);
+                    key.disconnect (key_planned_value_handler);
                 });
         }
         ulong button_press_event_handler = row.button_press_event.connect (on_button_pressed);


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