Skip to main content

floem/
context.rs

1use peniko::kurbo::{Point, Rect};
2use smallvec::SmallVec;
3use std::{cell::RefCell, rc::Rc};
4
5use crate::platform::menu::Menu;
6use crate::{
7    event::{Event, EventPropagation},
8    view::ViewId,
9};
10
11pub type EventCallback = dyn FnMut(&Event) -> EventPropagation;
12pub type ResizeCallback = dyn Fn(Rect);
13pub type MenuCallback = dyn Fn() -> Menu;
14
15/// Vector of event listeners, optimized for the common case of 0-1 listeners per event type.
16/// Uses SmallVec to avoid heap allocation when there's only one listener.
17/// Inspired by Chromium's HeapVector<..., 1> pattern for event listener storage.
18pub type EventListenerVec = SmallVec<[Rc<RefCell<EventCallback>>; 1]>;
19
20#[derive(Default)]
21pub(crate) struct ResizeListeners {
22    pub(crate) rect: Rect,
23    pub(crate) callbacks: Vec<Rc<ResizeCallback>>,
24}
25
26/// Listeners for when the view moves to a different position in the window
27#[derive(Default)]
28pub(crate) struct MoveListeners {
29    pub(crate) window_origin: Point,
30    pub(crate) callbacks: Vec<Rc<dyn Fn(Point)>>,
31}
32
33pub(crate) type CleanupListeners = Vec<Rc<dyn Fn()>>;
34
35pub(crate) enum FrameUpdate {
36    Style(ViewId),
37    Layout(ViewId),
38    Paint(ViewId),
39}
40
41// Re-export EventCx from event module for backward compatibility
42pub use crate::event::EventCx;
43// Re-export DragState from window_state
44pub use crate::window::state::DragState;
45
46// Re-export layout context types from layout module for backward compatibility
47pub use crate::layout::{ComputeLayoutCx, LayoutCx};
48// Re-export style context types from style module for backward compatibility
49pub use crate::style::{InteractionState, StyleCx};
50// Re-export paint context types from paint module for backward compatibility
51pub use crate::paint::{PaintCx, PaintState};
52// Re-export update context types from message module for backward compatibility
53pub use crate::message::UpdateCx;