Skip to content

Commit

Permalink
Making typedInput transparent (behaving the same way than the textInp…
Browse files Browse the repository at this point in the history
…ut beneath)
  • Loading branch information
Ultraxime committed Jul 19, 2024
1 parent e8b325c commit 000d93e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 87 deletions.
4 changes: 2 additions & 2 deletions examples/typed_input/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ impl TypedInputDemo {

fn view(&self) -> Element<Message> {
let lb_minute = Text::new("Typed Input:");
let txt_minute = typed_input::TypedInput::new(self.value, Message::TypedInpChanged);
let txt_minute = typed_input::TypedInput::new(&self.value, Message::TypedInpChanged);

Container::new(
Row::new()
.spacing(10)
.align_items(Alignment::Center)
.align_y(Alignment::Center)
.push(lb_minute)
.push(txt_minute),
)
Expand Down
40 changes: 16 additions & 24 deletions src/widgets/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ use std::{
};

use crate::style::{self, Status};
use crate::widgets::typed_input::TypedInput;
pub use crate::{
core::icons::{bootstrap::icon_to_string, Bootstrap, BOOTSTRAP_FONT},
style::{
number_input::{self, Catalog, Style},
StyleFn,
},
};
use crate::widgets::typed_input::TypedInput;

/// The default padding
const DEFAULT_PADDING: f32 = 5.0;
Expand Down Expand Up @@ -125,7 +125,7 @@ where
max: Self::set_max(bounds.end_bound()),
padding,
size: None,
content: TypedInput::new(value, on_changed)
content: TypedInput::new(&value, on_changed)
.padding(padding)
.width(Length::Fixed(127.0))
.class(Theme::default_input()),
Expand Down Expand Up @@ -453,14 +453,11 @@ where

let child = state.children.get_mut(0).expect("fail to get child");
let text_input = child
.children
.get_mut(0)
.expect("fail to get text input")
.state
.downcast_mut::<text_input::State<Renderer::Paragraph>>();
let modifiers = state.state.downcast_mut::<ModifierState>();

let current_text = self.content.text().to_string();
let current_text = self.content.text().to_owned();

let mut forward_to_text = |event, shell, child, clipboard| {
self.content.on_event(
Expand Down Expand Up @@ -492,9 +489,7 @@ where
}
let mut new_val = current_text;
match text_input.cursor().state(&Value::new(&new_val)) {
cursor::State::Index(idx)
if idx >= 1 && idx <= new_val.len() =>
{
cursor::State::Index(idx) if idx >= 1 && idx <= new_val.len() => {
_ = new_val.remove(idx - 1);
}
cursor::State::Selection { start, end }
Expand All @@ -511,17 +506,14 @@ where

match T::from_str(&new_val) {
Ok(val)
if (self.min..self.max).contains(&val)
&& val != self.value =>
if (self.min..self.max).contains(&val) && val != self.value =>
{
self.value = val;
forward_to_text(event, shell, child, clipboard)
}
Ok(val)
if (self.min..self.max).contains(&val) =>
{
forward_to_text(event, shell, child, clipboard)
}
Ok(val) if (self.min..self.max).contains(&val) => {
forward_to_text(event, shell, child, clipboard)
}
Ok(_) => event::Status::Captured,
_ => event::Status::Ignored,
}
Expand Down Expand Up @@ -560,11 +552,10 @@ where
self.value = val;
forward_to_text(event, shell, child, clipboard)
}
Ok(val)
if (self.min..self.max).contains(&val) =>
forward_to_text(event, shell, child, clipboard),
Ok(_) =>
event::Status::Captured,
Ok(val) if (self.min..self.max).contains(&val) => {
forward_to_text(event, shell, child, clipboard)
}
Ok(_) => event::Status::Captured,
_ => event::Status::Ignored,
}
}
Expand All @@ -579,9 +570,10 @@ where
event::Status::Captured
}
keyboard::Key::Named(
keyboard::key::Named::ArrowLeft | keyboard::key::Named::ArrowRight |
keyboard::key::Named::Home |
keyboard::key::Named::End,
keyboard::key::Named::ArrowLeft
| keyboard::key::Named::ArrowRight
| keyboard::key::Named::Home
| keyboard::key::Named::End,
) => forward_to_text(event, shell, child, clipboard),
_ => event::Status::Ignored,
},
Expand Down
133 changes: 72 additions & 61 deletions src/widgets/typed_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
//!
//! *This API requires the following crate features to be activated: `typed_input`*

use iced::mouse::{self, Cursor};
use iced::{widget::text_input::{self, TextInput}, Size, Event, event};
use iced::advanced::layout::{Node, Limits, Layout};
use iced::advanced::widget::{Tree, Widget, tree::{State, Tag}, Operation};
use iced::advanced::layout::{Layout, Limits, Node};
use iced::advanced::widget::{
tree::{State, Tag},
Operation, Tree, Widget,
};
use iced::advanced::{Clipboard, Shell};
use iced::{Length, Rectangle, Element};

