[dconf-editor] Add flags support.



commit a53621b56a78f68a4fa470bc766b260c3d4927d7
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Oct 17 04:42:01 2015 +0200

    Add flags support.

 editor/dconf-model.vala  |    2 +-
 editor/dconf-view.vala   |   44 ++++++++++++++++++++++++++++++++++++++
 editor/dconf-window.vala |   53 ++++++++++++++++++++++++++++++++++++++++++++++
 editor/key-editor.ui     |    2 +-
 4 files changed, 99 insertions(+), 2 deletions(-)
---
diff --git a/editor/dconf-model.vala b/editor/dconf-model.vala
index f8e6a40..48ca5fb 100644
--- a/editor/dconf-model.vala
+++ b/editor/dconf-model.vala
@@ -104,7 +104,7 @@ public class Directory : SettingObject
         switch (range_type)
         {
             case "enum":    type_string = "<enum>"; break;  // <choices> or enum="", and hopefully <aliases>
-            case "flags":   type_string = "as";     break;  // TODO better
+            case "flags":   type_string = "<flags>"; break; // flags=""
             default:
             case "type":    type_string = (string) settings_schema_key.get_value_type ().peek_string (); 
break;
         }
diff --git a/editor/dconf-view.vala b/editor/dconf-view.vala
index 2c4a870..dba1bee 100644
--- a/editor/dconf-view.vala
+++ b/editor/dconf-view.vala
@@ -42,6 +42,10 @@ private abstract class KeyEditorDialog : Dialog
                 KeyEditorChildEnum _key_editor_child = new KeyEditorChildEnum (key);
                 key_editor_child = (KeyEditorChild) _key_editor_child;
                 return (Widget) _key_editor_child;
+            case "<flags>":
+                KeyEditorChildFlags _key_editor_child = new KeyEditorChildFlags ((GSettingsKey) key);
+                key_editor_child = (KeyEditorChild) _key_editor_child;
+                return (Widget) _key_editor_child;
             case "b":
                 KeyEditorChildBool _key_editor_child = new KeyEditorChildBool (key.value.get_boolean ());
                 key_editor_child = (KeyEditorChild) _key_editor_child;
@@ -316,6 +320,46 @@ private class KeyEditorChildEnum : MenuButton, KeyEditorChild
     }
 }
 
+private class KeyEditorChildFlags : Grid, KeyEditorChild
+{
+    private Variant variant;
+
+    public KeyEditorChildFlags (GSettingsKey key)
+    {
+        this.variant = key.value;
+
+        this.visible = true;
+        this.hexpand = true;
+
+        Label label = new Label (variant.print (false));
+        label.visible = true;
+        label.halign = Align.START;
+        label.hexpand = true;
+        this.attach (label, 0, 0, 1, 1);
+
+        MenuButton button = new MenuButton ();
+        button.visible = true;
+        button.use_popover = true;
+        button.halign = Align.END;
+        ((StyleContext) button.get_style_context ()).add_class ("image-button");
+        this.attach (button, 1, 0, 1, 1);
+
+        ContextPopover popover = new ContextPopover ();
+        popover.create_flags_list (key);
+        popover.set_relative_to (button);
+        popover.value_changed.connect ((bytes) => {
+                variant = new Variant.from_bytes (VariantType.STRING_ARRAY, bytes, true);
+                label.label = variant.print (false);
+            });
+        button.set_popover ((Popover) popover);
+    }
+
+    public Variant get_variant ()
+    {
+        return variant;
+    }
+}
+
 private class KeyEditorChildNullableBool : MenuButton, KeyEditorChild
 {
     private Variant variant;
diff --git a/editor/dconf-window.vala b/editor/dconf-window.vala
index fec47bd..98175d9 100644
--- a/editor/dconf-window.vala
+++ b/editor/dconf-window.vala
@@ -458,6 +458,14 @@ private class KeyListBoxRowEditable : KeyListBoxRow
             popover.set_to_default.connect (() => { key.set_to_default (); popover.destroy (); });
             popover.value_changed.connect ((bytes) => { key.value = bytes == null ? new Variant.maybe 
(VariantType.BOOLEAN, null) : new Variant.from_bytes (key.value.get_type (), bytes, true); popover.destroy 
(); });
         }
+        else if (key.type_string == "<flags>")
+        {
+            popover.add_separator ();
+            popover.add_action_button (_("Default value"), () => { key.set_to_default (); popover.destroy 
(); }, true);     // TODO string duplication
+            popover.create_flags_list ((GSettingsKey) key);
+
+            popover.value_changed.connect ((bytes) => { key.value = new Variant.from_bytes 
(VariantType.STRING_ARRAY, bytes, true); });
+        }
         else if (!key.is_default)
         {
             popover.add_separator ();
@@ -497,6 +505,10 @@ private class ContextPopover : Popover
         this.add (grid);
     }
 
+    /*\
+    * * Simple actions
+    \*/
+
     public delegate void button_action ();
     public void add_action_button (string label, button_action action, bool is_default = false)
     {
@@ -516,6 +528,47 @@ private class ContextPopover : Popover
         grid.add (separator);
     }
 
+    /*\
+    * * Flags
+    \*/
+
+    public void create_flags_list (GSettingsKey key)
+    {
+        GLib.Settings settings = new GLib.Settings (key.schema_id);
+        string [] active_flags = settings.get_strv (key.name);
+        string [] all_flags = key.range_content.get_strv ();
+        CheckButton [] buttons = new CheckButton [0];
+        foreach (string flag in all_flags)
+            buttons += new_flag_button (flag, flag in active_flags);
+
+        foreach (CheckButton button in buttons)
+            button.toggled.connect (() => { calculate_flags_value (buttons); });
+    }
+
+    private CheckButton new_flag_button (string text, bool is_active)
+    {
+        CheckButton button = new CheckButton ();
+        button.visible = true;
+        button.label = text;
+        button.active = is_active;
+        grid.add (button);
+        return button;
+    }
+
+    private void calculate_flags_value (CheckButton [] buttons)
+    {
+        string [] new_flags = new string [0];
+        foreach (CheckButton button in buttons)
+            if (button.active)
+                new_flags += button.label;
+        Variant variant = new Variant.strv (new_flags);
+        value_changed (variant.get_data_as_bytes ());
+    }
+
+    /*\
+    * * Choices
+    \*/
+
     public void create_buttons_list (Key key, bool nullable)
     {
         VariantType original_type = key.value.get_type ();
diff --git a/editor/key-editor.ui b/editor/key-editor.ui
index 8bc816a..606dc17 100644
--- a/editor/key-editor.ui
+++ b/editor/key-editor.ui
@@ -109,8 +109,8 @@
               <object class="GtkLabel">
                 <property name="visible">True</property>
                 <property name="halign">end</property>
+                <property name="valign">end</property>
                 <property name="label" translatable="yes">Use default value</property>
-                <property name="valign">baseline</property>
               </object>
               <packing>
                 <property name="left-attach">0</property>


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