[connections/refactor-properties-instanciation: 26/26] properties: Introduce ComboProperty for multiple options




commit cd84f0adf38ee3b397264e5f330512c6c5b074c6
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Dec 1 18:32:29 2020 +0100

    properties: Introduce ComboProperty for multiple options

 src/properties.vala            | 53 ++++++++++++++++++++++++++++++++++++++----
 src/vnc-properties-dialog.vala | 17 ++++++--------
 2 files changed, 56 insertions(+), 14 deletions(-)
---
diff --git a/src/properties.vala b/src/properties.vala
index 99c1c18..2dd90df 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -126,15 +126,22 @@ namespace Connections {
         }
     }
 
-     private class Property : GLib.Object {
+     private abstract class Property : GLib.Object {
         public string label;
-        public Gtk.Widget widget;
+        public abstract Gtk.Widget widget { get; }
     }
 
     private class BooleanProperty : Property {
+        private Gtk.Switch switch_widget;
+
+        public override Gtk.Widget widget {
+            get {
+                return switch_widget;
+            }
+        }
+
         public BooleanProperty (Connection connection, string property_id) {
-            widget = new Gtk.Switch ();
-            var switch_widget = widget as Gtk.Switch;
+            switch_widget = new Gtk.Switch ();
 
             bool active = false;
             connection.get (property_id, out active);
@@ -143,4 +150,42 @@ namespace Connections {
             switch_widget.bind_property ("active", connection, property_id, BindingFlags.SYNC_CREATE);
         }
     }
+
+    private class ComboProperty : Property {
+        private Gtk.ComboBoxText combo_widget;
+        private string? default_option;
+
+        public signal void changed (string property_value);
+        public string active_id {
+            get {
+                return combo_widget.active_id;
+            }
+
+            set {
+                combo_widget.active_id = value;
+            }
+        }
+
+        public override Gtk.Widget widget {
+            get {
+                return combo_widget;
+            }
+        }
+
+        public ComboProperty (Connection connection, string property_id, string default_option) {
+            combo_widget = new Gtk.ComboBoxText ();
+            this.default_option = default_option;
+
+            combo_widget.notify["active-id"].connect (() => {
+                changed (combo_widget.active_id);
+            });
+        }
+
+        public void add_option (string option_id, string option_label) {
+            combo_widget.append (option_id, option_label);
+
+            if (option_id == default_option)
+                combo_widget.active_id = option_id;
+        }
+    }
 }
diff --git a/src/vnc-properties-dialog.vala b/src/vnc-properties-dialog.vala
index 5bf457f..04b50af 100644
--- a/src/vnc-properties-dialog.vala
+++ b/src/vnc-properties-dialog.vala
@@ -23,7 +23,6 @@ namespace Connections {
     private class VncPropertiesDialog : PropertiesDialog {
         public VncPropertiesDialog (Connection connection) {
             this.connection = connection;
-            var vnc = connection as VncConnection;
 
             var scaling = new BooleanProperty (connection, "scaling") {
                 label = _("Scaling")
@@ -40,16 +39,14 @@ namespace Connections {
             };
             add_property (local_pointer);
 
-            var combo_widget = new Gtk.ComboBoxText ();
-            combo_widget.append ("high-quality", _("High quality"));
-            combo_widget.append ("fast-refresh", _("Fast refresh"));
-            combo_widget.active_id = vnc.bandwidth.to_string ();
-            var bandwidth = new Property () {
-                label = _("Bandwidth"),
-                widget = combo_widget
+            var vnc = connection as VncConnection;
+            var bandwidth = new ComboProperty (vnc, "bandwidth", vnc.bandwidth.to_string ()) {
+                label = _("Bandwidth")
             };
-            combo_widget.notify["active-id"].connect (() => {
-                vnc.bandwidth = vnc.bandwidth.from_string (combo_widget.active_id);
+            bandwidth.add_option ("high-quality", _("High quality"));
+            bandwidth.add_option ("fast-refresh", _("Fast refresh"));
+            bandwidth.changed.connect ((property_value) => {
+               vnc.bandwidth = vnc.bandwidth.from_string (property_value);
             });
             add_property (bandwidth);
         }


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