summaryrefslogtreecommitdiffstats
path: root/native/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-01-01 18:26:49 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-01-01 18:26:49 +0100
commit5af4159848341b14f6ff9ae14a9a222d8d8b0fb8 (patch)
tree436220784428b33b8e378403d11496db2d87884b /native/src
parentd96ced8e2da703117a43399110ef2b8fa21a7546 (diff)
downloadiced-5af4159848341b14f6ff9ae14a9a222d8d8b0fb8.tar.gz
iced-5af4159848341b14f6ff9ae14a9a222d8d8b0fb8.tar.bz2
iced-5af4159848341b14f6ff9ae14a9a222d8d8b0fb8.zip
Draft basic styling for `TextInput`
Diffstat (limited to 'native/src')
-rw-r--r--native/src/renderer/null.rs3
-rw-r--r--native/src/widget/container.rs2
-rw-r--r--native/src/widget/text_input.rs33
3 files changed, 29 insertions, 9 deletions
diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs
index 56d7e472..96aa132c 100644
--- a/native/src/renderer/null.rs
+++ b/native/src/renderer/null.rs
@@ -97,6 +97,8 @@ impl scrollable::Renderer for Null {
}
impl text_input::Renderer for Null {
+ type Style = ();
+
fn default_size(&self) -> u16 {
20
}
@@ -124,6 +126,7 @@ impl text_input::Renderer for Null {
_placeholder: &str,
_value: &text_input::Value,
_state: &text_input::State,
+ _style: &Self::Style,
) -> Self::Output {
}
}
diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs
index 75c2d6b3..abe83264 100644
--- a/native/src/widget/container.rs
+++ b/native/src/widget/container.rs
@@ -94,7 +94,7 @@ where
self
}
- /// Sets the style the [`Container`].
+ /// Sets the style of the [`Container`].
///
/// [`Container`]: struct.Container.html
pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self {
diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs
index e2114f00..9952a9bf 100644
--- a/native/src/widget/text_input.rs
+++ b/native/src/widget/text_input.rs
@@ -15,8 +15,9 @@ use unicode_segmentation::UnicodeSegmentation;
///
/// # Example
/// ```
-/// # use iced_native::{text_input, TextInput};
+/// # use iced_native::{text_input, renderer::Null};
/// #
+/// # pub type TextInput<'a, Message> = iced_native::TextInput<'a, Message, Null>;
/// #[derive(Debug, Clone)]
/// enum Message {
/// TextInputChanged(String),
@@ -35,7 +36,7 @@ use unicode_segmentation::UnicodeSegmentation;
/// ```
/// ![Text input drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text_input.png?raw=true)
#[allow(missing_debug_implementations)]
-pub struct TextInput<'a, Message> {
+pub struct TextInput<'a, Message, Renderer: self::Renderer> {
state: &'a mut State,
placeholder: String,
value: Value,
@@ -46,9 +47,10 @@ pub struct TextInput<'a, Message> {
size: Option<u16>,
on_change: Box<dyn Fn(String) -> Message>,
on_submit: Option<Message>,
+ style: Renderer::Style,
}
-impl<'a, Message> TextInput<'a, Message> {
+impl<'a, Message, Renderer: self::Renderer> TextInput<'a, Message, Renderer> {
/// Creates a new [`TextInput`].
///
/// It expects:
@@ -79,6 +81,7 @@ impl<'a, Message> TextInput<'a, Message> {
size: None,
on_change: Box::new(on_change),
on_submit: None,
+ style: Renderer::Style::default(),
}
}
@@ -130,11 +133,20 @@ impl<'a, Message> TextInput<'a, Message> {
self.on_submit = Some(message);
self
}
+
+ /// Sets the style of the [`TextInput`].
+ ///
+ /// [`TextInput`]: struct.TextInput.html
+ pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self {
+ self.style = style.into();
+ self
+ }
}
-impl<'a, Message, Renderer> Widget<Message, Renderer> for TextInput<'a, Message>
+impl<'a, Message, Renderer> Widget<Message, Renderer>
+ for TextInput<'a, Message, Renderer>
where
- Renderer: self::Renderer,
+ Renderer: 'static + self::Renderer,
Message: Clone + std::fmt::Debug,
{
fn width(&self) -> Length {
@@ -359,6 +371,7 @@ where
&self.placeholder,
&self.value.secure(),
&self.state,
+ &self.style,
)
} else {
renderer.draw(
@@ -369,6 +382,7 @@ where
&self.placeholder,
&self.value,
&self.state,
+ &self.style,
)
}
}
@@ -376,7 +390,7 @@ where
fn hash_layout(&self, state: &mut Hasher) {
use std::{any::TypeId, hash::Hash};
- TypeId::of::<TextInput<'static, ()>>().hash(state);
+ TypeId::of::<TextInput<'static, (), Renderer>>().hash(state);
self.width.hash(state);
self.max_width.hash(state);
@@ -393,6 +407,8 @@ where
/// [`TextInput`]: struct.TextInput.html
/// [renderer]: ../../renderer/index.html
pub trait Renderer: crate::Renderer + Sized {
+ type Style: Default;
+
/// Returns the default size of the text of the [`TextInput`].
///
/// [`TextInput`]: struct.TextInput.html
@@ -441,17 +457,18 @@ pub trait Renderer: crate::Renderer + Sized {
placeholder: &str,
value: &Value,
state: &State,
+ style: &Self::Style,
) -> Self::Output;
}
-impl<'a, Message, Renderer> From<TextInput<'a, Message>>
+impl<'a, Message, Renderer> From<TextInput<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: 'static + self::Renderer,
Message: 'static + Clone + std::fmt::Debug,
{
fn from(
- text_input: TextInput<'a, Message>,
+ text_input: TextInput<'a, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(text_input)
}