Add explain toggle to layout example
This commit is contained in:
parent
5dbded61de
commit
d76705df29
2 changed files with 56 additions and 11 deletions
|
|
@ -1,11 +1,12 @@
|
||||||
use iced::executor;
|
use iced::executor;
|
||||||
use iced::keyboard;
|
use iced::keyboard;
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, column, container, horizontal_space, row, text, vertical_rule,
|
button, checkbox, column, container, horizontal_space, row, text,
|
||||||
|
vertical_rule,
|
||||||
};
|
};
|
||||||
use iced::{
|
use iced::{
|
||||||
color, Application, Color, Command, Element, Length, Settings,
|
color, Alignment, Application, Color, Command, Element, Font, Length,
|
||||||
Subscription, Theme,
|
Settings, Subscription, Theme,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
|
|
@ -15,12 +16,14 @@ pub fn main() -> iced::Result {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Layout {
|
struct Layout {
|
||||||
example: Example,
|
example: Example,
|
||||||
|
explain: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum Message {
|
enum Message {
|
||||||
Next,
|
Next,
|
||||||
Previous,
|
Previous,
|
||||||
|
ExplainToggled(bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Application for Layout {
|
impl Application for Layout {
|
||||||
|
|
@ -33,6 +36,7 @@ impl Application for Layout {
|
||||||
(
|
(
|
||||||
Self {
|
Self {
|
||||||
example: Example::default(),
|
example: Example::default(),
|
||||||
|
explain: false,
|
||||||
},
|
},
|
||||||
Command::none(),
|
Command::none(),
|
||||||
)
|
)
|
||||||
|
|
@ -50,6 +54,9 @@ impl Application for Layout {
|
||||||
Message::Previous => {
|
Message::Previous => {
|
||||||
self.example = self.example.previous();
|
self.example = self.example.previous();
|
||||||
}
|
}
|
||||||
|
Message::ExplainToggled(explain) => {
|
||||||
|
self.explain = explain;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Command::none()
|
Command::none()
|
||||||
|
|
@ -64,9 +71,24 @@ impl Application for Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
let example = container(self.example.view()).style(
|
let header = row![
|
||||||
container::Appearance::default().with_border(Color::BLACK, 2.0),
|
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([
|
let controls = row([
|
||||||
(!self.example.is_first()).then_some(
|
(!self.example.is_first()).then_some(
|
||||||
|
|
@ -86,7 +108,14 @@ impl Application for Layout {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flatten());
|
.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(
|
container(
|
||||||
row![vertical_rule(2), quotes].height(Length::Shrink)
|
row![vertical_rule(2), quotes].height(Length::Shrink)
|
||||||
)
|
)
|
||||||
.style(
|
.style(|theme: &Theme| {
|
||||||
container::Appearance::default()
|
let palette = theme.extended_palette();
|
||||||
.with_background(color!(0x000000, 0.05))
|
|
||||||
),
|
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}"))
|
text(format!("Reply {i}"))
|
||||||
]
|
]
|
||||||
.spacing(10)
|
.spacing(10)
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,8 @@ pub struct Extended {
|
||||||
pub success: Success,
|
pub success: Success,
|
||||||
/// The set of danger colors.
|
/// The set of danger colors.
|
||||||
pub danger: Danger,
|
pub danger: Danger,
|
||||||
|
/// Whether the palette is dark or not.
|
||||||
|
pub is_dark: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The built-in light variant of an [`Extended`] palette.
|
/// The built-in light variant of an [`Extended`] palette.
|
||||||
|
|
@ -113,6 +115,7 @@ impl Extended {
|
||||||
palette.background,
|
palette.background,
|
||||||
palette.text,
|
palette.text,
|
||||||
),
|
),
|
||||||
|
is_dark: is_dark(palette.background),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue