use std::fmt::{self, Debug};
use std::rc::Rc;
use waft_foundation::{App, Handle};
use crate::framework::{
BuildContext, InheritedWidget, IntoWidget, KeyRef, State, StateData, StatefulWidget, WidgetRef,
};
use crate::widgets::notification_listener::NotificationListener;
use crate::widgets::scroll_notification::ScrollNotification;
use crate::widgets::scroll_position::ScrollMetricsNotification;
pub type ScrollNotificationCallback = Rc<dyn Fn(&mut App, &dyn ScrollNotification)>;
#[derive(Debug)]
struct ScrollNotificationObserverScope {
scroll_notification_observer_state: Handle<ScrollNotificationObserverState>,
child: WidgetRef,
}
impl InheritedWidget for ScrollNotificationObserverScope {
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old: &ScrollNotificationObserverScope) -> bool {
self.scroll_notification_observer_state != old.scroll_notification_observer_state
}
}
#[derive(Debug)]
pub struct ScrollNotificationObserver {
pub key: Option<KeyRef>,
pub child: WidgetRef,
}
impl ScrollNotificationObserver {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(child: impl IntoWidget<K>) -> ScrollNotificationObserver {
ScrollNotificationObserver {
key: None,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> ScrollNotificationObserver {
self.key = Some(key);
self
}
pub fn maybe_of(
app: &mut App,
context: BuildContext,
) -> Option<Handle<ScrollNotificationObserverState>> {
context
.depend_on_inherited_widget_of_exact_type::<ScrollNotificationObserverScope>(app)
.map(|scope| scope.scroll_notification_observer_state)
}
pub fn of(app: &mut App, context: BuildContext) -> Handle<ScrollNotificationObserverState> {
ScrollNotificationObserver::maybe_of(app, context).expect(
"ScrollNotificationObserver.of() was called with a context that does not contain a \
ScrollNotificationObserver widget.\n\
No ScrollNotificationObserver widget ancestor could be found starting from the \
context that was passed to ScrollNotificationObserver.of(). This can happen \
because you are using a widget that looks for a ScrollNotificationObserver \
ancestor, but no such ancestor exists.",
)
}
}
impl StatefulWidget for ScrollNotificationObserver {
type State = ScrollNotificationObserverState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> ScrollNotificationObserverState {
ScrollNotificationObserverState {
state: StateData::new(),
listeners: Some(Vec::new()),
}
}
}
pub struct ScrollNotificationObserverState {
state: StateData<ScrollNotificationObserver>,
listeners: Option<Vec<ScrollNotificationCallback>>,
}
impl ScrollNotificationObserverState {
fn debug_assert_not_disposed(self: Handle<Self>, app: &App) -> bool {
debug_assert!(
app.get(self).listeners.is_some(),
"A ScrollNotificationObserverState was used after being disposed. Once you have \
called dispose() on a ScrollNotificationObserverState, it can no longer be used."
);
true
}
pub fn add_listener(self: Handle<Self>, app: &mut App, listener: ScrollNotificationCallback) {
debug_assert!(self.debug_assert_not_disposed(app));
app.get_mut(self)
.listeners
.as_mut()
.expect("a live observer keeps its listeners")
.push(listener);
}
pub fn remove_listener(
self: Handle<Self>,
app: &mut App,
listener: &ScrollNotificationCallback,
) {
debug_assert!(self.debug_assert_not_disposed(app));
let listeners = app
.get_mut(self)
.listeners
.as_mut()
.expect("a live observer keeps its listeners");
if let Some(index) = listeners
.iter()
.position(|entry| Rc::ptr_eq(entry, listener))
{
listeners.remove(index);
}
}
fn notify_listeners(self: Handle<Self>, app: &mut App, notification: &dyn ScrollNotification) {
debug_assert!(self.debug_assert_not_disposed(app));
let local_listeners = app
.get(self)
.listeners
.clone()
.expect("a live observer keeps its listeners");
if local_listeners.is_empty() {
return;
}
for entry in local_listeners {
let still_registered = app.get(self).listeners.as_ref().is_some_and(|listeners| {
listeners
.iter()
.any(|listener| Rc::ptr_eq(listener, &entry))
});
if still_registered {
entry(app, notification);
}
}
}
}
impl State for ScrollNotificationObserverState {
type Widget = ScrollNotificationObserver;
crate::state_accessors!();
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let child = self.widget(app).child.clone();
NotificationListener::<ScrollMetricsNotification>::new(
NotificationListener::<dyn ScrollNotification>::new(
ScrollNotificationObserverScope {
scroll_notification_observer_state: self,
child,
}
.into_widget(),
)
.on_notification(move |app, notification| {
self.notify_listeners(app, notification);
false
})
.into_widget(),
)
.on_notification(move |app, notification: &ScrollMetricsNotification| {
let update = notification.as_scroll_update();
self.notify_listeners(app, &update);
false
})
.into_widget()
}
fn dispose(self: Handle<Self>, app: &mut App) {
debug_assert!(self.debug_assert_not_disposed(app));
app.get_mut(self).listeners = None;
}
}
impl Debug for ScrollNotificationObserverState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ScrollNotificationObserverState")
.field(
"listeners",
&self.listeners.as_ref().map(|listeners| listeners.len()),
)
.finish()
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use waft_foundation::AppCell;
use waft_painting::AxisDirection;
use super::*;
use crate::framework::{AnyElement, Element, Notification};
use crate::test_harness::Harness;
use crate::widgets::basic::SizedBox;
use crate::widgets::scroll_metrics::{FixedScrollMetrics, ScrollMetrics};
use crate::widgets::scroll_notification::ScrollUpdateNotification;
fn descendant(harness: &Harness, app: &App, depth: usize) -> AnyElement {
let mut element = harness.root.as_element();
for _ in 0..=depth {
element = element.children(app)[0];
}
element
}
fn metrics() -> Rc<dyn ScrollMetrics> {
Rc::new(FixedScrollMetrics::new(
Some(0.0),
Some(400.0),
Some(10.0),
Some(100.0),
AxisDirection::Down,
1.0,
))
}
fn mount(app: &mut App) -> (Harness, BuildContext) {
let harness = Harness::mount(
app,
ScrollNotificationObserver::new(SizedBox::shrink()).into_widget(),
);
harness.pump(app);
let context = descendant(&harness, app, 4);
(harness, context)
}
#[test]
fn every_listener_sees_a_descendants_scroll_notification() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (_harness, context) = mount(&mut app);
let observer = ScrollNotificationObserver::of(&mut app, context);
let seen = Rc::new(RefCell::new(Vec::new()));
for label in ["first", "second"] {
let seen = seen.clone();
observer.add_listener(
&mut app,
Rc::new(move |_app, notification| {
seen.borrow_mut()
.push((label, notification.metrics().pixels()));
}),
);
}
ScrollUpdateNotification::new(metrics(), context).dispatch(&mut app, Some(context));
assert_eq!(*seen.borrow(), [("first", 10.0), ("second", 10.0)]);
}
#[test]
fn a_metrics_notification_arrives_as_a_scroll_update() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (_harness, context) = mount(&mut app);
let observer = ScrollNotificationObserver::of(&mut app, context);
let seen = Rc::new(RefCell::new(Vec::new()));
let recorded = seen.clone();
observer.add_listener(
&mut app,
Rc::new(move |_app, notification| {
recorded
.borrow_mut()
.push(notification.metrics().max_scroll_extent());
}),
);
ScrollMetricsNotification::new(metrics(), context).dispatch(&mut app, Some(context));
assert_eq!(*seen.borrow(), [400.0]);
}
#[test]
fn a_removed_listener_stops_hearing_notifications() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (_harness, context) = mount(&mut app);
let observer = ScrollNotificationObserver::of(&mut app, context);
let seen = Rc::new(RefCell::new(0));
let recorded = seen.clone();
let listener: ScrollNotificationCallback = Rc::new(move |_app, _notification| {
*recorded.borrow_mut() += 1;
});
observer.add_listener(&mut app, listener.clone());
ScrollUpdateNotification::new(metrics(), context).dispatch(&mut app, Some(context));
assert_eq!(*seen.borrow(), 1);
observer.remove_listener(&mut app, &listener);
ScrollUpdateNotification::new(metrics(), context).dispatch(&mut app, Some(context));
assert_eq!(*seen.borrow(), 1);
}
}