Make vertical scroll properties optional

Co-Authored-By: Austin M. Reppert <austinmreppert@gmail.com>
This commit is contained in:
Austin M. Reppert 2023-05-26 20:27:17 -04:00 committed by Héctor Ramón Jiménez
parent f63a9d1a79
commit fa04f40524
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 99 additions and 49 deletions

View file

@ -1,4 +1,6 @@
use iced::widget::scrollable::{Properties, Scrollbar, Scroller}; use iced::widget::scrollable::{
Properties, ScrollbarProperties, Scrollbar, Scroller,
};
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,
@ -199,12 +201,12 @@ impl Application for ScrollableDemo {
.spacing(40), .spacing(40),
) )
.height(Length::Fill) .height(Length::Fill)
.vertical_scroll( .scrollbar_properties(ScrollbarProperties::Vertical(
Properties::new() Properties::new()
.width(self.scrollbar_width) .width(self.scrollbar_width)
.margin(self.scrollbar_margin) .margin(self.scrollbar_margin)
.scroller_width(self.scroller_width), .scroller_width(self.scroller_width),
) ))
.id(SCROLLABLE_ID.clone()) .id(SCROLLABLE_ID.clone())
.on_scroll(Message::Scrolled), .on_scroll(Message::Scrolled),
Direction::Horizontal => scrollable( Direction::Horizontal => scrollable(
@ -223,12 +225,12 @@ impl Application for ScrollableDemo {
.spacing(40), .spacing(40),
) )
.height(Length::Fill) .height(Length::Fill)
.horizontal_scroll( .scrollbar_properties(ScrollbarProperties::Horizontal(
Properties::new() Properties::new()
.width(self.scrollbar_width) .width(self.scrollbar_width)
.margin(self.scrollbar_margin) .margin(self.scrollbar_margin)
.scroller_width(self.scroller_width), .scroller_width(self.scroller_width),
) ))
.style(theme::Scrollable::custom(ScrollbarCustomStyle)) .style(theme::Scrollable::custom(ScrollbarCustomStyle))
.id(SCROLLABLE_ID.clone()) .id(SCROLLABLE_ID.clone())
.on_scroll(Message::Scrolled), .on_scroll(Message::Scrolled),
@ -264,18 +266,16 @@ impl Application for ScrollableDemo {
.spacing(40), .spacing(40),
) )
.height(Length::Fill) .height(Length::Fill)
.vertical_scroll( .scrollbar_properties(ScrollbarProperties::Both(
Properties::new() Properties::new()
.width(self.scrollbar_width) .width(self.scrollbar_width)
.margin(self.scrollbar_margin) .margin(self.scrollbar_margin)
.scroller_width(self.scroller_width), .scroller_width(self.scroller_width),
)
.horizontal_scroll(
Properties::new() Properties::new()
.width(self.scrollbar_width) .width(self.scrollbar_width)
.margin(self.scrollbar_margin) .margin(self.scrollbar_margin)
.scroller_width(self.scroller_width), .scroller_width(self.scroller_width),
) ))
.style(theme::Scrollable::Custom(Box::new( .style(theme::Scrollable::Custom(Box::new(
ScrollbarCustomStyle, ScrollbarCustomStyle,
))) )))

View file

