diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ff082fd84..e8a036385f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `clipboard` module in `advanced` module. [#2272](https://github.com/iced-rs/iced/pull/2272) - Default `disabled` style for `checkbox` and `hovered` style for `Svg`. [#2273](https://github.com/iced-rs/iced/pull/2273) - `From` and `From` implementations for `border::Radius`. [#2274](https://github.com/iced-rs/iced/pull/2274) +- `size_hint` method for `Component` trait. [#2275](https://github.com/iced-rs/iced/pull/2275) ### Fixed - Black images when using OpenGL backend in `iced_wgpu`. [#2259](https://github.com/iced-rs/iced/pull/2259) diff --git a/examples/component/src/main.rs b/examples/component/src/main.rs index 81be4d7f03..4670824d72 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 edecbdaa6e..a512e0de91 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(