-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce disabled state for
TextInput
- Loading branch information
1 parent
ca828f0
commit ce4c4fe
Showing
18 changed files
with
395 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Cargo.lock | |
.cargo/ | ||
dist/ | ||
traces/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.