From 6c0365e7aec493872832fbd3a4a2f308a5fa4f88 Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Tue, 7 Nov 2023 08:35:17 -0500 Subject: [PATCH] Fixed Number input and card --- Cargo.toml | 8 +- examples/number_input/src/main.rs | 2 +- src/native/card.rs | 67 ++++++++++------ src/native/number_input.rs | 128 +++++++++++++++++------------- 4 files changed, 122 insertions(+), 83 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 98ba0bf9..a95a200c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,8 +41,8 @@ segmented_button = [] default = [ "badge", - #"card", - #"number_input", + "card", + "number_input", #"date_picker", "color_picker", #"floating_element", @@ -82,7 +82,7 @@ opt-level = 2 [workspace] members = [ "examples/badge", - #"examples/card", + "examples/card", "examples/color_picker", #"examples/cupertino/cupertino_alert", #"examples/cupertino/cupertino_button", @@ -99,7 +99,7 @@ members = [ #"examples/tabs", #"examples/time_picker", #"examples/wrap", - #"examples/number_input", + "examples/number_input", #"examples/selection_list", #"examples/split", #"examples/split_scroller", diff --git a/examples/number_input/src/main.rs b/examples/number_input/src/main.rs index 7132949a..06aa9325 100644 --- a/examples/number_input/src/main.rs +++ b/examples/number_input/src/main.rs @@ -26,7 +26,7 @@ pub enum Message { fn main() -> iced::Result { NumberInputDemo::run(Settings { - default_text_size: 12.0, + default_text_size: iced::Pixels(12.0), window: window::Settings { size: (250, 200), ..Default::default() diff --git a/src/native/card.rs b/src/native/card.rs index caf8a6e8..c35a62c1 100644 --- a/src/native/card.rs +++ b/src/native/card.rs @@ -13,8 +13,8 @@ use iced_widget::{ mouse::{self, Cursor}, renderer, touch, widget::{Operation, Tree}, - Alignment, BorderRadius, Clipboard, Color, Element, Event, Layout, Length, Padding, Point, - Rectangle, Shell, Size, Widget, + Alignment, BorderRadius, Clipboard, Color, Element, Event, Layout, Length, Padding, Pixels, + Point, Rectangle, Shell, Size, Widget, }, text::LineHeight, }; @@ -241,7 +241,7 @@ where self.height } - fn layout(&self, renderer: &Renderer, limits: &Limits) -> Node { + fn layout(&self, tree: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node { let limits = limits.max_width(self.max_width).max_height(self.max_height); let head_node = head_node( @@ -252,9 +252,17 @@ where self.width, self.on_close.is_some(), self.close_size, + tree, ); - let mut body_node = body_node(renderer, &limits, &self.body, self.padding_body, self.width); + let mut body_node = body_node( + renderer, + &limits, + &self.body, + self.padding_body, + self.width, + tree, + ); body_node.move_to(Point::new( body_node.bounds().x, @@ -262,7 +270,7 @@ where )); let mut foot_node = self.foot.as_ref().map_or_else(Node::default, |foot| { - foot_node(renderer, &limits, foot, self.padding_foot, self.width) + foot_node(renderer, &limits, foot, self.padding_foot, self.width, tree) }); foot_node.move_to(Point::new( @@ -595,6 +603,7 @@ fn head_node( width: Length, on_close: bool, close_size: Option, + tree: &mut Tree, ) -> Node where Renderer: core::Renderer + core::text::Renderer, @@ -606,7 +615,7 @@ where .height(head.as_widget().height()) .pad(pad); - let close_size = close_size.unwrap_or_else(|| renderer.default_size()); + let close_size = close_size.unwrap_or_else(|| renderer.default_size().0); let mut close = if on_close { limits = limits.shrink(Size::new(close_size, 0.0)); Some(Node::new(Size::new(close_size + 1.0, close_size + 1.0))) @@ -614,7 +623,9 @@ where None }; - let mut head = head.as_widget().layout(renderer, &limits); + let mut head = head + .as_widget() + .layout(&mut tree.children[0], renderer, &limits); let mut size = limits.resolve(head.size()); head.move_to(Point::new(padding, padding)); @@ -643,6 +654,7 @@ fn body_node( body: &Element<'_, Message, Renderer>, padding: f32, width: Length, + tree: &mut Tree, ) -> Node where Renderer: core::Renderer, @@ -655,7 +667,9 @@ where .height(body.as_widget().height()) .pad(pad); - let mut body = body.as_widget().layout(renderer, &limits); + let mut body = body + .as_widget() + .layout(&mut tree.children[1], renderer, &limits); let size = limits.resolve(body.size()); body.move_to(Point::new(padding, padding)); @@ -671,6 +685,7 @@ fn foot_node( foot: &Element<'_, Message, Renderer>, padding: f32, width: Length, + tree: &mut Tree, ) -> Node where Renderer: core::Renderer, @@ -683,7 +698,9 @@ where .height(foot.as_widget().height()) .pad(pad); - let mut foot = foot.as_widget().layout(renderer, &limits); + let mut foot = foot + .as_widget() + .layout(&mut tree.children[2], renderer, &limits); let size = limits.resolve(foot.size()); foot.move_to(Point::new(padding, padding)); @@ -758,23 +775,23 @@ fn draw_head( let close_bounds = close_layout.bounds(); let is_mouse_over_close = close_bounds.contains(cursor.position().unwrap_or_default()); - renderer.fill_text(core::text::Text { - content: &Icon::X.to_string(), - bounds: Rectangle { - x: close_bounds.center_x(), - y: close_bounds.center_y(), - height: close_bounds.height, - ..close_bounds + renderer.fill_text( + core::text::Text { + content: &Icon::X.to_string(), + bounds: Size::new(close_bounds.width, close_bounds.height), + size: Pixels( + close_size.unwrap_or_else(|| renderer.default_size().0) + + if is_mouse_over_close { 1.0 } else { 0.0 }, + ), + font: ICON_FONT, + horizontal_alignment: Horizontal::Center, + vertical_alignment: Vertical::Center, + line_height: LineHeight::Relative(1.3), + shaping: iced_widget::text::Shaping::Advanced, }, - size: close_size.unwrap_or_else(|| renderer.default_size()) - + if is_mouse_over_close { 1.0 } else { 0.0 }, - color: style_sheet.close_color, - font: ICON_FONT, - horizontal_alignment: Horizontal::Center, - vertical_alignment: Vertical::Center, - line_height: LineHeight::Relative(1.3), - shaping: iced_widget::text::Shaping::Advanced, - }); + Point::new(close_bounds.center_x(), close_bounds.center_y()), + style_sheet.close_color, + ); } } diff --git a/src/native/number_input.rs b/src/native/number_input.rs index 7590a2bc..479d3e71 100644 --- a/src/native/number_input.rs +++ b/src/native/number_input.rs @@ -14,8 +14,8 @@ use iced_widget::{ tree::{State, Tag}, Operation, Tree, }, - Alignment, Background, Clipboard, Color, Element, Event, Layout, Length, Padding, - Rectangle, Shell, Size, Widget, + Alignment, Background, Clipboard, Color, Element, Event, Layout, Length, Padding, Pixels, + Point, Rectangle, Shell, Size, Widget, }, text, text::LineHeight, @@ -295,37 +295,55 @@ where Length::Shrink } - fn layout(&self, renderer: &Renderer, limits: &Limits) -> Node { + fn layout(&self, tree: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node { let padding = Padding::from(self.padding); let limits = limits .width(self.width()) .height(Length::Shrink) .pad(padding); - let content = self.content.layout(renderer, &limits); + let content = self + .content + .layout(&mut tree.children[0], renderer, &limits, None); let limits2 = Limits::new(Size::new(0.0, 0.0), content.size()); - let txt_size = self.size.unwrap_or_else(|| renderer.default_size()); + let txt_size = self.size.unwrap_or_else(|| renderer.default_size().0); let icon_size = txt_size * 2.5 / 4.0; let btn_mod = |c| { - Container::<(), Renderer>::new(Text::new(format!(" {c} ")).size(icon_size)) + Container::::new(Text::new(format!(" {c} ")).size(icon_size)) .center_y() .center_x() }; - let mut modifier = if self.padding < DEFAULT_PADDING { - Row::<(), Renderer>::new() - .spacing(1) - .width(Length::Shrink) - .push(btn_mod('+')) - .push(btn_mod('-')) - .layout(renderer, &limits2.loose()) + + let mut element = if self.padding < DEFAULT_PADDING { + Element::new( + Row::::new() + .spacing(1) + .width(Length::Shrink) + .push(btn_mod('+')) + .push(btn_mod('-')), + ) + } else { + Element::new( + Column::::new() + .spacing(1) + .width(Length::Shrink) + .push(btn_mod('▲')) + .push(btn_mod('▼')), + ) + }; + + let mut input_tree = if let Some(child_tree) = tree.children.get_mut(1) { + child_tree.diff(element.as_widget()); + child_tree } else { - Column::<(), Renderer>::new() - .spacing(1) - .width(Length::Shrink) - .push(btn_mod('▲')) - .push(btn_mod('▼')) - .layout(renderer, &limits2.loose()) + let child_tree = Tree::new(element.as_widget()); + tree.children.insert(1, child_tree); + &mut tree.children[1] }; + + let mut modifier = element + .as_widget() + .layout(&mut input_tree, renderer, &limits2.loose()); let intrinsic = Size::new( content.size().width - 1.0, content.size().height.max(modifier.size().height), @@ -424,13 +442,16 @@ where } else { match event { Event::Keyboard(keyboard::Event::CharacterReceived(c)) - if child.state.downcast_mut::().is_focused() + if child + .state + .downcast_mut::>() + .is_focused() && c.is_numeric() => { let mut new_val = self.value.to_string(); match child .state - .downcast_mut::() + .downcast_mut::>() .cursor() .state(&Value::new(&new_val)) { @@ -476,7 +497,10 @@ where } } Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) - if child.state.downcast_mut::().is_focused() => + if child + .state + .downcast_mut::>() + .is_focused() => { match key_code { keyboard::KeyCode::Up => { @@ -494,7 +518,7 @@ where let mut new_val = self.value.to_string(); match child .state - .downcast_mut::() + .downcast_mut::>() .cursor() .state(&Value::new(&new_val)) { @@ -682,9 +706,9 @@ where theme.active(&self.style) }; - let txt_size = self.size.unwrap_or_else(|| renderer.default_size()); + let txt_size = self.size.unwrap_or_else(|| renderer.default_size().0); - let icon_size = txt_size * 2.5 / 4.0; + let icon_size = Pixels(txt_size * 2.5 / 4.0); // decrease button section renderer.fill_quad( @@ -701,21 +725,20 @@ where let mut buffer = [0; 4]; - renderer.fill_text(core::text::Text { - content: char::from(Icon::CaretDownFill).encode_utf8(&mut buffer), - bounds: Rectangle { - x: dec_bounds.center_x(), - y: dec_bounds.center_y(), - ..dec_bounds + renderer.fill_text( + core::text::Text { + content: char::from(Icon::CaretDownFill).encode_utf8(&mut buffer), + bounds: Size::new(dec_bounds.width, dec_bounds.height), + size: icon_size, + font: ICON_FONT, + horizontal_alignment: Horizontal::Center, + vertical_alignment: Vertical::Center, + line_height: LineHeight::Relative(1.3), + shaping: iced_widget::text::Shaping::Advanced, }, - size: icon_size, - color: decrease_btn_style.icon_color, - font: ICON_FONT, - horizontal_alignment: Horizontal::Center, - vertical_alignment: Vertical::Center, - line_height: LineHeight::Relative(1.3), - shaping: iced_widget::text::Shaping::Advanced, - }); + Point::new(dec_bounds.center_x(), dec_bounds.center_y()), + decrease_btn_style.icon_color, + ); // increase button section renderer.fill_quad( @@ -730,21 +753,20 @@ where .unwrap_or(Background::Color(Color::TRANSPARENT)), ); - renderer.fill_text(core::text::Text { - content: char::from(Icon::CaretUpFill).encode_utf8(&mut buffer), - bounds: Rectangle { - x: inc_bounds.center_x(), - y: inc_bounds.center_y(), - ..inc_bounds + renderer.fill_text( + core::text::Text { + content: char::from(Icon::CaretUpFill).encode_utf8(&mut buffer), + bounds: Size::new(inc_bounds.width, inc_bounds.height), + size: icon_size, + font: ICON_FONT, + horizontal_alignment: Horizontal::Center, + vertical_alignment: Vertical::Center, + line_height: LineHeight::Relative(1.3), + shaping: iced_widget::text::Shaping::Advanced, }, - size: icon_size, - color: increase_btn_style.icon_color, - font: ICON_FONT, - horizontal_alignment: Horizontal::Center, - vertical_alignment: Vertical::Center, - line_height: LineHeight::Relative(1.3), - shaping: iced_widget::text::Shaping::Advanced, - }); + Point::new(inc_bounds.center_x(), inc_bounds.center_y()), + increase_btn_style.icon_color, + ); } }