Skip to content

Commit 373029e

Browse files
committed
Use protocol for monitor labels
1 parent 3ff88a5 commit 373029e

File tree

6 files changed

+105
-75
lines changed

6 files changed

+105
-75
lines changed

data/Display.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,13 @@ scale.warmth:dir(rtl) trough {
169169
@ORANGE_500
170170
);
171171
}
172+
173+
.monitor-label {
174+
border-radius: 9px;
175+
font-weight: 600;
176+
}
177+
178+
.monitor-label label {
179+
margin: 1em;
180+
text-shadow: 0 1px 1px alpha(white, 0.1);
181+
}

src/DisplayPlug.vala

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,14 @@ public class Display.Plug : Switchboard.Plug {
8181
box = new Gtk.Box (VERTICAL, 0);
8282
box.append (headerbar);
8383
box.append (stack);
84-
85-
stack.notify["visible-child"].connect (() => {
86-
if (stack.visible_child == displays_view) {
87-
displays_view.displays_overlay.show_windows ();
88-
} else {
89-
displays_view.displays_overlay.hide_windows ();
90-
}
91-
});
9284
}
9385

9486
return box;
9587
}
9688

97-
public override void shown () {
98-
if (stack.visible_child == displays_view) {
99-
displays_view.displays_overlay.show_windows ();
100-
} else {
101-
displays_view.displays_overlay.hide_windows ();
102-
}
103-
}
89+
public override void shown () { } //Do nothing
10490

105-
public override void hidden () {
106-
displays_view.displays_overlay.hide_windows ();
107-
}
91+
public override void hidden () { } //Do nothing
10892

