feat: added handle to text_input
This commit is contained in:
parent
931b30dc5a
commit
7b36984295
8 changed files with 236 additions and 2 deletions
9
examples/text_input/Cargo.toml
Normal file
9
examples/text_input/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "text_input"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Casper Rogild Storm<casper@rogildstorm.com>"]
|
||||||
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
iced = { path = "../..", features = ["debug"] }
|
||||||
10
examples/text_input/README.md
Normal file
10
examples/text_input/README.md
Normal file
|
|
@ -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
|
||||||
BIN
examples/text_input/fonts/icons.ttf
Normal file
BIN
examples/text_input/fonts/icons.ttf
Normal file
Binary file not shown.
93
examples/text_input/src/main.rs
Normal file
93
examples/text_input/src/main.rs
Normal file
|
|
@ -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<Message> {
|
||||||
|
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,
|
||||||
|
{
|
||||||
|
<Self as iced::Application>::run(settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -31,6 +31,47 @@ use crate::{
|
||||||
|
|
||||||
pub use iced_style::text_input::{Appearance, StyleSheet};
|
pub use iced_style::text_input::{Appearance, StyleSheet};
|
||||||
|
|
||||||
|
/// The position of the [`Handle`].
|
||||||
|
#[derive(Clone, Default, Debug)]
|
||||||
|
pub enum HandlePosition {
|
||||||
|
/// Position the handle to the left.
|
||||||
|
Left,
|
||||||
|
/// Position the handle to the left.
|
||||||
|
///
|
||||||
|
/// This is the default.
|
||||||
|
#[default]
|
||||||
|
Right,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The content of the [`Handle`].
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Handle<Renderer>
|
||||||
|
where
|
||||||
|
Renderer: text::Renderer,
|
||||||
|
{
|
||||||
|
/// Font that will be used to display the `text`.
|
||||||
|
pub font: Renderer::Font,
|
||||||
|
/// Text that will be shown.
|
||||||
|
pub text: String,
|
||||||
|
/// Font size of the content.
|
||||||
|
pub size: Option<u16>,
|
||||||
|
/// Position of the handle.
|
||||||
|
pub position: HandlePosition,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Renderer> std::fmt::Debug for Handle<Renderer>
|
||||||
|
where
|
||||||
|
Renderer: text::Renderer,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("Handle")
|
||||||
|
.field("text", &self.text)
|
||||||
|
.field("size", &self.size)
|
||||||
|
.field("position", &self.position)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A field that can be filled with text.
|
/// A field that can be filled with text.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
|
@ -68,6 +109,7 @@ where
|
||||||
on_change: Box<dyn Fn(String) -> Message + 'a>,
|
on_change: Box<dyn Fn(String) -> Message + 'a>,
|
||||||
on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
|
on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
|
||||||
on_submit: Option<Message>,
|
on_submit: Option<Message>,
|
||||||
|
handle: Option<Handle<Renderer>>,
|
||||||
style: <Renderer::Theme as StyleSheet>::Style,
|
style: <Renderer::Theme as StyleSheet>::Style,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,6 +141,7 @@ where
|
||||||
on_change: Box::new(on_change),
|
on_change: Box::new(on_change),
|
||||||
on_paste: None,
|
on_paste: None,
|
||||||
on_submit: None,
|
on_submit: None,
|
||||||
|
handle: None,
|
||||||
style: Default::default(),
|
style: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -132,6 +175,13 @@ where
|
||||||
self.font = font;
|
self.font = font;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the [`Handle`] of the [`TextInput`].
|
||||||
|
pub fn handle(mut self, handle: Handle<Renderer>) -> Self {
|
||||||
|
self.handle = Some(handle);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the width of the [`TextInput`].
|
/// Sets the width of the [`TextInput`].
|
||||||
pub fn width(mut self, width: impl Into<Length>) -> Self {
|
pub fn width(mut self, width: impl Into<Length>) -> Self {
|
||||||
self.width = width.into();
|
self.width = width.into();
|
||||||
|
|
@ -188,8 +238,10 @@ where
|
||||||
value.unwrap_or(&self.value),
|
value.unwrap_or(&self.value),
|
||||||
&self.placeholder,
|
&self.placeholder,
|
||||||
self.size,
|
self.size,
|
||||||
|
self.padding,
|
||||||
&self.font,
|
&self.font,
|
||||||
self.is_secure,
|
self.is_secure,
|
||||||
|
self.handle.as_ref(),
|
||||||
&self.style,
|
&self.style,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -286,8 +338,10 @@ where
|
||||||
&self.value,
|
&self.value,
|
||||||
&self.placeholder,
|
&self.placeholder,
|
||||||
self.size,
|
self.size,
|
||||||
|
self.padding,
|
||||||
&self.font,
|
&self.font,
|
||||||
self.is_secure,
|
self.is_secure,
|
||||||
|
self.handle.as_ref(),
|
||||||
&self.style,
|
&self.style,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -812,8 +866,10 @@ pub fn draw<Renderer>(
|
||||||
value: &Value,
|
value: &Value,
|
||||||
placeholder: &str,
|
placeholder: &str,
|
||||||
size: Option<f32>,
|
size: Option<f32>,
|
||||||
|
padding: Padding,
|
||||||
font: &Renderer::Font,
|
font: &Renderer::Font,
|
||||||
is_secure: bool,
|
is_secure: bool,
|
||||||
|
handle: Option<&Handle<Renderer>>,
|
||||||
style: &<Renderer::Theme as StyleSheet>::Style,
|
style: &<Renderer::Theme as StyleSheet>::Style,
|
||||||
) where
|
) where
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
|
|
@ -823,7 +879,37 @@ pub fn draw<Renderer>(
|
||||||
let value = secure_value.as_ref().unwrap_or(value);
|
let value = secure_value.as_ref().unwrap_or(value);
|
||||||
|
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
let text_bounds = layout.children().next().unwrap().bounds();
|
let text_bounds = {
|
||||||
|
let bounds = layout.children().next().unwrap().bounds();
|
||||||
|
if let Some(handle) = handle {
|
||||||
|
let Handle {
|
||||||
|
font,
|
||||||
|
size,
|
||||||
|
text,
|
||||||
|
position,
|
||||||
|
} = handle;
|
||||||
|
|
||||||
|
let padding = f32::from(padding.horizontal());
|
||||||
|
let size = size.unwrap_or_else(|| renderer.default_size());
|
||||||
|
let width =
|
||||||
|
renderer.measure_width(text.as_str(), size, font.clone());
|
||||||
|
|
||||||
|
match position {
|
||||||
|
HandlePosition::Left => Rectangle {
|
||||||
|
x: bounds.x + (width + padding),
|
||||||
|
width: bounds.width - (width + padding),
|
||||||
|
..bounds
|
||||||
|
},
|
||||||
|
HandlePosition::Right => Rectangle {
|
||||||
|
x: bounds.x,
|
||||||
|
width: bounds.width - (width + padding),
|
||||||
|
..bounds
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bounds
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let is_mouse_over = bounds.contains(cursor_position);
|
let is_mouse_over = bounds.contains(cursor_position);
|
||||||
|
|
||||||
|
|
@ -845,6 +931,37 @@ pub fn draw<Renderer>(
|
||||||
appearance.background,
|
appearance.background,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let Some(handle) = handle {
|
||||||
|
let Handle {
|
||||||
|
size,
|
||||||
|
font,
|
||||||
|
text,
|
||||||
|
position,
|
||||||
|
} = handle;
|
||||||
|
|
||||||
|
let padding = f32::from(padding.horizontal());
|
||||||
|
let size = size.unwrap_or_else(|| renderer.default_size());
|
||||||
|
let width = renderer.measure_width(text.as_str(), size, font.clone());
|
||||||
|
|
||||||
|
renderer.fill_text(Text {
|
||||||
|
content: text,
|
||||||
|
size: f32::from(size),
|
||||||
|
font: font.clone(),
|
||||||
|
color: appearance.handle_color,
|
||||||
|
bounds: Rectangle {
|
||||||
|
x: match position {
|
||||||
|
HandlePosition::Left => bounds.x + width + padding,
|
||||||
|
HandlePosition::Right => bounds.x + bounds.width - padding,
|
||||||
|
},
|
||||||
|
y: bounds.center_y() - f32::from(size) / 2.0,
|
||||||
|
height: f32::from(size),
|
||||||
|
..bounds
|
||||||
|
},
|
||||||
|
horizontal_alignment: alignment::Horizontal::Right,
|
||||||
|
vertical_alignment: alignment::Vertical::Top,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let text = value.to_string();
|
let text = value.to_string();
|
||||||
let size = size.unwrap_or_else(|| renderer.default_size());
|
let size = size.unwrap_or_else(|| renderer.default_size());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ pub mod text_input {
|
||||||
//! Display fields that can be filled with text.
|
//! Display fields that can be filled with text.
|
||||||
pub use iced_native::widget::text_input::{
|
pub use iced_native::widget::text_input::{
|
||||||
focus, move_cursor_to, move_cursor_to_end, move_cursor_to_front,
|
focus, move_cursor_to, move_cursor_to_end, move_cursor_to_front,
|
||||||
select_all, Appearance, Id, StyleSheet,
|
select_all, Appearance, Handle, HandlePosition, Id, StyleSheet,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A field that can be filled with text.
|
/// A field that can be filled with text.
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ pub struct Appearance {
|
||||||
pub border_width: f32,
|
pub border_width: f32,
|
||||||
/// The border [`Color`] of the text input.
|
/// The border [`Color`] of the text input.
|
||||||
pub border_color: Color,
|
pub border_color: Color,
|
||||||
|
/// The handle [`Color`] of the text input.
|
||||||
|
pub handle_color: Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A set of rules that dictate the style of a text input.
|
/// A set of rules that dictate the style of a text input.
|
||||||
|
|
|
||||||
|
|
@ -1028,6 +1028,7 @@ impl text_input::StyleSheet for Theme {
|
||||||
border_radius: 2.0,
|
border_radius: 2.0,
|
||||||
border_width: 1.0,
|
border_width: 1.0,
|
||||||
border_color: palette.background.strong.color,
|
border_color: palette.background.strong.color,
|
||||||
|
handle_color: palette.background.weak.text,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1043,6 +1044,7 @@ impl text_input::StyleSheet for Theme {
|
||||||
border_radius: 2.0,
|
border_radius: 2.0,
|
||||||
border_width: 1.0,
|
border_width: 1.0,
|
||||||
border_color: palette.background.base.text,
|
border_color: palette.background.base.text,
|
||||||
|
handle_color: palette.background.weak.text,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1058,6 +1060,7 @@ impl text_input::StyleSheet for Theme {
|
||||||
border_radius: 2.0,
|
border_radius: 2.0,
|
||||||
border_width: 1.0,
|
border_width: 1.0,
|
||||||
border_color: palette.primary.strong.color,
|
border_color: palette.primary.strong.color,
|
||||||
|
handle_color: palette.primary.weak.text,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue