freya_winit/
winit_waker.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::sync::Arc;

use freya_core::event_loop_messages::{
    EventLoopMessage,
    EventLoopMessageAction,
};
use futures_task::{
    waker,
    ArcWake,
};
use winit::{
    event_loop::EventLoopProxy,
    window::WindowId,
};

/// Used to enqueue a new polling for the VirtualDOM once the current one has finished
pub fn winit_waker(proxy: &EventLoopProxy<EventLoopMessage>, id: WindowId) -> std::task::Waker {
    struct DomHandle(EventLoopProxy<EventLoopMessage>, WindowId);

    impl ArcWake for DomHandle {
        fn wake_by_ref(arc_self: &Arc<Self>) {
            _ = arc_self.0.send_event(EventLoopMessage {
                window_id: Some(arc_self.1),
                action: EventLoopMessageAction::PollVDOM,
            });
        }
    }

    waker(Arc::new(DomHandle(proxy.clone(), id)))
}