Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3768b73

Browse files
committedSep 19, 2024·
Switch to per display providers
1 parent 7ba37e4 commit 3768b73

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed
 

‎lib/Init.vala

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ namespace Granite {
2020

2121
typeof (Granite.Settings).ensure ();
2222

23-
Granite.StyleManager.get_default (); // Make sure everything is being set up there
23+
unowned var display_manager = Gdk.DisplayManager.@get ();
24+
display_manager.display_opened.connect (StyleManager.init_for_display);
25+
26+
foreach (unowned var display in display_manager.list_displays ()) {
27+
StyleManager.init_for_display (display);
28+
}
2429

2530
GLib.Intl.bindtextdomain (Granite.GETTEXT_PACKAGE, Granite.LOCALEDIR);
2631
GLib.Intl.bind_textdomain_codeset (Granite.GETTEXT_PACKAGE, "UTF-8");

‎lib/StyleManager.vala

+29-32
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ public class Granite.StyleManager : Object {
1212
private static Gtk.CssProvider? dark_provider = null;
1313
private static Gtk.CssProvider? app_provider = null;
1414

15-
private static GLib.Once<Granite.StyleManager> instance;
16-
public static unowned Granite.StyleManager get_default () {
17-
return instance.once (() => {
18-
return new Granite.StyleManager ();
19-
});
15+
private static HashTable<Gdk.Display, StyleManager> style_managers_by_displays = new HashTable<Gdk.Display, StyleManager> (null, null);
16+
17+
public static unowned StyleManager get_default () {
18+
return style_managers_by_displays[Gdk.Display.get_default ()];
19+
}
20+
21+
public static unowned StyleManager get_for_display (Gdk.Display display) {
22+
return style_managers_by_displays[display];
23+
}
24+
25+
internal static void init_for_display (Gdk.Display display) {
26+
style_managers_by_displays[display] = new StyleManager (display);
2027
}
2128

2229
/**
@@ -26,36 +33,30 @@ public class Granite.StyleManager : Object {
2633
*/
2734
public Settings.ColorScheme color_scheme_override { get; set; default = LIGHT; }
2835

29-
private StyleManager () { }
30-
31-
construct {
32-
unowned var display_manager = Gdk.DisplayManager.@get ();
33-
display_manager.display_opened.connect (register_display);
34-
35-
foreach (unowned var display in display_manager.list_displays ()) {
36-
register_display (display);
37-
}
38-
39-
Granite.Settings.get_default ().notify["prefers-color-scheme"].connect (update_color_scheme);
40-
update_color_scheme ();
36+
/**
37+
* The Gdk.Display this StyleManager handles.
38+
*/
39+
public Gdk.Display display { get; construct; }
4140

42-
notify["color-scheme-override"].connect (update_color_scheme);
41+
private StyleManager (Gdk.Display display) {
42+
Object (display: display);
4343
}
4444

45-
private void register_display (Gdk.Display display) {
45+
construct {
4646
var gtk_settings = Gtk.Settings.get_for_display (display);
47-
gtk_settings.gtk_application_prefer_dark_theme = prefers_dark ();
48-
gtk_settings.notify["gtk-application-prefer-dark-theme"].connect (() => {
49-
set_provider_for_display (display, gtk_settings.gtk_application_prefer_dark_theme);
50-
});
47+
gtk_settings.notify["gtk-application-prefer-dark-theme"].connect (set_provider_for_display);
48+
set_provider_for_display ();
5149

52-
set_provider_for_display (display, gtk_settings.gtk_application_prefer_dark_theme);
50+
var granite_settings = Granite.Settings.get_default ();
51+
granite_settings.notify["prefers-color-scheme"].connect (update_color_scheme);
52+
notify["color-scheme-override"].connect (update_color_scheme);
53+
update_color_scheme ();
5354

5455
var icon_theme = Gtk.IconTheme.get_for_display (display);
5556
icon_theme.add_resource_path ("/io/elementary/granite");
5657
}
5758

58-
private void set_provider_for_display (Gdk.Display display, bool prefer_dark_style) {
59+
private void set_provider_for_display () {
5960
if (app_provider == null) {
6061
var base_path = Application.get_default ().resource_base_path;
6162
if (base_path != null) {
@@ -70,7 +71,7 @@ public class Granite.StyleManager : Object {
7071
}
7172
}
7273

73-
if (prefer_dark_style) {
74+
if (Gtk.Settings.get_for_display (display).gtk_application_prefer_dark_theme) {
7475
if (base_provider != null) {
7576
Gtk.StyleContext.remove_provider_for_display (display, base_provider);
7677
}
@@ -107,12 +108,8 @@ public class Granite.StyleManager : Object {
107108
}
108109

109110
private void update_color_scheme () {
110-
var dark = prefers_dark ();
111-
112-
foreach (var display in Gdk.DisplayManager.@get ().list_displays ()) {
113-
var gtk_settings = Gtk.Settings.get_for_display (display);
114-
gtk_settings.gtk_application_prefer_dark_theme = dark;
115-
}
111+
var gtk_settings = Gtk.Settings.get_for_display (display);
112+
gtk_settings.gtk_application_prefer_dark_theme = prefers_dark ();
116113
}
117114

118115
private bool prefers_dark () {

0 commit comments

Comments
 (0)
Please sign in to comment.