From fa04f40524ab7a4ee23bcbc09bc4960c05b192db Mon Sep 17 00:00:00 2001 From: "Austin M. Reppert" Date: Fri, 26 May 2023 20:27:17 -0400 Subject: [PATCH 1/4] Make vertical scroll properties optional Co-Authored-By: Austin M. Reppert --- examples/scrollable/src/main.rs | 18 ++--- widget/src/scrollable.rs | 130 ++++++++++++++++++++++---------- 2 files changed, 99 insertions(+), 49 deletions(-) diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 3038661ee1..1ba816090b 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -1,4 +1,6 @@ -use iced::widget::scrollable::{Properties, Scrollbar, Scroller}; +use iced::widget::scrollable::{ + Properties, ScrollbarProperties, Scrollbar, Scroller, +}; use iced::widget::{ button, column, container, horizontal_space, progress_bar, radio, row, scrollable, slider, text, vertical_space, @@ -199,12 +201,12 @@ impl Application for ScrollableDemo { .spacing(40), ) .height(Length::Fill) - .vertical_scroll( + .scrollbar_properties(ScrollbarProperties::Vertical( Properties::new() .width(self.scrollbar_width) .margin(self.scrollbar_margin) .scroller_width(self.scroller_width), - ) + )) .id(SCROLLABLE_ID.clone()) .on_scroll(Message::Scrolled), Direction::Horizontal => scrollable( @@ -223,12 +225,12 @@ impl Application for ScrollableDemo { .spacing(40), ) .height(Length::Fill) - .horizontal_scroll( + .scrollbar_properties(ScrollbarProperties::Horizontal( Properties::new() .width(self.scrollbar_width) .margin(self.scrollbar_margin) .scroller_width(self.scroller_width), - ) + )) .style(theme::Scrollable::custom(ScrollbarCustomStyle)) .id(SCROLLABLE_ID.clone()) .on_scroll(Message::Scrolled), @@ -264,18 +266,16 @@ impl Application for ScrollableDemo { .spacing(40), ) .height(Length::Fill) - .vertical_scroll( + .scrollbar_properties(ScrollbarProperties::Both( Properties::new() .width(self.scrollbar_width) .margin(self.scrollbar_margin) .scroller_width(self.scroller_width), - ) - .horizontal_scroll( Properties::new() .width(self.scrollbar_width) .margin(self.scrollbar_margin) .scroller_width(self.scroller_width), - ) + )) .style(theme::Scrollable::Custom(Box::new( ScrollbarCustomStyle, ))) diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 010befac2d..1a326848ab 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -29,8 +29,7 @@ where id: Option, width: Length, height: Length, - vertical: Properties, - horizontal: Option, + scrollbar_properties: ScrollbarProperties, content: Element<'a, Message, Renderer>, on_scroll: Option Message + 'a>>, style: ::Style, @@ -47,8 +46,7 @@ where id: None, width: Length::Shrink, height: Length::Shrink, - vertical: Properties::default(), - horizontal: None, + scrollbar_properties: Default::default(), content: content.into(), on_scroll: None, style: Default::default(), @@ -73,15 +71,12 @@ where self } - /// Configures the vertical scrollbar of the [`Scrollable`] . - pub fn vertical_scroll(mut self, properties: Properties) -> Self { - self.vertical = properties; - self - } - - /// Configures the horizontal scrollbar of the [`Scrollable`] . - pub fn horizontal_scroll(mut self, properties: Properties) -> Self { - self.horizontal = Some(properties); + /// Configures the scrollbar(s) of the [`Scrollable`] . + pub fn scrollbar_properties( + mut self, + scrollbar_properties: ScrollbarProperties, + ) -> Self { + self.scrollbar_properties = scrollbar_properties; 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`]. #[derive(Debug)] pub struct Properties { @@ -186,7 +218,8 @@ where limits, self.width, self.height, - self.horizontal.is_some(), + self.scrollbar_properties.horizontal().is_some(), + self.scrollbar_properties.vertical().is_some(), |renderer, limits| { self.content.as_widget().layout(renderer, limits) }, @@ -234,8 +267,7 @@ where cursor, clipboard, shell, - &self.vertical, - self.horizontal.as_ref(), + &self.scrollbar_properties, &self.on_scroll, |event, layout, cursor, clipboard, shell| { self.content.as_widget_mut().on_event( @@ -267,8 +299,7 @@ where theme, layout, cursor, - &self.vertical, - self.horizontal.as_ref(), + &self.scrollbar_properties, &self.style, |renderer, layout, cursor, viewport| { self.content.as_widget().draw( @@ -296,8 +327,7 @@ where tree.state.downcast_ref::(), layout, cursor, - &self.vertical, - self.horizontal.as_ref(), + &self.scrollbar_properties, |layout, cursor, viewport| { self.content.as_widget().mouse_interaction( &tree.children[0], @@ -400,19 +430,24 @@ pub fn layout( width: Length, height: Length, horizontal_enabled: bool, + vertical_enabled: bool, layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { let limits = limits.width(width).height(height); let child_limits = layout::Limits::new( - Size::new(limits.min().width, 0.0), + Size::new(limits.min().width, limits.min().height), Size::new( if horizontal_enabled { f32::INFINITY } else { limits.max().width }, - f32::MAX, + if vertical_enabled { + f32::MAX + } else { + limits.max().height + }, ), ); @@ -431,8 +466,7 @@ pub fn update( cursor: mouse::Cursor, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, - vertical: &Properties, - horizontal: Option<&Properties>, + scrollbar_properties: &ScrollbarProperties, on_scroll: &Option Message + '_>>, update_content: impl FnOnce( Event, @@ -449,7 +483,7 @@ pub fn update( let content_bounds = content.bounds(); 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) = scrollbars.is_mouse_over(cursor); @@ -531,6 +565,17 @@ pub fn update( 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_area_touched_at = Some(cursor_position); @@ -713,8 +758,7 @@ pub fn mouse_interaction( state: &State, layout: Layout<'_>, cursor: mouse::Cursor, - vertical: &Properties, - horizontal: Option<&Properties>, + scrollbar_properties: &ScrollbarProperties, content_interaction: impl FnOnce( Layout<'_>, mouse::Cursor, @@ -728,7 +772,7 @@ pub fn mouse_interaction( let content_bounds = content_layout.bounds(); 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) = scrollbars.is_mouse_over(cursor); @@ -768,8 +812,7 @@ pub fn draw( theme: &Renderer::Theme, layout: Layout<'_>, cursor: mouse::Cursor, - vertical: &Properties, - horizontal: Option<&Properties>, + scrollbar_properties: &ScrollbarProperties, style: &::Style, draw_content: impl FnOnce(&mut Renderer, Layout<'_>, mouse::Cursor, &Rectangle), ) where @@ -781,7 +824,7 @@ pub fn draw( let content_bounds = content_layout.bounds(); 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 (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. fn new( state: &State, - vertical: &Properties, - horizontal: Option<&Properties>, + scrollbar_properties: &ScrollbarProperties, bounds: Rectangle, content_bounds: Rectangle, ) -> Self { let offset = state.offset(bounds, content_bounds); - let show_scrollbar_x = horizontal.and_then(|h| { - if content_bounds.width > bounds.width { - Some(h) + let show_scrollbar_x = + scrollbar_properties.horizontal().and_then(|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 { None } }); - let y_scrollbar = if content_bounds.height > bounds.height { + let y_scrollbar = if let Some(vertical) = show_scrollbar_y { let Properties { width, margin, @@ -1240,9 +1291,8 @@ impl Scrollbars { // Need to adjust the width of the horizontal scrollbar if the vertical scrollbar // is present - let scrollbar_y_width = y_scrollbar.map_or(0.0, |_| { - vertical.width.max(vertical.scroller_width) + vertical.margin - }); + let scrollbar_y_width = show_scrollbar_y + .map_or(0.0, |v| v.width.max(v.scroller_width) + v.margin); let total_scrollbar_height = width.max(scroller_width) + 2.0 * margin; From 493571695a8853ee91309a92d04b8dbea29bab8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 27 Jun 2023 22:30:54 +0200 Subject: [PATCH 2/4] Rename `ScrollbarProperties` to `Direction` in `scrollable` --- examples/scrollable/src/main.rs | 26 ++++---- widget/src/scrollable.rs | 105 +++++++++++++++----------------- 2 files changed, 61 insertions(+), 70 deletions(-) diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 1ba816090b..4104871fd1 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -1,12 +1,11 @@ -use iced::widget::scrollable::{ - Properties, ScrollbarProperties, Scrollbar, Scroller, -}; +use iced::widget::scrollable::{Properties, Scrollbar, Scroller}; use iced::widget::{ button, column, container, horizontal_space, progress_bar, radio, row, scrollable, slider, text, vertical_space, }; use iced::{executor, theme, Alignment, Color}; use iced::{Application, Command, Element, Length, Settings, Theme}; + use once_cell::sync::Lazy; static SCROLLABLE_ID: Lazy = Lazy::new(scrollable::Id::unique); @@ -201,7 +200,7 @@ impl Application for ScrollableDemo { .spacing(40), ) .height(Length::Fill) - .scrollbar_properties(ScrollbarProperties::Vertical( + .direction(scrollable::Direction::Vertical( Properties::new() .width(self.scrollbar_width) .margin(self.scrollbar_margin) @@ -225,7 +224,7 @@ impl Application for ScrollableDemo { .spacing(40), ) .height(Length::Fill) - .scrollbar_properties(ScrollbarProperties::Horizontal( + .direction(scrollable::Direction::Horizontal( Properties::new() .width(self.scrollbar_width) .margin(self.scrollbar_margin) @@ -266,16 +265,17 @@ impl Application for ScrollableDemo { .spacing(40), ) .height(Length::Fill) - .scrollbar_properties(ScrollbarProperties::Both( - Properties::new() - .width(self.scrollbar_width) - .margin(self.scrollbar_margin) - .scroller_width(self.scroller_width), - Properties::new() + .direction({ + let properties = Properties::new() .width(self.scrollbar_width) .margin(self.scrollbar_margin) - .scroller_width(self.scroller_width), - )) + .scroller_width(self.scroller_width); + + scrollable::Direction::Both { + horizontal: properties, + vertical: properties, + } + }) .style(theme::Scrollable::Custom(Box::new( ScrollbarCustomStyle, ))) diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 1a326848ab..82f71dff20 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -29,7 +29,7 @@ where id: Option, width: Length, height: Length, - scrollbar_properties: ScrollbarProperties, + direction: Direction, content: Element<'a, Message, Renderer>, on_scroll: Option Message + 'a>>, style: ::Style, @@ -46,7 +46,7 @@ where id: None, width: Length::Shrink, height: Length::Shrink, - scrollbar_properties: Default::default(), + direction: Default::default(), content: content.into(), on_scroll: None, style: Default::default(), @@ -71,12 +71,9 @@ where self } - /// Configures the scrollbar(s) of the [`Scrollable`] . - pub fn scrollbar_properties( - mut self, - scrollbar_properties: ScrollbarProperties, - ) -> Self { - self.scrollbar_properties = scrollbar_properties; + /// Sets the [`Direction`] of the [`Scrollable`] . + pub fn direction(mut self, direction: Direction) -> Self { + self.direction = direction; self } @@ -98,45 +95,50 @@ where } } -/// Properties of the scrollbar(s) within a [`Scrollable`]. -#[derive(Debug)] -pub enum ScrollbarProperties { - /// Vertical Scrollbar. +/// The direction of [`Scrollable`]. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Direction { + /// Vertical scrolling Vertical(Properties), - /// Horizontal Scrollbar. + /// Horizontal scrolling Horizontal(Properties), - /// Both Vertical and Horizontal Scrollbars. - Both(Properties, Properties), + /// Both vertical and horizontal scrolling + Both { + /// The properties of the vertical scrollbar. + vertical: Properties, + /// The properties of the horizontal scrollbar. + horizontal: Properties, + }, } -impl ScrollbarProperties { - /// Returns the horizontal [`Properties`] of the [`ScrollbarProperties`]. +impl Direction { + /// Returns the [`Properties`] of the horizontal scrollbar, if any. pub fn horizontal(&self) -> Option<&Properties> { match self { Self::Horizontal(properties) => Some(properties), - Self::Both(_, properties) => Some(properties), + Self::Both { horizontal, .. } => Some(horizontal), _ => None, } } - /// Returns the vertical [`Properties`] of the [`ScrollbarProperties`]. + /// Returns the [`Properties`] of the vertical scrollbar, if any. pub fn vertical(&self) -> Option<&Properties> { match self { Self::Vertical(properties) => Some(properties), - Self::Both(properties, _) => Some(properties), + Self::Both { vertical, .. } => Some(vertical), _ => None, } } } -impl Default for ScrollbarProperties { +impl Default for Direction { fn default() -> Self { Self::Vertical(Properties::default()) } } /// Properties of a scrollbar within a [`Scrollable`]. -#[derive(Debug)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Properties { width: f32, margin: f32, @@ -218,8 +220,7 @@ where limits, self.width, self.height, - self.scrollbar_properties.horizontal().is_some(), - self.scrollbar_properties.vertical().is_some(), + &self.direction, |renderer, limits| { self.content.as_widget().layout(renderer, limits) }, @@ -267,7 +268,7 @@ where cursor, clipboard, shell, - &self.scrollbar_properties, + &self.direction, &self.on_scroll, |event, layout, cursor, clipboard, shell| { self.content.as_widget_mut().on_event( @@ -299,7 +300,7 @@ where theme, layout, cursor, - &self.scrollbar_properties, + &self.direction, &self.style, |renderer, layout, cursor, viewport| { self.content.as_widget().draw( @@ -327,7 +328,7 @@ where tree.state.downcast_ref::(), layout, cursor, - &self.scrollbar_properties, + &self.direction, |layout, cursor, viewport| { self.content.as_widget().mouse_interaction( &tree.children[0], @@ -429,8 +430,7 @@ pub fn layout( limits: &layout::Limits, width: Length, height: Length, - horizontal_enabled: bool, - vertical_enabled: bool, + direction: &Direction, layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { let limits = limits.width(width).height(height); @@ -438,12 +438,12 @@ pub fn layout( let child_limits = layout::Limits::new( Size::new(limits.min().width, limits.min().height), Size::new( - if horizontal_enabled { + if direction.horizontal().is_some() { f32::INFINITY } else { limits.max().width }, - if vertical_enabled { + if direction.vertical().is_some() { f32::MAX } else { limits.max().height @@ -466,7 +466,7 @@ pub fn update( cursor: mouse::Cursor, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, - scrollbar_properties: &ScrollbarProperties, + direction: &Direction, on_scroll: &Option Message + '_>>, update_content: impl FnOnce( Event, @@ -482,8 +482,7 @@ pub fn update( let content = layout.children().next().unwrap(); let content_bounds = content.bounds(); - let scrollbars = - Scrollbars::new(state, &scrollbar_properties, bounds, content_bounds); + let scrollbars = Scrollbars::new(state, direction, bounds, content_bounds); let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = scrollbars.is_mouse_over(cursor); @@ -567,13 +566,8 @@ pub fn update( 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), + * direction.horizontal().map_or(0.0, |_| 1.0), + delta.y * direction.vertical().map_or(0.0, |_| 1.0), ); state.scroll(delta, bounds, content_bounds); @@ -758,7 +752,7 @@ pub fn mouse_interaction( state: &State, layout: Layout<'_>, cursor: mouse::Cursor, - scrollbar_properties: &ScrollbarProperties, + direction: &Direction, content_interaction: impl FnOnce( Layout<'_>, mouse::Cursor, @@ -771,8 +765,7 @@ pub fn mouse_interaction( let content_layout = layout.children().next().unwrap(); let content_bounds = content_layout.bounds(); - let scrollbars = - Scrollbars::new(state, scrollbar_properties, bounds, content_bounds); + let scrollbars = Scrollbars::new(state, direction, bounds, content_bounds); let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = scrollbars.is_mouse_over(cursor); @@ -812,7 +805,7 @@ pub fn draw( theme: &Renderer::Theme, layout: Layout<'_>, cursor: mouse::Cursor, - scrollbar_properties: &ScrollbarProperties, + direction: &Direction, style: &::Style, draw_content: impl FnOnce(&mut Renderer, Layout<'_>, mouse::Cursor, &Rectangle), ) where @@ -823,8 +816,7 @@ pub fn draw( let content_layout = layout.children().next().unwrap(); let content_bounds = content_layout.bounds(); - let scrollbars = - Scrollbars::new(state, &scrollbar_properties, bounds, content_bounds); + let scrollbars = Scrollbars::new(state, direction, bounds, content_bounds); let cursor_over_scrollable = cursor.position_over(bounds); let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = @@ -1200,22 +1192,21 @@ impl Scrollbars { /// Create y and/or x scrollbar(s) if content is overflowing the [`Scrollable`] bounds. fn new( state: &State, - scrollbar_properties: &ScrollbarProperties, + direction: &Direction, bounds: Rectangle, content_bounds: Rectangle, ) -> Self { let offset = state.offset(bounds, content_bounds); - let show_scrollbar_x = - scrollbar_properties.horizontal().and_then(|h| { - if content_bounds.width > bounds.width { - Some(h) - } else { - None - } - }); + let show_scrollbar_x = direction.horizontal().and_then(|h| { + if content_bounds.width > bounds.width { + Some(h) + } else { + None + } + }); - let show_scrollbar_y = scrollbar_properties.vertical().and_then(|v| { + let show_scrollbar_y = direction.vertical().and_then(|v| { if content_bounds.height > bounds.height { Some(v) } else { From 412e15b170a61f7d7369122d7d0b089491e1b0ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 27 Jun 2023 22:41:16 +0200 Subject: [PATCH 3/4] Require a `Direction` when computing `State::offset` in `scrollable` --- widget/src/scrollable.rs | 41 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 82f71dff20..5bc6914c16 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -358,10 +358,11 @@ where let bounds = layout.bounds(); let content_layout = layout.children().next().unwrap(); let content_bounds = content_layout.bounds(); - let offset = tree - .state - .downcast_ref::() - .offset(bounds, content_bounds); + let offset = tree.state.downcast_ref::().offset( + &self.direction, + bounds, + content_bounds, + ); overlay.translate(Vector::new(-offset.x, -offset.y)) }) @@ -493,7 +494,8 @@ pub fn update( if !(mouse_over_x_scrollbar || mouse_over_y_scrollbar) => { mouse::Cursor::Available( - cursor_position + state.offset(bounds, content_bounds), + cursor_position + + state.offset(direction, bounds, content_bounds), ) } _ => mouse::Cursor::Unavailable, @@ -564,12 +566,6 @@ pub fn update( cursor_position.y - scroll_box_touched_at.y, ); - let delta = Vector::new( - delta.x - * direction.horizontal().map_or(0.0, |_| 1.0), - delta.y * direction.vertical().map_or(0.0, |_| 1.0), - ); - state.scroll(delta, bounds, content_bounds); state.scroll_area_touched_at = Some(cursor_position); @@ -775,7 +771,7 @@ pub fn mouse_interaction( { mouse::Interaction::Idle } else { - let offset = state.offset(bounds, content_bounds); + let offset = state.offset(direction, bounds, content_bounds); let cursor = match cursor_over_scrollable { Some(cursor_position) @@ -822,7 +818,7 @@ pub fn draw( let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = scrollbars.is_mouse_over(cursor); - let offset = state.offset(bounds, content_bounds); + let offset = state.offset(direction, bounds, content_bounds); let cursor = match cursor_over_scrollable { Some(cursor_position) @@ -1161,16 +1157,25 @@ impl State { ); } - /// Returns the scrolling offset of the [`State`], given the bounds of the - /// [`Scrollable`] and its contents. + /// Returns the scrolling offset of the [`State`], given a [`Direction`], + /// the bounds of the [`Scrollable`] and its contents. pub fn offset( &self, + direction: &Direction, bounds: Rectangle, content_bounds: Rectangle, ) -> Vector { Vector::new( - self.offset_x.absolute(bounds.width, content_bounds.width), - self.offset_y.absolute(bounds.height, content_bounds.height), + if direction.horizontal().is_some() { + self.offset_x.absolute(bounds.width, content_bounds.width) + } else { + 0.0 + }, + if direction.vertical().is_some() { + self.offset_y.absolute(bounds.height, content_bounds.height) + } else { + 0.0 + }, ) } @@ -1196,7 +1201,7 @@ impl Scrollbars { bounds: Rectangle, content_bounds: Rectangle, ) -> Self { - let offset = state.offset(bounds, content_bounds); + let offset = state.offset(direction, bounds, content_bounds); let show_scrollbar_x = direction.horizontal().and_then(|h| { if content_bounds.width > bounds.width { From 1c26440f0bd8f7a002946524dd4d522ba9fb7f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 27 Jun 2023 22:46:04 +0200 Subject: [PATCH 4/4] Use `Option::filter` instead of `and_then` in `scrollable` --- widget/src/scrollable.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 5bc6914c16..473124ca0c 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -1203,21 +1203,13 @@ impl Scrollbars { ) -> Self { let offset = state.offset(direction, bounds, content_bounds); - let show_scrollbar_x = direction.horizontal().and_then(|h| { - if content_bounds.width > bounds.width { - Some(h) - } else { - None - } - }); + let show_scrollbar_x = direction + .horizontal() + .filter(|_| content_bounds.width > bounds.width); - let show_scrollbar_y = direction.vertical().and_then(|v| { - if content_bounds.height > bounds.height { - Some(v) - } else { - None - } - }); + let show_scrollbar_y = direction + .vertical() + .filter(|_| content_bounds.height > bounds.height); let y_scrollbar = if let Some(vertical) = show_scrollbar_y { let Properties {