Skip to content

Commit

Permalink
refactor: Bind color preference (#77)
Browse files Browse the repository at this point in the history
Also move to Application class because this is application-wide settings and not only for the MainWindow
  • Loading branch information
ryonakano authored Aug 11, 2024
1 parent eeb1695 commit c110e1d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
22 changes: 22 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ public class Application : Gtk.Application {
set_accels_for_action ("app.quit", { "<Control>q", "Escape" });
}

/**
* Follow elementary OS-wide dark preference
*/
private void setup_style () {
var granite_settings = Granite.Settings.get_default ();
var gtk_settings = Gtk.Settings.get_default ();

granite_settings.bind_property ("prefers-color-scheme", gtk_settings, "gtk-application-prefer-dark-theme",
BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE,
((binding, granite_prop, ref gtk_prop) => {
gtk_prop.set_boolean ((Granite.Settings.ColorScheme) granite_prop == Granite.Settings.ColorScheme.DARK);
return true;
})
);
}

protected override void startup () {
base.startup ();

setup_style ();
}

protected override void activate () {
if (window != null) {
return;
Expand Down
10 changes: 0 additions & 10 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ public class MainWindow : Gtk.ApplicationWindow {
// See https://gitlab.gnome.org/GNOME/gtk/-/issues/1874#note_509304
update_label_text.begin (magnified_label);
});

// Follow elementary OS-wide dark preference
var granite_settings = Granite.Settings.get_default ();
var gtk_settings = Gtk.Settings.get_default ();

gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;

granite_settings.notify["prefers-color-scheme"].connect (() => {
gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
});
}

private async void update_label_text (Gtk.Label label_widget) {
Expand Down

0 comments on commit c110e1d

Please sign in to comment.