summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-11 06:12:19 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-11 06:12:19 +0100
commit03c901d49b7cce901cfd76100f08dcff31420af8 (patch)
treef19e1708f3687edf20429dd493ab27d44a008305
parentfa53d9adbb0efbbe806a749476f83c04f756be75 (diff)
downloadiced-03c901d49b7cce901cfd76100f08dcff31420af8.tar.gz
iced-03c901d49b7cce901cfd76100f08dcff31420af8.tar.bz2
iced-03c901d49b7cce901cfd76100f08dcff31420af8.zip
Make `Button` sizing strategy adaptive
-rw-r--r--core/src/length.rs12
-rw-r--r--widget/src/button.rs9
-rw-r--r--widget/src/container.rs12
3 files changed, 20 insertions, 13 deletions
diff --git a/core/src/length.rs b/core/src/length.rs
index 6dc15049..4c139895 100644
--- a/core/src/length.rs
+++ b/core/src/length.rs
@@ -42,6 +42,18 @@ impl Length {
pub fn is_fill(&self) -> bool {
self.fill_factor() != 0
}
+
+ /// Returns the "fluid" variant of the [`Length`].
+ ///
+ /// Specifically:
+ /// - [`Length::Shrink`] if [`Length::Shrink`] or [`Length::Fixed`].
+ /// - [`Length::Fill`] otherwise.
+ pub fn fluid(&self) -> Length {
+ match self {
+ Length::Fill | Length::FillPortion(_) => Length::Fill,
+ Length::Shrink | Length::Fixed(_) => Length::Shrink,
+ }
+ }
}
impl From<Pixels> for Length {
diff --git a/widget/src/button.rs b/widget/src/button.rs
index 86abee77..0ebb8dcc 100644
--- a/widget/src/button.rs
+++ b/widget/src/button.rs
@@ -71,11 +71,14 @@ where
{
/// Creates a new [`Button`] with the given content.
pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
+ let content = content.into();
+ let size = content.as_widget().size_hint();
+
Button {
- content: content.into(),
+ content,
on_press: None,
- width: Length::Shrink,
- height: Length::Shrink,
+ width: size.width.fluid(),
+ height: size.height.fluid(),
padding: Padding::new(5.0),
style: <Renderer::Theme as StyleSheet>::Style::default(),
}
diff --git a/widget/src/container.rs b/widget/src/container.rs
index ecc5c651..cffb0458 100644
--- a/widget/src/container.rs
+++ b/widget/src/container.rs
@@ -52,16 +52,8 @@ where
Container {
id: None,
padding: Padding::ZERO,
- width: if size.width.is_fill() {
- Length::Fill
- } else {
- Length::Shrink
- },
- height: if size.height.is_fill() {
- Length::Fill
- } else {
- Length::Shrink
- },
+ width: size.width.fluid(),
+ height: size.height.fluid(),
max_width: f32::INFINITY,
max_height: f32::INFINITY,
horizontal_alignment: alignment::Horizontal::Left,