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
This commit is contained in:
Diego Iván 2022-12-22 16:39:55 -06:00 committed by GitHub
parent 0e8ae56ba9
commit 9c4569c61e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 122 additions and 18 deletions

View File

@ -1,14 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<enum id="dev.geopjr.tooth.ColorScheme">
<value value="0" nick="system"/>
<value value="1" nick="light"/>
<value value="2" nick="dark"/>
</enum>
<schema path="/dev/geopjr/tooth/" id="dev.geopjr.tooth" gettext-domain="dev.geopjr.tooth">
<key name="active-account" type="s">
<default>''</default>
</key>
<!-- <key name="dark-theme" type="b">
<default>false</default>
</key> -->
<key name="color-scheme" enum="dev.geopjr.tooth.ColorScheme">
<default>'system'</default>
</key>
<key name="autostart" type="b">
<default>false</default>
</key>

View File

@ -20,18 +20,20 @@
<child>
<object class="AdwPreferencesGroup">
<property name="title" translatable="yes">Appearance</property>
<!-- <child>
<object class="AdwActionRow">
<property name="title" translatable="yes">Dark theme</property>
<property name="activatable_widget">dark_theme</property>
<child>
<object class="GtkSwitch" id="dark_theme">
<property name="can_focus">0</property>
<property name="valign">center</property>
</object>
</child>
<child>
<object class="AdwComboRow" id="scheme_combo_row">
<property name="title" translatable="yes">Color Scheme</property>
<signal name="notify::selected-item" handler="on_scheme_changed" swapped="no"/>
<property name="model">
<object class="ToothColorSchemeListModel"/>
</property>
<property name="expression">
<lookup type="ToothColorSchemeListItem" name="name"/>
</property>
</object>
</child> -->
</child>
<child>
<object class="AdwActionRow">
<property name="title" translatable="yes">Multiple Columns</property>

View File

@ -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);

View File

@ -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<ColorSchemeListItem> array = new Gee.ArrayList<ColorSchemeListItem> ();
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);
}
}

View File

@ -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 ();
}
}
}