Use winit to obtain current theme::Mode

This commit is contained in:
Héctor Ramón Jiménez 2025-09-08 05:16:20 +02:00
parent 5c7ae8a3d6
commit 0111f514a1
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
25 changed files with 208 additions and 602 deletions

View file

@ -15,7 +15,7 @@ pub fn main() -> iced::Result {
#[derive(Default)]
struct Styling {
theme: Theme,
theme: Option<Theme>,
input_value: String,
slider_value: f32,
checkbox_value: bool,
@ -38,7 +38,7 @@ impl Styling {
fn update(&mut self, message: Message) {
match message {
Message::ThemeChanged(theme) => {
self.theme = theme;
self.theme = Some(theme);
}
Message::InputChanged(value) => self.input_value = value,
Message::ButtonPressed => {}
@ -46,20 +46,20 @@ impl Styling {
Message::CheckboxToggled(value) => self.checkbox_value = value,
Message::TogglerToggled(value) => self.toggler_value = value,
Message::PreviousTheme | Message::NextTheme => {
if let Some(current) = Theme::ALL
.iter()
.position(|candidate| &self.theme == candidate)
{
self.theme = if matches!(message, Message::NextTheme) {
Theme::ALL[(current + 1) % Theme::ALL.len()].clone()
} else if current == 0 {
Theme::ALL
.last()
.expect("Theme::ALL must not be empty")
.clone()
} else {
Theme::ALL[current - 1].clone()
};
if let Some(current) = Theme::ALL.iter().position(|candidate| {
self.theme.as_ref() == Some(candidate)
}) {
self.theme =
Some(if matches!(message, Message::NextTheme) {
Theme::ALL[(current + 1) % Theme::ALL.len()].clone()
} else if current == 0 {
Theme::ALL
.last()
.expect("Theme::ALL must not be empty")
.clone()
} else {
Theme::ALL[current - 1].clone()
});
}
}
}
@ -68,8 +68,9 @@ impl Styling {
fn view(&self) -> Element<'_, Message> {
let choose_theme = column![
text("Theme:"),
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeChanged)
.width(Fill),
pick_list(Theme::ALL, self.theme.as_ref(), Message::ThemeChanged)
.width(Fill)
.placeholder("System"),
]
.spacing(10);
@ -190,7 +191,7 @@ impl Styling {
})
}
fn theme(&self) -> Theme {
fn theme(&self) -> Option<Theme> {
self.theme.clone()
}
}
@ -210,9 +211,7 @@ mod tests {
.cloned()
.map(|theme| {
let mut styling = Styling::default();
styling.update(Message::ThemeChanged(theme));
let theme = styling.theme();
styling.update(Message::ThemeChanged(theme.clone()));
let mut ui = simulator(styling.view());
let snapshot = ui.snapshot(&theme)?;