Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

size_hint method for Component trait #2275

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading