From 7b369842959511f17d5c27941fd0308484bff8ea Mon Sep 17 00:00:00 2001 From: Casper Storm Date: Mon, 13 Feb 2023 11:38:05 +0100 Subject: feat: added handle to text_input --- examples/text_input/Cargo.toml | 9 ++++ examples/text_input/README.md | 10 ++++ examples/text_input/fonts/icons.ttf | Bin 0 -> 1612 bytes examples/text_input/src/main.rs | 93 ++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 examples/text_input/Cargo.toml create mode 100644 examples/text_input/README.md create mode 100644 examples/text_input/fonts/icons.ttf create 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 new file mode 100644 index 00000000..5937ef8e --- /dev/null +++ b/examples/text_input/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "text_input" +version = "0.1.0" +authors = ["Casper Rogild Storm"] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } diff --git a/examples/text_input/README.md b/examples/text_input/README.md new file mode 100644 index 00000000..2b2d8059 --- /dev/null +++ b/examples/text_input/README.md @@ -0,0 +1,10 @@ +## TextInput + +A `TextInput` is a field that can be filled with text. + +You can run it with `cargo run`: +``` +cargo run --package text_input +``` + +[`main`]: src/main.rs diff --git a/examples/text_input/fonts/icons.ttf b/examples/text_input/fonts/icons.ttf new file mode 100644 index 00000000..bfe8a24b Binary files /dev/null and b/examples/text_input/fonts/icons.ttf differ diff --git a/examples/text_input/src/main.rs b/examples/text_input/src/main.rs new file mode 100644 index 00000000..b25ed7e1 --- /dev/null +++ b/examples/text_input/src/main.rs @@ -0,0 +1,93 @@ +use iced::widget::{checkbox, column, container, text_input}; +use iced::{Element, Font, Length, Sandbox, Settings}; + +const ICON_FONT: Font = Font::External { + name: "Icons", + bytes: include_bytes!("../fonts/icons.ttf"), +}; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +#[derive(Default)] +struct Example { + value: String, + is_showing_handle: bool, +} + +#[derive(Debug, Clone)] +enum Message { + Changed(String), + ToggleHandle(bool), +} + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Text Input - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::Changed(value) => self.value = value, + Message::ToggleHandle(_) => { + self.is_showing_handle = !self.is_showing_handle + } + } + } + + fn view(&self) -> Element { + let checkbox = + checkbox("Handle", self.is_showing_handle, Message::ToggleHandle) + .spacing(5) + .text_size(16); + + let mut text_input = + text_input("Placeholder", self.value.as_str(), Message::Changed); + + if self.is_showing_handle { + text_input = text_input.handle(text_input::Handle { + font: ICON_FONT, + text: String::from('\u{e900}'), + size: Some(18), + position: text_input::HandlePosition::Right, + }); + } + + let content = column!["What is blazing fast?", text_input, checkbox] + .width(Length::Units(200)) + .spacing(10); + + container(content) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } + + fn theme(&self) -> iced::Theme { + iced::Theme::default() + } + + fn style(&self) -> iced::theme::Application { + iced::theme::Application::default() + } + + fn scale_factor(&self) -> f64 { + 1.0 + } + + fn run(settings: Settings<()>) -> Result<(), iced::Error> + where + Self: 'static + Sized, + { + ::run(settings) + } +} -- cgit From d24a4a46895ed711ddfc3199a0445f0b69a812e4 Mon Sep 17 00:00:00 2001 From: Casper Storm Date: Thu, 16 Feb 2023 14:32:59 +0100 Subject: Changed `Handle` to `Icon` to be consistent --- examples/text_input/src/main.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/text_input/src/main.rs b/examples/text_input/src/main.rs index b25ed7e1..e0ba1983 100644 --- a/examples/text_input/src/main.rs +++ b/examples/text_input/src/main.rs @@ -13,13 +13,13 @@ pub fn main() -> iced::Result { #[derive(Default)] struct Example { value: String, - is_showing_handle: bool, + is_showing_icon: bool, } #[derive(Debug, Clone)] enum Message { Changed(String), - ToggleHandle(bool), + ToggleIcon(bool), } impl Sandbox for Example { @@ -36,27 +36,27 @@ impl Sandbox for Example { fn update(&mut self, message: Message) { match message { Message::Changed(value) => self.value = value, - Message::ToggleHandle(_) => { - self.is_showing_handle = !self.is_showing_handle + Message::ToggleIcon(_) => { + self.is_showing_icon = !self.is_showing_icon } } } fn view(&self) -> Element { let checkbox = - checkbox("Handle", self.is_showing_handle, Message::ToggleHandle) + checkbox("Icon", self.is_showing_icon, Message::ToggleIcon) .spacing(5) .text_size(16); let mut text_input = text_input("Placeholder", self.value.as_str(), Message::Changed); - if self.is_showing_handle { - text_input = text_input.handle(text_input::Handle { + if self.is_showing_icon { + text_input = text_input.icon(text_input::Icon { font: ICON_FONT, - text: String::from('\u{e900}'), + code_point: '\u{e900}', size: Some(18), - position: text_input::HandlePosition::Right, + position: text_input::IconPosition::Right, }); } -- cgit From 898307e9ac8e11de275d7d4d58b93a6f24a1e800 Mon Sep 17 00:00:00 2001 From: Casper Storm Date: Mon, 20 Feb 2023 14:42:10 +0100 Subject: Removed text_input example in favor for Tour --- examples/text_input/Cargo.toml | 9 ---- examples/text_input/README.md | 10 ---- examples/text_input/fonts/icons.ttf | Bin 1612 -> 0 bytes examples/text_input/src/main.rs | 93 ------------------------------------ examples/tour/fonts/icons.ttf | Bin 0 -> 1612 bytes examples/tour/src/main.rs | 78 +++++++++++++++++++++++++----- 6 files changed, 66 insertions(+), 124 deletions(-) delete mode 100644 examples/text_input/Cargo.toml delete mode 100644 examples/text_input/README.md delete mode 100644 examples/text_input/fonts/icons.ttf delete mode 100644 examples/text_input/src/main.rs create mode 100644 examples/tour/fonts/icons.ttf (limited to 'examples') diff --git a/examples/text_input/Cargo.toml b/examples/text_input/Cargo.toml deleted file mode 100644 index 5937ef8e..00000000 --- a/examples/text_input/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "text_input" -version = "0.1.0" -authors = ["Casper Rogild Storm"] -edition = "2021" -publish = false - -[dependencies] -iced = { path = "../..", features = ["debug"] } diff --git a/examples/text_input/README.md b/examples/text_input/README.md deleted file mode 100644 index 2b2d8059..00000000 --- a/examples/text_input/README.md +++ /dev/null @@ -1,10 +0,0 @@ -## TextInput - -A `TextInput` is a field that can be filled with text. - -You can run it with `cargo run`: -``` -cargo run --package text_input -``` - -[`main`]: src/main.rs diff --git a/examples/text_input/fonts/icons.ttf b/examples/text_input/fonts/icons.ttf deleted file mode 100644 index bfe8a24b..00000000 Binary files a/examples/text_input/fonts/icons.ttf and /dev/null differ diff --git a/examples/text_input/src/main.rs b/examples/text_input/src/main.rs deleted file mode 100644 index e0ba1983..00000000 --- a/examples/text_input/src/main.rs +++ /dev/null @@ -1,93 +0,0 @@ -use iced::widget::{checkbox, column, container, text_input}; -use iced::{Element, Font, Length, Sandbox, Settings}; - -const ICON_FONT: Font = Font::External { - name: "Icons", - bytes: include_bytes!("../fonts/icons.ttf"), -}; - -pub fn main() -> iced::Result { - Example::run(Settings::default()) -} - -#[derive(Default)] -struct Example { - value: String, - is_showing_icon: bool, -} - -#[derive(Debug, Clone)] -enum Message { - Changed(String), - ToggleIcon(bool), -} - -impl Sandbox for Example { - type Message = Message; - - fn new() -> Self { - Self::default() - } - - fn title(&self) -> String { - String::from("Text Input - Iced") - } - - fn update(&mut self, message: Message) { - match message { - Message::Changed(value) => self.value = value, - Message::ToggleIcon(_) => { - self.is_showing_icon = !self.is_showing_icon - } - } - } - - fn view(&self) -> Element { - let checkbox = - checkbox("Icon", self.is_showing_icon, Message::ToggleIcon) - .spacing(5) - .text_size(16); - - let mut text_input = - text_input("Placeholder", self.value.as_str(), Message::Changed); - - if self.is_showing_icon { - text_input = text_input.icon(text_input::Icon { - font: ICON_FONT, - code_point: '\u{e900}', - size: Some(18), - position: text_input::IconPosition::Right, - }); - } - - let content = column!["What is blazing fast?", text_input, checkbox] - .width(Length::Units(200)) - .spacing(10); - - container(content) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() - .into() - } - - fn theme(&self) -> iced::Theme { - iced::Theme::default() - } - - fn style(&self) -> iced::theme::Application { - iced::theme::Application::default() - } - - fn scale_factor(&self) -> f64 { - 1.0 - } - - fn run(settings: Settings<()>) -> Result<(), iced::Error> - where - Self: 'static + Sized, - { - ::run(settings) - } -} diff --git a/examples/tour/fonts/icons.ttf b/examples/tour/fonts/icons.ttf new file mode 100644 index 00000000..bfe8a24b Binary files /dev/null and b/examples/tour/fonts/icons.ttf differ diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index de063d00..5edee850 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -5,8 +5,14 @@ use iced::widget::{ scrollable, slider, text, text_input, toggler, vertical_space, }; use iced::widget::{Button, Column, Container, Slider}; +use iced::Font; use iced::{Color, Element, Length, Renderer, Sandbox, Settings}; +const ICON_FONT: Font = Font::External { + name: "Icons", + bytes: include_bytes!("../fonts/icons.ttf"), +}; + pub fn main() -> iced::Result { env_logger::init(); @@ -127,6 +133,7 @@ impl Steps { Step::TextInput { value: String::new(), is_secure: false, + is_showing_icon: false, }, Step::Debugger, Step::End, @@ -171,14 +178,32 @@ impl Steps { enum Step { Welcome, - Slider { value: u8 }, - RowsAndColumns { layout: Layout, spacing: u16 }, - Text { size: u16, color: Color }, - Radio { selection: Option }, - Toggler { can_continue: bool }, - Image { width: u16 }, + Slider { + value: u8, + }, + RowsAndColumns { + layout: Layout, + spacing: u16, + }, + Text { + size: u16, + color: Color, + }, + Radio { + selection: Option, + }, + Toggler { + can_continue: bool, + }, + Image { + width: u16, + }, Scrollable, - TextInput { value: String, is_secure: bool }, + TextInput { + value: String, + is_secure: bool, + is_showing_icon: bool, + }, Debugger, End, } @@ -194,6 +219,7 @@ pub enum StepMessage { ImageWidthChanged(u16), InputChanged(String), ToggleSecureInput(bool), + ToggleTextInputIcon(bool), DebugToggled(bool), TogglerChanged(bool), } @@ -256,6 +282,14 @@ impl<'a> Step { *can_continue = value; } } + StepMessage::ToggleTextInputIcon(toggle) => { + if let Step::TextInput { + is_showing_icon, .. + } = self + { + *is_showing_icon = toggle + } + } }; } @@ -303,9 +337,11 @@ impl<'a> Step { Self::rows_and_columns(*layout, *spacing) } Step::Scrollable => Self::scrollable(), - Step::TextInput { value, is_secure } => { - Self::text_input(value, *is_secure) - } + Step::TextInput { + value, + is_secure, + is_showing_icon, + } => Self::text_input(value, *is_secure, *is_showing_icon), Step::Debugger => Self::debugger(debug), Step::End => Self::end(), } @@ -530,8 +566,12 @@ impl<'a> Step { ) } - fn text_input(value: &str, is_secure: bool) -> Column<'a, StepMessage> { - let text_input = text_input( + fn text_input( + value: &str, + is_secure: bool, + is_showing_icon: bool, + ) -> Column<'a, StepMessage> { + let mut text_input = text_input( "Type something to continue...", value, StepMessage::InputChanged, @@ -539,6 +579,15 @@ impl<'a> Step { .padding(10) .size(30); + if is_showing_icon { + text_input = text_input.icon(text_input::Icon { + font: ICON_FONT, + code_point: '\u{e900}', + size: Some(35), + position: text_input::IconPosition::Right, + }); + } + Self::container("Text input") .push("Use a text input to ask for different kinds of information.") .push(if is_secure { @@ -551,6 +600,11 @@ impl<'a> Step { is_secure, StepMessage::ToggleSecureInput, )) + .push(checkbox( + "Show icon", + is_showing_icon, + StepMessage::ToggleTextInputIcon, + )) .push( "A text input produces a message every time it changes. It is \ very easy to keep track of its contents:", -- cgit From 0e2fc99eb864800d2d1522c015054d84cad078f4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 11 Apr 2023 05:13:56 +0200 Subject: Use `f32` for `Icon::size` and remove unnecessary conversions --- examples/tour/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 5edee850..6a1380d7 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -583,7 +583,7 @@ impl<'a> Step { text_input = text_input.icon(text_input::Icon { font: ICON_FONT, code_point: '\u{e900}', - size: Some(35), + size: Some(35.0), position: text_input::IconPosition::Right, }); } -- cgit From 9852b4b36442ef036f0b308f798e892ddaa06c2d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 11 Apr 2023 05:45:55 +0200 Subject: Move `Icon` layout logic to `layout` in `text_input` Also add `Icon::spacing` field. --- examples/tour/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 6a1380d7..8a1b8d5a 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -138,7 +138,7 @@ impl Steps { Step::Debugger, Step::End, ], - current: 0, + current: 8, } } @@ -582,8 +582,9 @@ impl<'a> Step { if is_showing_icon { text_input = text_input.icon(text_input::Icon { font: ICON_FONT, - code_point: '\u{e900}', + code_point: '\u{E900}', size: Some(35.0), + spacing: 10.0, position: text_input::IconPosition::Right, }); } -- cgit From cf9d8e01048845ee503a62eb55e634a76a0e9163 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 11 Apr 2023 05:54:51 +0200 Subject: Rename `IconPosition` to `Side` in `text_input` --- examples/tour/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 8a1b8d5a..40ab33c2 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -585,7 +585,7 @@ impl<'a> Step { code_point: '\u{E900}', size: Some(35.0), spacing: 10.0, - position: text_input::IconPosition::Right, + side: text_input::Side::Right, }); } -- cgit From c794d8ba788b388d4fb7a8ef1bba208b98e1bd02 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 11 Apr 2023 05:56:34 +0200 Subject: Collapse `Font` import in `tour` example --- examples/tour/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 40ab33c2..9ee386d4 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -5,8 +5,7 @@ use iced::widget::{ scrollable, slider, text, text_input, toggler, vertical_space, }; use iced::widget::{Button, Column, Container, Slider}; -use iced::Font; -use iced::{Color, Element, Length, Renderer, Sandbox, Settings}; +use iced::{Color, Element, Font, Length, Renderer, Sandbox, Settings}; const ICON_FONT: Font = Font::External { name: "Icons", -- cgit From aa0be30656e30d116e60a5d06557aea27442ce76 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 11 Apr 2023 05:57:01 +0200 Subject: Move `ICON_FONT` constant in `tour` to `text_input` helper --- examples/tour/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 9ee386d4..e9b9fb3a 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -7,11 +7,6 @@ use iced::widget::{ use iced::widget::{Button, Column, Container, Slider}; use iced::{Color, Element, Font, Length, Renderer, Sandbox, Settings}; -const ICON_FONT: Font = Font::External { - name: "Icons", - bytes: include_bytes!("../fonts/icons.ttf"), -}; - pub fn main() -> iced::Result { env_logger::init(); @@ -570,6 +565,11 @@ impl<'a> Step { is_secure: bool, is_showing_icon: bool, ) -> Column<'a, StepMessage> { + const ICON_FONT: Font = Font::External { + name: "Icons", + bytes: include_bytes!("../fonts/icons.ttf"), + }; + let mut text_input = text_input( "Type something to continue...", value, -- cgit From 45015e37d4123f01b546337e741820975fccf66a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 11 Apr 2023 05:57:31 +0200 Subject: Fix `current` step in `tour` --- examples/tour/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index e9b9fb3a..90868877 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -132,7 +132,7 @@ impl Steps { Step::Debugger, Step::End, ], - current: 8, + current: 0, } } -- cgit