Expose scrollable offset properly

This commit is contained in:
Héctor Ramón Jiménez 2019-10-27 01:24:08 +02:00
parent 719c073fc6
commit 09bd2c46c0
4 changed files with 44 additions and 26 deletions

View file

@ -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)
}
}