[connections/refactor-properties-instanciation: 3/4] properties: Revamp the way we express properties




commit d4e1618a5a812da9a9fc22638f0527cc907300d1
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Dec 1 17:49:50 2020 +0100

    properties: Revamp the way we express properties
    
    There's no need to duplicate code when we can wrap the binding
    between the UI widget and the Connection property by having an
    specialized Property object.

 src/properties.vala            | 13 +++++++++++++
 src/rdp-connection.vala        |  9 ++-------
 src/vnc-properties-dialog.vala | 24 ++++++------------------
 3 files changed, 21 insertions(+), 25 deletions(-)
---
diff --git a/src/properties.vala b/src/properties.vala
index 3c717fd..99c1c18 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -130,4 +130,17 @@ namespace Connections {
         public string label;
         public Gtk.Widget widget;
     }
+
+    private class BooleanProperty : Property {
+        public BooleanProperty (Connection connection, string property_id) {
+            widget = new Gtk.Switch ();
+            var switch_widget = widget as Gtk.Switch;
+
+            bool active = false;
+            connection.get (property_id, out active);
+            switch_widget.active = active;
+
+            switch_widget.bind_property ("active", connection, property_id, BindingFlags.SYNC_CREATE);
+        }
+    }
 }
diff --git a/src/rdp-connection.vala b/src/rdp-connection.vala
index 87171db..57bc07e 100644
--- a/src/rdp-connection.vala
+++ b/src/rdp-connection.vala
@@ -124,14 +124,9 @@ namespace Connections {
         public RdpPropertiesDialog (Connection connection) {
             this.connection = connection;
 
-            var scaling = new Property () {
-                label = _("Scaling"),
-                widget = new Gtk.Switch () {
-                    active = connection.scaling
-                }
+            var scaling = new BooleanProperty (connection, "scaling") {
+                label = _("Scaling")
             };
-            scaling.widget.bind_property ("active", connection, "scaling", BindingFlags.SYNC_CREATE);
-            connection.notify["scaling"].connect (() => { connection.save (); });
             add_property (scaling);
         }
     }
diff --git a/src/vnc-properties-dialog.vala b/src/vnc-properties-dialog.vala
index abf6dcf..5bf457f 100644
--- a/src/vnc-properties-dialog.vala
+++ b/src/vnc-properties-dialog.vala
@@ -25,31 +25,19 @@ namespace Connections {
             this.connection = connection;
             var vnc = connection as VncConnection;
 
-            var scaling = new Property () {
-                label = _("Scaling"),
-                widget = new Gtk.Switch () {
-                    active = connection.scaling
-                }
+            var scaling = new BooleanProperty (connection, "scaling") {
+                label = _("Scaling")
             };
-            scaling.widget.bind_property ("active", connection, "scaling", BindingFlags.SYNC_CREATE);
             add_property (scaling);
 
-            var view_only = new Property () {
-                label = _("View only"),
-                widget = new Gtk.Switch () {
-                    active = vnc.view_only
-                }
+            var view_only = new BooleanProperty (connection, "view-only") {
+                label = _("View only")
             };
-            view_only.widget.bind_property ("active", vnc, "view_only", BindingFlags.SYNC_CREATE);
             add_property (view_only);
 
-            var local_pointer = new Property () {
-                label = _("Show local pointer"),
-                widget = new Gtk.Switch () {
-                    active = vnc.show_local_pointer
-                }
+            var local_pointer = new BooleanProperty (connection, "show-local-pointer") {
+                label = _("Show local pointer")
             };
-            local_pointer.widget.bind_property ("active", vnc, "show_local_pointer", 
BindingFlags.SYNC_CREATE);
             add_property (local_pointer);
 
             var combo_widget = new Gtk.ComboBoxText ();


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