[dconf-editor] Give up using SpinButton for double values.



commit c0eb038640e436bee78239867ec736d2cd767db7
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Nov 25 14:58:00 2017 +0100

    Give up using SpinButton for double values.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=789591

 editor/dconf-model.vala   |   11 ++++--
 editor/dconf-view.vala    |   73 +++++++++++++++++++++++++++------------------
 editor/registry-info.vala |    5 ++-
 3 files changed, 55 insertions(+), 34 deletions(-)
---
diff --git a/editor/dconf-model.vala b/editor/dconf-model.vala
index b7110bb..8d63b1a 100644
--- a/editor/dconf-model.vala
+++ b/editor/dconf-model.vala
@@ -277,9 +277,11 @@ public abstract class Key : SettingObject
                 max = (!) (nullable_max ?? "%llu".printf (uint64.MAX));
                 return;
             case "d":
-                min = double.MIN.to_string ();
-                max = double.MAX.to_string ();
-                return;     // TODO something
+                string? nullable_min = "%'g".printf (double.MIN).locale_to_utf8 (-1, null, null, null);
+                string? nullable_max = "%'g".printf (double.MAX).locale_to_utf8 (-1, null, null, null);
+                min = (!) (nullable_min ?? "%g".printf (double.MIN));
+                max = (!) (nullable_max ?? "%g".printf (double.MAX));
+                return;
             case "h":
                 string? nullable_min = "%'i".printf (int32.MIN).locale_to_utf8 (-1, null, null, null);
                 string? nullable_max = "%'i".printf (int32.MAX).locale_to_utf8 (-1, null, null, null);
@@ -318,7 +320,8 @@ public abstract class Key : SettingObject
                 string? nullable_text = "%'llu".printf (variant.get_uint64 ()).locale_to_utf8 (-1, null, 
null, null);
                 return (!) (nullable_text ?? "%llu".printf (variant.get_uint64 ()));
             case "d":
-                return variant.get_double ().to_string ();                                  // TODO 
something; notably, number of chars after coma
+                string? nullable_text = "%'.12g".printf (variant.get_double ()).locale_to_utf8 (-1, null, 
null, null);
+                return (!) (nullable_text ?? "%g".printf (variant.get_double ()));
             case "h":
                 string? nullable_text = "%'i".printf (variant.get_handle ()).locale_to_utf8 (-1, null, null, 
null);
                 return (!) (nullable_text ?? "%i".printf (variant.get_int32 ()));
diff --git a/editor/dconf-view.vala b/editor/dconf-view.vala
index 6d32ecb..a74080f 100644
--- a/editor/dconf-view.vala
+++ b/editor/dconf-view.vala
@@ -231,42 +231,29 @@ private class KeyEditorChildBool : Box, KeyEditorChild // might be managed by ac
     }
 }
 
-private class KeyEditorChildNumberDouble : SpinButton, KeyEditorChild
+private class KeyEditorChildNumberDouble : Entry, KeyEditorChild
 {
+    private Variant variant;
+
     private ulong deleted_text_handler = 0;
     private ulong inserted_text_handler = 0;
 
-    public KeyEditorChildNumberDouble (Key key)
-        requires (key.type_string == "d")
+    public KeyEditorChildNumberDouble (Variant initial_value)
     {
+        this.variant = initial_value;
+
         this.visible = true;
         this.hexpand = true;
-        this.halign = Align.START;
-
-        double min, max;
-        if (key is GSettingsKey && ((GSettingsKey) key).range_type == "range")
-        {
-            min = (((GSettingsKey) key).range_content.get_child_value (0)).get_double ();
-            max = (((GSettingsKey) key).range_content.get_child_value (1)).get_double ();
-        }
-        else
-        {
-            min = double.MIN;
-            max = double.MAX;
-        }
+        this.margin_right = 6;
+        this.secondary_icon_activatable = false;
+        this.set_icon_tooltip_text (EntryIconPosition.SECONDARY, _("Failed to parse as double."));    // 
TODO report bug, not displayed, neither like that nor by setting secondary_icon_tooltip_text
 
-        Adjustment adjustment = new Adjustment (key.planned_change && (key.planned_value != null) ? ((!) 
key.planned_value).get_double () : key.value.get_double (), min, max, 0.01, 0.1, 0.0);
-        this.configure (adjustment, 0.01, 2);
-
-        this.update_policy = SpinButtonUpdatePolicy.IF_VALID;
-        this.snap_to_ticks = false;
-        this.input_purpose = InputPurpose.NUMBER;
-        this.width_chars = 30;
+        this.text = initial_value.print (false);
 
         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 (); });
+        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);
@@ -275,9 +262,35 @@ private class KeyEditorChildNumberDouble : SpinButton, KeyEditorChild
             });
     }
 
-    public Variant get_variant ()   // TODO test_value against range
+    private bool test_value ()
     {
-        return new Variant.double (this.get_value ());  // TODO parse the text instead of getting the value, 
or updates when editing manually are buggy
+        string tmp_text = this.text; // don't put in the try{} for correct C code
+        try
+        {
+            Variant? tmp_variant = Variant.parse (VariantType.DOUBLE, tmp_text);
+            variant = (!) tmp_variant;
+
+            StyleContext context = get_style_context ();
+            if (context.has_class ("error"))
+                context.remove_class ("error");
+            set_icon_from_icon_name (EntryIconPosition.SECONDARY, null);
+
+            return true;
+        }
+        catch (VariantParseError e)
+        {
+            StyleContext context = get_style_context ();
+            if (!context.has_class ("error"))
+                context.add_class ("error");
+            secondary_icon_name = "dialog-error-symbolic";
+
+            return false;
+        }
+    }
+
+    public Variant get_variant ()
+    {
+        return variant;
     }
 
     private void set_lock (bool state)
@@ -298,7 +311,9 @@ private class KeyEditorChildNumberDouble : SpinButton, KeyEditorChild
     public void reload (Variant gvariant)
     {
         set_lock (true);
-        this.set_value (gvariant.get_double ());
+        this.text = gvariant.print (false);
+        if (!test_value ())
+            assert_not_reached ();
         set_lock (false);
     }
 }
diff --git a/editor/registry-info.vala b/editor/registry-info.vala
index ea0a694..da36c1a 100644
--- a/editor/registry-info.vala
+++ b/editor/registry-info.vala
@@ -247,7 +247,7 @@ class RegistryInfo : Grid
                 else
                     return (KeyEditorChild) new KeyEditorChildNumberInt (key);
             case "d":
-                return (KeyEditorChild) new KeyEditorChildNumberDouble (key);
+                return (KeyEditorChild) new KeyEditorChildNumberDouble (key.value);
             case "mb":
                 return (KeyEditorChild) new KeyEditorChildNullableBool (key);
             default:
@@ -317,6 +317,9 @@ class RegistryInfo : Grid
 
     private static Widget? add_warning (string type)
     {
+        if (type == "d")    // TODO if type contains "d"; on Intl.get_language_names ()[0] != "C"?
+            return warning_label (_("Use a dot as decimal mark and no thousands separator. You can use the 
X.Ye+Z notation."));
+
         if (type != "<flags>" && ((type != "s" && "s" in type) || (type != "g" && "g" in type)) || (type != 
"o" && "o" in type))
         {
             if ("m" in type)


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