Introduce disabled state for TextInput
This commit is contained in:
parent
ca828f03f5
commit
f10e936f00
17 changed files with 410 additions and 254 deletions
|
|
@ -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),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
11
examples/text_input/Cargo.toml
Normal file
11
examples/text_input/Cargo.toml
Normal file
|
|
@ -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"] }
|
||||
15
examples/text_input/README.md
Normal file
15
examples/text_input/README.md
Normal file
|
|
@ -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
|
||||
```
|
||||
94
examples/text_input/src/main.rs
Normal file
94
examples/text_input/src/main.rs
Normal file
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
@ -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()
|
||||
),
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue