From f10e936f00d4b83dcacfdebd2727b1a5cd1add95 Mon Sep 17 00:00:00 2001 From: Dan Mishin Date: Fri, 3 Mar 2023 10:01:49 +0300 Subject: Introduce disabled state for `TextInput` --- examples/component/src/main.rs | 2 +- examples/integration_wgpu/src/controls.rs | 9 ++- examples/lazy/src/main.rs | 9 +-- examples/modal/src/main.rs | 14 ++--- examples/qr_code/src/main.rs | 12 ++-- examples/styling/src/main.rs | 11 ++-- examples/text_input/Cargo.toml | 11 ++++ examples/text_input/README.md | 15 +++++ examples/text_input/src/main.rs | 94 +++++++++++++++++++++++++++++++ examples/toast/src/main.rs | 6 +- examples/todos/src/main.rs | 29 ++++------ examples/tour/src/main.rs | 11 ++-- examples/websocket/src/main.rs | 9 +-- 13 files changed, 166 insertions(+), 66 deletions(-) create mode 100644 examples/text_input/Cargo.toml create mode 100644 examples/text_input/README.md create mode 100644 examples/text_input/src/main.rs (limited to 'examples') 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 "] +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. + +
+ + + +
+ +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::default(), Command::none()) + } + + fn title(&self) -> String { + "TextInput example".into() + } + + fn update(&mut self, message: Self::Message) -> Command { + 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> { + 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") -- cgit From e6a93e960c3ac71a74f11555fd2d225c185f6e8c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Apr 2023 04:13:36 +0200 Subject: Rename `on_change` to `on_input` for `TextInput` --- examples/component/src/main.rs | 2 +- examples/integration_wgpu/src/controls.rs | 2 +- examples/lazy/src/main.rs | 2 +- examples/modal/src/main.rs | 4 ++-- examples/qr_code/src/main.rs | 2 +- examples/styling/src/main.rs | 2 +- examples/text_input/src/main.rs | 2 +- examples/toast/src/main.rs | 4 ++-- examples/todos/src/main.rs | 8 ++++---- examples/tour/src/main.rs | 2 +- examples/websocket/src/main.rs | 2 +- 11 files changed, 16 insertions(+), 16 deletions(-) (limited to 'examples') diff --git a/examples/component/src/main.rs b/examples/component/src/main.rs index 8be3f076..21c2747c 100644 --- a/examples/component/src/main.rs +++ b/examples/component/src/main.rs @@ -142,7 +142,7 @@ mod numeric_input { .as_deref() .unwrap_or(""), ) - .on_change(Event::InputChanged) + .on_input(Event::InputChanged) .padding(10), button("+", Event::IncrementPressed), ] diff --git a/examples/integration_wgpu/src/controls.rs b/examples/integration_wgpu/src/controls.rs index 8c42513f..42623b15 100644 --- a/examples/integration_wgpu/src/controls.rs +++ b/examples/integration_wgpu/src/controls.rs @@ -102,7 +102,7 @@ impl Program for Controls { ) .push( text_input("Placeholder", text) - .on_change(Message::TextChanged), + .on_input(Message::TextChanged), ), ), ) diff --git a/examples/lazy/src/main.rs b/examples/lazy/src/main.rs index 6b6dca26..55b13bcf 100644 --- a/examples/lazy/src/main.rs +++ b/examples/lazy/src/main.rs @@ -215,7 +215,7 @@ impl Sandbox for App { scrollable(options).height(Length::Fill), row![ text_input("Add a new option", &self.input) - .on_change(Message::InputChanged) + .on_input(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 1377f054..49038475 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -134,7 +134,7 @@ impl Application for App { column![ text("Email").size(12), text_input("abc@123.com", &self.email,) - .on_change(Message::Email) + .on_input(Message::Email) .on_submit(Message::Submit) .padding(5), ] @@ -142,7 +142,7 @@ impl Application for App { column![ text("Password").size(12), text_input("", &self.password) - .on_change(Message::Password) + .on_input(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 0486b068..867ebfa4 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -51,7 +51,7 @@ impl Sandbox for QRGenerator { let input = text_input("Type the data of your QR code here...", &self.data) - .on_change(Message::DataChanged) + .on_input(Message::DataChanged) .size(30) .padding(15); diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 58f983df..e2015bac 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -91,7 +91,7 @@ impl Sandbox for Styling { ); let text_input = text_input("Type something...", &self.input_value) - .on_change(Message::InputChanged) + .on_input(Message::InputChanged) .padding(10) .size(20); diff --git a/examples/text_input/src/main.rs b/examples/text_input/src/main.rs index 977b5099..418593f4 100644 --- a/examples/text_input/src/main.rs +++ b/examples/text_input/src/main.rs @@ -74,7 +74,7 @@ impl Application for Example { let mut txt_input = text_input(placeholder, &self.data); if self.text_edit_enabled { - txt_input = txt_input.on_change(Message::TextInputChanged); + txt_input = txt_input.on_input(Message::TextInputChanged); } let btn = button("Enable/Disable").on_press(StartTimer); diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 765afb8f..b4b4e007 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -120,14 +120,14 @@ impl Application for App { subtitle( "Title", text_input("", &self.editing.title) - .on_change(Message::Title) + .on_input(Message::Title) .on_submit(Message::Add) .into() ), subtitle( "Message", text_input("", &self.editing.body) - .on_change(Message::Body) + .on_input(Message::Body) .on_submit(Message::Add) .into() ), diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index a6670626..99cdb8f9 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -205,11 +205,11 @@ impl Application for Todos { .horizontal_alignment(alignment::Horizontal::Center); let input = text_input("What needs to be done?", input_value) - .on_change(Message::InputChanged) .id(INPUT_ID.clone()) + .on_input(Message::InputChanged) + .on_submit(Message::CreateTask) .padding(15) - .size(30) - .on_submit(Message::CreateTask); + .size(30); let controls = view_controls(tasks, *filter); let filtered_tasks = @@ -375,7 +375,7 @@ impl Task { let text_input = text_input("Describe your task...", &self.description) .id(Self::text_input_id(i)) - .on_change(TaskMessage::DescriptionEdited) + .on_input(TaskMessage::DescriptionEdited) .on_submit(TaskMessage::FinishEdition) .padding(10); diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 3b4cfd2d..16ee19c0 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -571,7 +571,7 @@ impl<'a> Step { }; let mut text_input = text_input("Type something to continue...", value) - .on_change(StepMessage::InputChanged) + .on_input(StepMessage::InputChanged) .padding(10) .size(30); diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index 1fda7f18..920189f5 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -126,7 +126,7 @@ impl Application for WebSocket { let new_message_input = { let mut input = text_input("Type a message...", &self.new_message) - .on_change(Message::NewMessageChanged) + .on_input(Message::NewMessageChanged) .padding(10); let mut button = button( -- cgit From 250ba3a7f1b41c7f7ca32b8db40a8c4069ebef77 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Apr 2023 04:19:54 +0200 Subject: Remove `text_input` example --- examples/text_input/Cargo.toml | 11 ----- examples/text_input/README.md | 15 ------- examples/text_input/src/main.rs | 94 ----------------------------------------- 3 files changed, 120 deletions(-) delete mode 100644 examples/text_input/Cargo.toml delete mode 100644 examples/text_input/README.md delete mode 100644 examples/text_input/src/main.rs (limited to 'examples') diff --git a/examples/text_input/Cargo.toml b/examples/text_input/Cargo.toml deleted file mode 100644 index aead9896..00000000 --- a/examples/text_input/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "text_input" -authors = ["Dan Mishin "] -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 deleted file mode 100644 index 435989cc..00000000 --- a/examples/text_input/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# 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. - - - -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 deleted file mode 100644 index 418593f4..00000000 --- a/examples/text_input/src/main.rs +++ /dev/null @@ -1,94 +0,0 @@ -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::default(), Command::none()) - } - - fn title(&self) -> String { - "TextInput example".into() - } - - fn update(&mut self, message: Self::Message) -> Command { - 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> { - 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_input(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() - } -} -- cgit