Basic theming with system/light/dark selection

This commit is contained in:
Squid committed 2026-10-29 22:29:31 +01:00
1 parent 995a5fbc1d
commit c265841334
2 files changed
+191 -19

No files matched your search

+56 -19
View File
@@ -1,28 +1,48 @@
use iced::widget::{button, column, container, row, stack, text};
use iced::{Alignment, Border, Element, Length, Padding, Task};
use iced::widget::{button, column, container, pick_list, row, stack, text};
use iced::{Alignment, Element, Length, Padding, Task};
#[allow(dead_code)]
mod terminal;
mod theme;
const MENUBAR_HEIGHT: f32 = 21.0;
fn main() -> iced::Result {
iced::application(Moorly::default, update, view)
.title("Moorly")
.theme(|state: &Moorly| state.theme.iced())
.run()
}
#[derive(Default)]
struct Moorly {
menu_open: bool,
theme: theme::Preference,
}
impl Default for Moorly {
fn default() -> Self {
Self {
menu_open: false,
theme: theme::load(),
}
}
}
#[derive(Debug, Clone)]
enum Message {
ToggleMenu,
SetTheme(theme::Preference),
Quit,
}
fn update(state: &mut Moorly, message: Message) -> Task<Message> {
match message {
Message::ToggleMenu => state.menu_open = !state.menu_open,
Message::SetTheme(preference) => {
state.theme = preference;
state.menu_open = false;
theme::save(preference);
}
Message::Quit => std::process::exit(0),
}
@@ -48,18 +68,7 @@ fn view(state: &Moorly) -> Element<'_, Message> {
.align_x(Alignment::End)
.align_y(Alignment::Center)
.padding([0, 8])
.style(|theme| {
let palette = theme.extended_palette();
container::Style {
background: Some(palette.background.weak.color.into()),
border: Border {
color: palette.background.strong.color,
width: 1.0,
radius: 0.0.into(),
},
..container::Style::default()
}
});
.style(theme::menubar);
let main_area = container(column![]).width(Length::Fill).height(Length::Fill);
@@ -69,19 +78,47 @@ fn view(state: &Moorly) -> Element<'_, Message> {
return base.into();
}
let menu = container(
column![
container(hamburger()).width(Length::Fill).align_x(Alignment::End),
let theme_field = stack![
container(
pick_list(
theme::Preference::ALL,
Some(state.theme),
Message::SetTheme
)
.width(Length::Fill),
)
.width(Length::Fill)
.padding(Padding::ZERO.top(6.0)),
container(
container(text("Theme").size(11))
.padding([0, 4])
.style(theme::field_label),
)
.padding(Padding::ZERO.left(8.0)),
]
.width(Length::Fill);
let menu_items = column![theme_field].spacing(4);
let menu_items = menu_items.push(
button("Quit")
.on_press(Message::Quit)
.width(Length::Fill)
.style(button::text),
);
let menu = container(
column![
container(hamburger())
.width(Length::Fill)
.align_x(Alignment::End),
menu_items,
]
.spacing(2),
)
.width(160)
.padding(4)
.style(container::rounded_box);
.style(theme::menu);
let dropdown = container(menu)
.width(Length::Fill)
+135
View File
@@ -0,0 +1,135 @@
//! Application-wide theme selection, persistence, and shared styling.
//!
//! `iced` resolves the actual palette. `Preference::System` yields no theme,
//! which makes `iced` follow the OS light/dark color scheme (including live
//! changes while the app is running).
use std::env;
use std::fs;
use std::path::PathBuf;
use iced::widget::container;
use iced::{Border, Theme};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Preference {
#[default]
System,
Light,
Dark,
}
impl Preference {
pub const ALL: [Self; 3] = [Self::System, Self::Light, Self::Dark];
pub fn label(self) -> &'static str {
match self {
Self::System => "System",
Self::Light => "Light",
Self::Dark => "Dark",
}
}
/// `None` tells `iced` to use the system color scheme.
pub fn iced(self) -> Option<Theme> {
match self {
Self::System => None,
Self::Light => Some(Theme::Light),
Self::Dark => Some(Theme::Dark),
}
}
fn as_str(self) -> &'static str {
match self {
Self::System => "system",
Self::Light => "light",
Self::Dark => "dark",
}
}
fn parse(value: &str) -> Option<Self> {
Self::ALL.into_iter().find(|pref| pref.as_str() == value)
}
}
impl std::fmt::Display for Preference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.label())
}
}
/// Loads the persisted preference, falling back to `System`.
pub fn load() -> Preference {
config_path()
.and_then(|path| fs::read_to_string(path).ok())
.and_then(|value| Preference::parse(value.trim()))
.unwrap_or_default()
}
/// Persists the preference. Failures are ignored so theme selection never
/// blocks the UI; the in-memory choice still applies for this session.
pub fn save(preference: Preference) {
let Some(path) = config_path() else {
return;
};
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
let _ = fs::write(path, preference.as_str());
}
fn config_path() -> Option<PathBuf> {
let base = if cfg!(windows) {
env::var_os("APPDATA").map(PathBuf::from)
} else {
env::var_os("XDG_CONFIG_HOME")
.map(PathBuf::from)
.or_else(|| env::var_os("HOME").map(|home| PathBuf::from(home).join(".config")))
}?;
Some(base.join("moorly").join("theme"))
}
/// Style for the top menubar.
pub fn menubar(theme: &Theme) -> container::Style {
let palette = theme.extended_palette();
container::Style {
background: Some(palette.background.weak.color.into()),
border: Border {
color: palette.background.strong.color,
width: 1.0,
radius: 0.0.into(),
},
..container::Style::default()
}
}
/// Style for dropdown menu panels.
pub fn menu(theme: &Theme) -> container::Style {
let mut style = container::rounded_box(theme);
style.background = Some(theme.extended_palette().background.weak.color.into());
style
}
/// Style for a label that sits over the outline of an input.
pub fn field_label(theme: &Theme) -> container::Style {
container::Style {
background: Some(theme.extended_palette().background.weak.color.into()),
..container::Style::default()
}
}
#[cfg(test)]
mod tests {
use super::Preference;
#[test]
fn preference_round_trips_through_text() {
for pref in Preference::ALL {
assert_eq!(Preference::parse(pref.as_str()), Some(pref));
}
assert_eq!(Preference::parse("unknown"), None);
}
}