Add explain toggle to layout example

This commit is contained in:
Héctor Ramón Jiménez 2024-01-10 03:56:39 +01:00
parent 5dbded61de
commit d76705df29
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 56 additions and 11 deletions

View file

@ -1,11 +1,12 @@
use iced::executor;
use iced::keyboard;
use iced::widget::{
button, column, container, horizontal_space, row, text, vertical_rule,
button, checkbox, column, container, horizontal_space, row, text,
vertical_rule,
};
use iced::{
color, Application, Color, Command, Element, Length, Settings,
Subscription, Theme,
color, Alignment, Application, Color, Command, Element, Font, Length,
Settings, Subscription, Theme,
};
pub fn main() -> iced::Result {
@ -15,12 +16,14 @@ pub fn main() -> iced::Result {
#[derive(Debug)]
struct Layout {
example: Example,
explain: bool,
}
#[derive(Debug, Clone, Copy)]
enum Message {
Next,
Previous,
ExplainToggled(bool),
}
impl Application for Layout {
@ -33,6 +36,7 @@ impl Application for Layout {
(
Self {
example: Example::default(),
explain: false,
},
Command::none(),
)
@ -50,6 +54,9 @@ impl Application for Layout {
Message::Previous => {
self.example = self.example.previous();
}
Message::ExplainToggled(explain) => {
self.explain = explain;
}
}
Command::none()
@ -64,9 +71,24 @@ impl Application for Layout {
}
fn view(&self) -> Element<Message> {
let example = container(self.example.view()).style(
container::Appearance::default().with_border(Color::BLACK, 2.0),
);
let header = row![
text(self.example.title).size(20).font(Font::MONOSPACE),
horizontal_space(Length::Fill),
checkbox("Explain", self.explain, Message::ExplainToggled),
]
.align_items(Alignment::Center);
let example = container(if self.explain {
self.example.view().explain(color!(0x0000ff))
} else {
self.example.view()
})
.style(|theme: &Theme| {
let palette = theme.extended_palette();
container::Appearance::default()
.with_border(palette.background.strong.color, 4.0)
});
let controls = row([
(!self.example.is_first()).then_some(
@ -86,7 +108,14 @@ impl Application for Layout {
.into_iter()
.flatten());
column![example, controls].spacing(10).padding(20).into()
column![header, example, controls]
.spacing(10)
.padding(20)
.into()
}
fn theme(&self) -> Theme {
Theme::Dark
}
}
@ -166,10 +195,23 @@ fn nested_quotes<'a>() -> Element<'a, Message> {
container(
row![vertical_rule(2), quotes].height(Length::Shrink)
)
.style(
container::Appearance::default()
.with_background(color!(0x000000, 0.05))
),
.style(|theme: &Theme| {
let palette = theme.extended_palette();
container::Appearance::default().with_background(
if palette.is_dark {
Color {
a: 0.01,
..Color::WHITE
}
} else {
Color {
a: 0.08,
..Color::BLACK
}
},
)
}),
text(format!("Reply {i}"))
]
.spacing(10)

View file

@ -82,6 +82,8 @@ pub struct Extended {
pub success: Success,
/// The set of danger colors.
pub danger: Danger,
/// Whether the palette is dark or not.
pub is_dark: bool,
}
/// The built-in light variant of an [`Extended`] palette.
@ -113,6 +115,7 @@ impl Extended {
palette.background,
palette.text,
),
is_dark: is_dark(palette.background),
}
}
}