diff --git a/src/DisplayPlug.vala b/src/DisplayPlug.vala index a73c4b24..9d397d33 100644 --- a/src/DisplayPlug.vala +++ b/src/DisplayPlug.vala @@ -114,11 +114,10 @@ public class Display.Plug : Switchboard.Plug { public override async Gee.TreeMap search (string search) { var search_results = new Gee.TreeMap ((GLib.CompareDataFunc)strcmp, (Gee.EqualDataFunc)str_equal); search_results.set ("%s → %s".printf (display_name, _("Screen Resolution")), "displays"); - search_results.set ("%s → %s".printf (display_name, _("Screen Rotation")), "displays"); search_results.set ("%s → %s".printf (display_name, _("Primary display")), "displays"); search_results.set ("%s → %s".printf (display_name, _("Screen mirroring")), "displays"); + search_results.set ("%s → %s".printf (display_name, _("Screen Rotation")), "displays"); search_results.set ("%s → %s".printf (display_name, _("Scaling factor")), "displays"); - search_results.set ("%s → %s".printf (display_name, _("Rotation lock")), "displays"); search_results.set ("%s → %s".printf (display_name, _("Night Light")), "night-light"); search_results.set ("%s → %s → %s".printf (display_name, _("Night Light"), _("Schedule")), "night-light"); search_results.set ("%s → %s → %s".printf (display_name, _("Night Light"), _("Color temperature")), "night-light"); @@ -126,6 +125,11 @@ public class Display.Plug : Switchboard.Plug { search_results.set ("%s → %s → %s".printf (display_name, _("Filters"), _("Color Vision Deficiency")), "filters"); search_results.set ("%s → %s → %s".printf (display_name, _("Filters"), _("Grayscale")), "filters"); search_results.set ("%s → %s → %s".printf (display_name, _("Filters"), _("Monochrome")), "filters"); + + if (SensorManager.get_default ().has_accelerometer) { + search_results.set ("%s → %s".printf (display_name, _("Rotation lock")), "displays"); + } + return search_results; } } diff --git a/src/Interfaces/SensorManager.vala b/src/Interfaces/SensorManager.vala new file mode 100644 index 00000000..a91998ec --- /dev/null +++ b/src/Interfaces/SensorManager.vala @@ -0,0 +1,30 @@ +/* + * SPDX-License-Identifier: LGPL-2.0-or-later + * SPDX-FileCopyrightText: 2024 elementary, Inc. (https://elementary.io) + */ + +[DBus (name = "net.hadess.SensorProxy")] +public interface Display.SensorProxy : GLib.DBusProxy { + public abstract bool has_accelerometer { get; } +} + +public class Display.SensorManager : Object { + public bool has_accelerometer { get; private set; } + + private static GLib.Once instance; + public static unowned SensorManager get_default () { + return instance.once (() => new SensorManager ()); + } + + private class SensorManager () { } + + construct { + try { + // Synchronous otherwise search might be false negative + SensorProxy sensor_proxy = Bus.get_proxy_sync (BusType.SYSTEM, "net.hadess.SensorProxy", "/net/hadess/SensorProxy"); + has_accelerometer = sensor_proxy.has_accelerometer; + } catch (Error e) { + info ("Unable to connect to SensorProxy bus, probably means no accelerometer supported: %s", e.message); + } + } +} diff --git a/src/Views/DisplaysView.vala b/src/Views/DisplaysView.vala index 722cebf1..45796772 100644 --- a/src/Views/DisplaysView.vala +++ b/src/Views/DisplaysView.vala @@ -75,22 +75,33 @@ public class Display.DisplaysView : Gtk.Box { action_bar.pack_start (mirror_box); - var schema_source = GLib.SettingsSchemaSource.get_default (); - var rotation_lock_schema = schema_source.lookup (TOUCHSCREEN_SETTINGS_PATH, true); - if (rotation_lock_schema != null) { - rotation_lock_box = new Gtk.Box (HORIZONTAL, 6) { - margin_top = 6, - margin_end = 6, - margin_bottom = 6, - margin_start = 6, - valign = CENTER - }; - - action_bar.pack_start (rotation_lock_box); - - detect_accelerometer.begin (); - } else { - info ("Schema \"org.gnome.settings-daemon.peripherals.touchscreen\" is not installed on your system."); + if (SensorManager.get_default ().has_accelerometer) { + var schema_source = GLib.SettingsSchemaSource.get_default (); + var rotation_lock_schema = schema_source.lookup (TOUCHSCREEN_SETTINGS_PATH, true); + if (rotation_lock_schema != null) { + var rotation_lock_switch = new Gtk.Switch (); + + var rotation_lock_label = new Gtk.Label (_("Rotation Lock:")) { + mnemonic_widget = rotation_lock_switch + }; + + rotation_lock_box = new Gtk.Box (HORIZONTAL, 6) { + margin_top = 6, + margin_end = 6, + margin_bottom = 6, + margin_start = 6, + valign = CENTER + }; + rotation_lock_box.append (rotation_lock_label); + rotation_lock_box.append (rotation_lock_switch); + + action_bar.pack_start (rotation_lock_box); + + var touchscreen_settings = new GLib.Settings (TOUCHSCREEN_SETTINGS_PATH); + touchscreen_settings.bind ("orientation-lock", rotation_lock_switch, "active", DEFAULT); + } else { + info ("Schema \"org.gnome.settings-daemon.peripherals.touchscreen\" is not installed on your system."); + } } action_bar.pack_end (button_box); @@ -151,32 +162,4 @@ public class Display.DisplaysView : Gtk.Box { error_dialog.response.connect (error_dialog.destroy); error_dialog.present (); } - - private async void detect_accelerometer () { - bool has_accelerometer = false; - - try { - SensorProxy sensors = yield GLib.Bus.get_proxy (BusType.SYSTEM, "net.hadess.SensorProxy", "/net/hadess/SensorProxy"); - has_accelerometer = sensors.has_accelerometer; - } catch (Error e) { - info ("Unable to connect to SensorProxy bus, probably means no accelerometer supported: %s", e.message); - } - - if (has_accelerometer) { - var touchscreen_settings = new GLib.Settings (TOUCHSCREEN_SETTINGS_PATH); - - var rotation_lock_label = new Gtk.Label (_("Rotation Lock:")); - var rotation_lock_switch = new Gtk.Switch (); - - rotation_lock_box.append (rotation_lock_label); - rotation_lock_box.append (rotation_lock_switch); - - touchscreen_settings.bind ("orientation-lock", rotation_lock_switch, "state", DEFAULT); - } - } - - [DBus (name = "net.hadess.SensorProxy")] - private interface SensorProxy : GLib.DBusProxy { - public abstract bool has_accelerometer { get; } - } } diff --git a/src/meson.build b/src/meson.build index 60897e1a..94aa9310 100644 --- a/src/meson.build +++ b/src/meson.build @@ -5,6 +5,7 @@ plug_files = files( 'Interfaces/GalaDBus.vala', 'Interfaces/NightLightManager.vala', 'Interfaces/MutterDisplayConfig.vala', + 'Interfaces/SensorManager.vala', 'Objects/VirtualMonitor.vala', 'Objects/MonitorMode.vala', 'Objects/MonitorManager.vala',