use std::{
fmt::Display,
str::FromStr,
use iced::mouse::{self, Cursor};
use iced::{
event,
widget::text_input::{self, TextInput},
Event, Size,
};
use iced::{Element, Length, Rectangle};

use std::{fmt::Display, str::FromStr};

/// The default padding
const DEFAULT_PADDING: f32 = 5.0;
Expand Down Expand Up @@ -72,26 +76,24 @@ where
/// It expects:
/// - the current value
/// - a function that produces a message when the [`TypedInput`] changes
pub fn new<F>(value: T, on_changed: F) -> Self
pub fn new<F>(value: &T, on_changed: F) -> Self
where
F: 'static + Fn(T) -> Message + Copy,
T: 'a + Clone,
{
let padding = DEFAULT_PADDING;
// let move_value = value.clone();
// let convert_to_t = move |s: String| on_changed(T::from_str(&s).unwrap_or(move_value.clone()));

Self {
value: value.clone(),
Self {
value: value.clone(),
text_input: text_input::TextInput::new("", format!("{value}").as_str())
.on_input(InternalMessage::OnChange)
.on_submit(InternalMessage::OnSubmit)
.padding(padding)
.width(Length::Fixed(127.0))
.class(<Theme as text_input::Catalog>::default()),
.class(<Theme as text_input::Catalog>::default()),
text: value.to_string(),
on_change: Box::new(on_changed),
on_submit: None,
on_submit: None,
font: Renderer::Font::default(),
}
}
Expand Down Expand Up @@ -157,11 +159,7 @@ where

/// Sets the class of the input of the [`TypedInput`].
#[must_use]
pub fn class(
mut self,
class: impl Into<<Theme as text_input::Catalog>::Class<'a>>,
) -> Self
{
pub fn class(mut self, class: impl Into<<Theme as text_input::Catalog>::Class<'a>>) -> Self {
self.text_input = self.text_input.class(class);
self
}
Expand All @@ -176,49 +174,48 @@ where
Theme: text_input::Catalog,
{
fn tag(&self) -> Tag {
Tag::of::<()>()
<TextInput<_, _, _> as Widget<_, _, _>>::tag(&self.text_input)
}
fn state(&self) -> State {
State::new(())
<TextInput<_, _, _> as Widget<_, _, _>>::state(&self.text_input)
}

fn children(&self) -> Vec<Tree> {
vec![Tree {
tag: self.text_input.tag(),
state: self.text_input.state(),
children: self.text_input.children(),
}]
<TextInput<_, _, _> as Widget<_, _, _>>::children(&self.text_input)
}

fn diff(&self, tree: &mut Tree) {
tree.diff_children_custom(
&[&self.text_input],
|state, content| content.diff(state),
|&content| Tree {
tag: content.tag(),
state: content.state(),
children: content.children(),
},
);
fn diff(&self, state: &mut Tree) {
<TextInput<_, _, _> as Widget<_, _, _>>::diff(&self.text_input, state);
}

fn size(&self) -> Size<Length> {
<TextInput<_, _, _> as Widget<_, _, _>>::size(&self.text_input)
}

fn layout(&self, tree: &mut Tree,renderer: &Renderer, limits: &Limits) -> Node {
let content = <TextInput<_, _, _> as Widget<_, _, _>>::layout(&self.text_input, &mut tree.children[0], renderer, limits);
let size = limits.resolve(Length::Shrink, Length::Shrink, content.size());
Node::with_children(
size,
vec![ content ]
)
fn layout(&self, state: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node {
<TextInput<_, _, _> as Widget<_, _, _>>::layout(&self.text_input, state, renderer, limits)
}

fn draw(&self, tree: &Tree, renderer: &mut Renderer, theme: &Theme, style: &iced::advanced::renderer::Style, layout: Layout<'_>, cursor: Cursor, viewport: &Rectangle) {
let mut children = layout.children();
let text_input_layout = children.next().expect("fail to get TextInput layout");
<TextInput<_, _, _> as Widget<_, _, _>>::draw(&self.text_input, &tree.children[0], renderer, theme, style, text_input_layout, cursor, viewport);
fn draw(
&self,
state: &Tree,
renderer: &mut Renderer,
theme: &Theme,
style: &iced::advanced::renderer::Style,
layout: Layout<'_>,
cursor: Cursor,
viewport: &Rectangle,
) {
<TextInput<_, _, _> as Widget<_, _, _>>::draw(
&self.text_input,
state,
renderer,
theme,
style,
layout,
cursor,
viewport,
);
}

fn mouse_interaction(
Expand All @@ -229,17 +226,30 @@ where
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
<TextInput<_, _, _> as Widget<_, _, _>>::mouse_interaction(&self.text_input, &state.children[0], layout.children().next().expect("TypedInput inner child Textbox was not created."), cursor, viewport, renderer)
<TextInput<_, _, _> as Widget<_, _, _>>::mouse_interaction(
&self.text_input,
state,
layout,
cursor,
viewport,
renderer,
)
}

fn operate(
&self,
tree: &mut Tree,
state: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
) {
<TextInput<_, _, _> as Widget<_, _, _>>::operate(&self.text_input, &mut tree.children[0], layout.children().next().expect("TypedInput inner child Textbox was not created."), renderer, operation)
<TextInput<_, _, _> as Widget<_, _, _>>::operate(
&self.text_input,
state,
layout,
renderer,
operation,
);
}

#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
Expand All @@ -254,17 +264,19 @@ where
shell: &mut Shell<Message>,
viewport: &Rectangle,
) -> event::Status {
let text_input_layout = layout.children().next().expect("fail to get text_input layout");

let child = &mut state.children[0];

let mut messages = Vec::new();
let mut sub_shell = Shell::new(&mut messages);
let status = self.text_input.on_event(
child, event, text_input_layout, cursor, renderer, clipboard, &mut sub_shell, viewport,
);
// todo!()
// println!("shell: {:?}", shell);
state,
event,
layout,
cursor,
renderer,
clipboard,
&mut sub_shell,
viewport,
);

if let Some(redraw) = sub_shell.redraw_request() {
shell.request_redraw(redraw);
}
Expand Down Expand Up @@ -296,7 +308,6 @@ where
}
status
}

}

impl<'a, T, Message, Theme, Renderer> From<TypedInput<'a, T, Message, Theme, Renderer>>
Expand Down

0 comments on commit 000d93e

Please sign in to comment.