Fix size_hint for keyed_column

This commit is contained in:
Héctor Ramón Jiménez 2024-01-09 06:44:15 +01:00
parent d62bb8193c
commit e710e76949
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 27 additions and 28 deletions

View file

@ -254,13 +254,7 @@ impl Application for Todos {
.spacing(20) .spacing(20)
.max_width(800); .max_width(800);
scrollable( scrollable(container(content).padding(40).center_x()).into()
container(content)
.width(Length::Fill)
.padding(40)
.center_x(),
)
.into()
} }
} }
} }
@ -472,7 +466,6 @@ fn empty_message(message: &str) -> Element<'_, Message> {
.horizontal_alignment(alignment::Horizontal::Center) .horizontal_alignment(alignment::Horizontal::Center)
.style(Color::from([0.7, 0.7, 0.7])), .style(Color::from([0.7, 0.7, 0.7])),
) )
.width(Length::Fill)
.height(200) .height(200)
.center_y() .center_y()
.into() .into()

View file

@ -80,6 +80,7 @@ pub fn keyed_column<'a, Key, Message, Renderer>(
) -> keyed::Column<'a, Key, Message, Renderer> ) -> keyed::Column<'a, Key, Message, Renderer>
where where
Key: Copy + PartialEq, Key: Copy + PartialEq,
Renderer: core::Renderer,
{ {
keyed::Column::with_children(children) keyed::Column::with_children(children)
} }

View file

@ -30,26 +30,10 @@ where
impl<'a, Key, Message, Renderer> Column<'a, Key, Message, Renderer> impl<'a, Key, Message, Renderer> Column<'a, Key, Message, Renderer>
where where
Key: Copy + PartialEq, Key: Copy + PartialEq,
Renderer: crate::core::Renderer,
{ {
/// Creates an empty [`Column`]. /// Creates an empty [`Column`].
pub fn new() -> Self { pub fn new() -> Self {
Self::with_children(Vec::new())
}
/// Creates a [`Column`] with the given elements.
pub fn with_children(
children: impl IntoIterator<Item = (Key, Element<'a, Message, Renderer>)>,
) -> Self {
let (keys, children) = children.into_iter().fold(
(Vec::new(), Vec::new()),
|(mut keys, mut children), (key, child)| {
keys.push(key);
children.push(child);
(keys, children)
},
);
Column { Column {
spacing: 0.0, spacing: 0.0,
padding: Padding::ZERO, padding: Padding::ZERO,
@ -57,11 +41,20 @@ where
height: Length::Shrink, height: Length::Shrink,
max_width: f32::INFINITY, max_width: f32::INFINITY,
align_items: Alignment::Start, align_items: Alignment::Start,
keys, keys: Vec::new(),
children, children: Vec::new(),
} }
} }
/// Creates a [`Column`] with the given elements.
pub fn with_children(
children: impl IntoIterator<Item = (Key, Element<'a, Message, Renderer>)>,
) -> Self {
children
.into_iter()
.fold(Self::new(), |column, (key, child)| column.push(key, child))
}
/// Sets the vertical spacing _between_ elements. /// Sets the vertical spacing _between_ elements.
/// ///
/// Custom margins per element do not exist in iced. You should use this /// Custom margins per element do not exist in iced. You should use this
@ -108,8 +101,19 @@ where
key: Key, key: Key,
child: impl Into<Element<'a, Message, Renderer>>, child: impl Into<Element<'a, Message, Renderer>>,
) -> Self { ) -> Self {
let child = child.into();
let size = child.as_widget().size_hint();
if size.width.is_fill() {
self.width = Length::Fill;
}
if size.height.is_fill() {
self.height = Length::Fill;
}
self.keys.push(key); self.keys.push(key);
self.children.push(child.into()); self.children.push(child);
self self
} }
} }
@ -117,6 +121,7 @@ where
impl<'a, Key, Message, Renderer> Default for Column<'a, Key, Message, Renderer> impl<'a, Key, Message, Renderer> Default for Column<'a, Key, Message, Renderer>
where where
Key: Copy + PartialEq, Key: Copy + PartialEq,
Renderer: crate::core::Renderer,
{ {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()