use std::time::Duration;
use waft_animation::Curves;
use waft_foundation::{App, ChangeNotifier, ChangeNotifierData, Handle, Listenable, Listener, Ref};
use waft_painting::{Axis, EdgeInsetsGeometry};
use waft_scheduler::FrameCallback;
use waft_widgets::*;
use super::after_next_frame;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ScrollViewportMetrics {
pub offset: f64,
pub scrollable_length: f64,
pub viewport_length: f64,
pub extent_length: f64,
pub is_scrolling: bool,
}
pub struct ScrollViewportController {
notifier: ChangeNotifierData,
native: Ref<ScrollController>,
native_listener: Option<Listener>,
reported: ScrollViewportMetrics,
report_pending: bool,
disposed: bool,
}
impl ScrollViewportController {
pub fn new(app: &mut App) -> Ref<Self> {
let native = ScrollController::default(app);
Self::with_native(app, native)
}
pub fn with_initial_offset(app: &mut App, offset: f64) -> Ref<Self> {
let native = ScrollController::new(app, offset, false, None, None, None);
Self::with_native(app, native)
}
fn with_native(app: &mut App, native: Ref<ScrollController>) -> Ref<Self> {
let controller = app.create(Self {
notifier: ChangeNotifierData::new(),
native: native.clone(),
native_listener: None,
reported: ScrollViewportMetrics::default(),
report_pending: false,
disposed: false,
});
let handle = controller.handle();
let listener = Listener::new(move |app| handle.schedule_report(app));
native.add_listener(app, listener.clone());
app.get_mut(&controller).native_listener = Some(listener);
controller
}
pub fn native_controller(self: Handle<Self>, app: &App) -> ScrollControllerRef {
app.get(self).native.erase()
}
pub fn has_clients(self: Handle<Self>, app: &App) -> bool {
self.native_controller(app).has_clients(app)
}
pub fn metrics(self: Handle<Self>, app: &App) -> ScrollViewportMetrics {
let controller = self.native_controller(app);
if !controller.has_clients(app) {
return ScrollViewportMetrics::default();
}
let position = controller.position(app);
if !position.has_content_dimensions(app) || !position.has_viewport_dimension(app) {
return ScrollViewportMetrics::default();
}
let minimum = position.min_scroll_extent(app);
let maximum = position.max_scroll_extent(app);
let viewport = position.viewport_dimension(app);
ScrollViewportMetrics {
offset: position.pixels(app) - minimum,
scrollable_length: (maximum - minimum).max(0.0),
viewport_length: viewport,
extent_length: (maximum - minimum).max(0.0) + viewport,
is_scrolling: *app.get(position.is_scrolling_notifier(app)).value(),
}
}
pub fn change_view(
self: Handle<Self>,
app: &mut App,
offset: f64,
disable_animation: bool,
) -> bool {
assert!(offset.is_finite(), "scroll offset must be finite");
let controller = self.native_controller(app);
if !controller.has_clients(app) {
return false;
}
let position = controller.position(app);
if !position.has_content_dimensions(app) {
return false;
}
let target = (position.min_scroll_extent(app) + offset).clamp(
position.min_scroll_extent(app),
position.max_scroll_extent(app),
);
if (position.pixels(app) - target).abs() < f64::EPSILON {
return false;
}
if disable_animation {
position.jump_to(app, target);
} else {
drop(position.move_to(
app,
target,
Some(Duration::from_millis(100)),
Some(Curves::ease_in_out()),
Some(true),
));
}
self.schedule_report(app);
true
}
pub fn scroll_by(
self: Handle<Self>,
app: &mut App,
delta: f64,
disable_animation: bool,
) -> bool {
self.change_view(app, self.metrics(app).offset + delta, disable_animation)
}
pub fn dispose(self: Handle<Self>, app: &mut App) {
let state = app.get_mut(self);
if state.disposed {
return;
}
state.disposed = true;
let native = state.native.clone();
let listener = state.native_listener.take();
if let Some(listener) = listener {
native.remove_listener(app, &listener);
}
ScrollControllerLeaf::dispose(native.handle(), app);
ChangeNotifierData::dispose(app, self);
}
fn schedule_report(self: Handle<Self>, app: &mut App) {
if app.get(self).disposed || app.get(self).report_pending {
return;
}
app.get_mut(self).report_pending = true;
after_next_frame(
app,
None,
FrameCallback::new(move |app, _| {
if !app.contains(self) || app.get(self).disposed {
return;
}
app.get_mut(self).report_pending = false;
let metrics = self.metrics(app);
if metrics != app.get(self).reported {
app.get_mut(self).reported = metrics;
self.notify_listeners(app);
}
}),
);
}
}
impl ChangeNotifier for ScrollViewportController {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.notifier
}
}
#[derive(Debug)]
pub struct ScrollMetricsObserver {
pub key: Option<KeyRef>,
pub controller: Ref<ScrollViewportController>,
pub child: WidgetRef,
pub depth: u32,
}
impl ScrollMetricsObserver {
pub fn new<K>(controller: Ref<ScrollViewportController>, child: impl IntoWidget<K>) -> Self {
Self {
key: None,
controller,
child: child.into_widget(),
depth: 0,
}
}
pub fn depth(mut self, depth: u32) -> Self {
self.depth = depth;
self
}
pub fn key(mut self, key: KeyRef) -> Self {
self.key = Some(key);
self
}
}
impl StatelessWidget for ScrollMetricsObserver {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
let controller = self.controller.clone();
let depth = self.depth;
let behavior = ScrollConfiguration::of(app, context)
.copy_with()
.scrollbars(false);
ScrollConfiguration::new(
ScrollBehaviorRef::new(behavior),
NotificationListener::<ScrollMetricsNotification>::new(
NotificationListener::<dyn ScrollNotification>::new(self.child.clone())
.on_notification({
let controller = controller.clone();
move |app, notification| {
if notification.depth() == depth {
controller.schedule_report(app);
}
false
}
})
.into_widget(),
)
.on_notification(move |app, notification| {
if notification.depth() == depth {
controller.schedule_report(app);
}
false
})
.into_widget(),
)
.into_widget()
}
}
#[derive(Debug)]
pub struct ScrollViewport {
pub key: Option<KeyRef>,
pub axis: Axis,
pub controller: Ref<ScrollViewportController>,
pub child: WidgetRef,
pub padding: EdgeInsetsGeometry,
}
impl ScrollViewport {
pub fn new<K>(
axis: Axis,
controller: Ref<ScrollViewportController>,
child: impl IntoWidget<K>,
) -> Self {
Self {
key: None,
axis,
controller,
child: child.into_widget(),
padding: EdgeInsetsGeometry::ZERO,
}
}
pub fn key(mut self, key: KeyRef) -> Self {
self.key = Some(key);
self
}
pub fn padding(mut self, padding: impl Into<EdgeInsetsGeometry>) -> Self {
let padding = padding.into();
self.padding = padding;
self
}
}
impl StatelessWidget for ScrollViewport {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, _: BuildContext) -> WidgetRef {
ScrollMetricsObserver::new(
self.controller.clone(),
SingleChildScrollView::new()
.scroll_direction(self.axis)
.controller(self.controller.native_controller(app))
.padding(self.padding)
.child(self.child.clone()),
)
.into_widget()
}
}