#![expect(
missing_debug_implementations,
reason = "Widgets are not expected to implement Debug"
)]
use std::any::TypeId;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use masonry_core::accesskit::{Node, Role};
use masonry_core::core::{
AccessCtx, AccessEvent, ActionCtx, ChildrenIds, ComposeCtx, CursorIcon, ErasedAction, EventCtx,
LayoutCtx, MeasureCtx, NewWidget, PaintCtx, PointerEvent, PropertiesMut, PropertiesRef,
QueryCtx, RegisterCtx, Subwindow, TextEvent, Update, UpdateCtx, Widget, WidgetId, WidgetRef,
};
use masonry_core::imaging::Painter;
use masonry_core::kurbo::{Axis, Point, Size};
use masonry_core::layout::{LenReq, Length};
pub struct Recorder<W: Widget> {
recording: Recording,
child: W,
}
#[derive(Debug, Clone, Default)]
pub struct Recording(Rc<RefCell<VecDeque<Record>>>);
#[derive(Debug, Clone)]
pub enum Record {
PointerEvent(PointerEvent),
TextEvent(TextEvent),
AccessEvent(AccessEvent),
AnimFrame(u64),
Action((String, WidgetId)),
RegisterChildren,
Update(Update),
PropertyChange(TypeId),
Measure(Length),
Layout(Size),
Compose,
PrePaint,
Paint,
PostPaint,
Accessibility,
}
impl Recording {
pub fn is_empty(&self) -> bool {
self.0.borrow().is_empty()
}
pub fn len(&self) -> usize {
self.0.borrow().len()
}
pub fn clear(&self) {
self.0.borrow_mut().clear();
}
pub fn next(&self) -> Option<Record> {
self.0.borrow_mut().pop_front()
}
pub fn drain(&self) -> Vec<Record> {
self.0.borrow_mut().drain(..).collect::<Vec<_>>()
}
fn push(&self, event: Record) {
self.0.borrow_mut().push_back(event);
}
}
impl<W: Widget> Recorder<W> {
pub fn new(child: W, recording: &Recording) -> Self {
Self {
child,
recording: recording.clone(),
}
}
pub fn recording(&self) -> &Recording {
&self.recording
}
pub fn inner(&self) -> &W {
&self.child
}
pub fn inner_mut(&mut self) -> &mut W {
&mut self.child
}
}
#[warn(clippy::missing_trait_methods)]
impl<W: Widget> Widget for Recorder<W> {
type Action = W::Action;
fn on_pointer_event(
&mut self,
ctx: &mut EventCtx<'_>,
props: &mut PropertiesMut<'_>,
event: &PointerEvent,
) {
self.recording.push(Record::PointerEvent(event.clone()));
self.child.on_pointer_event(ctx, props, event);
}
fn on_text_event(
&mut self,
ctx: &mut EventCtx<'_>,
props: &mut PropertiesMut<'_>,
event: &TextEvent,
) {
self.recording.push(Record::TextEvent(event.clone()));
self.child.on_text_event(ctx, props, event);
}
fn on_access_event(
&mut self,
ctx: &mut EventCtx<'_>,
props: &mut PropertiesMut<'_>,
event: &AccessEvent,
) {
self.recording.push(Record::AccessEvent(event.clone()));
self.child.on_access_event(ctx, props, event);
}
fn on_anim_frame(
&mut self,
ctx: &mut UpdateCtx<'_>,
props: &mut PropertiesMut<'_>,
interval: u64,
) {
self.recording.push(Record::AnimFrame(interval));
self.child.on_anim_frame(ctx, props, interval);
}
fn on_action(
&mut self,
ctx: &mut ActionCtx<'_>,
props: &mut PropertiesMut<'_>,
action: &ErasedAction,
source: WidgetId,
) {
self.recording
.push(Record::Action((action.type_name().into(), source)));
self.child.on_action(ctx, props, action, source);
}
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
self.recording.push(Record::RegisterChildren);
self.child.register_children(ctx);
}
fn update(&mut self, ctx: &mut UpdateCtx<'_>, props: &mut PropertiesMut<'_>, event: &Update) {
self.recording.push(Record::Update(event.clone()));
self.child.update(ctx, props, event);
}
fn property_changed(&mut self, ctx: &mut UpdateCtx<'_>, property_type: TypeId) {
self.recording.push(Record::PropertyChange(property_type));
self.child.property_changed(ctx, property_type);
}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
let length = self.child.measure(ctx, props, axis, len_req, cross_length);
self.recording.push(Record::Measure(length));
length
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, props: &PropertiesRef<'_>, size: Size) {
self.recording.push(Record::Layout(size));
self.child.layout(ctx, props, size);
}
fn compose(&mut self, ctx: &mut ComposeCtx<'_>) {
self.recording.push(Record::Compose);
self.child.compose(ctx);
}
fn pre_paint(
&mut self,
ctx: &mut PaintCtx<'_>,
props: &PropertiesRef<'_>,
painter: &mut Painter<'_>,
) {
self.recording.push(Record::PrePaint);
self.child.pre_paint(ctx, props, painter);
}
fn paint(
&mut self,
ctx: &mut PaintCtx<'_>,
props: &PropertiesRef<'_>,
painter: &mut Painter<'_>,
) {
self.recording.push(Record::Paint);
self.child.paint(ctx, props, painter);
}
fn post_paint(
&mut self,
ctx: &mut PaintCtx<'_>,
props: &PropertiesRef<'_>,
painter: &mut Painter<'_>,
) {
self.recording.push(Record::PostPaint);
self.child.post_paint(ctx, props, painter);
}
fn accessibility_role(&self) -> Role {
self.child.accessibility_role()
}
fn accessibility(
&mut self,
ctx: &mut AccessCtx<'_>,
props: &PropertiesRef<'_>,
node: &mut Node,
) {
self.recording.push(Record::Accessibility);
self.child.accessibility(ctx, props, node);
}
fn children_ids(&self) -> ChildrenIds {
self.child.children_ids()
}
fn as_subwindow(&mut self) -> Option<&mut dyn Subwindow> {
None
}
fn accepts_pointer_interaction(&self) -> bool {
self.child.accepts_pointer_interaction()
}
fn accepts_tooltips(&self) -> bool {
self.child.accepts_tooltips()
}
fn propagates_pointer_interaction(&self) -> bool {
self.child.propagates_pointer_interaction()
}
fn accepts_focus(&self) -> bool {
self.child.accepts_focus()
}
fn accepts_text_input(&self) -> bool {
self.child.accepts_text_input()
}
fn make_trace_span(&self, id: WidgetId) -> tracing::Span {
self.child.make_trace_span(id)
}
fn get_debug_text(&self) -> Option<String> {
self.child.get_debug_text()
}
fn get_cursor(&self, ctx: &QueryCtx<'_>, pos: Point) -> CursorIcon {
self.child.get_cursor(ctx, pos)
}
fn find_widget_under_pointer<'c>(
&'c self,
ctx: QueryCtx<'c>,
pos: Point,
) -> Option<WidgetRef<'c, dyn Widget>> {
self.child.find_widget_under_pointer(ctx, pos)
}
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn short_type_name(&self) -> &'static str {
"Recorder"
}
fn prepare(self) -> NewWidget<Self>
where
Self: Sized,
{
NewWidget::new(self)
}
}