2023-03-27 15:43:52 +02:00
|
|
|
use iced::executor;
|
2024-01-10 02:16:29 +01:00
|
|
|
use iced::keyboard;
|
2024-01-10 02:58:40 +01:00
|
|
|
use iced::widget::{
|
|
|
|
|
button, column, container, horizontal_space, row, text, vertical_rule,
|
|
|
|
|
};
|
2023-03-27 15:43:52 +02:00
|
|
|
use iced::{
|
2024-01-10 02:58:40 +01:00
|
|
|
color, Application, Color, Command, Element, Length, Settings,
|
|
|
|
|
Subscription, Theme,
|
2023-03-27 15:43:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub fn main() -> iced::Result {
|
|
|
|
|
Layout::run(Settings::default())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Layout {
|
2024-01-10 02:16:29 +01:00
|
|
|
example: Example,
|
2023-03-27 15:43:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
enum Message {
|
|
|
|
|
Next,
|
|
|
|
|
Previous,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Application for Layout {
|
|
|
|
|
type Message = Message;
|
|
|
|
|
type Theme = Theme;
|
|
|
|
|
type Executor = executor::Default;
|
|
|
|
|
type Flags = ();
|
|
|
|
|
|
|
|
|
|
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
|
|
|
|
|
(
|
|
|
|
|
Self {
|
2024-01-10 02:16:29 +01:00
|
|
|
example: Example::default(),
|
2023-03-27 15:43:52 +02:00
|
|
|
},
|
|
|
|
|
Command::none(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn title(&self) -> String {
|
2024-01-10 02:16:29 +01:00
|
|
|
format!("{} - Layout - Iced", self.example.title)
|
2023-03-27 15:43:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update(&mut self, message: Self::Message) -> Command<Message> {
|
|
|
|
|
match message {
|
|
|
|
|
Message::Next => {
|
2024-01-10 02:16:29 +01:00
|
|
|
self.example = self.example.next();
|
2023-03-27 15:43:52 +02:00
|
|
|
}
|
|
|
|
|
Message::Previous => {
|
2024-01-10 02:16:29 +01:00
|
|
|
self.example = self.example.previous();
|
2023-03-27 15:43:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn subscription(&self) -> Subscription<Message> {
|
2024-01-10 02:16:29 +01:00
|
|
|
keyboard::on_key_release(|key_code, _modifiers| match key_code {
|
|
|
|
|
keyboard::KeyCode::Left => Some(Message::Previous),
|
|
|
|
|
keyboard::KeyCode::Right => Some(Message::Next),
|
2023-03-27 15:43:52 +02:00
|
|
|
_ => None,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn view(&self) -> Element<Message> {
|
2024-01-10 02:58:40 +01:00
|
|
|
let example = container(self.example.view()).style(
|
|
|
|
|
container::Appearance::default().with_border(Color::BLACK, 2.0),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let controls = row([
|
|
|
|
|
(!self.example.is_first()).then_some(
|
|
|
|
|
button("← Previous")
|
|
|
|
|
.padding([5, 10])
|
|
|
|
|
.on_press(Message::Previous)
|
|
|
|
|
.into(),
|
|
|
|
|
),
|
|
|
|
|
Some(horizontal_space(Length::Fill).into()),
|
|
|
|
|
(!self.example.is_last()).then_some(
|
|
|
|
|
button("Next →")
|
|
|
|
|
.padding([5, 10])
|
|
|
|
|
.on_press(Message::Next)
|
|
|
|
|
.into(),
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(std::convert::identity));
|
|
|
|
|
|
|
|
|
|
column![example, controls].spacing(10).padding(20).into()
|
2023-03-27 15:43:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 02:16:29 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
struct Example {
|
|
|
|
|
title: &'static str,
|
|
|
|
|
view: fn() -> Element<'static, Message>,
|
2023-03-27 15:43:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Example {
|
2024-01-10 02:16:29 +01:00
|
|
|
const LIST: &'static [Self] = &[
|
|
|
|
|
Self {
|
|
|
|
|
title: "Centered",
|
|
|
|
|
view: centered,
|
|
|
|
|
},
|
|
|
|
|
Self {
|
|
|
|
|
title: "Nested Quotes",
|
|
|
|
|
view: nested_quotes,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2024-01-10 02:58:40 +01:00
|
|
|
fn is_first(self) -> bool {
|
|
|
|
|
Self::LIST.first() == Some(&self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_last(self) -> bool {
|
|
|
|
|
Self::LIST.last() == Some(&self)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 02:16:29 +01:00
|
|
|
fn previous(self) -> Self {
|
|
|
|
|
let Some(index) =
|
|
|
|
|
Self::LIST.iter().position(|&example| example == self)
|
|
|
|
|
else {
|
|
|
|
|
return self;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Self::LIST
|
|
|
|
|
.get(index.saturating_sub(1))
|
|
|
|
|
.copied()
|
|
|
|
|
.unwrap_or(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn next(self) -> Self {
|
|
|
|
|
let Some(index) =
|
|
|
|
|
Self::LIST.iter().position(|&example| example == self)
|
|
|
|
|
else {
|
|
|
|
|
return self;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Self::LIST.get(index + 1).copied().unwrap_or(self)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 15:43:52 +02:00
|
|
|
fn view(&self) -> Element<Message> {
|
2024-01-10 02:16:29 +01:00
|
|
|
(self.view)()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Example {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::LIST[0]
|
2023-03-27 15:43:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-10 02:16:29 +01:00
|
|
|
|
|
|
|
|
fn centered<'a>() -> Element<'a, Message> {
|
|
|
|
|
container(text("I am centered!").size(50))
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.center_x()
|
|
|
|
|
.center_y()
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn nested_quotes<'a>() -> Element<'a, Message> {
|
2024-01-10 02:58:40 +01:00
|
|
|
let quotes =
|
|
|
|
|
(1..5).fold(column![text("Original text")].padding(10), |quotes, i| {
|
2024-01-10 02:16:29 +01:00
|
|
|
column![
|
2024-01-10 02:58:40 +01:00
|
|
|
container(
|
|
|
|
|
row![vertical_rule(2), quotes].height(Length::Shrink)
|
|
|
|
|
)
|
|
|
|
|
.style(
|
|
|
|
|
container::Appearance::default()
|
|
|
|
|
.with_background(color!(0x000000, 0.05))
|
|
|
|
|
),
|
2024-01-10 02:16:29 +01:00
|
|
|
text(format!("Reply {i}"))
|
|
|
|
|
]
|
|
|
|
|
.spacing(10)
|
|
|
|
|
.padding(10)
|
2024-01-10 02:58:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
container(quotes)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.center_x()
|
|
|
|
|
.center_y()
|
|
|
|
|
.into()
|
2024-01-10 02:16:29 +01:00
|
|
|
}
|