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

@ -10,7 +10,7 @@ use iced::{Element, Fill, Point, Rectangle, Renderer, Subscription, Theme};
pub fn main() -> iced::Result {
iced::application(Arc::new, Arc::update, Arc::view)
.subscription(Arc::subscription)
.theme(|_| Theme::Dark)
.theme(Theme::Dark)
.run()
}

View file

@ -4,7 +4,7 @@ use iced::{Element, Theme};
pub fn main() -> iced::Result {
iced::application(Example::default, Example::update, Example::view)
.theme(|_| Theme::CatppuccinMocha)
.theme(Theme::CatppuccinMocha)
.run()
}

View file

@ -11,7 +11,7 @@ use iced::{
pub fn main() -> iced::Result {
iced::application(Image::default, Image::update, Image::view)
.subscription(Image::subscription)
.theme(|_| Theme::TokyoNight)
.theme(Theme::TokyoNight)
.run()
}

View file

@ -16,7 +16,7 @@ pub fn main() -> iced::Result {
iced::application(GameOfLife::default, GameOfLife::update, GameOfLife::view)
.subscription(GameOfLife::subscription)
.theme(|_| Theme::Dark)
.theme(Theme::Dark)
.centered()
.run()
}

View file

@ -19,11 +19,11 @@ pub fn main() -> iced::Result {
.run()
}
#[derive(Default, Debug)]
#[derive(Debug, Default)]
struct Layout {
example: Example,
explain: bool,
theme: Theme,
theme: Option<Theme>,
}
#[derive(Debug, Clone)]
@ -51,7 +51,7 @@ impl Layout {
self.explain = explain;
}
Message::ThemeSelected(theme) => {
self.theme = theme;
self.theme = Some(theme);
}
}
}
@ -74,7 +74,8 @@ impl Layout {
horizontal_space(),
checkbox("Explain", self.explain)
.on_toggle(Message::ExplainToggled),
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeSelected),
pick_list(Theme::ALL, self.theme.as_ref(), Message::ThemeSelected)
.placeholder("Theme"),
]
.spacing(20)
.align_y(Center);
@ -116,7 +117,7 @@ impl Layout {
.into()
}
fn theme(&self) -> Theme {
fn theme(&self) -> Option<Theme> {
self.theme.clone()
}
}

View file

@ -138,12 +138,8 @@ impl Example {
}
}
fn theme(&self, window: window::Id) -> Theme {
if let Some(window) = self.windows.get(&window) {
window.theme.clone()
} else {
Theme::default()
}
fn theme(&self, window: window::Id) -> Option<Theme> {
Some(self.windows.get(&window)?.theme.clone())
}
fn scale_factor(&self, window: window::Id) -> f32 {

View file

@ -20,7 +20,7 @@ struct QRGenerator {
data: String,
qr_code: Option<qr_code::Data>,
total_size: Option<f32>,
theme: Theme,
theme: Option<Theme>,
}
#[derive(Debug, Clone)]
@ -58,7 +58,7 @@ impl QRGenerator {
self.total_size = Some(total_size);
}
Message::ThemeChanged(theme) => {
self.theme = theme;
self.theme = Some(theme);
}
}
}
@ -78,7 +78,8 @@ impl QRGenerator {
let choose_theme = row![
text("Theme:"),
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeChanged,)
pick_list(Theme::ALL, self.theme.as_ref(), Message::ThemeChanged)
.placeholder("Theme")
]
.spacing(10)
.align_y(Center);
@ -107,7 +108,7 @@ impl QRGenerator {
center(content).padding(20).into()
}
fn theme(&self) -> Theme {
fn theme(&self) -> Option<Theme> {
self.theme.clone()
}
}

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

View file

@ -8,7 +8,7 @@ use iced::{Center, Element, Fill, Font, Right, Theme};
pub fn main() -> iced::Result {
iced::application(Table::new, Table::update, Table::view)
.theme(|_| Theme::CatppuccinMocha)
.theme(Theme::CatppuccinMocha)
.run()
}

View file

@ -1,11 +1,10 @@
use iced::border;
use iced::widget::{Button, Column, Container, Slider};
use iced::widget::{
button, center_x, center_y, checkbox, column, horizontal_space, image,
radio, rich_text, row, scrollable, slider, span, text, text_input, toggler,
vertical_space,
};
use iced::{Center, Color, Element, Fill, Font, Pixels, Theme};
use iced::{Center, Color, Element, Fill, Font, Pixels, color};
pub fn main() -> iced::Result {
#[cfg(target_arch = "wasm32")]
@ -201,7 +200,7 @@ impl Tour {
Self::container("Welcome!")
.push(
"This is a simple tour meant to showcase a bunch of \
widgets that can be easily implemented on top of Iced.",
widgets that come bundled in Iced.",
)
.push(
"Iced is a cross-platform GUI library for Rust focused on \
@ -216,28 +215,19 @@ impl Tour {
built on top of wgpu, a graphics library supporting Vulkan, \
Metal, DX11, and DX12.",
)
.push({
let theme = Theme::default();
let palette = theme.extended_palette();
.push(
rich_text![
"Additionally, this tour can also run on WebAssembly ",
"by leveraging ",
span("trunk")
.color(palette.primary.base.color)
.background(palette.background.weakest.color)
.border(
border::rounded(2)
.width(1)
.color(palette.background.weak.color)
)
.padding([0, 2])
.color(color!(0x0000FF))
.underline(true)
.font(Font::MONOSPACE)
.link(Message::OpenTrunk),
"."
]
.on_link_click(std::convert::identity)
})
.on_link_click(std::convert::identity),
)
.push(
"You will need to interact with the UI in order to reach \
the end!",

View file

@ -11,7 +11,7 @@ pub fn main() -> iced::Result {
VectorialText::update,
VectorialText::view,
)
.theme(|_| Theme::Dark)
.theme(Theme::Dark)
.run()
}

View file

@ -12,7 +12,7 @@ use iced::{
pub fn main() -> iced::Result {
iced::application(Example::default, Example::update, Example::view)
.subscription(Example::subscription)
.theme(|_| Theme::Dark)
.theme(Theme::Dark)
.run()
}