Merge pull request #2287 from iced-rs/assert-scrollable-content

Assert `scrollable` content size never fills scrolling axis
This commit is contained in:
Héctor Ramón 2024-02-24 19:56:08 +01:00 committed by GitHub
commit f67db901ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 39 deletions

View file

@ -2,7 +2,7 @@ use iced::executor;
use iced::widget::scrollable::Properties; use iced::widget::scrollable::Properties;
use iced::widget::{ use iced::widget::{
button, column, container, horizontal_space, progress_bar, radio, row, button, column, container, horizontal_space, progress_bar, radio, row,
scrollable, slider, text, vertical_space, scrollable, slider, text, vertical_space, Scrollable,
}; };
use iced::{ use iced::{
Alignment, Application, Color, Command, Element, Length, Settings, Theme, Alignment, Application, Color, Command, Element, Length, Settings, Theme,
@ -212,7 +212,7 @@ impl Application for ScrollableDemo {
let scrollable_content: Element<Message> = let scrollable_content: Element<Message> =
Element::from(match self.scrollable_direction { Element::from(match self.scrollable_direction {
Direction::Vertical => scrollable( Direction::Vertical => Scrollable::with_direction(
column![ column![
scroll_to_end_button(), scroll_to_end_button(),
text("Beginning!"), text("Beginning!"),
@ -225,19 +225,19 @@ impl Application for ScrollableDemo {
.align_items(Alignment::Center) .align_items(Alignment::Center)
.padding([40, 0, 40, 0]) .padding([40, 0, 40, 0])
.spacing(40), .spacing(40),
scrollable::Direction::Vertical(
Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment),
),
) )
.width(Length::Fill) .width(Length::Fill)
.height(Length::Fill) .height(Length::Fill)
.direction(scrollable::Direction::Vertical(
Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment),
))
.id(SCROLLABLE_ID.clone()) .id(SCROLLABLE_ID.clone())
.on_scroll(Message::Scrolled), .on_scroll(Message::Scrolled),
Direction::Horizontal => scrollable( Direction::Horizontal => Scrollable::with_direction(
row![ row![
scroll_to_end_button(), scroll_to_end_button(),
text("Beginning!"), text("Beginning!"),
@ -251,19 +251,19 @@ impl Application for ScrollableDemo {
.align_items(Alignment::Center) .align_items(Alignment::Center)
.padding([0, 40, 0, 40]) .padding([0, 40, 0, 40])
.spacing(40), .spacing(40),
scrollable::Direction::Horizontal(
Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment),
),
) )
.width(Length::Fill) .width(Length::Fill)
.height(Length::Fill) .height(Length::Fill)
.direction(scrollable::Direction::Horizontal(
Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment),
))
.id(SCROLLABLE_ID.clone()) .id(SCROLLABLE_ID.clone())
.on_scroll(Message::Scrolled), .on_scroll(Message::Scrolled),
Direction::Multi => scrollable( Direction::Multi => Scrollable::with_direction(
//horizontal content //horizontal content
row![ row![
column![ column![
@ -293,21 +293,21 @@ impl Application for ScrollableDemo {
.align_items(Alignment::Center) .align_items(Alignment::Center)
.padding([0, 40, 0, 40]) .padding([0, 40, 0, 40])
.spacing(40), .spacing(40),
{
let properties = Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment);
scrollable::Direction::Both {
horizontal: properties,
vertical: properties,
}
},
) )
.width(Length::Fill) .width(Length::Fill)
.height(Length::Fill) .height(Length::Fill)
.direction({
let properties = Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment);
scrollable::Direction::Both {
horizontal: properties,
vertical: properties,
}
})
.id(SCROLLABLE_ID.clone()) .id(SCROLLABLE_ID.clone())
.on_scroll(Message::Scrolled), .on_scroll(Message::Scrolled),
}); });

View file

@ -47,16 +47,38 @@ where
Theme: StyleSheet, Theme: StyleSheet,
Renderer: crate::core::Renderer, Renderer: crate::core::Renderer,
{ {
/// Creates a new [`Scrollable`]. /// Creates a new vertical [`Scrollable`].
pub fn new( pub fn new(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self { ) -> Self {
Self::with_direction(content, Direction::default())
}
/// Creates a new [`Scrollable`] with the given [`Direction`].
pub fn with_direction(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
direction: Direction,
) -> Self {
let content = content.into();
debug_assert!(
direction.vertical().is_none()
|| !content.as_widget().size_hint().height.is_fill(),
"scrollable content must not fill its vertical scrolling axis"
);
debug_assert!(
direction.horizontal().is_none()
|| !content.as_widget().size_hint().width.is_fill(),
"scrollable content must not fill its horizontal scrolling axis"
);
Scrollable { Scrollable {
id: None, id: None,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
direction: Direction::default(), direction,
content: content.into(), content,
on_scroll: None, on_scroll: None,
style: Default::default(), style: Default::default(),
} }
@ -80,12 +102,6 @@ where
self self
} }
/// Sets the [`Direction`] of the [`Scrollable`] .
pub fn direction(mut self, direction: Direction) -> Self {
self.direction = direction;
self
}
/// Sets a function to call when the [`Scrollable`] is scrolled. /// Sets a function to call when the [`Scrollable`] is scrolled.
/// ///
/// The function takes the [`Viewport`] of the [`Scrollable`] /// The function takes the [`Viewport`] of the [`Scrollable`]