add example
This commit is contained in:
parent
26b68d74f3
commit
11986547cb
3 changed files with 114 additions and 0 deletions
|
|
@ -93,6 +93,7 @@ members = [
|
|||
"examples/pure/pick_list",
|
||||
"examples/pure/todos",
|
||||
"examples/pure/tour",
|
||||
"examples/pure/stateless_component",
|
||||
"examples/websocket",
|
||||
]
|
||||
|
||||
|
|
|
|||
12
examples/pure/stateless_component/Cargo.toml
Normal file
12
examples/pure/stateless_component/Cargo.toml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "stateless_component"
|
||||
version = "0.1.0"
|
||||
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
iced = { path = "../../..", features = ["debug", "pure"] }
|
||||
iced_native = { path = "../../../native" }
|
||||
iced_lazy = { path = "../../../lazy", features = ["pure"] }
|
||||
iced_pure = { path = "../../../pure" }
|
||||
101
examples/pure/stateless_component/src/main.rs
Normal file
101
examples/pure/stateless_component/src/main.rs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
use iced::pure::{button, text};
|
||||
use iced::pure::{Element, Sandbox};
|
||||
use iced::Settings;
|
||||
|
||||
use custom_text::custom_text;
|
||||
|
||||
pub fn main() -> iced::Result {
|
||||
Mode::run(Settings::default())
|
||||
}
|
||||
|
||||
enum Mode {
|
||||
Standard,
|
||||
Custom,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Message {
|
||||
SwitchToComponent,
|
||||
}
|
||||
|
||||
impl Sandbox for Mode {
|
||||
type Message = Message;
|
||||
|
||||
fn new() -> Self {
|
||||
Self::Standard
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
String::from("Stateless Component Bug")
|
||||
}
|
||||
|
||||
fn update(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::SwitchToComponent => *self = Mode::Custom,
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self) -> Element<Message> {
|
||||
match self {
|
||||
Self::Standard => button(text("Click to Panic"))
|
||||
.on_press(Message::SwitchToComponent)
|
||||
.into(),
|
||||
Self::Custom => button(custom_text("..")).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod custom_text {
|
||||
use iced::pure::text;
|
||||
use iced_lazy::pure::{self, Component};
|
||||
use iced_native::text;
|
||||
use iced_pure::Element;
|
||||
|
||||
pub struct CustomText<'a> {
|
||||
text: &'a str,
|
||||
}
|
||||
|
||||
pub fn custom_text<'a>(text: &'a str) -> CustomText<'a> {
|
||||
CustomText { text }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Event {}
|
||||
|
||||
impl<'a> CustomText<'a> {
|
||||
pub fn new(text: &'a str) -> Self {
|
||||
Self { text }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> Component<Message, Renderer> for CustomText<'a>
|
||||
where
|
||||
Renderer: text::Renderer + 'static,
|
||||
{
|
||||
type State = ();
|
||||
type Event = Event;
|
||||
|
||||
fn update(
|
||||
&mut self,
|
||||
_state: &mut Self::State,
|
||||
_event: Event,
|
||||
) -> Option<Message> {
|
||||
None
|
||||
}
|
||||
|
||||
fn view(&self, _state: &Self::State) -> Element<Event, Renderer> {
|
||||
text(self.text).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> From<CustomText<'a>>
|
||||
for Element<'a, Message, Renderer>
|
||||
where
|
||||
Message: 'a,
|
||||
Renderer: 'static + text::Renderer,
|
||||
{
|
||||
fn from(x: CustomText<'a>) -> Self {
|
||||
pure::component(x)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue