use super::*;
pub struct ViewContext<'a, V> {
pub(super) size: Size,
pub(super) scale_factor: f32,
pub(super) metrics: FrameMetrics,
pub(super) focused: Option<ElementId>,
pub(super) focused_path: Vec<ElementId>,
pub(super) request_animation_frame: bool,
pub(super) repaint_deadline: Option<Instant>,
pub(super) listeners: &'a mut ListenerRegistry,
pub(super) window: WindowHandle,
pub(super) window_state: WindowState,
pub(super) displays: &'a Displays,
pub(super) keyboard_layout: &'a KeyboardLayout,
pub(super) assets: &'a Assets,
pub(super) app_info: Option<&'a AppInfo>,
pub(super) app_paths: Option<&'a AppPaths>,
pub(super) system_info: &'a SystemInfo,
pub(super) system_preferences: &'a SystemPreferences,
pub(super) background_tasks: Option<&'a BackgroundTaskPoolHandle>,
pub(super) foreground_tasks: &'a ForegroundTaskSpawner,
pub(super) globals: &'a GlobalStore,
pub(super) event_proxy: Option<&'a EventLoopProxy<RuntimeEvent>>,
pub(super) marker: PhantomData<fn(&mut V)>,
}
impl<V: 'static> ViewContext<'_, V> {
pub fn size(&mut self) -> Size {
self.listeners.observes_viewport = true;
self.size
}
pub fn scale_factor(&mut self) -> f32 {
self.listeners.observes_viewport = true;
self.scale_factor
}
pub fn window_handle(&self) -> WindowHandle {
self.window
}
pub fn window_invalidator(&self) -> WindowInvalidator {
WindowInvalidator {
runtime: self.event_proxy.map(|proxy| (proxy.clone(), self.window)),
}
}
pub fn window_state(&mut self) -> WindowState {
self.listeners.observes_window_state = true;
self.window_state
}
pub fn window_bounds(&mut self) -> WindowBounds {
self.window_state().bounds
}
pub fn displays(&mut self) -> &[Display] {
self.listeners.observes_displays = true;
self.displays.all()
}
pub fn primary_display(&mut self) -> Option<&Display> {
self.listeners.observes_displays = true;
self.displays.primary()
}
pub fn find_display(&mut self, id: DisplayId) -> Option<&Display> {
self.listeners.observes_displays = true;
self.displays.find(id)
}
pub fn current_display(&mut self) -> Option<&Display> {
self.listeners.observes_displays = true;
self.listeners.observes_window_state = true;
self.window_state
.display_id
.and_then(|id| self.displays.find(id))
}
pub fn keyboard_layout(&mut self) -> &KeyboardLayout {
self.listeners.observes_keyboard_layout = true;
self.keyboard_layout
}
pub fn assets(&self) -> &Assets {
self.assets
}
pub fn asset_source(&self) -> &Assets {
self.assets()
}
pub fn app_info(&self) -> Option<&AppInfo> {
self.app_info
}
pub fn app_paths(&self) -> Option<&AppPaths> {
self.app_paths
}
pub fn system_info(&self) -> &SystemInfo {
self.system_info
}
pub fn system_preferences(&mut self) -> SystemPreferences {
self.listeners.observes_system_preferences = true;
*self.system_preferences
}
pub fn appearance(&mut self) -> WindowAppearance {
self.window_state().appearance
}
pub fn metrics(&self) -> FrameMetrics {
self.metrics
}
pub fn focus_handle(&self, id: impl Into<ElementId>) -> FocusHandle {
FocusHandle::new(id)
}
pub fn focused(&self) -> Option<ElementId> {
self.focused
}
pub fn is_focused(&self, handle: FocusHandle) -> bool {
self.focused == Some(handle.id())
}
pub fn contains_focused(&self, handle: FocusHandle) -> bool {
self.focused_path.contains(&handle.id())
}
pub fn has_global<G: Global>(&self) -> bool {
self.globals.has::<G>()
}
pub fn global<G: Global>(&self) -> Ref<'_, G> {
self.globals.get::<G>()
}
pub fn try_global<G: Global>(&self) -> Option<Ref<'_, G>> {
self.globals.try_get::<G>()
}
pub fn watch_global<G: Global, R>(&mut self, read: impl FnOnce(&G) -> R) -> R {
self.listeners.observe_global(TypeId::of::<G>());
let global = self.globals.get::<G>();
read(&global)
}
pub fn observe_global<G: Global>(
&mut self,
mut callback: impl FnMut(&mut V, &mut EventContext) + 'static,
) -> Subscription {
let callback: GlobalObserverCallback = Rc::new(RefCell::new(
move |view: &mut dyn Any, cx: &mut EventContext| {
callback(
view.downcast_mut::<V>()
.expect("global observer received the wrong view type"),
cx,
);
},
));
self.listeners.subscribe_global(TypeId::of::<G>(), callback)
}
pub fn subscribe_global<G: Global>(
&mut self,
callback: impl FnMut(&mut V, &mut EventContext) + 'static,
) -> Subscription {
self.observe_global::<G>(callback)
}
pub fn observe<T, R>(&mut self, entity: &Entity<T>, read: impl FnOnce(&T) -> R) -> R {
self.listeners.observe_entity(entity.id());
entity.read(read)
}
pub fn subscribe<T, E>(
&mut self,
entity: &Entity<T>,
mut callback: impl FnMut(&mut V, Entity<T>, &E, &mut EventContext) + 'static,
) -> Subscription
where
T: EventEmitter<E>,
E: Any,
{
let source = entity.downgrade();
let callback: EntityEventCallback = Rc::new(RefCell::new(
move |view: &mut dyn Any, event: &dyn Any, cx: &mut EventContext| {
let Some(source) = source.upgrade() else {
return;
};
callback(
view.downcast_mut::<V>()
.expect("entity-event subscriber received the wrong view type"),
source,
event
.downcast_ref::<E>()
.expect("entity-event subscriber received the wrong event type"),
cx,
);
},
));
self.listeners
.subscribe_entity_event(entity.id(), TypeId::of::<E>(), callback)
}
pub fn request_animation_frame(&mut self) {
self.request_animation_frame = true;
}
pub fn request_repaint_at(&mut self, deadline: Instant) {
self.repaint_deadline = Some(
self.repaint_deadline
.map_or(deadline, |current| current.min(deadline)),
);
}
pub fn spawn<Build, Fut, R>(&self, build: Build) -> Result<Task<R>, ForegroundTaskSpawnError>
where
Build: FnOnce(AsyncViewContext<V>) -> Fut,
Fut: Future<Output = R> + 'static,
R: 'static,
{
self.foreground_tasks
.spawn::<V, _, _, _>(self.window, build)
}
pub fn spawn_background<T, Work, Complete>(
&self,
work: Work,
complete: Complete,
) -> Result<(), TaskSpawnError>
where
T: Send + 'static,
Work: FnOnce() -> T + Send + 'static,
Complete:
FnOnce(&mut V, Result<T, BackgroundTaskError>, &mut EventContext) + Send + 'static,
{
self.background_tasks
.ok_or(TaskSpawnError::Unavailable)?
.spawn::<V, T, Work, Complete>(self.window, work, complete)
}
pub fn spawn_blocking<T, Work, Complete>(
&self,
work: Work,
complete: Complete,
) -> Result<(), TaskSpawnError>
where
T: Send + 'static,
Work: FnOnce() -> T + Send + 'static,
Complete:
FnOnce(&mut V, Result<T, BackgroundTaskError>, &mut EventContext) + Send + 'static,
{
self.spawn_background(work, complete)
}
pub fn on_child_window_closed(
&mut self,
child: WindowHandle,
callback: impl Fn(&mut V, WindowHandle, &mut EventContext) + 'static,
) {
assert_ne!(
child, self.window,
"a window cannot observe itself as a child"
);
assert!(
self.listeners.child_window_closed.len() + self.listeners.any_child_window_closed.len()
< MAX_CHILD_WINDOW_CLOSE_LISTENERS_PER_WINDOW,
"a window cannot declare more than {MAX_CHILD_WINDOW_CLOSE_LISTENERS_PER_WINDOW} child-window close listeners"
);
let callback: ChildWindowClosedCallback = Arc::new(move |view, child, context| {
callback(
view.downcast_mut::<V>()
.expect("child-window close listener received the wrong view type"),
child,
context,
);
});
let previous = self.listeners.child_window_closed.insert(child, callback);
assert!(
previous.is_none(),
"child window {child:?} was observed more than once by the same view"
);
}
pub fn on_any_child_window_closed(
&mut self,
callback: impl Fn(&mut V, WindowHandle, &mut EventContext) + 'static,
) {
assert!(
self.listeners.child_window_closed.len() + self.listeners.any_child_window_closed.len()
< MAX_CHILD_WINDOW_CLOSE_LISTENERS_PER_WINDOW,
"a window cannot declare more than {MAX_CHILD_WINDOW_CLOSE_LISTENERS_PER_WINDOW} child-window close listeners"
);
self.listeners
.any_child_window_closed
.push(Arc::new(move |view, child, context| {
callback(
view.downcast_mut::<V>()
.expect("child-window close listener received the wrong view type"),
child,
context,
);
}));
}
pub fn listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &mut EventContext) + 'static,
) -> ClickListener<V> {
let id = id.into();
let callback = Arc::new(move |view: &mut dyn Any, context: &mut EventContext| {
callback(
view.downcast_mut::<V>()
.expect("click listener received the wrong view type"),
context,
);
});
let previous = self.listeners.clicks.insert(id, callback);
assert!(
previous.is_none(),
"listener id {id:?} was registered more than once"
);
ClickListener {
id,
marker: PhantomData,
}
}
pub fn context_menu_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &ContextMenuEvent, &mut EventContext) + 'static,
) -> ContextMenuListener<V> {
let id = id.into();
let callback: ContextMenuCallback = Arc::new(
move |view: &mut dyn Any, event: &ContextMenuEvent, context: &mut EventContext| {
callback(
view.downcast_mut::<V>()
.expect("context-menu listener received the wrong view type"),
event,
context,
);
},
);
let previous = self.listeners.context_menus.insert(id, callback);
assert!(
previous.is_none(),
"context-menu listener id {id:?} was registered more than once"
);
ContextMenuListener {
id,
marker: PhantomData,
}
}
pub fn pointer_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &PointerEvent, &mut EventContext) + 'static,
) -> PointerListener<V> {
let id = id.into();
let callback = Arc::new(
move |view: &mut dyn Any, event: &PointerEvent, context: &mut EventContext| {
callback(
view.downcast_mut::<V>()
.expect("pointer listener received the wrong view type"),
event,
context,
);
},
);
let previous = self.listeners.pointers.insert(id, callback);
assert!(
previous.is_none(),
"pointer listener id {id:?} was registered more than once"
);
PointerListener {
id,
marker: PhantomData,
}
}
pub fn mouse_down_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &MouseDownEvent, &mut EventContext) + 'static,
) -> MouseDownListener<V> {
let id = id.into();
let callback: MouseListenerCallback = Arc::new(move |view, event, context| {
let MouseListenerEvent::Down(event) = event else {
unreachable!("mouse-down callback received the wrong event kind")
};
callback(
view.downcast_mut::<V>()
.expect("mouse-down listener received the wrong view type"),
event,
context,
);
});
MouseDownListener {
id,
key: self.listeners.push_mouse_listener(callback),
marker: PhantomData,
}
}
pub fn mouse_up_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &MouseUpEvent, &mut EventContext) + 'static,
) -> MouseUpListener<V> {
let id = id.into();
let callback: MouseListenerCallback = Arc::new(move |view, event, context| {
let MouseListenerEvent::Up(event) = event else {
unreachable!("mouse-up callback received the wrong event kind")
};
callback(
view.downcast_mut::<V>()
.expect("mouse-up listener received the wrong view type"),
event,
context,
);
});
MouseUpListener {
id,
key: self.listeners.push_mouse_listener(callback),
marker: PhantomData,
}
}
pub fn mouse_move_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &MouseMoveEvent, &mut EventContext) + 'static,
) -> MouseMoveListener<V> {
let id = id.into();
let callback: MouseListenerCallback = Arc::new(move |view, event, context| {
let MouseListenerEvent::Move(event) = event else {
unreachable!("mouse-move callback received the wrong event kind")
};
callback(
view.downcast_mut::<V>()
.expect("mouse-move listener received the wrong view type"),
event,
context,
);
});
MouseMoveListener {
id,
key: self.listeners.push_mouse_listener(callback),
marker: PhantomData,
}
}
pub fn mouse_exit_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &MouseExitEvent, &mut EventContext) + 'static,
) -> MouseExitListener<V> {
let id = id.into();
let callback: MouseListenerCallback = Arc::new(move |view, event, context| {
let MouseListenerEvent::Exit(event) = event else {
unreachable!("mouse-exit callback received the wrong event kind")
};
callback(
view.downcast_mut::<V>()
.expect("mouse-exit listener received the wrong view type"),
event,
context,
);
});
MouseExitListener {
id,
key: self.listeners.push_mouse_listener(callback),
marker: PhantomData,
}
}
pub fn hover_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &bool, &mut EventContext) + 'static,
) -> HoverListener<V> {
let id = id.into();
let callback: MouseListenerCallback = Arc::new(move |view, event, context| {
let MouseListenerEvent::Hover(hovered) = event else {
unreachable!("hover callback received the wrong event kind")
};
callback(
view.downcast_mut::<V>()
.expect("hover listener received the wrong view type"),
hovered,
context,
);
});
HoverListener {
id,
key: self.listeners.push_mouse_listener(callback),
marker: PhantomData,
}
}
pub fn key_down_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &KeyDownEvent, &mut EventContext) + 'static,
) -> KeyDownListener<V> {
let id = id.into();
let callback: KeyListenerCallback = Arc::new(move |view, event, context| {
let KeyListenerEvent::Down(event) = event else {
unreachable!("key-down callback received the wrong event kind")
};
callback(
view.downcast_mut::<V>()
.expect("key-down listener received the wrong view type"),
event,
context,
);
});
KeyDownListener {
id,
key: self.listeners.push_key_listener(callback),
marker: PhantomData,
}
}
pub fn key_up_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &KeyUpEvent, &mut EventContext) + 'static,
) -> KeyUpListener<V> {
let id = id.into();
let callback: KeyListenerCallback = Arc::new(move |view, event, context| {
let KeyListenerEvent::Up(event) = event else {
unreachable!("key-up callback received the wrong event kind")
};
callback(
view.downcast_mut::<V>()
.expect("key-up listener received the wrong view type"),
event,
context,
);
});
KeyUpListener {
id,
key: self.listeners.push_key_listener(callback),
marker: PhantomData,
}
}
pub fn scroll_wheel_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &ScrollWheelEvent, &mut EventContext) + 'static,
) -> ScrollWheelListener<V> {
let id = id.into();
let callback: ScrollWheelCallback = Arc::new(move |view, event, context| {
callback(
view.downcast_mut::<V>()
.expect("scroll-wheel listener received the wrong view type"),
event,
context,
);
});
let previous = self.listeners.scroll_wheels.insert(id, callback);
assert!(
previous.is_none(),
"scroll-wheel listener id {id:?} was registered more than once"
);
ScrollWheelListener {
id,
marker: PhantomData,
}
}
pub fn touch_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &TouchEvent, &mut EventContext) + 'static,
) -> TouchListener<V> {
let id = id.into();
let callback: TouchCallback = Arc::new(move |view, event, context| {
callback(
view.downcast_mut::<V>()
.expect("touch listener received the wrong view type"),
event,
context,
);
});
let previous = self.listeners.touches.insert(id, callback);
assert!(
previous.is_none(),
"touch listener id {id:?} was registered more than once"
);
TouchListener {
id,
marker: PhantomData,
}
}
pub fn mouse_pressure_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &MousePressureEvent, &mut EventContext) + 'static,
) -> MousePressureListener<V> {
let id = id.into();
let callback: MousePressureCallback = Arc::new(move |view, event, context| {
callback(
view.downcast_mut::<V>()
.expect("mouse-pressure listener received the wrong view type"),
event,
context,
);
});
let previous = self.listeners.mouse_pressures.insert(id, callback);
assert!(
previous.is_none(),
"mouse-pressure listener id {id:?} was registered more than once"
);
MousePressureListener {
id,
marker: PhantomData,
}
}
pub fn pinch_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &PinchEvent, &mut EventContext) + 'static,
) -> PinchListener<V> {
let id = id.into();
let callback: PinchCallback = Arc::new(move |view, event, context| {
callback(
view.downcast_mut::<V>()
.expect("pinch listener received the wrong view type"),
event,
context,
);
});
let previous = self.listeners.pinches.insert(id, callback);
assert!(
previous.is_none(),
"pinch listener id {id:?} was registered more than once"
);
PinchListener {
id,
marker: PhantomData,
}
}
pub fn rotation_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &RotationEvent, &mut EventContext) + 'static,
) -> RotationListener<V> {
let id = id.into();
let callback: RotationCallback = Arc::new(move |view, event, context| {
callback(
view.downcast_mut::<V>()
.expect("rotation listener received the wrong view type"),
event,
context,
);
});
let previous = self.listeners.rotations.insert(id, callback);
assert!(
previous.is_none(),
"rotation listener id {id:?} was registered more than once"
);
RotationListener {
id,
marker: PhantomData,
}
}
pub fn smart_magnify_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &SmartMagnifyEvent, &mut EventContext) + 'static,
) -> SmartMagnifyListener<V> {
let id = id.into();
let callback: SmartMagnifyCallback = Arc::new(move |view, event, context| {
callback(
view.downcast_mut::<V>()
.expect("smart-magnify listener received the wrong view type"),
event,
context,
);
});
let previous = self.listeners.smart_magnifies.insert(id, callback);
assert!(
previous.is_none(),
"smart-magnify listener id {id:?} was registered more than once"
);
SmartMagnifyListener {
id,
marker: PhantomData,
}
}
pub fn drag_listener<T: 'static>(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &DragStartEvent, &mut EventContext) -> Drag<T> + 'static,
) -> DragListener<V, T> {
let id = id.into();
let callback: DragStartCallback = Arc::new(move |view, event, context| {
let drag = callback(
view.downcast_mut::<V>()
.expect("drag listener received the wrong view type"),
event,
context,
);
AnyDrag::new(drag)
});
let previous = self
.listeners
.drag_sources
.insert(id, (TypeId::of::<T>(), callback));
assert!(
previous.is_none(),
"drag listener id {id:?} was registered more than once"
);
DragListener {
id,
marker: PhantomData,
}
}
pub fn drop_listener<T: 'static>(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &T, &DropEvent, &mut EventContext) + 'static,
) -> DropListener<V, T> {
let id = id.into();
let callback: DropCallback = Arc::new(move |view, value, event, context| {
callback(
view.downcast_mut::<V>()
.expect("drop listener received the wrong view type"),
value
.downcast_ref::<T>()
.expect("drop listener received the wrong payload type"),
event,
context,
);
});
let previous = self
.listeners
.drops
.insert((id, TypeId::of::<T>()), callback);
assert!(
previous.is_none(),
"drop listener id {id:?} for this payload type was registered more than once"
);
self.listeners.drop_order.push((id, TypeId::of::<T>()));
DropListener {
id,
marker: PhantomData,
}
}
pub fn input_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &str, &mut EventContext) + 'static,
) -> InputListener<V> {
let id = id.into();
let callback = Arc::new(
move |view: &mut dyn Any, value: &str, context: &mut EventContext| {
callback(
view.downcast_mut::<V>()
.expect("input listener received the wrong view type"),
value,
context,
);
},
);
let previous = self.listeners.inputs.insert(id, callback);
assert!(
previous.is_none(),
"input listener id {id:?} was registered more than once"
);
InputListener {
id,
marker: PhantomData,
}
}
pub fn submit_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &str, &mut EventContext) + 'static,
) -> SubmitListener<V> {
let id = id.into();
let callback = Arc::new(
move |view: &mut dyn Any, value: &str, context: &mut EventContext| {
callback(
view.downcast_mut::<V>()
.expect("submit listener received the wrong view type"),
value,
context,
);
},
);
let previous = self.listeners.submits.insert(id, callback);
assert!(
previous.is_none(),
"submit listener id {id:?} was registered more than once"
);
SubmitListener {
id,
marker: PhantomData,
}
}
pub fn form_submit_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &FormSubmitEvent, &mut EventContext) + 'static,
) -> FormSubmitListener<V> {
let id = id.into();
let callback: FormSubmitCallback = Arc::new(move |view, event, context| {
callback(
view.downcast_mut::<V>()
.expect("form submit listener received the wrong view type"),
event,
context,
);
});
let previous = self.listeners.form_submits.insert(id, callback);
assert!(
previous.is_none(),
"form submit listener id {id:?} was registered more than once"
);
FormSubmitListener {
id,
marker: PhantomData,
}
}
pub fn form_invalid_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &ValidationReport, &mut EventContext) + 'static,
) -> FormInvalidListener<V> {
let id = id.into();
let callback: FormInvalidCallback = Arc::new(move |view, report, context| {
callback(
view.downcast_mut::<V>()
.expect("form invalid listener received the wrong view type"),
report,
context,
);
});
let previous = self.listeners.form_invalids.insert(id, callback);
assert!(
previous.is_none(),
"form invalid listener id {id:?} was registered more than once"
);
FormInvalidListener {
id,
marker: PhantomData,
}
}
pub fn dismiss_listener(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &mut EventContext) + 'static,
) -> DismissListener<V> {
let id = id.into();
let callback = Arc::new(move |view: &mut dyn Any, context: &mut EventContext| {
callback(
view.downcast_mut::<V>()
.expect("dismiss listener received the wrong view type"),
context,
);
});
let previous = self.listeners.dismisses.insert(id, callback);
assert!(
previous.is_none(),
"dismiss listener id {id:?} was registered more than once"
);
DismissListener {
id,
marker: PhantomData,
}
}
pub fn action_listener<A: Action>(
&mut self,
id: impl Into<ElementId>,
callback: impl Fn(&mut V, &A, &mut EventContext) + 'static,
) -> ActionListener<V, A> {
let id = id.into();
let erased: ActionCallback = Arc::new(move |view, action, context| {
let action = action
.downcast_ref::<A>()
.expect("action listener received the wrong concrete action type");
callback(
view.downcast_mut::<V>()
.expect("action listener received the wrong view type"),
action,
context,
);
});
let action_type = TypeId::of::<A>();
ActionListener {
id,
key: self.listeners.push_action_listener(erased),
action_type,
marker: PhantomData,
}
}
}
pub(super) type ClickCallback = Arc<dyn Fn(&mut dyn Any, &mut EventContext)>;
pub(super) type PointerCallback = Arc<dyn Fn(&mut dyn Any, &PointerEvent, &mut EventContext)>;
pub(super) type MouseListenerCallback =
Arc<dyn Fn(&mut dyn Any, &MouseListenerEvent, &mut EventContext)>;
pub(super) type KeyListenerCallback =
Arc<dyn Fn(&mut dyn Any, &KeyListenerEvent, &mut EventContext)>;
pub(super) type ScrollWheelCallback =
Arc<dyn Fn(&mut dyn Any, &ScrollWheelEvent, &mut EventContext)>;
pub(super) type TouchCallback = Arc<dyn Fn(&mut dyn Any, &TouchEvent, &mut EventContext)>;
pub(super) type ContextMenuCallback =
Arc<dyn Fn(&mut dyn Any, &ContextMenuEvent, &mut EventContext)>;
pub(super) type MousePressureCallback =
Arc<dyn Fn(&mut dyn Any, &MousePressureEvent, &mut EventContext)>;
pub(super) type PinchCallback = Arc<dyn Fn(&mut dyn Any, &PinchEvent, &mut EventContext)>;
pub(super) type RotationCallback = Arc<dyn Fn(&mut dyn Any, &RotationEvent, &mut EventContext)>;
pub(super) type SmartMagnifyCallback =
Arc<dyn Fn(&mut dyn Any, &SmartMagnifyEvent, &mut EventContext)>;
pub(super) type InputCallback = Arc<dyn Fn(&mut dyn Any, &str, &mut EventContext)>;
pub(super) type FormSubmitCallback = Arc<dyn Fn(&mut dyn Any, &FormSubmitEvent, &mut EventContext)>;
pub(super) type FormInvalidCallback =
Arc<dyn Fn(&mut dyn Any, &ValidationReport, &mut EventContext)>;
pub(super) type ActionCallback = Arc<dyn Fn(&mut dyn Any, &dyn Any, &mut EventContext)>;
pub(super) type DragStartCallback =
Arc<dyn Fn(&mut dyn Any, &DragStartEvent, &mut EventContext) -> AnyDrag>;
pub(super) type DropCallback = Arc<dyn Fn(&mut dyn Any, &dyn Any, &DropEvent, &mut EventContext)>;
pub(super) type EntityEventCallback =
Rc<RefCell<dyn FnMut(&mut dyn Any, &dyn Any, &mut EventContext)>>;
pub(super) type GlobalObserverCallback = Rc<RefCell<dyn FnMut(&mut dyn Any, &mut EventContext)>>;
pub(super) type ChildWindowClosedCallback =
Arc<dyn Fn(&mut dyn Any, WindowHandle, &mut EventContext)>;
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum MouseListenerEvent {
Down(MouseDownEvent),
Up(MouseUpEvent),
Move(MouseMoveEvent),
Exit(MouseExitEvent),
Hover(bool),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) enum KeyListenerEvent {
Down(KeyDownEvent),
Up(KeyUpEvent),
}
impl KeyListenerEvent {
pub(super) fn kind(&self) -> KeyListenerKind {
match self {
Self::Down(_) => KeyListenerKind::Down,
Self::Up(_) => KeyListenerKind::Up,
}
}
}
#[derive(Clone)]
pub(super) struct EntityEventSubscription {
pub(super) state: Rc<SubscriptionState>,
pub(super) callback: EntityEventCallback,
}
impl EntityEventSubscription {
pub(super) fn is_active(&self) -> bool {
self.state.is_active()
}
}
#[derive(Clone)]
pub(super) struct GlobalObserverSubscription {
pub(super) global_type: TypeId,
pub(super) state: Rc<SubscriptionState>,
pub(super) callback: GlobalObserverCallback,
}
impl GlobalObserverSubscription {
pub(super) fn is_active(&self) -> bool {
self.state.is_active()
}
}
#[derive(Default)]
pub(super) struct ListenerRegistry {
pub(super) clicks: HashMap<ElementId, ClickCallback>,
pub(super) pointers: HashMap<ElementId, PointerCallback>,
pub(super) mouse_listeners: Vec<MouseListenerCallback>,
pub(super) key_listeners: Vec<KeyListenerCallback>,
pub(super) scroll_wheels: HashMap<ElementId, ScrollWheelCallback>,
pub(super) touches: HashMap<ElementId, TouchCallback>,
pub(super) context_menus: HashMap<ElementId, ContextMenuCallback>,
pub(super) mouse_pressures: HashMap<ElementId, MousePressureCallback>,
pub(super) pinches: HashMap<ElementId, PinchCallback>,
pub(super) rotations: HashMap<ElementId, RotationCallback>,
pub(super) smart_magnifies: HashMap<ElementId, SmartMagnifyCallback>,
pub(super) drag_sources: HashMap<ElementId, (TypeId, DragStartCallback)>,
pub(super) drops: HashMap<(ElementId, TypeId), DropCallback>,
pub(super) drop_order: Vec<(ElementId, TypeId)>,
pub(super) inputs: HashMap<ElementId, InputCallback>,
pub(super) submits: HashMap<ElementId, InputCallback>,
pub(super) form_submits: HashMap<ElementId, FormSubmitCallback>,
pub(super) form_invalids: HashMap<ElementId, FormInvalidCallback>,
pub(super) dismisses: HashMap<ElementId, ClickCallback>,
pub(super) actions: Vec<ActionCallback>,
pub(super) observed_entities: HashSet<EntityId>,
pub(super) observed_globals: HashSet<TypeId>,
pub(super) observes_window_state: bool,
pub(super) observes_viewport: bool,
pub(super) observes_displays: bool,
pub(super) observes_keyboard_layout: bool,
pub(super) observes_system_preferences: bool,
pub(super) entity_events: HashMap<(EntityId, TypeId), Vec<EntityEventSubscription>>,
pub(super) entity_subscription_count: usize,
pub(super) global_observers: Vec<GlobalObserverSubscription>,
pub(super) child_window_closed: HashMap<WindowHandle, ChildWindowClosedCallback>,
pub(super) any_child_window_closed: Vec<ChildWindowClosedCallback>,
}
impl ListenerRegistry {
pub(super) fn requires_window_state_rebuild(&self, state_changed: bool) -> bool {
state_changed && self.observes_window_state
}
pub(super) fn push_mouse_listener(
&mut self,
callback: MouseListenerCallback,
) -> MouseListenerKey {
assert!(
self.mouse_listeners.len() < MAX_MOUSE_LISTENERS_PER_WINDOW,
"a window cannot declare more than {MAX_MOUSE_LISTENERS_PER_WINDOW} targeted desktop mouse listeners"
);
let key = MouseListenerKey(self.mouse_listeners.len() as u32);
self.mouse_listeners.push(callback);
key
}
pub(super) fn mouse_listener(&self, key: MouseListenerKey) -> Option<MouseListenerCallback> {
self.mouse_listeners.get(key.0 as usize).cloned()
}
pub(super) fn push_key_listener(&mut self, callback: KeyListenerCallback) -> KeyListenerKey {
assert!(
self.key_listeners.len() < MAX_KEY_LISTENERS_PER_WINDOW,
"a window cannot declare more than {MAX_KEY_LISTENERS_PER_WINDOW} focused key listeners"
);
let key = KeyListenerKey(self.key_listeners.len() as u32);
self.key_listeners.push(callback);
key
}
pub(super) fn key_listener(&self, key: KeyListenerKey) -> Option<KeyListenerCallback> {
self.key_listeners.get(key.0 as usize).cloned()
}
pub(super) fn push_action_listener(&mut self, callback: ActionCallback) -> ActionListenerKey {
assert!(
self.actions.len() < MAX_ACTION_LISTENERS_PER_WINDOW,
"a window cannot declare more than {MAX_ACTION_LISTENERS_PER_WINDOW} typed action listeners"
);
let key = ActionListenerKey(self.actions.len() as u32);
self.actions.push(callback);
key
}
pub(super) fn action_listener(&self, key: ActionListenerKey) -> Option<ActionCallback> {
self.actions.get(key.0 as usize).cloned()
}
pub(super) fn observe_global(&mut self, global_type: TypeId) {
if self.observed_globals.contains(&global_type) {
return;
}
assert!(
self.observed_globals.len() < MAX_OBSERVED_GLOBALS_PER_WINDOW,
"a window cannot observe more than {MAX_OBSERVED_GLOBALS_PER_WINDOW} global types"
);
self.observed_globals.insert(global_type);
}
pub(super) fn observes_global_change(&self, global_types: &[TypeId], all: bool) -> bool {
all || global_types
.iter()
.any(|global_type| self.observed_globals.contains(global_type))
}
pub(super) fn subscribe_global(
&mut self,
global_type: TypeId,
callback: GlobalObserverCallback,
) -> Subscription {
if self.global_observers.len() >= MAX_GLOBAL_SUBSCRIPTIONS_PER_WINDOW {
self.prune_global_subscriptions();
}
assert!(
self.global_observers.len() < MAX_GLOBAL_SUBSCRIPTIONS_PER_WINDOW,
"a window cannot retain more than {MAX_GLOBAL_SUBSCRIPTIONS_PER_WINDOW} global subscriptions"
);
let (subscription, state) = Subscription::new();
self.global_observers.push(GlobalObserverSubscription {
global_type,
state,
callback,
});
subscription
}
pub(super) fn has_global_subscribers(&self, global_type: Option<TypeId>) -> bool {
self.global_observers.iter().any(|subscription| {
subscription.is_active()
&& global_type.is_none_or(|global_type| subscription.global_type == global_type)
})
}
pub(super) fn global_subscriptions(
&self,
global_type: Option<TypeId>,
) -> Vec<GlobalObserverSubscription> {
self.global_observers
.iter()
.filter(|subscription| {
subscription.is_active()
&& global_type.is_none_or(|global_type| subscription.global_type == global_type)
})
.cloned()
.collect()
}
pub(super) fn prune_global_subscriptions(&mut self) {
self.global_observers
.retain(GlobalObserverSubscription::is_active);
}
pub(super) fn observe_entity(&mut self, entity: EntityId) {
if self.observed_entities.contains(&entity) {
return;
}
assert!(
self.observed_entities.len() < MAX_OBSERVED_ENTITIES_PER_WINDOW,
"a window cannot observe more than {MAX_OBSERVED_ENTITIES_PER_WINDOW} entities"
);
self.observed_entities.insert(entity);
}
pub(super) fn observes_entity_change(&self, entities: &[EntityId], all: bool) -> bool {
all || entities
.iter()
.any(|entity| self.observed_entities.contains(entity))
}
pub(super) fn subscribe_entity_event(
&mut self,
entity: EntityId,
event_type: TypeId,
callback: EntityEventCallback,
) -> Subscription {
if self.entity_subscription_count >= MAX_ENTITY_SUBSCRIPTIONS_PER_WINDOW {
self.prune_entity_event_subscriptions();
}
assert!(
self.entity_subscription_count < MAX_ENTITY_SUBSCRIPTIONS_PER_WINDOW,
"a window cannot retain more than {MAX_ENTITY_SUBSCRIPTIONS_PER_WINDOW} entity-event subscriptions"
);
let (subscription, state) = Subscription::new();
self.entity_events
.entry((entity, event_type))
.or_default()
.push(EntityEventSubscription { state, callback });
self.entity_subscription_count += 1;
subscription
}
pub(super) fn has_entity_event_subscribers(
&self,
entity: EntityId,
event_type: TypeId,
) -> bool {
self.entity_events
.get(&(entity, event_type))
.is_some_and(|subscriptions| {
subscriptions.iter().any(EntityEventSubscription::is_active)
})
}
pub(super) fn entity_event_callbacks(
&self,
entity: EntityId,
event_type: TypeId,
) -> Vec<EntityEventCallback> {
self.entity_events
.get(&(entity, event_type))
.into_iter()
.flatten()
.filter(|subscription| subscription.is_active())
.map(|subscription| subscription.callback.clone())
.collect()
}
pub(super) fn prune_entity_event_subscriptions(&mut self) {
self.entity_events.retain(|_, subscriptions| {
subscriptions.retain(EntityEventSubscription::is_active);
!subscriptions.is_empty()
});
self.entity_subscription_count = self.entity_events.values().map(Vec::len).sum();
}
pub(super) fn clear(&mut self) {
self.clicks.clear();
self.pointers.clear();
self.mouse_listeners.clear();
self.key_listeners.clear();
self.scroll_wheels.clear();
self.touches.clear();
self.context_menus.clear();
self.mouse_pressures.clear();
self.pinches.clear();
self.rotations.clear();
self.smart_magnifies.clear();
self.drag_sources.clear();
self.drops.clear();
self.drop_order.clear();
self.inputs.clear();
self.submits.clear();
self.form_submits.clear();
self.form_invalids.clear();
self.dismisses.clear();
self.actions.clear();
self.observed_entities.clear();
self.observed_globals.clear();
self.observes_window_state = false;
self.observes_viewport = false;
self.observes_displays = false;
self.observes_keyboard_layout = false;
self.observes_system_preferences = false;
self.child_window_closed.clear();
self.any_child_window_closed.clear();
self.prune_entity_event_subscriptions();
self.prune_global_subscriptions();
}
}
pub struct ClickListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct PointerListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct MouseDownListener<V> {
pub(super) id: ElementId,
pub(super) key: MouseListenerKey,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct MouseUpListener<V> {
pub(super) id: ElementId,
pub(super) key: MouseListenerKey,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct MouseMoveListener<V> {
pub(super) id: ElementId,
pub(super) key: MouseListenerKey,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct MouseExitListener<V> {
pub(super) id: ElementId,
pub(super) key: MouseListenerKey,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct HoverListener<V> {
pub(super) id: ElementId,
pub(super) key: MouseListenerKey,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct KeyDownListener<V> {
pub(super) id: ElementId,
pub(super) key: KeyListenerKey,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct KeyUpListener<V> {
pub(super) id: ElementId,
pub(super) key: KeyListenerKey,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct ScrollWheelListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct TouchListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct ContextMenuListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct MousePressureListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct PinchListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct RotationListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct SmartMagnifyListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct Drag<T> {
pub(super) value: T,
pub(super) preview: Option<Element>,
pub(super) cursor_offset: Option<Point>,
pub(super) external_payload: Option<ExternalDragPayload>,
}
impl<T> Drag<T> {
pub fn new(value: T) -> Self {
Self {
value,
preview: None,
cursor_offset: None,
external_payload: None,
}
}
pub fn preview(mut self, preview: impl IntoElement) -> Self {
self.preview = Some(preview.into_element());
self
}
pub fn cursor_offset(mut self, offset: Point) -> Self {
self.cursor_offset = Some(offset);
self
}
pub fn external_payload(mut self, payload: ExternalDragPayload) -> Self {
self.external_payload = Some(payload);
self
}
pub fn external_files(self, paths: FileDragPaths) -> Self {
self.external_payload(ExternalDragPayload::Files(paths))
}
pub fn external_text(self, text: impl Into<ExternalDragText>) -> Self {
self.external_payload(ExternalDragPayload::Text(text.into()))
}
pub fn external_url(self, url: ExternalDragUrl) -> Self {
self.external_payload(ExternalDragPayload::Url(url))
}
}
pub(super) struct AnyDrag {
pub(super) value: Arc<dyn Any>,
pub(super) value_type: TypeId,
pub(super) preview: Option<Element>,
pub(super) cursor_offset: Option<Point>,
pub(super) external_payload: Option<ExternalDragPayload>,
}
impl AnyDrag {
pub(super) fn new<T: 'static>(drag: Drag<T>) -> Self {
Self {
value: Arc::new(drag.value),
value_type: TypeId::of::<T>(),
preview: drag.preview,
cursor_offset: drag.cursor_offset,
external_payload: drag.external_payload,
}
}
}
pub struct DragListener<V, T> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V, T)>,
}
impl<V, T> DragListener<V, T> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
pub struct DropListener<V, T> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V, T)>,
}
impl<V, T> DropListener<V, T> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> PointerListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
macro_rules! impl_mouse_listener_handle {
($name:ident) => {
impl<V> $name<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
pub(crate) fn key(&self) -> MouseListenerKey {
self.key
}
}
};
}
impl_mouse_listener_handle!(MouseDownListener);
impl_mouse_listener_handle!(MouseUpListener);
impl_mouse_listener_handle!(MouseMoveListener);
impl_mouse_listener_handle!(MouseExitListener);
impl_mouse_listener_handle!(HoverListener);
macro_rules! impl_key_listener_handle {
($name:ident) => {
impl<V> $name<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
pub(crate) fn key(&self) -> KeyListenerKey {
self.key
}
}
};
}
impl_key_listener_handle!(KeyDownListener);
impl_key_listener_handle!(KeyUpListener);
impl<V> ScrollWheelListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> TouchListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> ContextMenuListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> MousePressureListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> PinchListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> RotationListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> SmartMagnifyListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> ClickListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
pub struct InputListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct SubmitListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct FormSubmitListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
pub struct FormInvalidListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
impl<V> InputListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> SubmitListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> FormSubmitListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
impl<V> FormInvalidListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
pub struct DismissListener<V> {
pub(super) id: ElementId,
pub(super) marker: PhantomData<fn(&mut V)>,
}
impl<V> DismissListener<V> {
pub(crate) fn id(&self) -> ElementId {
self.id
}
}
macro_rules! impl_copy_listener_handle {
($name:ident<$($parameter:ident),+>) => {
impl<$($parameter),+> Copy for $name<$($parameter),+> {}
impl<$($parameter),+> Clone for $name<$($parameter),+> {
fn clone(&self) -> Self {
*self
}
}
};
}
impl_copy_listener_handle!(ClickListener<V>);
impl_copy_listener_handle!(PointerListener<V>);
impl_copy_listener_handle!(MouseDownListener<V>);
impl_copy_listener_handle!(MouseUpListener<V>);
impl_copy_listener_handle!(MouseMoveListener<V>);
impl_copy_listener_handle!(MouseExitListener<V>);
impl_copy_listener_handle!(HoverListener<V>);
impl_copy_listener_handle!(ScrollWheelListener<V>);
impl_copy_listener_handle!(TouchListener<V>);
impl_copy_listener_handle!(ContextMenuListener<V>);
impl_copy_listener_handle!(MousePressureListener<V>);
impl_copy_listener_handle!(PinchListener<V>);
impl_copy_listener_handle!(RotationListener<V>);
impl_copy_listener_handle!(SmartMagnifyListener<V>);
impl_copy_listener_handle!(DragListener<V, T>);
impl_copy_listener_handle!(DropListener<V, T>);
impl_copy_listener_handle!(InputListener<V>);
impl_copy_listener_handle!(SubmitListener<V>);
impl_copy_listener_handle!(FormSubmitListener<V>);
impl_copy_listener_handle!(FormInvalidListener<V>);
impl_copy_listener_handle!(DismissListener<V>);