Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature / Fix?: Follow the system theme #548

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add option to follow the system theme
The theme preference now an enum with 3 options:

  - light   => maps to libadwaita::ColorScheme::ForceLight
    The app uses the light theme - regardless of system prefs

  - dark    => maps to libadwaita::ColorScheme::ForceDark
    The app uses the dark theme - regardless of system prefs

  - system  => maps to libadwaita::ColorScheme::Default
    The Apps style follows the system theme preferences
  • Loading branch information
ctsk authored and xou816 committed Feb 12, 2023
commit fe00d9ecc464adefe1c1f524a3de5b545d60b0be
11 changes: 8 additions & 3 deletions data/dev.alextren.Spot.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
<value value="1" nick="160"/>
<value value="2" nick="320"/>
</enum>
<enum id="dev.alextren.Spot.ThemePref">
<value value="0" nick="light" />
<value value="1" nick="dark" />
<value value="2" nick="system" />
</enum>
<schema id="dev.alextren.Spot" path="/dev/alextren/Spot/">
<key name='prefers-dark-theme' type='b'>
<default>true</default>
<summary>Prefer dark theme</summary>
<key name='theme-preference' enum='dev.alextren.Spot.ThemePref'>
<default>'system'</default>
<summary>The theme preference</summary>
</key>
<key name="window-width" type="i">
<default>1080</default>
Expand Down
45 changes: 33 additions & 12 deletions src/app/components/settings/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,30 +168,51 @@ impl SettingsWindow {
.set_mapping(|value, _| value.get::<u32>().ok().map(|u| u.to_variant()))
.build();

let theme = widget.theme.downcast_ref::<libadwaita::ComboRow>().unwrap();
let theme = widget
.theme
.downcast_ref::<libadwaita::ComboRow>()
.unwrap();
settings
.bind("prefers-dark-theme", theme, "selected")
.bind("theme-preference", theme, "selected")
.mapping(|variant, _| {
variant
.get::<bool>()
.map(|prefer_dark| if prefer_dark { 1 } else { 0 }.to_value())
variant.str().map(|s| {
match s {
"light" => 0,
"dark" => 1,
"system" => 2,
_ => unreachable!(),
}
.to_value()
})
})
.set_mapping(|value, _| {
value.get::<u32>().ok().map(|u| {
match u {
0 => "light",
1 => "dark",
2 => "system",
_ => unreachable!(),
}
.to_variant()
})
})
.set_mapping(|value, _| value.get::<u32>().ok().map(|u| (u == 1).to_variant()))
.build();
}

fn connect_theme_select(&self) {
let widget = self.imp();
let theme = widget.theme.downcast_ref::<libadwaita::ComboRow>().unwrap();
theme.connect_selected_notify(|theme| {
let prefers_dark_theme = theme.selected() == 1;
debug!("Theme switched! --> value: {}", theme.selected());
let manager = libadwaita::StyleManager::default();

manager.set_color_scheme(if prefers_dark_theme {
libadwaita::ColorScheme::PreferDark
} else {
libadwaita::ColorScheme::PreferLight
});
let pref = match theme.selected() {
0 => libadwaita::ColorScheme::ForceLight,
1 => libadwaita::ColorScheme::ForceDark,
_ => libadwaita::ColorScheme::Default,
};

manager.set_color_scheme(pref);
});
}

Expand Down
1 change: 1 addition & 0 deletions src/app/components/settings/settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<items>
<item translatable="yes">Light</item>
<item translatable="yes">Dark</item>
<item translatable="yes">System</item>
</items>
</object>
</property>
Expand Down
7 changes: 1 addition & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use gio::prelude::*;
use gio::ApplicationFlags;
use gio::SimpleAction;
use gtk::prelude::*;
use libadwaita::ColorScheme;

mod api;
mod app;
Expand Down Expand Up @@ -98,11 +97,7 @@ fn startup(settings: &settings::SpotSettings) {
.expect("Could not load resources");
gio::resources_register(&res);

manager.set_color_scheme(if settings.prefers_dark_theme {
ColorScheme::PreferDark
} else {
ColorScheme::PreferLight
});
manager.set_color_scheme(settings.theme_preference);

let provider = gtk::CssProvider::new();
provider.load_from_resource("/dev/alextren/Spot/app.css");
Expand Down
14 changes: 10 additions & 4 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::player::{AudioBackend, SpotifyPlayerSettings};
use gio::prelude::SettingsExt;
use librespot::playback::config::Bitrate;
use libadwaita::ColorScheme;

const SETTINGS: &str = "dev.alextren.Spot";

Expand Down Expand Up @@ -73,17 +74,22 @@ impl SpotifyPlayerSettings {

#[derive(Debug, Clone)]
pub struct SpotSettings {
pub prefers_dark_theme: bool,
pub theme_preference: ColorScheme,
pub player_settings: SpotifyPlayerSettings,
pub window: WindowGeometry,
}

impl SpotSettings {
pub fn new_from_gsettings() -> Option<Self> {
let settings = gio::Settings::new(SETTINGS);
let prefers_dark_theme = settings.boolean("prefers-dark-theme");
let theme_preference = match settings.enum_("theme-preference") {
0 => Some(ColorScheme::ForceLight),
1 => Some(ColorScheme::ForceDark),
2 => Some(ColorScheme::Default),
_ => None,
}?;
Some(Self {
prefers_dark_theme,
theme_preference,
player_settings: SpotifyPlayerSettings::new_from_gsettings()?,
window: WindowGeometry::new_from_gsettings(),
})
Expand All @@ -93,7 +99,7 @@ impl SpotSettings {
impl Default for SpotSettings {
fn default() -> Self {
Self {
prefers_dark_theme: true,
theme_preference: ColorScheme::PreferDark,
player_settings: Default::default(),
window: Default::default(),
}
Expand Down