From b623f280ed43b04cef16a82de4cdf13497b5d63e Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Fri, 14 Apr 2023 13:32:44 -0700 Subject: Add `scroll_to` operation for absolute scroll --- native/src/widget/scrollable.rs | 55 +++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 18 deletions(-) (limited to 'native/src/widget/scrollable.rs') diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 78dcdca2..eedddfd0 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -15,7 +15,9 @@ use crate::{ }; pub use iced_style::scrollable::StyleSheet; -pub use operation::scrollable::RelativeOffset; +pub use operation::scrollable::{ + AbsoluteOffset, CurrentOffset, RelativeOffset, +}; pub mod style { //! The styles of a [`Scrollable`]. @@ -38,7 +40,7 @@ where vertical: Properties, horizontal: Option, content: Element<'a, Message, Renderer>, - on_scroll: Option Message + 'a>>, + on_scroll: Option Message + 'a>>, style: ::Style, } @@ -93,11 +95,10 @@ where /// Sets a function to call when the [`Scrollable`] is scrolled. /// - /// The function takes the new relative x & y offset of the [`Scrollable`] - /// (e.g. `0` means beginning, while `1` means end). + /// The function takes the [`CurrentOffset`] of the [`Scrollable`] pub fn on_scroll( mut self, - f: impl Fn(RelativeOffset) -> Message + 'a, + f: impl Fn(CurrentOffset) -> Message + 'a, ) -> Self { self.on_scroll = Some(Box::new(f)); self @@ -436,7 +437,7 @@ pub fn update( shell: &mut Shell<'_, Message>, vertical: &Properties, horizontal: Option<&Properties>, - on_scroll: &Option Message + '_>>, + on_scroll: &Option Message + '_>>, update_content: impl FnOnce( Event, Layout<'_>, @@ -896,7 +897,7 @@ pub fn draw( fn notify_on_scroll( state: &mut State, - on_scroll: &Option Message + '_>>, + on_scroll: &Option Message + '_>>, bounds: Rectangle, content_bounds: Rectangle, shell: &mut Shell<'_, Message>, @@ -908,31 +909,39 @@ fn notify_on_scroll( return; } - let x = state.offset_x.absolute(bounds.width, content_bounds.width) - / (content_bounds.width - bounds.width); + let absolute_x = + state.offset_x.absolute(bounds.width, content_bounds.width); + let relative_x = absolute_x / (content_bounds.width - bounds.width); - let y = state + let absolute_y = state .offset_y - .absolute(bounds.height, content_bounds.height) - / (content_bounds.height - bounds.height); + .absolute(bounds.height, content_bounds.height); + let relative_y = absolute_y / (content_bounds.height - bounds.height); - let new_offset = RelativeOffset { x, y }; + let absolute = AbsoluteOffset { + x: absolute_x, + y: absolute_y, + }; + let relative = RelativeOffset { + x: relative_x, + y: relative_y, + }; // Don't publish redundant offsets to shell - if let Some(prev_offset) = state.last_notified { + if let Some(prev_relative) = state.last_notified { let unchanged = |a: f32, b: f32| { (a - b).abs() <= f32::EPSILON || (a.is_nan() && b.is_nan()) }; - if unchanged(prev_offset.x, new_offset.x) - && unchanged(prev_offset.y, new_offset.y) + if unchanged(prev_relative.x, relative.x) + && unchanged(prev_relative.y, relative.y) { return; } } - shell.publish(on_scroll(new_offset)); - state.last_notified = Some(new_offset); + shell.publish(on_scroll(CurrentOffset { absolute, relative })); + state.last_notified = Some(relative); } } @@ -966,6 +975,10 @@ impl operation::Scrollable for State { fn snap_to(&mut self, offset: RelativeOffset) { State::snap_to(self, offset); } + + fn scroll_to(&mut self, offset: AbsoluteOffset) { + State::scroll_to(self, offset) + } } #[derive(Debug, Clone, Copy)] @@ -1052,6 +1065,12 @@ impl State { self.offset_y = Offset::Relative(offset.y.clamp(0.0, 1.0)); } + /// Scroll to the provided [`AbsoluteOffset`]. + pub fn scroll_to(&mut self, offset: AbsoluteOffset) { + self.offset_x = Offset::Absolute(offset.x.max(0.0)); + self.offset_y = Offset::Absolute(offset.y.max(0.0)); + } + /// Unsnaps the current scroll position, if snapped, given the bounds of the /// [`Scrollable`] and its contents. pub fn unsnap(&mut self, bounds: Rectangle, content_bounds: Rectangle) { -- cgit From 6ad5e03d71db3dee174ac6512dceccc80e3a70a8 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Mon, 17 Apr 2023 13:55:40 -0700 Subject: Add scrollable `Viewport` --- native/src/widget/scrollable.rs | 94 ++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 38 deletions(-) (limited to 'native/src/widget/scrollable.rs') diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index eedddfd0..c66a166b 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -15,9 +15,7 @@ use crate::{ }; pub use iced_style::scrollable::StyleSheet; -pub use operation::scrollable::{ - AbsoluteOffset, CurrentOffset, RelativeOffset, -}; +pub use operation::scrollable::{AbsoluteOffset, RelativeOffset}; pub mod style { //! The styles of a [`Scrollable`]. @@ -40,7 +38,7 @@ where vertical: Properties, horizontal: Option, content: Element<'a, Message, Renderer>, - on_scroll: Option Message + 'a>>, + on_scroll: Option Message + 'a>>, style: ::Style, } @@ -95,11 +93,8 @@ where /// Sets a function to call when the [`Scrollable`] is scrolled. /// - /// The function takes the [`CurrentOffset`] of the [`Scrollable`] - pub fn on_scroll( - mut self, - f: impl Fn(CurrentOffset) -> Message + 'a, - ) -> Self { + /// The function takes the [`Viewport`] of the [`Scrollable`] + pub fn on_scroll(mut self, f: impl Fn(Viewport) -> Message + 'a) -> Self { self.on_scroll = Some(Box::new(f)); self } @@ -437,7 +432,7 @@ pub fn update( shell: &mut Shell<'_, Message>, vertical: &Properties, horizontal: Option<&Properties>, - on_scroll: &Option Message + '_>>, + on_scroll: &Option Message + '_>>, update_content: impl FnOnce( Event, Layout<'_>, @@ -897,7 +892,7 @@ pub fn draw( fn notify_on_scroll( state: &mut State, - on_scroll: &Option Message + '_>>, + on_scroll: &Option Message + '_>>, bounds: Rectangle, content_bounds: Rectangle, shell: &mut Shell<'_, Message>, @@ -909,39 +904,29 @@ fn notify_on_scroll( return; } - let absolute_x = - state.offset_x.absolute(bounds.width, content_bounds.width); - let relative_x = absolute_x / (content_bounds.width - bounds.width); - - let absolute_y = state - .offset_y - .absolute(bounds.height, content_bounds.height); - let relative_y = absolute_y / (content_bounds.height - bounds.height); - - let absolute = AbsoluteOffset { - x: absolute_x, - y: absolute_y, - }; - let relative = RelativeOffset { - x: relative_x, - y: relative_y, + let viewport = Viewport { + offset_x: state.offset_x, + offset_y: state.offset_y, + bounds, + content_bounds, }; - // Don't publish redundant offsets to shell - if let Some(prev_relative) = state.last_notified { + // Don't publish redundant viewports to shell + if let Some(last_notified) = state.last_notified { + let prev = last_notified.relative_offset(); + let curr = viewport.relative_offset(); + let unchanged = |a: f32, b: f32| { (a - b).abs() <= f32::EPSILON || (a.is_nan() && b.is_nan()) }; - if unchanged(prev_relative.x, relative.x) - && unchanged(prev_relative.y, relative.y) - { + if unchanged(prev.x, curr.x) && unchanged(prev.y, curr.y) { return; } } - shell.publish(on_scroll(CurrentOffset { absolute, relative })); - state.last_notified = Some(relative); + shell.publish(on_scroll(viewport)); + state.last_notified = Some(viewport); } } @@ -954,7 +939,7 @@ pub struct State { offset_x: Offset, x_scroller_grabbed_at: Option, keyboard_modifiers: keyboard::Modifiers, - last_notified: Option, + last_notified: Option, } impl Default for State { @@ -988,18 +973,51 @@ enum Offset { } impl Offset { - fn absolute(self, window: f32, content: f32) -> f32 { + fn absolute(self, viewport: f32, content: f32) -> f32 { match self { Offset::Absolute(absolute) => { - absolute.min((content - window).max(0.0)) + absolute.min((content - viewport).max(0.0)) } Offset::Relative(percentage) => { - ((content - window) * percentage).max(0.0) + ((content - viewport) * percentage).max(0.0) } } } } +/// The current [`Viewport`] of the [`Scrollable`]. +#[derive(Debug, Clone, Copy)] +pub struct Viewport { + offset_x: Offset, + offset_y: Offset, + bounds: Rectangle, + content_bounds: Rectangle, +} + +impl Viewport { + /// Returns the [`AbsoluteOffset`] of the current [`Viewport`]. + pub fn absolute_offset(&self) -> AbsoluteOffset { + let x = self + .offset_x + .absolute(self.bounds.width, self.content_bounds.width); + let y = self + .offset_y + .absolute(self.bounds.height, self.content_bounds.height); + + AbsoluteOffset { x, y } + } + + /// Returns the [`RelativeOffset`] of the current [`Viewport`]. + pub fn relative_offset(&self) -> RelativeOffset { + let AbsoluteOffset { x, y } = self.absolute_offset(); + + let x = x / (self.content_bounds.width - self.bounds.width); + let y = y / (self.content_bounds.height - self.bounds.height); + + RelativeOffset { x, y } + } +} + impl State { /// Creates a new [`State`] with the scrollbar(s) at the beginning. pub fn new() -> Self { -- cgit From 8a711408de26c53cf6b18045ea19c677e8edbd95 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 20 Apr 2023 15:48:34 +0200 Subject: Compare absolute offsets as well in `notify_on_scroll` --- native/src/widget/scrollable.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'native/src/widget/scrollable.rs') diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index c66a166b..c9ad5947 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -913,14 +913,21 @@ fn notify_on_scroll( // Don't publish redundant viewports to shell if let Some(last_notified) = state.last_notified { - let prev = last_notified.relative_offset(); - let curr = viewport.relative_offset(); + let last_relative_offset = last_notified.relative_offset(); + let current_relative_offset = viewport.relative_offset(); + + let last_absolute_offset = last_notified.absolute_offset(); + let current_absolute_offset = viewport.absolute_offset(); let unchanged = |a: f32, b: f32| { (a - b).abs() <= f32::EPSILON || (a.is_nan() && b.is_nan()) }; - if unchanged(prev.x, curr.x) && unchanged(prev.y, curr.y) { + if unchanged(last_relative_offset.x, current_relative_offset.x) + && unchanged(last_relative_offset.y, current_relative_offset.y) + && unchanged(last_absolute_offset.x, current_absolute_offset.x) + && unchanged(last_absolute_offset.y, current_absolute_offset.y) + { return; } } -- cgit