Implement cross-theme dark mode.
27min
This commit is contained in:
parent
da5817e12e
commit
2452f6d313
@ -1,6 +1,8 @@
|
||||
public class WriteAs.MainWindow : Gtk.ApplicationWindow {
|
||||
private Gtk.TextView canvas;
|
||||
|
||||
private bool dark_mode = false;
|
||||
|
||||
construct {
|
||||
construct_toolbar();
|
||||
|
||||
@ -31,7 +33,28 @@ public class WriteAs.MainWindow : Gtk.ApplicationWindow {
|
||||
var settings = Gtk.Settings.get_default();
|
||||
darkmode_button.toggled.connect(() => {
|
||||
settings.gtk_application_prefer_dark_theme = darkmode_button.active;
|
||||
dark_mode = darkmode_button.active;
|
||||
adjust_text_style();
|
||||
});
|
||||
header.pack_end(darkmode_button);
|
||||
}
|
||||
|
||||
private Gtk.CssProvider cur_styles = null;
|
||||
private void adjust_text_style() {
|
||||
try {
|
||||
var styles = canvas.get_style_context();
|
||||
if (cur_styles != null) styles.remove_provider(cur_styles);
|
||||
|
||||
var css = "";
|
||||
if (dark_mode) {
|
||||
css = "* {background: black; color: white;}";
|
||||
}
|
||||
cur_styles = new Gtk.CssProvider();
|
||||
cur_styles.load_from_data(css);
|
||||
|
||||
styles.add_provider(cur_styles, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
} catch (Error e) {
|
||||
warning(e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user