From 11287c882e23e1f3081d06bf5ff3e99a02ef030a Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 12 Jul 2023 16:30:12 -0700 Subject: Expose methods to change viewport alignment --- widget/src/scrollable.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 88746ac4..3f49584c 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -554,7 +554,14 @@ pub fn update( state.scroll(delta, direction, bounds, content_bounds); - notify_on_scroll(state, on_scroll, bounds, content_bounds, shell); + notify_on_scroll( + state, + on_scroll, + bounds, + content_bounds, + direction, + shell, + ); return event::Status::Captured; } @@ -592,6 +599,7 @@ pub fn update( on_scroll, bounds, content_bounds, + direction, shell, ); } @@ -637,6 +645,7 @@ pub fn update( on_scroll, bounds, content_bounds, + direction, shell, ); @@ -672,6 +681,7 @@ pub fn update( on_scroll, bounds, content_bounds, + direction, shell, ); } @@ -712,6 +722,7 @@ pub fn update( on_scroll, bounds, content_bounds, + direction, shell, ); } @@ -747,6 +758,7 @@ pub fn update( on_scroll, bounds, content_bounds, + direction, shell, ); @@ -962,6 +974,7 @@ fn notify_on_scroll( on_scroll: &Option Message + '_>>, bounds: Rectangle, content_bounds: Rectangle, + direction: Direction, shell: &mut Shell<'_, Message>, ) { if let Some(on_scroll) = on_scroll { @@ -971,11 +984,23 @@ fn notify_on_scroll( return; } + let horizontal_alignment = direction + .horizontal() + .map(|p| p.alignment) + .unwrap_or_default(); + + let vertical_alignment = direction + .vertical() + .map(|p| p.alignment) + .unwrap_or_default(); + let viewport = Viewport { offset_x: state.offset_x, offset_y: state.offset_y, bounds, content_bounds, + vertical_alignment, + horizontal_alignment, }; // Don't publish redundant viewports to shell @@ -1080,6 +1105,8 @@ pub struct Viewport { offset_y: Offset, bounds: Rectangle, content_bounds: Rectangle, + vertical_alignment: Alignment, + horizontal_alignment: Alignment, } impl Viewport { @@ -1104,6 +1131,36 @@ impl Viewport { RelativeOffset { x, y } } + + /// Returns a new [`Viewport`] with the supplied vertical [`Alignment`]. + pub fn with_vertical_alignment(self, alignment: Alignment) -> Self { + if self.vertical_alignment != alignment { + let relative = 1.0 - self.relative_offset().y; + + Self { + offset_y: Offset::Relative(relative), + vertical_alignment: alignment, + ..self + } + } else { + self + } + } + + /// Returns a new [`Viewport`] with the supplied horizontal [`Alignment`]. + pub fn with_horizontal_alignment(self, alignment: Alignment) -> Self { + if self.horizontal_alignment != alignment { + let relative = 1.0 - self.relative_offset().x; + + Self { + offset_x: Offset::Relative(relative), + horizontal_alignment: alignment, + ..self + } + } else { + self + } + } } impl State { -- cgit From a9987cb32e4d65b83f134ba54f51dffe16e93a50 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 13 Jul 2023 02:53:45 +0200 Subject: Introduce `absolute_offset_reversed` to `scrollable::Viewport` --- widget/src/scrollable.rs | 75 +++++++++++------------------------------------- 1 file changed, 17 insertions(+), 58 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 3f49584c..5cd94538 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -554,14 +554,7 @@ pub fn update( state.scroll(delta, direction, bounds, content_bounds); - notify_on_scroll( - state, - on_scroll, - bounds, - content_bounds, - direction, - shell, - ); + notify_on_scroll(state, on_scroll, bounds, content_bounds, shell); return event::Status::Captured; } @@ -599,7 +592,6 @@ pub fn update( on_scroll, bounds, content_bounds, - direction, shell, ); } @@ -645,7 +637,6 @@ pub fn update( on_scroll, bounds, content_bounds, - direction, shell, ); @@ -681,7 +672,6 @@ pub fn update( on_scroll, bounds, content_bounds, - direction, shell, ); } @@ -722,7 +712,6 @@ pub fn update( on_scroll, bounds, content_bounds, - direction, shell, ); } @@ -758,7 +747,6 @@ pub fn update( on_scroll, bounds, content_bounds, - direction, shell, ); @@ -974,7 +962,6 @@ fn notify_on_scroll( on_scroll: &Option Message + '_>>, bounds: Rectangle, content_bounds: Rectangle, - direction: Direction, shell: &mut Shell<'_, Message>, ) { if let Some(on_scroll) = on_scroll { @@ -984,23 +971,11 @@ fn notify_on_scroll( return; } - let horizontal_alignment = direction - .horizontal() - .map(|p| p.alignment) - .unwrap_or_default(); - - let vertical_alignment = direction - .vertical() - .map(|p| p.alignment) - .unwrap_or_default(); - let viewport = Viewport { offset_x: state.offset_x, offset_y: state.offset_y, bounds, content_bounds, - vertical_alignment, - horizontal_alignment, }; // Don't publish redundant viewports to shell @@ -1105,8 +1080,6 @@ pub struct Viewport { offset_y: Offset, bounds: Rectangle, content_bounds: Rectangle, - vertical_alignment: Alignment, - horizontal_alignment: Alignment, } impl Viewport { @@ -1122,6 +1095,22 @@ impl Viewport { AbsoluteOffset { x, y } } + /// Returns the [`AbsoluteOffset`] of the current [`Viewport`], but with its + /// alignment reversed. + /// + /// This method can be useful to switch the alignment of a [`Scrollable`] + /// while maintaining its scrolling position. + pub fn absolute_offset_reversed(&self) -> AbsoluteOffset { + let AbsoluteOffset { x, y } = self.absolute_offset(); + + AbsoluteOffset { + x: ((self.content_bounds.width - self.bounds.width).max(0.0) - x) + .max(0.0), + y: ((self.content_bounds.height - self.bounds.height).max(0.0) - y) + .max(0.0), + } + } + /// Returns the [`RelativeOffset`] of the current [`Viewport`]. pub fn relative_offset(&self) -> RelativeOffset { let AbsoluteOffset { x, y } = self.absolute_offset(); @@ -1131,36 +1120,6 @@ impl Viewport { RelativeOffset { x, y } } - - /// Returns a new [`Viewport`] with the supplied vertical [`Alignment`]. - pub fn with_vertical_alignment(self, alignment: Alignment) -> Self { - if self.vertical_alignment != alignment { - let relative = 1.0 - self.relative_offset().y; - - Self { - offset_y: Offset::Relative(relative), - vertical_alignment: alignment, - ..self - } - } else { - self - } - } - - /// Returns a new [`Viewport`] with the supplied horizontal [`Alignment`]. - pub fn with_horizontal_alignment(self, alignment: Alignment) -> Self { - if self.horizontal_alignment != alignment { - let relative = 1.0 - self.relative_offset().x; - - Self { - offset_x: Offset::Relative(relative), - horizontal_alignment: alignment, - ..self - } - } else { - self - } - } } impl State { -- cgit From d36758405789d6a305d978eefb46a1ca90d141d2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 13 Jul 2023 03:01:53 +0200 Subject: Avoid redundant `max` in `absolute_offset_reversed` I believe `Offset::absolute` guarantees the offset never exceeds the maximum scrolling boundaries already. --- widget/src/scrollable.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 5cd94538..e0aeeebd 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -1104,10 +1104,8 @@ impl Viewport { let AbsoluteOffset { x, y } = self.absolute_offset(); AbsoluteOffset { - x: ((self.content_bounds.width - self.bounds.width).max(0.0) - x) - .max(0.0), - y: ((self.content_bounds.height - self.bounds.height).max(0.0) - y) - .max(0.0), + x: (self.content_bounds.width - self.bounds.width).max(0.0) - x, + y: (self.content_bounds.height - self.bounds.height).max(0.0) - y, } } -- cgit From 42c423b4a89613c4e1c552c891c1391a34837122 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Sat, 15 Jul 2023 10:04:25 -0700 Subject: Add viewport to Widget::on_event --- widget/src/scrollable.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index e0aeeebd..f621fb26 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -278,6 +278,7 @@ where renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, + _viewport: &Rectangle, ) -> event::Status { update( tree.state.downcast_mut::(), @@ -288,7 +289,7 @@ where shell, self.direction, &self.on_scroll, - |event, layout, cursor, clipboard, shell| { + |event, layout, cursor, clipboard, shell, viewport| { self.content.as_widget_mut().on_event( &mut tree.children[0], event, @@ -297,6 +298,7 @@ where renderer, clipboard, shell, + viewport, ) }, ) @@ -492,6 +494,7 @@ pub fn update( mouse::Cursor, &mut dyn Clipboard, &mut Shell<'_, Message>, + &Rectangle, ) -> event::Status, ) -> event::Status { let bounds = layout.bounds(); @@ -518,7 +521,20 @@ pub fn update( _ => mouse::Cursor::Unavailable, }; - update_content(event.clone(), content, cursor, clipboard, shell) + let translation = state.translation(direction, bounds, content_bounds); + + update_content( + event.clone(), + content, + cursor, + clipboard, + shell, + &Rectangle { + y: bounds.y + translation.y, + x: bounds.x + translation.x, + ..bounds + }, + ) }; if let event::Status::Captured = event_status { -- cgit From e2ba7ece83f141c149659747977147392df008f4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 27 Jul 2023 01:02:28 +0200 Subject: Introduce `visible_bounds` operation for `Container` --- widget/src/scrollable.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index f621fb26..103e3944 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -254,10 +254,22 @@ where ) { let state = tree.state.downcast_mut::(); - operation.scrollable(state, self.id.as_ref().map(|id| &id.0)); + let bounds = layout.bounds(); + let content_layout = layout.children().next().unwrap(); + let content_bounds = content_layout.bounds(); + let translation = + state.translation(self.direction, bounds, content_bounds); + + operation.scrollable( + state, + self.id.as_ref().map(|id| &id.0), + bounds, + translation, + ); operation.container( self.id.as_ref().map(|id| &id.0), + bounds, &mut |operation| { self.content.as_widget().operate( &mut tree.children[0], -- cgit From 36120d5685048f761caf4b6a12a4f3a6007f9363 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 26 Aug 2023 01:31:11 +0200 Subject: Run `cargo fmt` with Rust 1.72 --- widget/src/scrollable.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 103e3944..a83ed985 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -593,7 +593,7 @@ pub fn update( match event { touch::Event::FingerPressed { .. } => { let Some(cursor_position) = cursor.position() else { - return event::Status::Ignored + return event::Status::Ignored; }; state.scroll_area_touched_at = Some(cursor_position); @@ -603,7 +603,7 @@ pub fn update( state.scroll_area_touched_at { let Some(cursor_position) = cursor.position() else { - return event::Status::Ignored + return event::Status::Ignored; }; let delta = Vector::new( @@ -648,7 +648,7 @@ pub fn update( | Event::Touch(touch::Event::FingerMoved { .. }) => { if let Some(scrollbar) = scrollbars.y { let Some(cursor_position) = cursor.position() else { - return event::Status::Ignored + return event::Status::Ignored; }; state.scroll_y_to( @@ -678,7 +678,7 @@ pub fn update( Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) | Event::Touch(touch::Event::FingerPressed { .. }) => { let Some(cursor_position) = cursor.position() else { - return event::Status::Ignored + return event::Status::Ignored; }; if let (Some(scroller_grabbed_at), Some(scrollbar)) = @@ -722,7 +722,7 @@ pub fn update( Event::Mouse(mouse::Event::CursorMoved { .. }) | Event::Touch(touch::Event::FingerMoved { .. }) => { let Some(cursor_position) = cursor.position() else { - return event::Status::Ignored + return event::Status::Ignored; }; if let Some(scrollbar) = scrollbars.x { @@ -753,7 +753,7 @@ pub fn update( Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) | Event::Touch(touch::Event::FingerPressed { .. }) => { let Some(cursor_position) = cursor.position() else { - return event::Status::Ignored + return event::Status::Ignored; }; if let (Some(scroller_grabbed_at), Some(scrollbar)) = -- cgit From ed3454301e663a7cb7d73cd56b57b188f4d14a2f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 30 Aug 2023 04:31:21 +0200 Subject: Implement explicit text caching in the widget state tree --- widget/src/scrollable.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index a83ed985..ce96883d 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -230,6 +230,7 @@ where fn layout( &self, + tree: &Tree, renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { @@ -240,7 +241,11 @@ where self.height, &self.direction, |renderer, limits| { - self.content.as_widget().layout(renderer, limits) + self.content.as_widget().layout( + &tree.children[0], + renderer, + limits, + ) }, ) } -- cgit From a026e917d3364e58fd827995261158d8cb356ce9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 30 Aug 2023 06:36:24 +0200 Subject: Make `widget::Tree` mutable in `Widget::layout` --- widget/src/scrollable.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index ce96883d..5decfd9d 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -230,7 +230,7 @@ where fn layout( &self, - tree: &Tree, + tree: &mut Tree, renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { @@ -242,7 +242,7 @@ where &self.direction, |renderer, limits| { self.content.as_widget().layout( - &tree.children[0], + &mut tree.children[0], renderer, limits, ) -- cgit From e5afaa08924cb0f34789b5a7de1720dc91978923 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 6 Sep 2023 21:50:59 -0400 Subject: Add access to bounds/content bounds from a scrollable viewport. (#2072) * Add access to bounds/content bounds from a scrollable viewport in order to perform certain scrollable optimizations as a consumer. * Move bounds/content_bounds after relative_offset as per feedback. --- widget/src/scrollable.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index a83ed985..d0c77e6b 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -1146,6 +1146,16 @@ impl Viewport { RelativeOffset { x, y } } + + /// Returns the bounds of the current [`Viewport`]. + pub fn bounds(&self) -> Rectangle { + self.bounds + } + + /// Returns the content bounds of the current [`Viewport`]. + pub fn content_bounds(&self) -> Rectangle { + self.content_bounds + } } impl State { -- cgit From 2c782bbe7a048f6f091e15f68de29a846b9bb059 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Sep 2023 19:35:29 +0200 Subject: Fix width of horizontal scrollbar in `Scrollable` --- widget/src/scrollable.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index def28821..7b1d7a30 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -1399,8 +1399,8 @@ impl Scrollbars { // Need to adjust the width of the horizontal scrollbar if the vertical scrollbar // is present - let scrollbar_y_width = show_scrollbar_y - .map_or(0.0, |v| v.width.max(v.scroller_width) + v.margin); + let scrollbar_y_width = y_scrollbar + .map_or(0.0, |scrollbar| scrollbar.total_bounds.width); let total_scrollbar_height = width.max(scroller_width) + 2.0 * margin; @@ -1425,12 +1425,12 @@ impl Scrollbars { let ratio = bounds.width / content_bounds.width; // min width for easier grabbing with extra wide content - let scroller_length = (bounds.width * ratio).max(2.0); - let scroller_offset = translation.x * ratio; + let scroller_length = (scrollbar_bounds.width * ratio).max(2.0); + let scroller_offset = + translation.x * ratio * scrollbar_bounds.width / bounds.width; let scroller_bounds = Rectangle { - x: (scrollbar_bounds.x + scroller_offset - scrollbar_y_width) - .max(0.0), + x: (scrollbar_bounds.x + scroller_offset).max(0.0), y: bounds.y + bounds.height - total_scrollbar_height / 2.0 - scroller_width / 2.0, -- cgit From ed11b04f6054809ea55ec9b3f0bd221ac2caf9ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Sep 2023 22:59:02 +0200 Subject: Fix `height` of vertical scroller in `Scrollbar` --- widget/src/scrollable.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 7b1d7a30..f92e6223 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -1364,15 +1364,15 @@ impl Scrollbars { let ratio = bounds.height / content_bounds.height; // min height for easier grabbing with super tall content - let scroller_height = (bounds.height * ratio).max(2.0); - let scroller_offset = translation.y * ratio; + let scroller_height = (scrollbar_bounds.height * ratio).max(2.0); + let scroller_offset = + translation.y * ratio * scrollbar_bounds.height / bounds.height; let scroller_bounds = Rectangle { x: bounds.x + bounds.width - total_scrollbar_width / 2.0 - scroller_width / 2.0, - y: (scrollbar_bounds.y + scroller_offset - x_scrollbar_height) - .max(0.0), + y: (scrollbar_bounds.y + scroller_offset).max(0.0), width: scroller_width, height: scroller_height, }; -- cgit From 34f07b60273d6cfe13834af54cd0e24d34569387 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 04:11:52 +0200 Subject: Fix `clippy::semicolon_if_nothing_returned` --- widget/src/scrollable.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index f92e6223..6f1e68fc 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -217,7 +217,7 @@ where } fn diff(&self, tree: &mut Tree) { - tree.diff_children(std::slice::from_ref(&self.content)) + tree.diff_children(std::slice::from_ref(&self.content)); } fn width(&self) -> Length { @@ -348,9 +348,9 @@ where layout, cursor, viewport, - ) + ); }, - ) + ); } fn mouse_interaction( @@ -1069,7 +1069,7 @@ impl operation::Scrollable for State { } fn scroll_to(&mut self, offset: AbsoluteOffset) { - State::scroll_to(self, offset) + State::scroll_to(self, offset); } } @@ -1203,7 +1203,7 @@ impl State { (self.offset_y.absolute(bounds.height, content_bounds.height) - delta.y) .clamp(0.0, content_bounds.height - bounds.height), - ) + ); } if bounds.width < content_bounds.width { -- cgit From 42ed90bc6f92b2085d193e7f143430b8d3847c21 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 04:51:08 +0200 Subject: Fix `clippy::default_trait_access` --- widget/src/scrollable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 6f1e68fc..4cc97684 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -46,7 +46,7 @@ where id: None, width: Length::Shrink, height: Length::Shrink, - direction: Default::default(), + direction: Direction::default(), content: content.into(), on_scroll: None, style: Default::default(), -- cgit From caed50b277495e4375975f3f4e271b8fcbc0c33f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 05:03:25 +0200 Subject: Fix `clippy::match-wildcard-for-single-variants` --- widget/src/scrollable.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 4cc97684..49aed2f0 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -117,7 +117,7 @@ impl Direction { match self { Self::Horizontal(properties) => Some(properties), Self::Both { horizontal, .. } => Some(horizontal), - _ => None, + Self::Vertical(_) => None, } } @@ -126,7 +126,7 @@ impl Direction { match self { Self::Vertical(properties) => Some(properties), Self::Both { vertical, .. } => Some(vertical), - _ => None, + Self::Horizontal(_) => None, } } } -- cgit