Draft basic styling for TextInput

This commit is contained in:
Héctor Ramón Jiménez 2020-01-01 18:26:49 +01:00
parent d96ced8e2d
commit 5af4159848
10 changed files with 143 additions and 36 deletions

View file

@ -1,4 +1,4 @@
use crate::{Primitive, Renderer};
use crate::{text_input::StyleSheet, Primitive, Renderer};
use iced_native::{
text_input, Background, Color, Font, HorizontalAlignment, MouseCursor,
@ -7,6 +7,8 @@ use iced_native::{
use std::f32;
impl text_input::Renderer for Renderer {
type Style = Box<dyn StyleSheet>;
fn default_size(&self) -> u16 {
// TODO: Make this configurable
20
@ -61,20 +63,24 @@ impl text_input::Renderer for Renderer {
placeholder: &str,
value: &text_input::Value,
state: &text_input::State,
style_sheet: &Self::Style,
) -> Self::Output {
let is_mouse_over = bounds.contains(cursor_position);
let style = if state.is_focused() {
style_sheet.focused()
} else if is_mouse_over {
style_sheet.hovered()
} else {
style_sheet.active()
};
let input = Primitive::Quad {
bounds,
background: Background::Color(Color::WHITE),
border_radius: 5,
border_width: 1,
border_color: if is_mouse_over || state.is_focused() {
[0.5, 0.5, 0.5]
} else {
[0.7, 0.7, 0.7]
}
.into(),
background: style.background,
border_radius: style.border_radius,
border_width: style.border_width,
border_color: style.border_color,
};
let text = value.to_string();
@ -86,9 +92,9 @@ impl text_input::Renderer for Renderer {
text.clone()
},
color: if text.is_empty() {
[0.7, 0.7, 0.7]
style_sheet.placeholder_color()
} else {
[0.3, 0.3, 0.3]
style_sheet.value_color()
}
.into(),
font: Font::Default,
@ -117,7 +123,7 @@ impl text_input::Renderer for Renderer {
width: 1.0,
height: text_bounds.height,
},
background: Background::Color(Color::BLACK),
background: Background::Color(style_sheet.value_color()),
border_radius: 0,
border_width: 0,
border_color: Color::TRANSPARENT,

View file

@ -1,2 +1,3 @@
pub mod button;
pub mod container;
pub mod text_input;

View file

@ -0,0 +1,15 @@
//! Display fields that can be filled with text.
//!
//! A [`TextInput`] has some local [`State`].
//!
//! [`TextInput`]: struct.TextInput.html
//! [`State`]: struct.State.html
use crate::Renderer;
pub use iced_native::text_input::State;
pub use iced_style::text_input::{Style, StyleSheet};
/// A field that can be filled with text.
///
/// This is an alias of an `iced_native` text input with an `iced_wgpu::Renderer`.
pub type TextInput<'a, Message> = iced_native::TextInput<'a, Message, Renderer>;