@ -29,8 +29,7 @@ where
id: Option<Id>, id: Option<Id>,
width: Length, width: Length,
height: Length, height: Length,
vertical: Properties, scrollbar_properties: ScrollbarProperties,
horizontal: Option<Properties>,
content: Element<'a, Message, Renderer>, content: Element<'a, Message, Renderer>,
on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>, on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
@ -47,8 +46,7 @@ where
id: None, id: None,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
vertical: Properties::default(), scrollbar_properties: Default::default(),
horizontal: None,
content: content.into(), content: content.into(),
on_scroll: None, on_scroll: None,
style: Default::default(), style: Default::default(),
@ -73,15 +71,12 @@ where
self self
} }
/// Configures the vertical scrollbar of the [`Scrollable`] . /// Configures the scrollbar(s) of the [`Scrollable`] .
pub fn vertical_scroll(mut self, properties: Properties) -> Self { pub fn scrollbar_properties(
self.vertical = properties; mut self,
self scrollbar_properties: ScrollbarProperties,
} ) -> Self {
self.scrollbar_properties = scrollbar_properties;
/// Configures the horizontal scrollbar of the [`Scrollable`] .
pub fn horizontal_scroll(mut self, properties: Properties) -> Self {
self.horizontal = Some(properties);
self self
} }
@ -103,6 +98,43 @@ where
} }
} }
/// Properties of the scrollbar(s) within a [`Scrollable`].
#[derive(Debug)]
pub enum ScrollbarProperties {
/// Vertical Scrollbar.
Vertical(Properties),
/// Horizontal Scrollbar.
Horizontal(Properties),
/// Both Vertical and Horizontal Scrollbars.
Both(Properties, Properties),
}
impl ScrollbarProperties {
/// Returns the horizontal [`Properties`] of the [`ScrollbarProperties`].
pub fn horizontal(&self) -> Option<&Properties> {
match self {
Self::Horizontal(properties) => Some(properties),
Self::Both(_, properties) => Some(properties),
_ => None,
}
}
/// Returns the vertical [`Properties`] of the [`ScrollbarProperties`].
pub fn vertical(&self) -> Option<&Properties> {
match self {
Self::Vertical(properties) => Some(properties),
Self::Both(properties, _) => Some(properties),
_ => None,
}
}
}
impl Default for ScrollbarProperties {
fn default() -> Self {
Self::Vertical(Properties::default())
}
}
/// Properties of a scrollbar within a [`Scrollable`]. /// Properties of a scrollbar within a [`Scrollable`].
#[derive(Debug)] #[derive(Debug)]
pub struct Properties { pub struct Properties {
@ -186,7 +218,8 @@ where
limits, limits,
self.width, self.width,
self.height, self.height,
self.horizontal.is_some(), self.scrollbar_properties.horizontal().is_some(),
self.scrollbar_properties.vertical().is_some(),
|renderer, limits| { |renderer, limits| {
self.content.as_widget().layout(renderer, limits) self.content.as_widget().layout(renderer, limits)
}, },
@ -234,8 +267,7 @@ where
cursor, cursor,
clipboard, clipboard,
shell, shell,
&self.vertical, &self.scrollbar_properties,
self.horizontal.as_ref(),
&self.on_scroll, &self.on_scroll,
|event, layout, cursor, clipboard, shell| { |event, layout, cursor, clipboard, shell| {
self.content.as_widget_mut().on_event( self.content.as_widget_mut().on_event(
@ -267,8 +299,7 @@ where
theme, theme,
layout, layout,
cursor, cursor,
&self.vertical, &self.scrollbar_properties,
self.horizontal.as_ref(),
&self.style, &self.style,
|renderer, layout, cursor, viewport| { |renderer, layout, cursor, viewport| {
self.content.as_widget().draw( self.content.as_widget().draw(
@ -296,8 +327,7 @@ where
tree.state.downcast_ref::<State>(), tree.state.downcast_ref::<State>(),
layout, layout,
cursor, cursor,
&self.vertical, &self.scrollbar_properties,
self.horizontal.as_ref(),
|layout, cursor, viewport| { |layout, cursor, viewport| {
self.content.as_widget().mouse_interaction( self.content.as_widget().mouse_interaction(
&tree.children[0], &tree.children[0],
@ -400,19 +430,24 @@ pub fn layout<Renderer>(
width: Length, width: Length,
height: Length, height: Length,
horizontal_enabled: bool, horizontal_enabled: bool,
vertical_enabled: bool,
layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
) -> layout::Node { ) -> layout::Node {
let limits = limits.width(width).height(height); let limits = limits.width(width).height(height);
let child_limits = layout::Limits::new( let child_limits = layout::Limits::new(
Size::new(limits.min().width, 0.0), Size::new(limits.min().width, limits.min().height),
Size::new( Size::new(
if horizontal_enabled { if horizontal_enabled {
f32::INFINITY f32::INFINITY
} else { } else {
limits.max().width limits.max().width
}, },
f32::MAX, if vertical_enabled {
f32::MAX
} else {
limits.max().height
},
), ),
); );
@ -431,8 +466,7 @@ pub fn update<Message>(
cursor: mouse::Cursor, cursor: mouse::Cursor,
clipboard: &mut dyn Clipboard, clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>, shell: &mut Shell<'_, Message>,
vertical: &Properties, scrollbar_properties: &ScrollbarProperties,
horizontal: Option<&Properties>,
on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>, on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>,
update_content: impl FnOnce( update_content: impl FnOnce(
Event, Event,
@ -449,7 +483,7 @@ pub fn update<Message>(
let content_bounds = content.bounds(); let content_bounds = content.bounds();
let scrollbars = let scrollbars =
Scrollbars::new(state, vertical, horizontal, bounds, content_bounds); Scrollbars::new(state, &scrollbar_properties, bounds, content_bounds);
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
scrollbars.is_mouse_over(cursor); scrollbars.is_mouse_over(cursor);
@ -531,6 +565,17 @@ pub fn update<Message>(
cursor_position.y - scroll_box_touched_at.y, cursor_position.y - scroll_box_touched_at.y,
); );
let delta = Vector::new(
delta.x
* scrollbar_properties
.horizontal()
.map_or(0.0, |_| 1.0),
delta.y
* scrollbar_properties
.vertical()
.map_or(0.0, |_| 1.0),
);
state.scroll(delta, bounds, content_bounds); state.scroll(delta, bounds, content_bounds);
state.scroll_area_touched_at = Some(cursor_position); state.scroll_area_touched_at = Some(cursor_position);
@ -713,8 +758,7 @@ pub fn mouse_interaction(
state: &State, state: &State,
layout: Layout<'_>, layout: Layout<'_>,
cursor: mouse::Cursor, cursor: mouse::Cursor,
vertical: &Properties, scrollbar_properties: &ScrollbarProperties,
horizontal: Option<&Properties>,
content_interaction: impl FnOnce( content_interaction: impl FnOnce(
Layout<'_>, Layout<'_>,
mouse::Cursor, mouse::Cursor,
@ -728,7 +772,7 @@ pub fn mouse_interaction(
let content_bounds = content_layout.bounds(); let content_bounds = content_layout.bounds();
let scrollbars = let scrollbars =
Scrollbars::new(state, vertical, horizontal, bounds, content_bounds); Scrollbars::new(state, scrollbar_properties, bounds, content_bounds);
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
scrollbars.is_mouse_over(cursor); scrollbars.is_mouse_over(cursor);
@ -768,8 +812,7 @@ pub fn draw<Renderer>(
theme: &Renderer::Theme, theme: &Renderer::Theme,
layout: Layout<'_>, layout: Layout<'_>,
cursor: mouse::Cursor, cursor: mouse::Cursor,
vertical: &Properties, scrollbar_properties: &ScrollbarProperties,
horizontal: Option<&Properties>,
style: &<Renderer::Theme as StyleSheet>::Style, style: &<Renderer::Theme as StyleSheet>::Style,
draw_content: impl FnOnce(&mut Renderer, Layout<'_>, mouse::Cursor, &Rectangle), draw_content: impl FnOnce(&mut Renderer, Layout<'_>, mouse::Cursor, &Rectangle),
) where ) where
@ -781,7 +824,7 @@ pub fn draw<Renderer>(
let content_bounds = content_layout.bounds(); let content_bounds = content_layout.bounds();
let scrollbars = let scrollbars =
Scrollbars::new(state, vertical, horizontal, bounds, content_bounds); Scrollbars::new(state, &scrollbar_properties, bounds, content_bounds);
let cursor_over_scrollable = cursor.position_over(bounds); let cursor_over_scrollable = cursor.position_over(bounds);
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
@ -1157,22 +1200,30 @@ impl Scrollbars {
/// Create y and/or x scrollbar(s) if content is overflowing the [`Scrollable`] bounds. /// Create y and/or x scrollbar(s) if content is overflowing the [`Scrollable`] bounds.
fn new( fn new(
state: &State, state: &State,
vertical: &Properties, scrollbar_properties: &ScrollbarProperties,
horizontal: Option<&Properties>,
bounds: Rectangle, bounds: Rectangle,
content_bounds: Rectangle, content_bounds: Rectangle,
) -> Self { ) -> Self {
let offset = state.offset(bounds, content_bounds); let offset = state.offset(bounds, content_bounds);
let show_scrollbar_x = horizontal.and_then(|h| { let show_scrollbar_x =
if content_bounds.width > bounds.width { scrollbar_properties.horizontal().and_then(|h| {
Some(h) if content_bounds.width > bounds.width {
Some(h)
} else {
None
}
});
let show_scrollbar_y = scrollbar_properties.vertical().and_then(|v| {
if content_bounds.height > bounds.height {
Some(v)
} else { } else {
None None
} }
}); });
let y_scrollbar = if content_bounds.height > bounds.height { let y_scrollbar = if let Some(vertical) = show_scrollbar_y {
let Properties { let Properties {
width, width,
margin, margin,
@ -1240,9 +1291,8 @@ impl Scrollbars {
// Need to adjust the width of the horizontal scrollbar if the vertical scrollbar // Need to adjust the width of the horizontal scrollbar if the vertical scrollbar
// is present // is present
let scrollbar_y_width = y_scrollbar.map_or(0.0, |_| { let scrollbar_y_width = show_scrollbar_y
vertical.width.max(vertical.scroller_width) + vertical.margin .map_or(0.0, |v| v.width.max(v.scroller_width) + v.margin);
});
let total_scrollbar_height = let total_scrollbar_height =
width.max(scroller_width) + 2.0 * margin; width.max(scroller_width) + 2.0 * margin;