From 25cf6dded724d5b2a5891ea9d1e5d0feabce8755 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Sun, 29 Sep 2024 19:56:41 +0200 Subject: [PATCH 1/4] Use protocol for monitor labels --- data/Display.css | 10 ++++++ src/Interfaces/GalaDBus.vala | 19 ---------- src/Widgets/DisplaysOverlay.vala | 46 +++++------------------- src/Widgets/MonitorLabel.vala | 60 ++++++++++++++++++++++++++++++++ src/meson.build | 3 +- 5 files changed, 81 insertions(+), 57 deletions(-) delete mode 100644 src/Interfaces/GalaDBus.vala create mode 100644 src/Widgets/MonitorLabel.vala diff --git a/data/Display.css b/data/Display.css index 2f0b057f..57fb3655 100644 --- a/data/Display.css +++ b/data/Display.css @@ -143,3 +143,13 @@ display-widget.disabled button.flat.image-button:focus { display-widget.disabled button.flat.image-button:checked { background: alpha(@SLATE_700, 0.5); } + +.monitor-label { + border-radius: 9px; + font-weight: 600; +} + +.monitor-label label { + margin: 1em; + text-shadow: 0 1px 1px alpha(white, 0.1); +} diff --git a/src/Interfaces/GalaDBus.vala b/src/Interfaces/GalaDBus.vala deleted file mode 100644 index cc7e85d0..00000000 --- a/src/Interfaces/GalaDBus.vala +++ /dev/null @@ -1,19 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.0-or-later - * SPDX-FileCopyrightText: 2024 elementary, Inc. (https://elementary.io) - */ - -[DBus (name = "org.pantheon.gala.daemon")] -public interface GalaDBus : Object { - public abstract void show_monitor_labels (MonitorLabelInfo[] label_infos) throws GLib.DBusError, GLib.IOError; - public abstract void hide_monitor_labels () throws GLib.DBusError, GLib.IOError; -} - -public struct MonitorLabelInfo { - public int monitor; - public string label; - public string background_color; - public string text_color; - public int x; - public int y; -} diff --git a/src/Widgets/DisplaysOverlay.vala b/src/Widgets/DisplaysOverlay.vala index e1d682ea..8f78b8b4 100644 --- a/src/Widgets/DisplaysOverlay.vala +++ b/src/Widgets/DisplaysOverlay.vala @@ -39,7 +39,6 @@ public class Display.DisplaysOverlay : Gtk.Box { private int default_y_margin = 0; private unowned Display.MonitorManager monitor_manager; - private static GalaDBus gala_dbus = null; public int active_displays { get; set; default = 0; } private List display_widgets; @@ -70,6 +69,7 @@ public class Display.DisplaysOverlay : Gtk.Box { }; private Gtk.GestureDrag drag_gesture; + private MonitorLabel[] monitor_labels = {}; construct { add_css_class (Granite.STYLE_CLASS_VIEW); @@ -102,20 +102,6 @@ public class Display.DisplaysOverlay : Gtk.Box { display_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION ); - - GLib.Bus.get_proxy.begin ( - GLib.BusType.SESSION, - "org.pantheon.gala.daemon", - "/org/pantheon/gala/daemon", - GLib.DBusProxyFlags.NONE, - null, - (obj, res) => { - try { - gala_dbus = GLib.Bus.get_proxy.end (res); - } catch (GLib.Error e) { - critical (e.message); - } - }); } private double prev_dx = 0; @@ -202,38 +188,24 @@ public class Display.DisplaysOverlay : Gtk.Box { scanning = false; } - public void show_windows () requires (gala_dbus != null) { + public void show_windows () { + hide_windows (); + monitor_labels = {}; + if (monitor_manager.is_mirrored) { return; } - MonitorLabelInfo[] label_infos = {}; - foreach (unowned var widget in display_widgets) { if (widget.virtual_monitor.is_active) { - label_infos += MonitorLabelInfo () { - monitor = label_infos.length, - label = widget.virtual_monitor.get_display_name (), - background_color = widget.bg_color, - text_color = widget.text_color, - x = widget.virtual_monitor.current_x, - y = widget.virtual_monitor.current_y - }; + monitor_labels += new MonitorLabel (monitor_labels.length, widget.virtual_monitor.get_display_name (), widget.bg_color, widget.text_color); } } - - try { - gala_dbus.show_monitor_labels (label_infos); - } catch (Error e) { - warning ("Couldn't show monitor labels: %s", e.message); - } } - public void hide_windows () requires (gala_dbus != null) { - try { - gala_dbus.hide_monitor_labels (); - } catch (Error e) { - warning ("Couldn't hide monitor labels: %s", e.message); + public void hide_windows () { + foreach (var monitor_label in monitor_labels) { + monitor_label.close (); } } diff --git a/src/Widgets/MonitorLabel.vala b/src/Widgets/MonitorLabel.vala new file mode 100644 index 00000000..5c1aeddb --- /dev/null +++ b/src/Widgets/MonitorLabel.vala @@ -0,0 +1,60 @@ +/* + * Copyright 2024 elementary, Inc. (https://elementary.io) + * SPDX-License-Identifier: LGPL-3.0-or-later + */ + +public class Display.MonitorLabel : Gtk.Window, PantheonWayland.ExtendedBehavior { + private const int SPACING = 12; + private const string COLORED_STYLE_CSS = """ + .label-%d { + background-color: alpha(%s, 0.8); + color: %s; + } + """; + + public int index { get; construct; } + public string label { get; construct; } + public string bg_color { get; construct; } + public string text_color { get; construct; } + + public MonitorLabel (int index, string label, string bg_color, string text_color) { + Object ( + index: index, + label: label, + bg_color: bg_color, + text_color: text_color + ); + } + + construct { + child = new Gtk.Label (label); + + decorated = false; + resizable = false; + deletable = false; + can_focus = false; + titlebar = new Gtk.Grid (); + + var provider = new Gtk.CssProvider (); + try { + provider.load_from_string (COLORED_STYLE_CSS.printf (index, bg_color, text_color)); + get_style_context ().add_class ("label-%d".printf (index)); + get_style_context ().add_class ("monitor-label"); + + Gtk.StyleContext.add_provider_for_display ( + Gdk.Display.get_default (), + provider, + Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION + ); + } catch (Error e) { + warning ("Failed to load CSS: %s", e.message); + } + + child.realize.connect (() => { + connect_to_shell (); + make_monitor_label (index); + }); + + present (); + } +} diff --git a/src/meson.build b/src/meson.build index 94aa9310..ea920b21 100644 --- a/src/meson.build +++ b/src/meson.build @@ -2,7 +2,6 @@ plug_files = files( 'Utils.vala', 'SettingsDaemon.vala', 'DisplayPlug.vala', - 'Interfaces/GalaDBus.vala', 'Interfaces/NightLightManager.vala', 'Interfaces/MutterDisplayConfig.vala', 'Interfaces/SensorManager.vala', @@ -15,6 +14,7 @@ plug_files = files( 'Views' / 'FiltersView.vala', 'Widgets/DisplayWidget.vala', 'Widgets/DisplaysOverlay.vala', + 'Widgets' / 'MonitorLabel.vala', ) switchboard_dep = dependency('switchboard-3') @@ -32,6 +32,7 @@ shared_module( dependency('granite-7'), dependency('gtk4'), dependency('libadwaita-1'), + dependency('pantheon-wayland-1'), meson.get_compiler('vala').find_library('posix'), switchboard_dep ], From 636b39df6c777e7273c70f2e356f5a7060d59fea Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Mon, 30 Sep 2024 14:20:00 +0200 Subject: [PATCH 2/4] Support X --- src/Widgets/MonitorLabel.vala | 21 +++++++++++++++++---- src/meson.build | 1 + 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Widgets/MonitorLabel.vala b/src/Widgets/MonitorLabel.vala index 5c1aeddb..4902f0f3 100644 --- a/src/Widgets/MonitorLabel.vala +++ b/src/Widgets/MonitorLabel.vala @@ -50,10 +50,23 @@ public class Display.MonitorLabel : Gtk.Window, PantheonWayland.ExtendedBehavior warning ("Failed to load CSS: %s", e.message); } - child.realize.connect (() => { - connect_to_shell (); - make_monitor_label (index); - }); + var display = Gdk.Display.get_default (); + if (display is Gdk.X11.Display) { + unowned var xdisplay = ((Gdk.X11.Display) display).get_xdisplay (); + + var window = ((Gdk.X11.Surface) get_surface ()).get_xid (); + + var prop = xdisplay.intern_atom ("_MUTTER_HINTS", false); + + var value = "monitor-label=%d".printf (index); + + xdisplay.change_property (window, prop, X.XA_STRING, 8, 0, (uchar[]) value, value.length); + } else { + child.realize.connect (() => { + connect_to_shell (); + make_monitor_label (index); + }); + } present (); } diff --git a/src/meson.build b/src/meson.build index ea920b21..d696ed19 100644 --- a/src/meson.build +++ b/src/meson.build @@ -31,6 +31,7 @@ shared_module( dependency('gobject-2.0'), dependency('granite-7'), dependency('gtk4'), + dependency('gtk4-x11'), dependency('libadwaita-1'), dependency('pantheon-wayland-1'), meson.get_compiler('vala').find_library('posix'), From 52f71ef01ab32119586eedd409e3a5f317fa0919 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Mon, 30 Sep 2024 14:22:31 +0200 Subject: [PATCH 3/4] Fix X support --- src/Widgets/MonitorLabel.vala | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Widgets/MonitorLabel.vala b/src/Widgets/MonitorLabel.vala index 4902f0f3..b0874735 100644 --- a/src/Widgets/MonitorLabel.vala +++ b/src/Widgets/MonitorLabel.vala @@ -50,23 +50,23 @@ public class Display.MonitorLabel : Gtk.Window, PantheonWayland.ExtendedBehavior warning ("Failed to load CSS: %s", e.message); } - var display = Gdk.Display.get_default (); - if (display is Gdk.X11.Display) { - unowned var xdisplay = ((Gdk.X11.Display) display).get_xdisplay (); + child.realize.connect (() => { + var display = Gdk.Display.get_default (); + if (display is Gdk.X11.Display) { + unowned var xdisplay = ((Gdk.X11.Display) display).get_xdisplay (); - var window = ((Gdk.X11.Surface) get_surface ()).get_xid (); + var window = ((Gdk.X11.Surface) get_surface ()).get_xid (); - var prop = xdisplay.intern_atom ("_MUTTER_HINTS", false); + var prop = xdisplay.intern_atom ("_MUTTER_HINTS", false); - var value = "monitor-label=%d".printf (index); + var value = "monitor-label=%d".printf (index); - xdisplay.change_property (window, prop, X.XA_STRING, 8, 0, (uchar[]) value, value.length); - } else { - child.realize.connect (() => { + xdisplay.change_property (window, prop, X.XA_STRING, 8, 0, (uchar[]) value, value.length); + } else { connect_to_shell (); make_monitor_label (index); - }); - } + } + }); present (); } From a9e6b0a0cb20c4fbee1e880703d3c126f94801fb Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Sat, 12 Oct 2024 18:14:15 +0200 Subject: [PATCH 4/4] Fix double shown on start --- src/DisplayPlug.vala | 20 ++------------------ src/Widgets/DisplaysOverlay.vala | 3 +++ 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/DisplayPlug.vala b/src/DisplayPlug.vala index 9d397d33..dbb523a6 100644 --- a/src/DisplayPlug.vala +++ b/src/DisplayPlug.vala @@ -81,30 +81,14 @@ public class Display.Plug : Switchboard.Plug { box = new Gtk.Box (VERTICAL, 0); box.append (headerbar); box.append (stack); - - stack.notify["visible-child"].connect (() => { - if (stack.visible_child == displays_view) { - displays_view.displays_overlay.show_windows (); - } else { - displays_view.displays_overlay.hide_windows (); - } - }); } return box; } - public override void shown () { - if (stack.visible_child == displays_view) { - displays_view.displays_overlay.show_windows (); - } else { - displays_view.displays_overlay.hide_windows (); - } - } + public override void shown () { } //Do nothing - public override void hidden () { - displays_view.displays_overlay.hide_windows (); - } + public override void hidden () { } //Do nothing public override void search_callback (string location) { stack.visible_child_name = location; diff --git a/src/Widgets/DisplaysOverlay.vala b/src/Widgets/DisplaysOverlay.vala index 8f78b8b4..6d9f1aa8 100644 --- a/src/Widgets/DisplaysOverlay.vala +++ b/src/Widgets/DisplaysOverlay.vala @@ -91,6 +91,9 @@ public class Display.DisplaysOverlay : Gtk.Box { rescan_displays (); overlay.get_child_position.connect (get_child_position); + + map.connect (show_windows); + unmap.connect (hide_windows); } static construct {