Changed Handle to Icon to be consistent

This commit is contained in:
Casper Storm 2023-02-16 14:32:59 +01:00 committed by Héctor Ramón Jiménez
parent bfc5db9009
commit d24a4a4689
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
5 changed files with 59 additions and 58 deletions

View file

@ -13,13 +13,13 @@ pub fn main() -> iced::Result {
#[derive(Default)] #[derive(Default)]
struct Example { struct Example {
value: String, value: String,
is_showing_handle: bool, is_showing_icon: bool,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Message { enum Message {
Changed(String), Changed(String),
ToggleHandle(bool), ToggleIcon(bool),
} }
impl Sandbox for Example { impl Sandbox for Example {
@ -36,27 +36,27 @@ impl Sandbox for Example {
fn update(&mut self, message: Message) { fn update(&mut self, message: Message) {
match message { match message {
Message::Changed(value) => self.value = value, Message::Changed(value) => self.value = value,
Message::ToggleHandle(_) => { Message::ToggleIcon(_) => {
self.is_showing_handle = !self.is_showing_handle self.is_showing_icon = !self.is_showing_icon
} }
} }
} }
fn view(&self) -> Element<Message> { fn view(&self) -> Element<Message> {
let checkbox = let checkbox =
checkbox("Handle", self.is_showing_handle, Message::ToggleHandle) checkbox("Icon", self.is_showing_icon, Message::ToggleIcon)
.spacing(5) .spacing(5)
.text_size(16); .text_size(16);
let mut text_input = let mut text_input =
text_input("Placeholder", self.value.as_str(), Message::Changed); text_input("Placeholder", self.value.as_str(), Message::Changed);
if self.is_showing_handle { if self.is_showing_icon {
text_input = text_input.handle(text_input::Handle { text_input = text_input.icon(text_input::Icon {
font: ICON_FONT, font: ICON_FONT,
text: String::from('\u{e900}'), code_point: '\u{e900}',
size: Some(18), size: Some(18),
position: text_input::HandlePosition::Right, position: text_input::IconPosition::Right,
}); });
} }

View file

@ -31,41 +31,38 @@ use crate::{
pub use iced_style::text_input::{Appearance, StyleSheet}; pub use iced_style::text_input::{Appearance, StyleSheet};
/// The position of the [`Handle`]. /// The position of the [`Icon`].
#[derive(Clone, Default, Debug)] #[derive(Clone, Default, Debug)]
pub enum HandlePosition { pub enum IconPosition {
/// Position the handle to the left. /// Position the [`Icon`] to the left.
Left, Left,
/// Position the handle to the left. /// Position the [`Icon`] to the left.
/// ///
/// This is the default. /// This is the default.
#[default] #[default]
Right, Right,
} }
/// The content of the [`Handle`]. /// The content of the [`Icon`].
#[derive(Clone)] #[derive(Clone)]
pub struct Handle<Renderer> pub struct Icon<Font> {
where /// Font that will be used to display the `code_point`.
Renderer: text::Renderer, pub font: Font,
{ /// The unicode code point that will be used as the icon.
/// Font that will be used to display the `text`. pub code_point: char,
pub font: Renderer::Font,
/// Text that will be shown.
pub text: String,
/// Font size of the content. /// Font size of the content.
pub size: Option<u16>, pub size: Option<u16>,
/// Position of the handle. /// Position of the icon.
pub position: HandlePosition, pub position: IconPosition,
} }
impl<Renderer> std::fmt::Debug for Handle<Renderer> impl<Renderer> std::fmt::Debug for Icon<Renderer>
where where
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Handle") f.debug_struct("Icon")
.field("text", &self.text) .field("code_point", &self.code_point)
.field("size", &self.size) .field("size", &self.size)
.field("position", &self.position) .field("position", &self.position)
.finish() .finish()
@ -109,7 +106,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>>, icon: Option<Icon<Renderer::Font>>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -141,7 +138,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, icon: None,
style: Default::default(), style: Default::default(),
} }
} }
@ -176,9 +173,9 @@ where
self self
} }
/// Sets the [`Handle`] of the [`TextInput`]. /// Sets the [`Icon`] of the [`TextInput`].
pub fn handle(mut self, handle: Handle<Renderer>) -> Self { pub fn icon(mut self, icon: Icon<Renderer::Font>) -> Self {
self.handle = Some(handle); self.icon = Some(icon);
self self
} }
@ -241,7 +238,7 @@ where
self.padding, self.padding,
&self.font, &self.font,
self.is_secure, self.is_secure,
self.handle.as_ref(), self.icon.as_ref(),
&self.style, &self.style,
) )
} }
@ -341,7 +338,7 @@ where
self.padding, self.padding,
&self.font, &self.font,
self.is_secure, self.is_secure,
self.handle.as_ref(), self.icon.as_ref(),
&self.style, &self.style,
) )
} }
@ -869,7 +866,7 @@ pub fn draw<Renderer>(
padding: Padding, padding: Padding,
font: &Renderer::Font, font: &Renderer::Font,
is_secure: bool, is_secure: bool,
handle: Option<&Handle<Renderer>>, icon: Option<&Icon<Renderer::Font>>,
style: &<Renderer::Theme as StyleSheet>::Style, style: &<Renderer::Theme as StyleSheet>::Style,
) where ) where
Renderer: text::Renderer, Renderer: text::Renderer,
@ -881,26 +878,29 @@ pub fn draw<Renderer>(
let bounds = layout.bounds(); let bounds = layout.bounds();
let text_bounds = { let text_bounds = {
let bounds = layout.children().next().unwrap().bounds(); let bounds = layout.children().next().unwrap().bounds();
if let Some(handle) = handle { if let Some(icon) = icon {
let Handle { let Icon {
font, font,
size, size,
text, code_point,
position, position,
} = handle; } = icon;
let padding = f32::from(padding.horizontal()); let padding = f32::from(padding.horizontal());
let size = size.unwrap_or_else(|| renderer.default_size()); let size = size.unwrap_or_else(|| renderer.default_size());
let width = let width = renderer.measure_width(
renderer.measure_width(text.as_str(), size, font.clone()); &code_point.to_string(),
size,
font.clone(),
);
match position { match position {
HandlePosition::Left => Rectangle { IconPosition::Left => Rectangle {
x: bounds.x + (width + padding), x: bounds.x + (width + padding),
width: bounds.width - (width + padding), width: bounds.width - (width + padding),
..bounds ..bounds
}, },
HandlePosition::Right => Rectangle { IconPosition::Right => Rectangle {
x: bounds.x, x: bounds.x,
width: bounds.width - (width + padding), width: bounds.width - (width + padding),
..bounds ..bounds
@ -931,27 +931,28 @@ pub fn draw<Renderer>(
appearance.background, appearance.background,
); );
if let Some(handle) = handle { if let Some(icon) = icon {
let Handle { let Icon {
size, size,
font, font,
text, code_point,
position, position,
} = handle; } = icon;
let padding = f32::from(padding.horizontal()); let padding = f32::from(padding.horizontal());
let size = size.unwrap_or_else(|| renderer.default_size()); let size = size.unwrap_or_else(|| renderer.default_size());
let width = renderer.measure_width(text.as_str(), size, font.clone()); let width =
renderer.measure_width(&code_point.to_string(), size, font.clone());
renderer.fill_text(Text { renderer.fill_text(Text {
content: text, content: &code_point.to_string(),
size: f32::from(size), size: f32::from(size),
font: font.clone(), font: font.clone(),
color: appearance.handle_color, color: appearance.icon_color,
bounds: Rectangle { bounds: Rectangle {
x: match position { x: match position {
HandlePosition::Left => bounds.x + width + padding, IconPosition::Left => bounds.x + width + padding,
HandlePosition::Right => bounds.x + bounds.width - padding, IconPosition::Right => bounds.x + bounds.width - padding,
}, },
y: bounds.center_y() - f32::from(size) / 2.0, y: bounds.center_y() - f32::from(size) / 2.0,
height: f32::from(size), height: f32::from(size),

View file

@ -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, Handle, HandlePosition, Id, StyleSheet, select_all, Appearance, Icon, IconPosition, Id, StyleSheet,
}; };
/// A field that can be filled with text. /// A field that can be filled with text.

View file

@ -12,8 +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. /// The icon [`Color`] of the text input.
pub handle_color: Color, pub icon_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.

View file

@ -1028,7 +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, icon_color: palette.background.weak.text,
} }
} }
@ -1044,7 +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, icon_color: palette.background.weak.text,
} }
} }
@ -1060,7 +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.background.weak.text, icon_color: palette.background.weak.text,
} }
} }