[gnome-shell/wip/fmuellner/dont-disturb: 7/11] popupMenu: Turn Switch state into a GObject property



commit ddd70146a644c55996c1cce7714b5489d0240578
Author: Florian Müllner <fmuellner gnome org>
Date:   Tue Jan 21 14:52:15 2020 +0100

    popupMenu: Turn Switch state into a GObject property
    
    A property is often more convenient than a method, as it can be used
    with bind_property() and friends.
    
    https://gitlab.gnome.org/GNOME/gnome-shell/issues/239

 js/ui/popupMenu.js | 39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)
---
diff --git a/js/ui/popupMenu.js b/js/ui/popupMenu.js
index 56638050f9..3119124f97 100644
--- a/js/ui/popupMenu.js
+++ b/js/ui/popupMenu.js
@@ -305,25 +305,44 @@ class PopupSeparatorMenuItem extends PopupBaseMenuItem {
     }
 });
 
-var Switch = GObject.registerClass(
-class Switch extends St.Bin {
+var Switch = GObject.registerClass({
+    Properties: {
+        'state': GObject.ParamSpec.boolean(
+            'state', 'state', 'state',
+            GObject.ParamFlags.READWRITE,
+            false),
+    },
+}, class Switch extends St.Bin {
     _init(state) {
-        super._init({ style_class: 'toggle-switch',
-                      accessible_role: Atk.Role.CHECK_BOX,
-                      can_focus: true });
-        this.setToggleState(state);
+        this._state = false;
+
+        super._init({
+            style_class: 'toggle-switch',
+            accessible_role: Atk.Role.CHECK_BOX,
+            can_focus: true,
+            state,
+        });
     }
 
-    setToggleState(state) {
+    get state() {
+        return this._state;
+    }
+
+    set state(state) {
+        if (this._state === state)
+            return;
+
         if (state)
             this.add_style_pseudo_class('checked');
         else
             this.remove_style_pseudo_class('checked');
-        this.state = state;
+
+        this._state = state;
+        this.notify('state');
     }
 
     toggle() {
-        this.setToggleState(!this.state);
+        this.state = !this.state;
     }
 });
 
@@ -393,7 +412,7 @@ var PopupSwitchMenuItem = GObject.registerClass({
     }
 
     setToggleState(state) {
-        this._switch.setToggleState(state);
+        this._switch.state = state;
         this.checkAccessibleState();
     }
 


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