diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/component/src/main.rs | 2 | ||||
-rw-r--r-- | examples/integration_wgpu/src/controls.rs | 9 | ||||
-rw-r--r-- | examples/lazy/src/main.rs | 9 | ||||
-rw-r--r-- | examples/modal/src/main.rs | 14 | ||||
-rw-r--r-- | examples/qr_code/src/main.rs | 12 | ||||
-rw-r--r-- | examples/styling/src/main.rs | 11 | ||||
-rw-r--r-- | examples/text_input/Cargo.toml | 11 | ||||
-rw-r--r-- | examples/text_input/README.md | 15 | ||||
-rw-r--r-- | examples/text_input/src/main.rs | 94 | ||||
-rw-r--r-- | examples/toast/src/main.rs | 6 | ||||
-rw-r--r-- | examples/todos/src/main.rs | 29 | ||||
-rw-r--r-- | examples/tour/src/main.rs | 11 | ||||
-rw-r--r-- | examples/websocket/src/main.rs | 9 |
13 files changed, 166 insertions, 66 deletions
diff --git a/examples/component/src/main.rs b/examples/component/src/main.rs index bbf549e7..8be3f076 100644 --- a/examples/component/src/main.rs +++ b/examples/component/src/main.rs @@ -141,8 +141,8 @@ mod numeric_input { .map(u32::to_string) .as_deref() .unwrap_or(""), - Event::InputChanged, ) + .on_change(Event::InputChanged) .padding(10), button("+", Event::IncrementPressed), ] diff --git a/examples/integration_wgpu/src/controls.rs b/examples/integration_wgpu/src/controls.rs index 533cb6e2..8c42513f 100644 --- a/examples/integration_wgpu/src/controls.rs +++ b/examples/integration_wgpu/src/controls.rs @@ -100,11 +100,10 @@ impl Program for Controls { .size(14) .style(Color::WHITE), ) - .push(text_input( - "Placeholder", - text, - Message::TextChanged, - )), + .push( + text_input("Placeholder", text) + .on_change(Message::TextChanged), + ), ), ) .into() diff --git a/examples/lazy/src/main.rs b/examples/lazy/src/main.rs index 6512106f..6b6dca26 100644 --- a/examples/lazy/src/main.rs +++ b/examples/lazy/src/main.rs @@ -214,12 +214,9 @@ impl Sandbox for App { column![ scrollable(options).height(Length::Fill), row![ - text_input( - "Add a new option", - &self.input, - Message::InputChanged, - ) - .on_submit(Message::AddItem(self.input.clone())), + text_input("Add a new option", &self.input) + .on_change(Message::InputChanged) + .on_submit(Message::AddItem(self.input.clone())), button(text(format!("Toggle Order ({})", self.order))) .on_press(Message::ToggleOrder) ] diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 54555684..1377f054 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -133,18 +133,16 @@ impl Application for App { column![ column![ text("Email").size(12), - text_input( - "abc@123.com", - &self.email, - Message::Email - ) - .on_submit(Message::Submit) - .padding(5), + text_input("abc@123.com", &self.email,) + .on_change(Message::Email) + .on_submit(Message::Submit) + .padding(5), ] .spacing(5), column![ text("Password").size(12), - text_input("", &self.password, Message::Password) + text_input("", &self.password) + .on_change(Message::Password) .on_submit(Message::Submit) .password() .padding(5), diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index d8041745..0486b068 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -49,13 +49,11 @@ impl Sandbox for QRGenerator { .size(70) .style(Color::from([0.5, 0.5, 0.5])); - let input = text_input( - "Type the data of your QR code here...", - &self.data, - Message::DataChanged, - ) - .size(30) - .padding(15); + let input = + text_input("Type the data of your QR code here...", &self.data) + .on_change(Message::DataChanged) + .size(30) + .padding(15); let mut content = column![title, input] .width(700) diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 448c9792..58f983df 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -90,13 +90,10 @@ impl Sandbox for Styling { }, ); - let text_input = text_input( - "Type something...", - &self.input_value, - Message::InputChanged, - ) - .padding(10) - .size(20); + let text_input = text_input("Type something...", &self.input_value) + .on_change(Message::InputChanged) + .padding(10) + .size(20); let button = button("Submit") .padding(10) diff --git a/examples/text_input/Cargo.toml b/examples/text_input/Cargo.toml new file mode 100644 index 00000000..aead9896 --- /dev/null +++ b/examples/text_input/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "text_input" +authors = ["Dan Mishin <jungletryne@yandex.com>"] +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +iced = { path = "../..", features = ["tokio"] } +tokio = { version = "1.26.0", features = ["time"] } diff --git a/examples/text_input/README.md b/examples/text_input/README.md new file mode 100644 index 00000000..435989cc --- /dev/null +++ b/examples/text_input/README.md @@ -0,0 +1,15 @@ +# Text Input + +This example shows basic usage of text edit. +The button delays the change of the text field state to allow testing of the corner cases. + +<div align="center"> + <a href="https://gfycat.com/everycarelessisabellinewheatear"> + <img src="https://thumbs.gfycat.com/EveryCarelessIsabellinewheatear-max-1mb.gif" height="400px"> + </a> +</div> + +You can run it with cargo run: +```bash +cargo run --package text_input +```
\ No newline at end of file diff --git a/examples/text_input/src/main.rs b/examples/text_input/src/main.rs new file mode 100644 index 00000000..977b5099 --- /dev/null +++ b/examples/text_input/src/main.rs @@ -0,0 +1,94 @@ +use crate::Message::{StartTimer, TextEditModeChange}; +use iced::widget::{button, column, container, row, text, text_input}; +use iced::{ + executor, window, Application, Command, Element, Length, Renderer, + Settings, Theme, +}; +use tokio::time::{sleep, Duration}; + +fn main() -> iced::Result { + let settings = Settings { + window: window::Settings { + size: (700, 100), + ..window::Settings::default() + }, + ..Settings::default() + }; + + Example::run(settings) +} + +#[derive(Default)] +struct Example { + data: String, + text_edit_enabled: bool, +} + +#[derive(Debug, Clone)] +enum Message { + StartTimer, + TextEditModeChange, + TextInputChanged(String), +} + +impl Application for Example { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command<Self::Message>) { + (Self::default(), Command::none()) + } + + fn title(&self) -> String { + "TextInput example".into() + } + + fn update(&mut self, message: Self::Message) -> Command<Self::Message> { + match message { + Message::TextEditModeChange => { + self.text_edit_enabled = !self.text_edit_enabled; + Command::none() + } + Message::TextInputChanged(updated_text) => { + self.data = updated_text; + Command::none() + } + StartTimer => { + let timer_f = async { + sleep(Duration::from_secs(3)).await; + }; + Command::perform(timer_f, |_| TextEditModeChange) + } + } + } + + fn view(&self) -> Element<'_, Self::Message, Renderer<Self::Theme>> { + let placeholder = if self.text_edit_enabled { + "Enabled TextEdit" + } else { + "Disabled TextEdit" + }; + + let mut txt_input = text_input(placeholder, &self.data); + + if self.text_edit_enabled { + txt_input = txt_input.on_change(Message::TextInputChanged); + } + + let btn = button("Enable/Disable").on_press(StartTimer); + let label = text( + "The mode will be changed after 3s when the button is pressed", + ); + + let content = row![txt_input, btn].spacing(10); + let content = column![content, label].spacing(10); + + container(content) + .width(Length::Shrink) + .height(Length::Shrink) + .padding(20) + .into() + } +} diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index e74b3ee6..765afb8f 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -119,13 +119,15 @@ impl Application for App { column![ subtitle( "Title", - text_input("", &self.editing.title, Message::Title) + text_input("", &self.editing.title) + .on_change(Message::Title) .on_submit(Message::Add) .into() ), subtitle( "Message", - text_input("", &self.editing.body, Message::Body) + text_input("", &self.editing.body) + .on_change(Message::Body) .on_submit(Message::Add) .into() ), diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 6361667e..a6670626 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -204,15 +204,12 @@ impl Application for Todos { .style(Color::from([0.5, 0.5, 0.5])) .horizontal_alignment(alignment::Horizontal::Center); - let input = text_input( - "What needs to be done?", - input_value, - Message::InputChanged, - ) - .id(INPUT_ID.clone()) - .padding(15) - .size(30) - .on_submit(Message::CreateTask); + let input = text_input("What needs to be done?", input_value) + .on_change(Message::InputChanged) + .id(INPUT_ID.clone()) + .padding(15) + .size(30) + .on_submit(Message::CreateTask); let controls = view_controls(tasks, *filter); let filtered_tasks = @@ -375,14 +372,12 @@ impl Task { .into() } TaskState::Editing => { - let text_input = text_input( - "Describe your task...", - &self.description, - TaskMessage::DescriptionEdited, - ) - .id(Self::text_input_id(i)) - .on_submit(TaskMessage::FinishEdition) - .padding(10); + let text_input = + text_input("Describe your task...", &self.description) + .id(Self::text_input_id(i)) + .on_change(TaskMessage::DescriptionEdited) + .on_submit(TaskMessage::FinishEdition) + .padding(10); row![ text_input, diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 90868877..3b4cfd2d 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -570,13 +570,10 @@ impl<'a> Step { bytes: include_bytes!("../fonts/icons.ttf"), }; - let mut text_input = text_input( - "Type something to continue...", - value, - StepMessage::InputChanged, - ) - .padding(10) - .size(30); + let mut text_input = text_input("Type something to continue...", value) + .on_change(StepMessage::InputChanged) + .padding(10) + .size(30); if is_showing_icon { text_input = text_input.icon(text_input::Icon { diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index e617b8ce..1fda7f18 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -125,12 +125,9 @@ impl Application for WebSocket { }; let new_message_input = { - let mut input = text_input( - "Type a message...", - &self.new_message, - Message::NewMessageChanged, - ) - .padding(10); + let mut input = text_input("Type a message...", &self.new_message) + .on_change(Message::NewMessageChanged) + .padding(10); let mut button = button( text("Send") |