From cd2130a69a6f0089f8fda378e19ade0de330ed56 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Feb 2024 08:24:08 +0100 Subject: Add `size_hint` method to `Component` trait This can be used to aid the sizing strategy of some containers directly in the component definition, instead of stating the sizes explicitly in `view` logic. --- examples/component/src/main.rs | 9 ++++++++- widget/src/lazy/component.rs | 21 +++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/component/src/main.rs b/examples/component/src/main.rs index 81be4d7f..4670824d 100644 --- a/examples/component/src/main.rs +++ b/examples/component/src/main.rs @@ -48,7 +48,7 @@ impl Sandbox for Component { mod numeric_input { use iced::alignment::{self, Alignment}; use iced::widget::{button, component, row, text, text_input, Component}; - use iced::{Element, Length}; + use iced::{Element, Length, Size}; pub struct NumericInput { value: Option, @@ -143,6 +143,13 @@ mod numeric_input { .spacing(10) .into() } + + fn size_hint(&self) -> Size { + Size { + width: Length::Fill, + height: Length::Shrink, + } + } } impl<'a, Message> From> for Element<'a, Message> diff --git a/widget/src/lazy/component.rs b/widget/src/lazy/component.rs index edecbdaa..a512e0de 100644 --- a/widget/src/lazy/component.rs +++ b/widget/src/lazy/component.rs @@ -62,6 +62,17 @@ pub trait Component { _operation: &mut dyn widget::Operation, ) { } + + /// Returns a [`Size`] hint for laying out the [`Component`]. + /// + /// This hint may be used by some widget containers to adjust their sizing strategy + /// during construction. + fn size_hint(&self) -> Size { + Size { + width: Length::Shrink, + height: Length::Shrink, + } + } } struct Tag(T); @@ -255,10 +266,12 @@ where } fn size_hint(&self) -> Size { - Size { - width: Length::Shrink, - height: Length::Shrink, - } + self.state + .borrow() + .as_ref() + .expect("Borrow instance state") + .borrow_component() + .size_hint() } fn layout( -- cgit