Use first-class functions in layout example
This commit is contained in:
parent
88f8c343fa
commit
a79b2adf5c
1 changed files with 83 additions and 60 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
use iced::executor;
|
use iced::executor;
|
||||||
|
use iced::keyboard;
|
||||||
use iced::widget::{column, container, row, text, vertical_rule};
|
use iced::widget::{column, container, row, text, vertical_rule};
|
||||||
use iced::{
|
use iced::{
|
||||||
Application, Command, Element, Length, Settings, Subscription, Theme,
|
Application, Command, Element, Length, Settings, Subscription, Theme,
|
||||||
|
|
@ -10,9 +11,7 @@ pub fn main() -> iced::Result {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Layout {
|
struct Layout {
|
||||||
previous: Vec<Example>,
|
example: Example,
|
||||||
current: Example,
|
|
||||||
next: Vec<Example>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
|
@ -30,36 +29,23 @@ impl Application for Layout {
|
||||||
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
|
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
|
||||||
(
|
(
|
||||||
Self {
|
Self {
|
||||||
previous: vec![],
|
example: Example::default(),
|
||||||
current: Example::Centered,
|
|
||||||
next: vec![Example::NestedQuotes],
|
|
||||||
},
|
},
|
||||||
Command::none(),
|
Command::none(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self) -> String {
|
fn title(&self) -> String {
|
||||||
String::from("Counter - Iced")
|
format!("{} - Layout - Iced", self.example.title)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, message: Self::Message) -> Command<Message> {
|
fn update(&mut self, message: Self::Message) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::Next => {
|
Message::Next => {
|
||||||
if !self.next.is_empty() {
|
self.example = self.example.next();
|
||||||
let previous = std::mem::replace(
|
|
||||||
&mut self.current,
|
|
||||||
self.next.remove(0),
|
|
||||||
);
|
|
||||||
|
|
||||||
self.previous.push(previous);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Message::Previous => {
|
Message::Previous => {
|
||||||
if let Some(previous) = self.previous.pop() {
|
self.example = self.example.previous();
|
||||||
let next = std::mem::replace(&mut self.current, previous);
|
|
||||||
|
|
||||||
self.next.insert(0, next);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,57 +53,94 @@ impl Application for Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
use iced::event::{self, Event};
|
keyboard::on_key_release(|key_code, _modifiers| match key_code {
|
||||||
use iced::keyboard;
|
keyboard::KeyCode::Left => Some(Message::Previous),
|
||||||
|
keyboard::KeyCode::Right => Some(Message::Next),
|
||||||
event::listen_with(|event, status| match event {
|
|
||||||
Event::Keyboard(keyboard::Event::KeyReleased {
|
|
||||||
key_code, ..
|
|
||||||
}) if status == event::Status::Ignored => match key_code {
|
|
||||||
keyboard::KeyCode::Left => Some(Message::Previous),
|
|
||||||
keyboard::KeyCode::Right => Some(Message::Next),
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
self.current.view()
|
self.example.view()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
enum Example {
|
struct Example {
|
||||||
Centered,
|
title: &'static str,
|
||||||
NestedQuotes,
|
view: fn() -> Element<'static, Message>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Example {
|
impl Example {
|
||||||
|
const LIST: &'static [Self] = &[
|
||||||
|
Self {
|
||||||
|
title: "Centered",
|
||||||
|
view: centered,
|
||||||
|
},
|
||||||
|
Self {
|
||||||
|
title: "Nested Quotes",
|
||||||
|
view: nested_quotes,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
match self {
|
(self.view)()
|
||||||
Self::Centered => container(text("I am centered!").size(50))
|
|
||||||
.width(Length::Fill)
|
|
||||||
.height(Length::Fill)
|
|
||||||
.center_x()
|
|
||||||
.center_y()
|
|
||||||
.into(),
|
|
||||||
Self::NestedQuotes => container((1..5).fold(
|
|
||||||
column![text("Original text")].padding(10),
|
|
||||||
|quotes, i| {
|
|
||||||
column![
|
|
||||||
row![vertical_rule(2), quotes].height(Length::Shrink),
|
|
||||||
text(format!("Reply {i}"))
|
|
||||||
]
|
|
||||||
.spacing(10)
|
|
||||||
.padding(10)
|
|
||||||
},
|
|
||||||
))
|
|
||||||
.width(Length::Fill)
|
|
||||||
.height(Length::Fill)
|
|
||||||
.center_x()
|
|
||||||
.center_y()
|
|
||||||
.into(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Example {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::LIST[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
||||||
|
container((1..5).fold(
|
||||||
|
column![text("Original text")].padding(10),
|
||||||
|
|quotes, i| {
|
||||||
|
column![
|
||||||
|
row![vertical_rule(2), quotes].height(Length::Shrink),
|
||||||
|
text(format!("Reply {i}"))
|
||||||
|
]
|
||||||
|
.spacing(10)
|
||||||
|
.padding(10)
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.center_x()
|
||||||
|
.center_y()
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue