Skip to content

Commit

Permalink
Introduce disabled state for TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
JungleTryne authored and hecrj committed Apr 12, 2023
1 parent ca828f0 commit ce4c4fe
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 238 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Cargo.lock
.cargo/
dist/
traces/
.idea
2 changes: 1 addition & 1 deletion examples/component/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
Expand Down
9 changes: 4 additions & 5 deletions examples/integration_wgpu/src/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 3 additions & 6 deletions examples/lazy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
Expand Down
14 changes: 6 additions & 8 deletions examples/modal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
12 changes: 5 additions & 7 deletions examples/qr_code/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions examples/styling/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions examples/text_input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions examples/text_input/README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 94 additions & 0 deletions examples/text_input/src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
6 changes: 4 additions & 2 deletions examples/toast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
),
Expand Down
29 changes: 12 additions & 17 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 4 additions & 7 deletions examples/tour/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions examples/websocket/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions native/src/widget/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,13 @@ where
pub fn text_input<'a, Message, Renderer>(
placeholder: &str,
value: &str,
on_change: impl Fn(String) -> Message + 'a,
) -> widget::TextInput<'a, Message, Renderer>
where
Message: Clone,
Renderer: crate::text::Renderer,
Renderer::Theme: widget::text_input::StyleSheet,
{
widget::TextInput::new(placeholder, value, on_change)
widget::TextInput::new(placeholder, value)
}

/// Creates a new [`Slider`].
Expand Down
Loading

0 comments on commit ce4c4fe

Please sign in to comment.