From 9c4569c61e9a9ebdb657f1e5eb007eba0768ff4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <76420970+Diego-Ivan@users.noreply.github.com> Date: Thu, 22 Dec 2022 16:39:55 -0600 Subject: [PATCH] feat: color scheme preference (#37) * Add enumerations for ColorScheme * PreferencesDialog: The dialog now sets the corresponding scheme to settings and Adwaita * Set color scheme depending on user's preference when starting the app --- data/dev.geopjr.tooth.gschema.xml | 12 ++++-- data/ui/dialogs/preferences.ui | 24 ++++++------ src/Application.vala | 4 ++ src/Dialogs/Preferences.vala | 63 ++++++++++++++++++++++++++++++- src/Services/Settings.vala | 37 ++++++++++++++++-- 5 files changed, 122 insertions(+), 18 deletions(-) diff --git a/data/dev.geopjr.tooth.gschema.xml b/data/dev.geopjr.tooth.gschema.xml index db270c1..bc0a79f 100644 --- a/data/dev.geopjr.tooth.gschema.xml +++ b/data/dev.geopjr.tooth.gschema.xml @@ -1,14 +1,20 @@ + + + + + + '' - + + 'system' + false diff --git a/data/ui/dialogs/preferences.ui b/data/ui/dialogs/preferences.ui index fc8ec52..a3fe055 100644 --- a/data/ui/dialogs/preferences.ui +++ b/data/ui/dialogs/preferences.ui @@ -20,18 +20,20 @@ Appearance - + Multiple Columns diff --git a/src/Application.vala b/src/Application.vala index e653897..13673de 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -109,6 +109,10 @@ namespace Tooth { error (msg); } + var style_manager = Adw.StyleManager.get_default (); + ColorScheme color_scheme = (ColorScheme) settings.get_enum ("color-scheme"); + style_manager.color_scheme = color_scheme.to_adwaita_scheme (); + set_accels_for_action ("app.about", ACCEL_ABOUT); set_accels_for_action ("app.compose", ACCEL_NEW_POST); set_accels_for_action ("app.back", ACCEL_BACK); diff --git a/src/Dialogs/Preferences.vala b/src/Dialogs/Preferences.vala index 7240f82..0e23076 100644 --- a/src/Dialogs/Preferences.vala +++ b/src/Dialogs/Preferences.vala @@ -3,7 +3,7 @@ using Gtk; [GtkTemplate (ui = "/dev/geopjr/tooth/ui/dialogs/preferences.ui")] public class Tooth.Dialogs.Preferences : Adw.PreferencesWindow { - // [GtkChild] unowned Switch dark_theme; + [GtkChild] unowned Adw.ComboRow scheme_combo_row; [GtkChild] unowned Switch autostart; [GtkChild] unowned Switch work_in_background; [GtkChild] unowned SpinButton timeline_page_size; @@ -12,6 +12,10 @@ public class Tooth.Dialogs.Preferences : Adw.PreferencesWindow { [GtkChild] unowned Switch public_live_updates; [GtkChild] unowned Switch show_spoilers; + static construct { + typeof (ColorSchemeListModel).ensure (); + } + construct { transient_for = app.main_window; @@ -23,6 +27,9 @@ public class Tooth.Dialogs.Preferences : Adw.PreferencesWindow { // return vis.get_name (); // }); + // Setup scheme combo row + scheme_combo_row.selected = settings.get_enum ("color-scheme"); + bind (); show (); } @@ -42,4 +49,58 @@ public class Tooth.Dialogs.Preferences : Adw.PreferencesWindow { settings.bind ("show-spoilers", show_spoilers, "active", SettingsBindFlags.DEFAULT); } + [GtkCallback] + private void on_scheme_changed () { + var selected_item = (ColorSchemeListItem) scheme_combo_row.selected_item; + var style_manager = Adw.StyleManager.get_default (); + + style_manager.color_scheme = selected_item.adwaita_scheme; + settings.color_scheme = selected_item.color_scheme; + } } + +public class Tooth.ColorSchemeListModel : Object, ListModel { + private Gee.ArrayList array = new Gee.ArrayList (); + + construct { + array.add (new ColorSchemeListItem (SYSTEM)); + array.add (new ColorSchemeListItem (LIGHT)); + array.add (new ColorSchemeListItem (DARK)); + } + + public Object? get_item (uint position) + requires (position < array.size) + { + return array.get ((int) position); + } + + public Type get_item_type () { + return typeof(ColorSchemeListItem); + } + + public uint get_n_items () { + return array.size; + } + + public Object? get_object (uint position) { + return get_item (position); + } +} + +public class Tooth.ColorSchemeListItem : Object { + public ColorScheme color_scheme { get; construct; } + public string name { + owned get { + return color_scheme.to_string (); + } + } + public Adw.ColorScheme adwaita_scheme { + get { + return color_scheme.to_adwaita_scheme (); + } + } + + public ColorSchemeListItem (ColorScheme color_scheme) { + Object (color_scheme: color_scheme); + } +} \ No newline at end of file diff --git a/src/Services/Settings.vala b/src/Services/Settings.vala index e5d6fab..a877328 100644 --- a/src/Services/Settings.vala +++ b/src/Services/Settings.vala @@ -3,7 +3,7 @@ using GLib; public class Tooth.Settings : GLib.Settings { public string active_account { get; set; } - // public bool dark_theme { get; set; } + public ColorScheme color_scheme { get; set; } public bool autostart { get; set; } public bool work_in_background { get; set; } public int timeline_page_size { get; set; } @@ -16,7 +16,7 @@ public class Tooth.Settings : GLib.Settings { public Settings () { Object (schema_id: Build.DOMAIN); init ("active-account"); - // init ("dark-theme"); + init ("color-scheme"); init ("autostart"); init ("work-in-background"); init ("timeline-page-size"); @@ -30,5 +30,36 @@ public class Tooth.Settings : GLib.Settings { void init (string key) { bind (key, this, key, SettingsBindFlags.DEFAULT); } - } + +public enum Tooth.ColorScheme { + SYSTEM, + LIGHT, + DARK; + + public string to_string () { + switch (this) { + case SYSTEM: + return _("Follow System"); + case LIGHT: + return _("Light"); + case DARK: + return _("Dark"); + default: + assert_not_reached (); + } + } + + public Adw.ColorScheme to_adwaita_scheme () { + switch (this) { + case SYSTEM: + return DEFAULT; + case LIGHT: + return FORCE_LIGHT; + case DARK: + return FORCE_DARK; + default: + assert_not_reached (); + } + } +} \ No newline at end of file