use crate::widget::{Id, Operation};
pub trait Scrollable {
fn snap_to(&mut self, offset: RelativeOffset);
}
pub fn snap_to<T>(target: Id, offset: RelativeOffset) -> impl Operation<T> {
struct SnapTo {
target: Id,
offset: RelativeOffset,
}
impl<T> Operation<T> for SnapTo {
fn container(
&mut self,
_id: Option<&Id>,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
) {
operate_on_children(self)
}
fn scrollable(&mut self, state: &mut dyn Scrollable, id: Option<&Id>) {
if Some(&self.target) == id {
state.snap_to(self.offset);
}
}
}
SnapTo { target, offset }
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct RelativeOffset {
pub x: f32,
pub y: f32,
}
impl RelativeOffset {
pub const START: Self = Self { x: 0.0, y: 0.0 };
pub const END: Self = Self { x: 1.0, y: 1.0 };
}