[gnome-maps/wip/mlundblad/night-mode: 1/3] utils: Add ability to create actions mapped to a GSettings key



commit 3e5c2531b4de68d6247d315b9fac5d1d9f6be0ec
Author: Marcus Lundblad <ml update uu se>
Date:   Tue Jun 2 22:34:14 2020 +0200

    utils: Add ability to create actions mapped to a GSettings key

 src/utils.js | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)
---
diff --git a/src/utils.js b/src/utils.js
index 613e87e1..54ce556b 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -33,6 +33,8 @@ const GWeather = imports.gi.GWeather;
 const Soup = imports.gi.Soup;
 const ByteArray = imports.byteArray;
 
+const Application = imports.application;
+
 var METRIC_SYSTEM = 1;
 var IMPERIAL_SYSTEM = 2;
 
@@ -110,23 +112,34 @@ function setAccelsForActionMap(actionMap, actionName, accels) {
     app.set_accels_for_action(prefix + '.' + actionName, accels);
 }
 
-function createAction(name, { state, paramType, onActivate, onChangeState }) {
-    let entry = { name: name };
+function createAction(name, { state, paramType, onActivate, onChangeState,
+                              setting }) {
 
-    if(Array.isArray(state)) {
-        let [type, value] = state;
-        entry.state = new GLib.Variant.new(type, value);
-    }
 
-    if(paramType !== undefined)
-        entry.parameter_type = GLib.VariantType.new(paramType);
+    let action;
+
+    if (setting) {
+        action = Application.settings.create_action(setting);
+
+        if (onChangeState)
+            action.connect('notify::state', onChangeState);
+    } else {
+        let entry = { name: name };
+
+        if (Array.isArray(state)) {
+            let [type, value] = state;
+            entry.state = new GLib.Variant.new(type, value);
+        }
 
-    let action = new Gio.SimpleAction(entry);
+        if (paramType !== undefined)
+            entry.parameter_type = GLib.VariantType.new(paramType);
+            action = new Gio.SimpleAction(entry);
 
-    if(onActivate)
-        action.connect('activate', onActivate);
-    if(onChangeState)
-        action.connect('change-state', onChangeState);
+        if (onActivate)
+            action.connect('activate', onActivate);
+        if (onChangeState)
+            action.connect('change-state', onChangeState);
+    }
 
     return action;
 }


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