Skip to content

Commit

Permalink
Merge pull request #2275 from iced-rs/component-size-hint
Browse files Browse the repository at this point in the history
`size_hint` method for `Component` trait
  • Loading branch information
hecrj authored Feb 21, 2024
2 parents 5ba818a + 28f0ecc commit 56ac21c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>` and `From<i32>` 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)
Expand Down
9 changes: 8 additions & 1 deletion examples/component/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> {
value: Option<u32>,
Expand Down Expand Up @@ -143,6 +143,13 @@ mod numeric_input {
.spacing(10)
.into()
}

fn size_hint(&self) -> Size<Length> {
Size {
width: Length::Fill,
height: Length::Shrink,
}
}
}

impl<'a, Message> From<NumericInput<Message>> for Element<'a, Message>
Expand Down
21 changes: 17 additions & 4 deletions widget/src/lazy/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ pub trait Component<Message, Theme = crate::Theme, Renderer = crate::Renderer> {
_operation: &mut dyn widget::Operation<Message>,
) {
}

/// 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<Length> {
Size {
width: Length::Shrink,
height: Length::Shrink,
}
}
}

struct Tag<T>(T);
Expand Down Expand Up @@ -255,10 +266,12 @@ where
}

fn size_hint(&self) -> Size<Length> {
Size {
width: Length::Shrink,
height: Length::Shrink,
}
self.state
.borrow()
.as_ref()
.expect("Borrow instance state")
.borrow_component()
.size_hint()
}

fn layout(
Expand Down

0 comments on commit 56ac21c

Please sign in to comment.