10993
public override void search_callback (string location) {
11094
stack.visible_child_name = location;

src/Interfaces/GalaDBus.vala

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Widgets/DisplaysOverlay.vala

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class Display.DisplaysOverlay : Gtk.Box {
3939
private int default_y_margin = 0;
4040

4141
private unowned Display.MonitorManager monitor_manager;
42-
private static GalaDBus gala_dbus = null;
4342
public int active_displays { get; set; default = 0; }
4443

4544
private List<DisplayWidget> display_widgets;
@@ -70,6 +69,7 @@ public class Display.DisplaysOverlay : Gtk.Box {
7069
};
7170

7271
private Gtk.GestureDrag drag_gesture;
72+
private MonitorLabel[] monitor_labels = {};
7373

7474
construct {
7575
add_css_class (Granite.STYLE_CLASS_VIEW);
@@ -91,6 +91,9 @@ public class Display.DisplaysOverlay : Gtk.Box {
9191
rescan_displays ();
9292

9393
overlay.get_child_position.connect (get_child_position);
94+
95+
map.connect (show_windows);
96+
unmap.connect (hide_windows);
9497
}
9598

9699
static construct {
@@ -102,20 +105,6 @@ public class Display.DisplaysOverlay : Gtk.Box {
102105
display_provider,
103106
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
104107
);
105-
106-
GLib.Bus.get_proxy.begin<GalaDBus> (
107-
GLib.BusType.SESSION,
108-
"org.pantheon.gala.daemon",
109-
"/org/pantheon/gala/daemon",
110-
GLib.DBusProxyFlags.NONE,
111-
null,
112-
(obj, res) => {
113-
try {
114-
gala_dbus = GLib.Bus.get_proxy.end (res);
115-
} catch (GLib.Error e) {
116-
critical (e.message);
117-
}
118-
});
119108
}
120109

121110
private double prev_dx = 0;
@@ -204,39 +193,30 @@ public class Display.DisplaysOverlay : Gtk.Box {
204193
scanning = false;
205194
}
206195

207-
public void show_windows () requires (gala_dbus != null) {
196+
public void show_windows () {
197+
hide_windows ();
198+
208199
if (monitor_manager.is_mirrored) {
209200
return;
210201
}
211202

212-
MonitorLabelInfo[] label_infos = {};
213-
214203
foreach (unowned var widget in display_widgets) {
215204
if (widget.virtual_monitor.is_active) {
216-
label_infos += MonitorLabelInfo () {
217-
monitor = label_infos.length,
218-
label = widget.virtual_monitor.get_display_name (),
219-
background_color = widget.bg_color,
220-
text_color = widget.text_color,
221-
x = widget.virtual_monitor.current_x,
222-
y = widget.virtual_monitor.current_y
223-
};
205+
var monitor_label = new MonitorLabel (
206+
monitor_labels.length, widget.virtual_monitor.get_display_name (),
207+
widget.bg_color, widget.text_color
208+
);
209+
monitor_label.present ();
210+
monitor_labels += monitor_label;
224211
}
225212
}
226-
227-
try {
228-
gala_dbus.show_monitor_labels (label_infos);
229-
} catch (Error e) {
230-
warning ("Couldn't show monitor labels: %s", e.message);
231-
}
232213
}
233214

234-
public void hide_windows () requires (gala_dbus != null) {
235-
try {
236-
gala_dbus.hide_monitor_labels ();
237-
} catch (Error e) {
238-
warning ("Couldn't hide monitor labels: %s", e.message);
215+
public void hide_windows () {
216+
foreach (var monitor_label in monitor_labels) {
217+
monitor_label.destroy ();
239218
}
219+
monitor_labels = {};
240220
}
241221

242222
private void change_active_displays_sensitivity () {

src/Widgets/MonitorLabel.vala

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2024 elementary, Inc. (https://elementary.io)
3+
* SPDX-License-Identifier: LGPL-3.0-or-later
4+
*/
5+
6+
public class Display.MonitorLabel : Gtk.Window, PantheonWayland.ExtendedBehavior {
7+
private const int SPACING = 12;
8+
private const string COLORED_STYLE_CSS = """
9+
.label-%d {
10+
background-color: alpha(%s, 0.8);
11+
color: %s;
12+
}
13+
""";
14+
15+
public int index { get; construct; }
16+
public string label { get; construct; }
17+
public string bg_color { get; construct; }
18+
public string text_color { get; construct; }
19+
20+
public MonitorLabel (int index, string label, string bg_color, string text_color) {
21+
Object (
22+
index: index,
23+
label: label,
24+
bg_color: bg_color,
25+
text_color: text_color
26+
);
27+
}
28+
29+
construct {
30+
child = new Gtk.Label (label);
31+
32+
decorated = false;
33+
resizable = false;
34+
deletable = false;
35+
can_focus = false;
36+
titlebar = new Gtk.Grid () { visible = false };
37+
38+
var provider = new Gtk.CssProvider ();
39+
try {
40+
provider.load_from_string (COLORED_STYLE_CSS.printf (index, bg_color, text_color));
41+
add_css_class ("label-%d".printf (index));
42+
add_css_class ("monitor-label");
43+
44+
Gtk.StyleContext.add_provider_for_display (
45+
Gdk.Display.get_default (),
46+
provider,
47+
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
48+
);
49+
} catch (Error e) {
50+
warning ("Failed to load CSS: %s", e.message);
51+
}
52+
53+
child.realize.connect (on_realize);
54+
}
55+
56+
private void on_realize () {
57+
var display = Gdk.Display.get_default ();
58+
if (display is Gdk.X11.Display) {
59+
unowned var xdisplay = ((Gdk.X11.Display) display).get_xdisplay ();
60+
61+
var window = ((Gdk.X11.Surface) get_surface ()).get_xid ();
62+
63+
var prop = xdisplay.intern_atom ("_MUTTER_HINTS", false);
64+
65+
var value = "monitor-label=%d".printf (index);
66+
67+
xdisplay.change_property (window, prop, X.XA_STRING, 8, 0, (uchar[]) value, value.length);
68+
} else {
69+
connect_to_shell ();
70+
make_monitor_label (index);
71+
}
72+
}
73+
}

src/meson.build

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ plug_files = files(
22
'Utils.vala',
33
'SettingsDaemon.vala',
44
'DisplayPlug.vala',
5-
'Interfaces/GalaDBus.vala',
65
'Interfaces/NightLightManager.vala',
76
'Interfaces/MutterDisplayConfig.vala',
87
'Interfaces/SensorManager.vala',
@@ -15,6 +14,7 @@ plug_files = files(
1514
'Views' / 'FiltersView.vala',
1615
'Widgets/DisplayWidget.vala',
1716
'Widgets/DisplaysOverlay.vala',
17+
'Widgets' / 'MonitorLabel.vala',
1818
)
1919

2020
switchboard_dep = dependency('switchboard-3')
@@ -31,7 +31,9 @@ shared_module(
3131
dependency('gobject-2.0'),
3232
dependency('granite-7'),
3333
dependency('gtk4'),
34+
dependency('gtk4-x11'),
3435
dependency('libadwaita-1'),
36+
dependency('pantheon-wayland-1'),
3537
meson.get_compiler('vala').find_library('posix'),
3638
switchboard_dep
3739
],

0 commit comments

Comments
 (0)