summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-10-27 01:24:08 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-10-27 01:24:08 +0200
commit09bd2c46c06eba72da40852d82a52e7353cc9e9b (patch)
treebf9f9caa19f7f4fd33a1eb99958e0da5c2ad7a23 /core
parent719c073fc67c87d6b2da1bc01b74751d3f5e59f0 (diff)
downloadiced-09bd2c46c06eba72da40852d82a52e7353cc9e9b.tar.gz
iced-09bd2c46c06eba72da40852d82a52e7353cc9e9b.tar.bz2
iced-09bd2c46c06eba72da40852d82a52e7353cc9e9b.zip
Expose scrollable offset properly
Diffstat (limited to 'core')
-rw-r--r--core/src/widget/scrollable.rs28
1 files changed, 26 insertions, 2 deletions
diff --git a/core/src/widget/scrollable.rs b/core/src/widget/scrollable.rs
index dd62b658..4e5c4ad0 100644
--- a/core/src/widget/scrollable.rs
+++ b/core/src/widget/scrollable.rs
@@ -1,4 +1,4 @@
-use crate::{Align, Column, Justify, Length};
+use crate::{Align, Column, Justify, Length, Rectangle};
#[derive(Debug)]
pub struct Scrollable<'a, Element> {
@@ -112,11 +112,35 @@ impl<'a, Element> Scrollable<'a, Element> {
#[derive(Debug, Clone, Copy, Default)]
pub struct State {
- pub offset: u32,
+ offset: u32,
}
impl State {
pub fn new() -> Self {
State::default()
}
+
+ pub fn scroll(
+ &mut self,
+ delta_y: f32,
+ bounds: Rectangle,
+ content_bounds: Rectangle,
+ ) {
+ if bounds.height >= content_bounds.height {
+ return;
+ }
+
+ // TODO: Configurable speed (?)
+ self.offset = (self.offset as i32 - delta_y.round() as i32 * 15)
+ .max(0)
+ .min((content_bounds.height - bounds.height) as i32)
+ as u32;
+ }
+
+ pub fn offset(&self, bounds: Rectangle, content_bounds: Rectangle) -> u32 {
+ let hidden_content =
+ (content_bounds.height - bounds.height).round() as u32;
+
+ self.offset.min(hidden_content).max(0)
+ }
}