From 4518ea438802895cee9b7b7f1348b04f24acb3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Wed, 23 Apr 2025 20:51:27 -0700 Subject: [PATCH 1/2] SettingsToggle: use action-name --- src/PopoverWidget.vala | 36 +++++++++++++++- src/Widgets/DarkModeToggle.vala | 29 +++++++++---- src/Widgets/PreventSleepToggle.vala | 64 +++++++++++++++++------------ src/Widgets/RotationToggle.vala | 32 +++++++++++---- src/Widgets/SettingsToggle.vala | 5 +-- 5 files changed, 120 insertions(+), 46 deletions(-) diff --git a/src/PopoverWidget.vala b/src/PopoverWidget.vala index 8859281..59c39f2 100644 --- a/src/PopoverWidget.vala +++ b/src/PopoverWidget.vala @@ -117,8 +117,40 @@ public class QuickSettings.PopoverWidget : Gtk.Box { }); var applications_settings = new Settings ("org.gnome.desktop.a11y.applications"); - applications_settings.bind ("screen-keyboard-enabled", onscreen_keyboard, "active", DEFAULT); - applications_settings.bind ("screen-reader-enabled", screen_reader, "active", DEFAULT); + + var screen_reader_action = new SimpleAction.stateful ( + "screen-reader", + null, + applications_settings.get_boolean ("screen-reader-enabled") + ); + + screen_reader_action.activate.connect (() => { + applications_settings.set_boolean ("screen-reader-enabled", !applications_settings.get_boolean ("screen-reader-enabled")); + }); + + applications_settings.changed["screen-reader-enabled"].connect (() => { + screen_reader_action.set_state (applications_settings.get_boolean ("screen-reader-enabled")); + }); + + var screen_keyboard_action = new SimpleAction.stateful ( + "screen-keyboard", + null, + applications_settings.get_boolean ("screen-keyboard-enabled") + ); + + screen_keyboard_action.activate.connect (() => { + applications_settings.set_boolean ("screen-keyboard-enabled", !applications_settings.get_boolean ("screen-keyboard-enabled")); + }); + + applications_settings.changed["screen-keyboard-enabled"].connect (() => { + screen_keyboard_action.set_state (applications_settings.get_boolean ("screen-keyboard-enabled")); + }); + + var action_group = new SimpleActionGroup (); + action_group.add_action (screen_reader_action); + action_group.add_action (screen_keyboard_action); + + insert_action_group ("quick-settings", action_group); var glib_settings = new Settings ("io.elementary.desktop.quick-settings"); diff --git a/src/Widgets/DarkModeToggle.vala b/src/Widgets/DarkModeToggle.vala index 1e26dc5..99839a6 100644 --- a/src/Widgets/DarkModeToggle.vala +++ b/src/Widgets/DarkModeToggle.vala @@ -12,21 +12,34 @@ public class QuickSettings.DarkModeToggle: SettingsToggle { } construct { + action_name = "quick-settings.dark-mode"; settings_uri = "settings://desktop/appearance"; var settings = new GLib.Settings ("io.elementary.settings-daemon.prefers-color-scheme"); - active = settings.get_enum ("color-scheme") == Granite.Settings.ColorScheme.DARK; - settings.changed["color-scheme"].connect (() => { - active = settings.get_enum ("color-scheme") == Granite.Settings.ColorScheme.DARK; - }); + var dark_mode_action = new SimpleAction.stateful ( + "dark-mode", + null, + new Variant.boolean (settings.get_enum ("color-scheme") == Granite.Settings.ColorScheme.DARK) + ); - notify["active"].connect (() => { - if (active) { - settings.set_enum ("color-scheme", Granite.Settings.ColorScheme.DARK); - } else { + dark_mode_action.activate.connect (() => { + if (settings.get_enum ("color-scheme") == Granite.Settings.ColorScheme.DARK) { settings.set_enum ("color-scheme", Granite.Settings.ColorScheme.NO_PREFERENCE); + } else { + settings.set_enum ("color-scheme", Granite.Settings.ColorScheme.DARK); } }); + + settings.changed["color-scheme"].connect (() => { + dark_mode_action.set_state ( + new Variant.boolean (settings.get_enum ("color-scheme") == Granite.Settings.ColorScheme.DARK) + ); + }); + + map.connect (() => { + var action_group = (SimpleActionGroup) get_action_group ("quick-settings"); + action_group.add_action (dark_mode_action); + }); } } diff --git a/src/Widgets/PreventSleepToggle.vala b/src/Widgets/PreventSleepToggle.vala index 728bed3..5b1415f 100644 --- a/src/Widgets/PreventSleepToggle.vala +++ b/src/Widgets/PreventSleepToggle.vala @@ -7,6 +7,8 @@ public class QuickSettings.PreventSleepToggle: SettingsToggle { private uint suspend_cookie = 0; private uint idle_cookie = 0; + private SimpleAction inhibit_action; + public PreventSleepToggle () { Object ( icon: new ThemedIcon ("system-suspend-symbolic"), @@ -15,34 +17,44 @@ public class QuickSettings.PreventSleepToggle: SettingsToggle { } construct { + action_name = "quick-settings.inhibit"; settings_uri = "settings://power"; - notify["active"].connect ((obj, pspec) => { - var _prevent_sleep_toggle = (SettingsToggle) obj; - unowned var application = (Gtk.Application) GLib.Application.get_default (); - - if (_prevent_sleep_toggle.active && suspend_cookie == 0 && idle_cookie == 0) { - suspend_cookie = application.inhibit ( - (Gtk.Window) get_toplevel (), - Gtk.ApplicationInhibitFlags.SUSPEND, - "Prevent session from suspending" - ); - idle_cookie = application.inhibit ( - (Gtk.Window) get_toplevel (), - Gtk.ApplicationInhibitFlags.IDLE, - "Prevent session from idle" - ); - - icon = new ThemedIcon ("system-suspend-disabled-symbolic"); - } else if (!_prevent_sleep_toggle.active && suspend_cookie > 0 && idle_cookie > 0) { - application.uninhibit (suspend_cookie); - application.uninhibit (idle_cookie); - - icon = new ThemedIcon ("system-suspend-symbolic"); - - suspend_cookie = 0; - idle_cookie = 0; - } + inhibit_action = new SimpleAction.stateful ("inhibit", null, new Variant.boolean (suspend_cookie > 0 && idle_cookie > 0)); + inhibit_action.activate.connect (toggle_inibit); + + map.connect (() => { + var action_group = (SimpleActionGroup) get_action_group ("quick-settings"); + action_group.add_action (inhibit_action); }); } + + private void toggle_inibit () { + unowned var application = (Gtk.Application) GLib.Application.get_default (); + + if (suspend_cookie == 0 && idle_cookie == 0) { + suspend_cookie = application.inhibit ( + (Gtk.Window) get_toplevel (), + Gtk.ApplicationInhibitFlags.SUSPEND, + "Prevent session from suspending" + ); + idle_cookie = application.inhibit ( + (Gtk.Window) get_toplevel (), + Gtk.ApplicationInhibitFlags.IDLE, + "Prevent session from idle" + ); + + inhibit_action.set_state (new Variant.boolean (true)); + icon = new ThemedIcon ("system-suspend-disabled-symbolic"); + } else if (suspend_cookie > 0 && idle_cookie > 0) { + application.uninhibit (suspend_cookie); + application.uninhibit (idle_cookie); + + inhibit_action.set_state (new Variant.boolean (false)); + icon = new ThemedIcon ("system-suspend-symbolic"); + + suspend_cookie = 0; + idle_cookie = 0; + } + } } diff --git a/src/Widgets/RotationToggle.vala b/src/Widgets/RotationToggle.vala index 59782c9..c5d8de8 100644 --- a/src/Widgets/RotationToggle.vala +++ b/src/Widgets/RotationToggle.vala @@ -12,18 +12,36 @@ public class QuickSettings.RotationToggle: SettingsToggle { } construct { + action_name = "quick-settings.rotation-lock"; settings_uri = "settings://display"; - var touchscreen_settings = new Settings ("org.gnome.settings-daemon.peripherals.touchscreen"); - touchscreen_settings.bind ("orientation-lock", this, "active", DEFAULT); + var settings = new Settings ("org.gnome.settings-daemon.peripherals.touchscreen"); - bind_property ("active", this, "icon", SYNC_CREATE, (binding, srcval, ref targetval) => { - if ((bool) srcval) { - targetval = new ThemedIcon ("quick-settings-rotation-locked-symbolic"); + var rotation_lock_action = new SimpleAction.stateful ( + "rotation-lock", + null, + settings.get_boolean ("orientation-lock") + ); + + rotation_lock_action.activate.connect (() => { + settings.set_boolean ("orientation-lock", !settings.get_boolean ("orientation-lock")); + }); + + settings.changed["orientation-lock"].connect (() => { + rotation_lock_action.set_state (settings.get_boolean ("orientation-lock")); + }); + + rotation_lock_action.change_state.connect ((value) => { + if (value.get_boolean ()) { + icon = new ThemedIcon ("quick-settings-rotation-locked-symbolic"); } else { - targetval = new ThemedIcon ("quick-settings-rotation-allowed-symbolic"); + icon = new ThemedIcon ("quick-settings-rotation-allowed-symbolic"); } - return true; + }); + + map.connect (() => { + var action_group = (SimpleActionGroup) get_action_group ("quick-settings"); + action_group.add_action (rotation_lock_action); }); } } diff --git a/src/Widgets/SettingsToggle.vala b/src/Widgets/SettingsToggle.vala index 2fe4b94..e79f4d3 100644 --- a/src/Widgets/SettingsToggle.vala +++ b/src/Widgets/SettingsToggle.vala @@ -4,7 +4,7 @@ */ public class QuickSettings.SettingsToggle : Gtk.FlowBoxChild { - public bool active { get; set; } + public string action_name { get; set; } public Icon icon { get; construct set; } public string label { get; construct; } public string settings_uri { get; set; default = "settings://"; } @@ -42,8 +42,7 @@ public class QuickSettings.SettingsToggle : Gtk.FlowBoxChild { can_focus = false; child = box; - button.bind_property ("active", this, "active", SYNC_CREATE | BIDIRECTIONAL); - + bind_property ("action-name", button, "action-name"); bind_property ("icon", image, "gicon"); middle_click_gesture = new Gtk.GestureMultiPress (button) { From e2e853d680141958e44554644698567429def0c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Thu, 24 Apr 2025 08:21:11 -0700 Subject: [PATCH 2/2] Use settings.create_action --- src/PopoverWidget.vala | 34 ++++----------------------------- src/Widgets/RotationToggle.vala | 21 ++++---------------- 2 files changed, 8 insertions(+), 47 deletions(-) diff --git a/src/PopoverWidget.vala b/src/PopoverWidget.vala index 46e7520..817afed 100644 --- a/src/PopoverWidget.vala +++ b/src/PopoverWidget.vala @@ -27,6 +27,7 @@ public class QuickSettings.PopoverWidget : Gtk.Box { var screen_reader = new SettingsToggle ( _("Screen Reader") ) { + action_name = "quick-settings.screen-reader-enabled", icon_name = "orca-symbolic", settings_uri = "settings://sound" }; @@ -34,6 +35,7 @@ public class QuickSettings.PopoverWidget : Gtk.Box { var onscreen_keyboard = new SettingsToggle ( _("Onscreen Keyboard") ) { + action_name = "quick-settings.screen-keyboard-enabled", icon_name = "input-keyboard-symbolic", settings_uri = "settings://input/keyboard/behavior" }; @@ -118,37 +120,9 @@ public class QuickSettings.PopoverWidget : Gtk.Box { var applications_settings = new Settings ("org.gnome.desktop.a11y.applications"); - var screen_reader_action = new SimpleAction.stateful ( - "screen-reader", - null, - applications_settings.get_boolean ("screen-reader-enabled") - ); - - screen_reader_action.activate.connect (() => { - applications_settings.set_boolean ("screen-reader-enabled", !applications_settings.get_boolean ("screen-reader-enabled")); - }); - - applications_settings.changed["screen-reader-enabled"].connect (() => { - screen_reader_action.set_state (applications_settings.get_boolean ("screen-reader-enabled")); - }); - - var screen_keyboard_action = new SimpleAction.stateful ( - "screen-keyboard", - null, - applications_settings.get_boolean ("screen-keyboard-enabled") - ); - - screen_keyboard_action.activate.connect (() => { - applications_settings.set_boolean ("screen-keyboard-enabled", !applications_settings.get_boolean ("screen-keyboard-enabled")); - }); - - applications_settings.changed["screen-keyboard-enabled"].connect (() => { - screen_keyboard_action.set_state (applications_settings.get_boolean ("screen-keyboard-enabled")); - }); - var action_group = new SimpleActionGroup (); - action_group.add_action (screen_reader_action); - action_group.add_action (screen_keyboard_action); + action_group.add_action (applications_settings.create_action ("screen-reader-enabled")); + action_group.add_action (applications_settings.create_action ("screen-keyboard-enabled")); insert_action_group ("quick-settings", action_group); diff --git a/src/Widgets/RotationToggle.vala b/src/Widgets/RotationToggle.vala index cb1280f..e86d3b9 100644 --- a/src/Widgets/RotationToggle.vala +++ b/src/Widgets/RotationToggle.vala @@ -11,28 +11,15 @@ public class QuickSettings.RotationToggle: SettingsToggle { } construct { - action_name = "quick-settings.rotation-lock"; + action_name = "quick-settings.orientation-lock"; icon_name = "quick-settings-rotation-locked-symbolic"; settings_uri = "settings://display"; var settings = new Settings ("org.gnome.settings-daemon.peripherals.touchscreen"); + var rotation_lock_action = settings.create_action ("orientation-lock"); - var rotation_lock_action = new SimpleAction.stateful ( - "rotation-lock", - null, - settings.get_boolean ("orientation-lock") - ); - - rotation_lock_action.activate.connect (() => { - settings.set_boolean ("orientation-lock", !settings.get_boolean ("orientation-lock")); - }); - - settings.changed["orientation-lock"].connect (() => { - rotation_lock_action.set_state (settings.get_boolean ("orientation-lock")); - }); - - rotation_lock_action.change_state.connect ((value) => { - if (value.get_boolean ()) { + rotation_lock_action.notify["state"].connect (() => { + if (rotation_lock_action.state.get_boolean ()) { icon_name = "quick-settings-rotation-locked-symbolic"; } else { icon_name = "quick-settings-rotation-allowed-symbolic";