diff --git a/Cargo.toml b/Cargo.toml index a95a200c..b0af6983 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ default = [ "badge", "card", "number_input", - #"date_picker", + "date_picker", "color_picker", #"floating_element", #"icon_text", @@ -88,7 +88,7 @@ members = [ #"examples/cupertino/cupertino_button", #"examples/cupertino/cupertino_spinner", #"examples/cupertino/cupertino_switch", - #"examples/date_picker", + "examples/date_picker", #"examples/floating_element", #"examples/floating_element_anchors", #"examples/grid", diff --git a/src/native/date_picker.rs b/src/native/date_picker.rs index 914a6267..ef53775e 100644 --- a/src/native/date_picker.rs +++ b/src/native/date_picker.rs @@ -181,8 +181,10 @@ where self.underlay.as_widget().width() } - fn layout(&self, renderer: &Renderer, limits: &Limits) -> Node { - self.underlay.as_widget().layout(renderer, limits) + fn layout(&self, tree: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node { + self.underlay + .as_widget() + .layout(&mut tree.children[0], renderer, limits) } fn on_event( diff --git a/src/native/number_input.rs b/src/native/number_input.rs index 479d3e71..0135538a 100644 --- a/src/native/number_input.rs +++ b/src/native/number_input.rs @@ -314,7 +314,7 @@ where .center_x() }; - let mut element = if self.padding < DEFAULT_PADDING { + let element = if self.padding < DEFAULT_PADDING { Element::new( Row::::new() .spacing(1) diff --git a/src/native/overlay/color_picker.rs b/src/native/overlay/color_picker.rs index 4a6ff026..cd0e7d01 100644 --- a/src/native/overlay/color_picker.rs +++ b/src/native/overlay/color_picker.rs @@ -27,8 +27,8 @@ use iced_widget::{ text::Renderer as _, touch, widget::{self, tree::Tree}, - Alignment, Clipboard, Color, Element, Event, Layout, Length, Overlay, Padding, Pixels, - Point, Rectangle, Renderer as _, Shell, Size, Text, Vector, Widget, + Alignment, Clipboard, Color, Element, Event, Layout, Length, Overlay, Padding, Point, + Rectangle, Renderer as _, Shell, Size, Text, Vector, Widget, }, graphics::geometry::Renderer as _, renderer::Renderer, @@ -938,24 +938,10 @@ where let hex_text_limits = block2_limits; - let mut hex_text = Row::>::new() + let mut hex_text_layout = Row::>::new() .width(Length::Fill) - .height(Length::Fixed(renderer.default_size().0 + 2.0 * PADDING)); - - let element: Element> = Element::new(hex_text); - let mut rgba_tree = if let Some(child_tree) = color_picker.tree.children.get_mut(2) { - child_tree.diff(element.as_widget()); - child_tree - } else { - let child_tree = Tree::new(element.as_widget()); - color_picker.tree.children.insert(2, child_tree); - color_picker.tree.children.get_mut(2).unwrap() - }; - - let mut hex_text_layout = - element - .as_widget() - .layout(color_picker.tree, renderer, &hex_text_limits); + .height(Length::Fixed(renderer.default_size().0 + 2.0 * PADDING)) + .layout(color_picker.tree, renderer, &hex_text_limits); let block2_limits = block2_limits.shrink(Size::new( 0.0, @@ -991,13 +977,13 @@ where ); } let element: Element> = Element::new(rgba_colors); - let mut rgba_tree = if let Some(child_tree) = color_picker.tree.children.get_mut(3) { + let mut rgba_tree = if let Some(child_tree) = color_picker.tree.children.get_mut(2) { child_tree.diff(element.as_widget()); child_tree } else { let child_tree = Tree::new(element.as_widget()); - color_picker.tree.children.insert(3, child_tree); - color_picker.tree.children.get_mut(3).unwrap() + color_picker.tree.children.insert(2, child_tree); + &mut color_picker.tree.children[2] }; let mut rgba_colors = element diff --git a/src/native/overlay/date_picker.rs b/src/native/overlay/date_picker.rs index c0cb0afc..c4360473 100644 --- a/src/native/overlay/date_picker.rs +++ b/src/native/overlay/date_picker.rs @@ -368,7 +368,7 @@ where Theme: 'a + StyleSheet + button::StyleSheet + text::StyleSheet + container::StyleSheet, { #[allow(clippy::too_many_lines)] - fn layout(&self, renderer: &Renderer, bounds: Size, position: Point) -> Node { + fn layout(&mut self, renderer: &Renderer, bounds: Size, position: Point) -> Node { let limits = Limits::new(Size::ZERO, bounds) .pad(Padding::from(PADDING)) .width(Length::Fill) @@ -378,86 +378,68 @@ where // Pre-Buttons TODO: get rid of it let cancel_limits = limits; - let cancel_button = self.cancel_button.layout(renderer, &cancel_limits); + let cancel_button = + self.cancel_button + .layout(&mut self.tree.children[0], renderer, &cancel_limits); let limits = limits.shrink(Size::new(0.0, cancel_button.bounds().height + SPACING)); // Month/Year let font_size = renderer.default_size(); - let month_year = Row::<(), Renderer>::new() + let month_year = Row::>::new() .width(Length::Fill) .spacing(SPACING) .push( Row::new() .width(Length::Fill) - .push( - Container::new( - Row::new() // Left Month arrow - .width(Length::Shrink) - .push( - Text::new(char::from(Icon::CaretLeftFill).to_string()) - .size(font_size + 1.0) - .font(ICON_FONT), - ), - ) - .height(Length::Shrink), - ) + .push(Container::new( + Row::new().push( + Text::new(char::from(Icon::CaretLeftFill).to_string()) + .size(font_size.0 + 1.0) + .font(ICON_FONT), + ), + )) .push( // Month - Text::new("October") - .width(Length::Fill) - .height(Length::Shrink), + Text::new("October").width(Length::Fill), ) .push( // Right Month arrow Container::new( Text::new(char::from(Icon::CaretRightFill).to_string()) - .size(font_size + 1.0) + .size(font_size.0 + 1.0) .font(ICON_FONT), - ) - .height(Length::Shrink) - .width(Length::Shrink), + ), ), ) .push( Row::new() .width(Length::Fill) - .push( - Container::new( - Row::new() // Left Year arrow - .width(Length::Shrink) - .push( - Text::new(char::from(Icon::CaretLeftFill).to_string()) - .size(font_size + 1.0) - .font(ICON_FONT), - ), - ) - .height(Length::Shrink) - .width(Length::Shrink), - ) + .push(Container::new( + Row::new().push( + Text::new(char::from(Icon::CaretLeftFill).to_string()) + .size(font_size.0 + 1.0) + .font(ICON_FONT), + ), + )) .push( // Year - Text::new("9999").width(Length::Fill).height(Length::Shrink), + Text::new("9999").width(Length::Fill), ) .push( // Right Year arrow Container::new( - Row::new() - .width(Length::Shrink) - .height(Length::Shrink) - .push( - Text::new(char::from(Icon::CaretRightFill).to_string()) - .size(font_size + 1.0) - .font(ICON_FONT), - ), - ) - .height(Length::Shrink) - .width(Length::Shrink), + Row::new().push( + Text::new(char::from(Icon::CaretRightFill).to_string()) + .size(font_size.0 + 1.0) + .font(ICON_FONT), + ), + ), ), ); - let days = Container::<(), Renderer>::new((0..7).fold( + let days = Container::>::new((0..7).fold( Column::new().width(Length::Fill).height(Length::Fill), |column, _y| { column.push( @@ -468,14 +450,9 @@ where .padding(DAY_CELL_PADDING), |row, _x| { row.push( - Container::new( - Row::new() - .width(Length::Shrink) - .height(Length::Shrink) - .push(Text::new("31").size(font_size)), - ) - .width(Length::Fill) - .height(Length::Shrink), + Container::new(Row::new().push(Text::new("31").size(font_size))) + .width(Length::Fill) + .height(Length::Fill), ) }, ), @@ -486,13 +463,23 @@ where .height(Length::Fill) .center_y(); - let mut col = Column::<(), Renderer>::new() + let col = Column::>::new() .spacing(SPACING) .align_items(Alignment::Center) .push(month_year) - .push(days) - .layout(renderer, &limits); + .push(days); + + let element: Element> = Element::new(col); + let mut col_tree = if let Some(child_tree) = self.tree.children.get_mut(2) { + child_tree.diff(element.as_widget()); + child_tree + } else { + let child_tree = Tree::new(element.as_widget()); + self.tree.children.insert(2, child_tree); + &mut self.tree.children[2] + }; + let mut col = element.as_widget().layout(&mut col_tree, renderer, &limits); col.move_to(Point::new( col.bounds().x + PADDING, col.bounds().y + PADDING, @@ -502,12 +489,16 @@ where let cancel_limits = limits.max_width(((col.bounds().width / 2.0) - BUTTON_SPACING).max(0.0)); - let mut cancel_button = self.cancel_button.layout(renderer, &cancel_limits); + let mut cancel_button = + self.cancel_button + .layout(&mut self.tree.children[0], renderer, &cancel_limits); let submit_limits = limits.max_width(((col.bounds().width / 2.0) - BUTTON_SPACING).max(0.0)); - let mut submit_button = self.submit_button.layout(renderer, &submit_limits); + let mut submit_button = + self.submit_button + .layout(&mut self.tree.children[1], renderer, &submit_limits); cancel_button.move_to(Point { x: cancel_button.bounds().x + PADDING, @@ -960,7 +951,7 @@ where unimplemented!("This should never be reached!") } - fn layout(&self, _renderer: &Renderer, _limits: &Limits) -> Node { + fn layout(&self, _tree: &mut Tree, _renderer: &Renderer, _limits: &Limits) -> Node { unimplemented!("This should never be reached!") } @@ -1124,64 +1115,65 @@ fn month_year( let mut buffer = [0; 4]; // Left caret - renderer.fill_text(core::text::Text { - content: char::from(Icon::CaretLeftFill).encode_utf8(&mut buffer), - bounds: Rectangle { - x: left_bounds.center_x(), - y: left_bounds.center_y(), - ..left_bounds + renderer.fill_text( + core::text::Text { + content: char::from(Icon::CaretLeftFill).encode_utf8(&mut buffer), + bounds: Size::new(left_bounds.width, left_bounds.height), + size: core::Pixels( + renderer.default_size().0 + if left_arrow_hovered { 1.0 } else { 0.0 }, + ), + font: ICON_FONT, + horizontal_alignment: Horizontal::Center, + vertical_alignment: Vertical::Center, + line_height: text::LineHeight::Relative(1.3), + shaping: text::Shaping::Advanced, }, - size: renderer.default_size() + if left_arrow_hovered { 1.0 } else { 0.0 }, - color: style + Point::new(left_bounds.center_x(), left_bounds.center_y()), + style .get(&style_state) .expect("Style Sheet not found.") .text_color, - font: ICON_FONT, - horizontal_alignment: Horizontal::Center, - vertical_alignment: Vertical::Center, - line_height: text::LineHeight::Relative(1.3), - shaping: text::Shaping::Advanced, - }); + ); // Text - renderer.fill_text(core::text::Text { - content: text, - bounds: Rectangle { - x: center_bounds.center_x(), - y: center_bounds.center_y(), - ..center_bounds + renderer.fill_text( + core::text::Text { + content: text, + bounds: Size::new(center_bounds.width, center_bounds.height), + size: renderer.default_size(), + font: renderer.default_font(), + horizontal_alignment: Horizontal::Center, + vertical_alignment: Vertical::Center, + line_height: text::LineHeight::Relative(1.3), + shaping: text::Shaping::Basic, }, - size: renderer.default_size(), - color: style + Point::new(center_bounds.center_x(), center_bounds.center_y()), + style .get(&style_state) .expect("Style Sheet not found.") .text_color, - font: renderer.default_font(), - horizontal_alignment: Horizontal::Center, - vertical_alignment: Vertical::Center, - line_height: text::LineHeight::Relative(1.3), - shaping: text::Shaping::Basic, - }); + ); // Right caret - renderer.fill_text(core::text::Text { - content: char::from(Icon::CaretRightFill).encode_utf8(&mut buffer), - bounds: Rectangle { - x: right_bounds.center_x(), - y: right_bounds.center_y(), - ..right_bounds + renderer.fill_text( + core::text::Text { + content: char::from(Icon::CaretRightFill).encode_utf8(&mut buffer), + bounds: Size::new(right_bounds.width, right_bounds.height), + size: core::Pixels( + renderer.default_size().0 + if right_arrow_hovered { 1.0 } else { 0.0 }, + ), + font: ICON_FONT, + horizontal_alignment: Horizontal::Center, + vertical_alignment: Vertical::Center, + line_height: text::LineHeight::Relative(1.3), + shaping: text::Shaping::Advanced, }, - size: renderer.default_size() + if right_arrow_hovered { 1.0 } else { 0.0 }, - color: style + Point::new(right_bounds.center_x(), right_bounds.center_y()), + style .get(&style_state) .expect("Style Sheet not found.") .text_color, - font: ICON_FONT, - horizontal_alignment: Horizontal::Center, - vertical_alignment: Vertical::Center, - line_height: text::LineHeight::Relative(1.3), - shaping: text::Shaping::Advanced, - }); + ); }; // Draw month @@ -1225,24 +1217,23 @@ fn day_labels( for (i, label) in layout.children().enumerate() { let bounds = label.bounds(); - renderer.fill_text(core::text::Text { - content: &crate::core::date::WEEKDAY_LABELS[i], - bounds: Rectangle { - x: bounds.center_x(), - y: bounds.center_y(), - ..bounds + renderer.fill_text( + core::text::Text { + content: &crate::core::date::WEEKDAY_LABELS[i], + bounds: Size::new(bounds.width, bounds.height), + size: renderer.default_size(), + font: renderer.default_font(), + horizontal_alignment: Horizontal::Center, + vertical_alignment: Vertical::Center, + line_height: text::LineHeight::Relative(1.3), + shaping: text::Shaping::Basic, }, - size: renderer.default_size(), - color: style + Point::new(bounds.center_x(), bounds.center_y()), + style .get(&StyleState::Active) .expect("Style Sheet not found.") .text_color, - font: renderer.default_font(), - horizontal_alignment: Horizontal::Center, - vertical_alignment: Vertical::Center, - line_height: text::LineHeight::Relative(1.3), - shaping: text::Shaping::Basic, - }); + ); } } @@ -1310,15 +1301,19 @@ fn day_table( ); } - renderer.fill_text(core::text::Text { - content: &format!("{number:02}"), // Todo: is there some way of static format as this has a fixed size? - bounds: Rectangle { - x: bounds.center_x(), - y: bounds.center_y(), - ..bounds + renderer.fill_text( + core::text::Text { + content: &format!("{number:02}"), // Todo: is there some way of static format as this has a fixed size? + bounds: Size::new(bounds.width, bounds.height), + size: renderer.default_size(), + font: renderer.default_font(), + horizontal_alignment: Horizontal::Center, + vertical_alignment: Vertical::Center, + line_height: text::LineHeight::Relative(1.3), + shaping: text::Shaping::Basic, }, - size: renderer.default_size(), - color: if is_in_month == IsInMonth::Same { + Point::new(bounds.center_x(), bounds.center_y()), + if is_in_month == IsInMonth::Same { style .get(&style_state) .expect("Style Sheet not found.") @@ -1329,12 +1324,7 @@ fn day_table( .expect("Style Sheet not found.") .text_attenuated_color }, - font: renderer.default_font(), - horizontal_alignment: Horizontal::Center, - vertical_alignment: Vertical::Center, - line_height: text::LineHeight::Relative(1.3), - shaping: text::Shaping::Basic, - }); + ); } } }