1use std::{
2 borrow::Cow,
3 fmt,
4 pin::Pin,
5 task::Waker,
6};
7
8use accesskit_winit::WindowEvent as AccessibilityWindowEvent;
9use freya_core::integration::*;
10use freya_engine::prelude::{
11 FontCollection,
12 FontMgr,
13};
14use futures_lite::future::FutureExt as _;
15use futures_util::{
16 FutureExt as _,
17 StreamExt,
18 select,
19};
20use ragnarok::{
21 EventsExecutorRunner,
22 EventsMeasurerRunner,
23};
24use rustc_hash::FxHashMap;
25use torin::prelude::{
26 CursorPoint,
27 Size2D,
28};
29#[cfg(all(feature = "tray", not(target_os = "linux")))]
30use tray_icon::TrayIcon;
31use winit::{
32 application::ApplicationHandler,
33 dpi::{
34 LogicalPosition,
35 LogicalSize,
36 },
37 event::{
38 ElementState,
39 Ime,
40 MouseScrollDelta,
41 Touch,
42 TouchPhase,
43 WindowEvent,
44 },
45 event_loop::{
46 ActiveEventLoop,
47 EventLoopProxy,
48 },
49 window::{
50 Theme,
51 WindowId,
52 },
53};
54
55use crate::{
56 accessibility::AccessibilityTask,
57 config::{
58 CloseDecision,
59 WindowConfig,
60 },
61 drivers::GraphicsDriver,
62 integration::is_ime_role,
63 plugins::{
64 PluginEvent,
65 PluginHandle,
66 PluginsManager,
67 },
68 window::AppWindow,
69 winit_mappings::{
70 self,
71 map_winit_mouse_button,
72 map_winit_touch_force,
73 map_winit_touch_phase,
74 },
75};
76
77pub struct WinitRenderer {
78 pub windows_configs: Vec<WindowConfig>,
79 #[cfg(feature = "tray")]
80 pub(crate) tray: (
81 Option<crate::config::TrayIconGetter>,
82 Option<crate::config::TrayHandler>,
83 ),
84 #[cfg(all(feature = "tray", not(target_os = "linux")))]
85 pub(crate) tray_icon: Option<TrayIcon>,
86 pub resumed: bool,
87 pub windows: FxHashMap<WindowId, AppWindow>,
88 pub proxy: EventLoopProxy<NativeEvent>,
89 pub plugins: PluginsManager,
90 pub fallback_fonts: Vec<Cow<'static, str>>,
91 pub screen_reader: ScreenReader,
92 pub font_manager: FontMgr,
93 pub font_collection: FontCollection,
94 pub futures: Vec<Pin<Box<dyn std::future::Future<Output = ()>>>>,
95 pub waker: Waker,
96 pub exit_on_close: bool,
97 pub gpu_resource_cache_limit: usize,
98}
99
100pub struct RendererContext<'a> {
101 pub windows: &'a mut FxHashMap<WindowId, AppWindow>,
102 pub proxy: &'a mut EventLoopProxy<NativeEvent>,
103 pub plugins: &'a mut PluginsManager,
104 pub fallback_fonts: &'a mut Vec<Cow<'static, str>>,
105 pub screen_reader: &'a mut ScreenReader,
106 pub font_manager: &'a mut FontMgr,
107 pub font_collection: &'a mut FontCollection,
108 pub active_event_loop: &'a ActiveEventLoop,
109 pub gpu_resource_cache_limit: usize,
110}
111
112impl RendererContext<'_> {
113 pub fn launch_window(&mut self, window_config: WindowConfig) -> WindowId {
114 let app_window = AppWindow::new(
115 window_config,
116 self.active_event_loop,
117 self.proxy,
118 self.plugins,
119 self.font_collection,
120 self.font_manager,
121 self.fallback_fonts,
122 self.screen_reader.clone(),
123 self.gpu_resource_cache_limit,
124 );
125
126 let window_id = app_window.window.id();
127
128 self.proxy
129 .send_event(NativeEvent::Window(NativeWindowEvent {
130 window_id,
131 action: NativeWindowEventAction::PollRunner,
132 }))
133 .ok();
134
135 self.windows.insert(window_id, app_window);
136
137 window_id
138 }
139
140 pub fn windows(&self) -> &FxHashMap<WindowId, AppWindow> {
141 self.windows
142 }
143
144 pub fn windows_mut(&mut self) -> &mut FxHashMap<WindowId, AppWindow> {
145 self.windows
146 }
147
148 pub fn exit(&mut self) {
149 self.active_event_loop.exit();
150 }
151}
152
153#[derive(Debug)]
154pub enum NativeWindowEventAction {
155 PollRunner,
156
157 Accessibility(AccessibilityWindowEvent),
158
159 PlatformEvent(PlatformEvent),
160
161 User(UserEvent),
162}
163
164#[derive(Clone)]
166pub struct LaunchProxy(pub EventLoopProxy<NativeEvent>);
167
168impl LaunchProxy {
169 pub fn post_callback<F, T: 'static>(&self, f: F) -> futures_channel::oneshot::Receiver<T>
179 where
180 F: FnOnce(&mut RendererContext) -> T + 'static,
181 {
182 let (tx, rx) = futures_channel::oneshot::channel::<T>();
183 let cb = Box::new(move |ctx: &mut RendererContext| {
184 let res = (f)(ctx);
185 let _ = tx.send(res);
186 });
187 let _ = self
188 .0
189 .send_event(NativeEvent::Generic(NativeGenericEvent::RendererCallback(
190 cb,
191 )));
192 rx
193 }
194}
195
196pub type RendererCallback = Box<dyn FnOnce(WindowId, &mut RendererContext) + 'static>;
197
198pub enum NativeWindowErasedEventAction {
199 LaunchWindow {
200 window_config: WindowConfig,
201 ack: futures_channel::oneshot::Sender<WindowId>,
202 },
203 CloseWindow(WindowId),
204 RendererCallback(RendererCallback),
205}
206
207#[derive(Debug)]
208pub struct NativeWindowEvent {
209 pub window_id: WindowId,
210 pub action: NativeWindowEventAction,
211}
212
213#[cfg(feature = "tray")]
214#[derive(Debug)]
215pub enum NativeTrayEventAction {
216 TrayEvent(tray_icon::TrayIconEvent),
217 MenuEvent(tray_icon::menu::MenuEvent),
218 LaunchWindow(SingleThreadErasedEvent),
219}
220
221#[cfg(feature = "tray")]
222#[derive(Debug)]
223pub struct NativeTrayEvent {
224 pub action: NativeTrayEventAction,
225}
226
227pub enum NativeGenericEvent {
228 PollFutures,
229 RendererCallback(Box<dyn FnOnce(&mut RendererContext) + 'static>),
230}
231
232impl fmt::Debug for NativeGenericEvent {
233 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234 match self {
235 NativeGenericEvent::PollFutures => f.write_str("PollFutures"),
236 NativeGenericEvent::RendererCallback(_) => f.write_str("RendererCallback"),
237 }
238 }
239}
240
241unsafe impl Send for NativeGenericEvent {}
245unsafe impl Sync for NativeGenericEvent {}
246
247#[derive(Debug)]
248pub enum NativeEvent {
249 Window(NativeWindowEvent),
250 #[cfg(feature = "tray")]
251 Tray(NativeTrayEvent),
252 Generic(NativeGenericEvent),
253 Preferences(mundy::Preferences),
254}
255
256impl From<accesskit_winit::Event> for NativeEvent {
257 fn from(event: accesskit_winit::Event) -> Self {
258 NativeEvent::Window(NativeWindowEvent {
259 window_id: event.window_id,
260 action: NativeWindowEventAction::Accessibility(event.window_event),
261 })
262 }
263}
264
265impl ApplicationHandler<NativeEvent> for WinitRenderer {
266 fn resumed(&mut self, active_event_loop: &winit::event_loop::ActiveEventLoop) {
267 if !self.resumed {
268 #[cfg(feature = "tray")]
269 {
270 #[cfg(not(target_os = "linux"))]
271 if let Some(tray_icon) = self.tray.0.take() {
272 self.tray_icon = Some((tray_icon)());
273 }
274
275 #[cfg(target_os = "macos")]
276 {
277 use objc2_core_foundation::CFRunLoop;
278
279 let rl = CFRunLoop::main().expect("Failed to run CFRunLoop");
280 CFRunLoop::wake_up(&rl);
281 }
282 }
283
284 for window_config in self.windows_configs.drain(..) {
285 let app_window = AppWindow::new(
286 window_config,
287 active_event_loop,
288 &self.proxy,
289 &mut self.plugins,
290 &mut self.font_collection,
291 &self.font_manager,
292 &self.fallback_fonts,
293 self.screen_reader.clone(),
294 self.gpu_resource_cache_limit,
295 );
296
297 self.proxy
298 .send_event(NativeEvent::Window(NativeWindowEvent {
299 window_id: app_window.window.id(),
300 action: NativeWindowEventAction::PollRunner,
301 }))
302 .ok();
303
304 self.windows.insert(app_window.window.id(), app_window);
305 }
306 self.resumed = true;
307
308 subscribe_preferences(self.proxy.clone());
309
310 let _ = self
311 .proxy
312 .send_event(NativeEvent::Generic(NativeGenericEvent::PollFutures));
313 } else {
314 let old_windows: Vec<_> = self.windows.drain().collect();
317 for (_, mut app_window) in old_windows {
318 let (new_driver, new_window) = GraphicsDriver::new(
319 active_event_loop,
320 app_window.window_attributes.clone(),
321 self.gpu_resource_cache_limit,
322 );
323
324 let new_id = new_window.id();
325 app_window.driver = new_driver;
326 app_window.window = new_window;
327 app_window.process_layout_on_next_render = true;
328 app_window.tree.layout.reset();
329
330 self.windows.insert(new_id, app_window);
331
332 self.proxy
333 .send_event(NativeEvent::Window(NativeWindowEvent {
334 window_id: new_id,
335 action: NativeWindowEventAction::PollRunner,
336 }))
337 .ok();
338 }
339 }
340 }
341
342 fn user_event(
343 &mut self,
344 active_event_loop: &winit::event_loop::ActiveEventLoop,
345 event: NativeEvent,
346 ) {
347 match event {
348 NativeEvent::Generic(NativeGenericEvent::RendererCallback(cb)) => {
349 let mut renderer_context = RendererContext {
350 fallback_fonts: &mut self.fallback_fonts,
351 active_event_loop,
352 windows: &mut self.windows,
353 proxy: &mut self.proxy,
354 plugins: &mut self.plugins,
355 screen_reader: &mut self.screen_reader,
356 font_manager: &mut self.font_manager,
357 font_collection: &mut self.font_collection,
358 gpu_resource_cache_limit: self.gpu_resource_cache_limit,
359 };
360 (cb)(&mut renderer_context);
361 }
362 NativeEvent::Generic(NativeGenericEvent::PollFutures) => {
363 let mut cx = std::task::Context::from_waker(&self.waker);
364 self.futures
365 .retain_mut(|fut| fut.poll(&mut cx).is_pending());
366 }
367 NativeEvent::Preferences(prefs) => {
368 for app in self.windows.values_mut() {
369 app.platform
370 .accent_color
371 .set_if_modified(prefs.accent_color);
372 }
373 }
374 #[cfg(feature = "tray")]
375 NativeEvent::Tray(NativeTrayEvent { action }) => {
376 let renderer_context = RendererContext {
377 fallback_fonts: &mut self.fallback_fonts,
378 active_event_loop,
379 windows: &mut self.windows,
380 proxy: &mut self.proxy,
381 plugins: &mut self.plugins,
382 screen_reader: &mut self.screen_reader,
383 font_manager: &mut self.font_manager,
384 font_collection: &mut self.font_collection,
385 gpu_resource_cache_limit: self.gpu_resource_cache_limit,
386 };
387 match action {
388 NativeTrayEventAction::TrayEvent(icon_event) => {
389 use crate::tray::TrayEvent;
390 if let Some(tray_handler) = &mut self.tray.1 {
391 (tray_handler)(TrayEvent::Icon(icon_event), renderer_context)
392 }
393 }
394 NativeTrayEventAction::MenuEvent(menu_event) => {
395 use crate::tray::TrayEvent;
396 if let Some(tray_handler) = &mut self.tray.1 {
397 (tray_handler)(TrayEvent::Menu(menu_event), renderer_context)
398 }
399 }
400 NativeTrayEventAction::LaunchWindow(data) => {
401 let window_config = data
402 .0
403 .downcast::<WindowConfig>()
404 .expect("Expected WindowConfig");
405 let app_window = AppWindow::new(
406 *window_config,
407 active_event_loop,
408 &self.proxy,
409 &mut self.plugins,
410 &mut self.font_collection,
411 &self.font_manager,
412 &self.fallback_fonts,
413 self.screen_reader.clone(),
414 self.gpu_resource_cache_limit,
415 );
416
417 self.proxy
418 .send_event(NativeEvent::Window(NativeWindowEvent {
419 window_id: app_window.window.id(),
420 action: NativeWindowEventAction::PollRunner,
421 }))
422 .ok();
423
424 self.windows.insert(app_window.window.id(), app_window);
425 }
426 }
427 }
428 NativeEvent::Window(NativeWindowEvent { action, window_id }) => {
429 if let Some(app) = &mut self.windows.get_mut(&window_id) {
430 match action {
431 NativeWindowEventAction::PollRunner => {
432 let mut cx = std::task::Context::from_waker(&app.waker);
433
434 #[cfg(feature = "hotreload")]
435 let hotreload_triggered = app
436 .hot_reload_pending
437 .swap(false, std::sync::atomic::Ordering::AcqRel);
438
439 #[cfg(feature = "hotreload")]
440 if hotreload_triggered {
441 app.runner.reload();
442 }
443
444 {
445 let fut = std::pin::pin!(async {
446 select! {
447 events_chunk = app.events_receiver.next() => {
448 match events_chunk {
449 Some(EventsChunk::Processed(processed_events)) => {
450 let events_executor_adapter = EventsExecutorAdapter {
451 runner: &mut app.runner,
452 };
453 events_executor_adapter.run(&mut app.nodes_state, processed_events);
454 }
455 Some(EventsChunk::Batch(events)) => {
456 for event in events {
457 app.runner.handle_event(event.node_id, event.name, event.data, event.bubbles);
458 }
459 }
460 _ => {}
461 }
462 },
463 _ = app.runner.handle_events().fuse() => {},
464 }
465 });
466
467 match fut.poll(&mut cx) {
468 std::task::Poll::Ready(_) => {
469 self.proxy
470 .send_event(NativeEvent::Window(NativeWindowEvent {
471 window_id: app.window.id(),
472 action: NativeWindowEventAction::PollRunner,
473 }))
474 .ok();
475 }
476 std::task::Poll::Pending => {}
477 }
478 }
479
480 self.plugins.send(
481 PluginEvent::StartedUpdatingTree {
482 window: &app.window,
483 tree: &app.tree,
484 },
485 PluginHandle::new(&self.proxy),
486 );
487 let mutations = app.runner.sync_and_update();
488 let result = app.runner.run_in(|| app.tree.apply_mutations(mutations));
489 if result.needs_render {
490 app.process_layout_on_next_render = true;
491 app.window.request_redraw();
492 }
493 #[cfg(feature = "hotreload")]
494 if hotreload_triggered {
495 app.process_layout_on_next_render = true;
498 app.window.request_redraw();
499 }
500 if result.needs_accessibility {
501 app.accessibility_tasks_for_next_render |=
502 AccessibilityTask::ProcessUpdate { mode: None };
503 app.window.request_redraw();
504 }
505 self.plugins.send(
506 PluginEvent::FinishedUpdatingTree {
507 window: &app.window,
508 tree: &app.tree,
509 },
510 PluginHandle::new(&self.proxy),
511 );
512 #[cfg(debug_assertions)]
513 {
514 tracing::info!("Updated app tree.");
515 tracing::info!("{:#?}", app.tree);
516 tracing::info!("{:#?}", app.runner);
517 }
518 }
519 NativeWindowEventAction::Accessibility(
520 accesskit_winit::WindowEvent::AccessibilityDeactivated,
521 ) => {
522 self.screen_reader.set(false);
523 }
524 NativeWindowEventAction::Accessibility(
525 accesskit_winit::WindowEvent::ActionRequested(_),
526 ) => {}
527 NativeWindowEventAction::Accessibility(
528 accesskit_winit::WindowEvent::InitialTreeRequested,
529 ) => {
530 app.accessibility_tasks_for_next_render = AccessibilityTask::Init;
531 app.window.request_redraw();
532 self.screen_reader.set(true);
533 }
534 NativeWindowEventAction::User(user_event) => match user_event {
535 UserEvent::RequestRedraw => {
536 app.window.request_redraw();
537 }
538 UserEvent::FocusAccessibilityNode(strategy) => {
539 let task = match strategy {
540 AccessibilityFocusStrategy::Backward(_)
541 | AccessibilityFocusStrategy::Forward(_) => {
542 AccessibilityTask::ProcessUpdate {
543 mode: Some(NavigationMode::Keyboard),
544 }
545 }
546 _ => AccessibilityTask::ProcessUpdate { mode: None },
547 };
548 app.tree.accessibility_diff.request_focus(strategy);
549 app.accessibility_tasks_for_next_render = task;
550 app.window.request_redraw();
551 }
552 UserEvent::SetCursorIcon(cursor_icon) => {
553 app.window.set_cursor(cursor_icon);
554 }
555 UserEvent::Erased(data) => {
556 let action = data
557 .0
558 .downcast::<NativeWindowErasedEventAction>()
559 .expect("Expected NativeWindowErasedEventAction");
560 match *action {
561 NativeWindowErasedEventAction::LaunchWindow {
562 window_config,
563 ack,
564 } => {
565 let app_window = AppWindow::new(
566 window_config,
567 active_event_loop,
568 &self.proxy,
569 &mut self.plugins,
570 &mut self.font_collection,
571 &self.font_manager,
572 &self.fallback_fonts,
573 self.screen_reader.clone(),
574 self.gpu_resource_cache_limit,
575 );
576
577 let window_id = app_window.window.id();
578
579 let _ = self.proxy.send_event(NativeEvent::Window(
580 NativeWindowEvent {
581 window_id,
582 action: NativeWindowEventAction::PollRunner,
583 },
584 ));
585
586 self.windows.insert(window_id, app_window);
587 let _ = ack.send(window_id);
588 }
589 NativeWindowErasedEventAction::CloseWindow(window_id) => {
590 let _ = self.windows.remove(&window_id);
592 let has_windows = !self.windows.is_empty();
593
594 let has_tray = {
595 #[cfg(feature = "tray")]
596 {
597 self.tray.1.is_some()
598 }
599 #[cfg(not(feature = "tray"))]
600 {
601 false
602 }
603 };
604
605 if !has_windows && !has_tray && self.exit_on_close {
607 active_event_loop.exit();
608 }
609 }
610 NativeWindowErasedEventAction::RendererCallback(cb) => {
611 let window_id = app.window.id();
612 let mut renderer_context = RendererContext {
613 fallback_fonts: &mut self.fallback_fonts,
614 active_event_loop,
615 windows: &mut self.windows,
616 proxy: &mut self.proxy,
617 plugins: &mut self.plugins,
618 screen_reader: &mut self.screen_reader,
619 font_manager: &mut self.font_manager,
620 font_collection: &mut self.font_collection,
621 gpu_resource_cache_limit: self.gpu_resource_cache_limit,
622 };
623 (cb)(window_id, &mut renderer_context);
624 }
625 }
626 }
627 },
628 NativeWindowEventAction::PlatformEvent(platform_event) => {
629 let mut events_measurer_adapter = EventsMeasurerAdapter {
630 scale_factor: app.effective_scale_factor(),
631 tree: &mut app.tree,
632 };
633 let processed_events = events_measurer_adapter.run(
634 &mut vec![platform_event],
635 &mut app.nodes_state,
636 app.accessibility.focused_node_id(),
637 );
638 app.events_sender
639 .unbounded_send(EventsChunk::Processed(processed_events))
640 .unwrap();
641 }
642 }
643 }
644 }
645 }
646 }
647
648 fn window_event(
649 &mut self,
650 event_loop: &winit::event_loop::ActiveEventLoop,
651 window_id: winit::window::WindowId,
652 event: winit::event::WindowEvent,
653 ) {
654 if let Some(app) = &mut self.windows.get_mut(&window_id) {
655 app.accessibility_adapter.process_event(&app.window, &event);
656 match event {
657 WindowEvent::ThemeChanged(theme) => {
658 app.platform.preferred_theme.set(match theme {
659 Theme::Light => PreferredTheme::Light,
660 Theme::Dark => PreferredTheme::Dark,
661 });
662 }
663 WindowEvent::ScaleFactorChanged { .. } => {
664 app.sync_scale_factor();
665 app.window.request_redraw();
666 app.process_layout_on_next_render = true;
667 app.tree.layout.reset();
668 app.tree.text_cache.reset();
669 }
670 WindowEvent::CloseRequested => {
671 let mut on_close_hook = self
672 .windows
673 .get_mut(&window_id)
674 .and_then(|app| app.on_close.take());
675
676 let decision = if let Some(ref mut on_close) = on_close_hook {
677 let renderer_context = RendererContext {
678 fallback_fonts: &mut self.fallback_fonts,
679 active_event_loop: event_loop,
680 windows: &mut self.windows,
681 proxy: &mut self.proxy,
682 plugins: &mut self.plugins,
683 screen_reader: &mut self.screen_reader,
684 font_manager: &mut self.font_manager,
685 font_collection: &mut self.font_collection,
686 gpu_resource_cache_limit: self.gpu_resource_cache_limit,
687 };
688 on_close(renderer_context, window_id)
689 } else {
690 CloseDecision::Close
691 };
692
693 if matches!(decision, CloseDecision::KeepOpen)
694 && let Some(app) = self.windows.get_mut(&window_id)
695 {
696 app.on_close = on_close_hook;
697 }
698
699 if matches!(decision, CloseDecision::Close) {
700 self.windows.remove(&window_id);
701 let has_windows = !self.windows.is_empty();
702
703 let has_tray = {
704 #[cfg(feature = "tray")]
705 {
706 self.tray.1.is_some()
707 }
708 #[cfg(not(feature = "tray"))]
709 {
710 false
711 }
712 };
713
714 if !has_windows && !has_tray && self.exit_on_close {
716 event_loop.exit();
717 }
718 }
719 }
720 WindowEvent::ModifiersChanged(modifiers) => {
721 app.modifiers_state = modifiers.state();
722 }
723 WindowEvent::Focused(is_focused) => {
724 app.platform.is_app_focused.set_if_modified(is_focused);
725 }
726 WindowEvent::RedrawRequested => {
727 let scale_factor = app.effective_scale_factor();
728 hotpath::measure_block!("RedrawRequested", {
729 if app.process_layout_on_next_render {
730 self.plugins.send(
731 PluginEvent::StartedMeasuringLayout {
732 window: &app.window,
733 tree: &app.tree,
734 },
735 PluginHandle::new(&self.proxy),
736 );
737 let size: Size2D = (
738 app.window.inner_size().width as f32,
739 app.window.inner_size().height as f32,
740 )
741 .into();
742
743 app.tree.measure_layout(
744 size,
745 &mut self.font_collection,
746 &self.font_manager,
747 &app.events_sender,
748 &mut app.nodes_state,
749 scale_factor,
750 &self.fallback_fonts,
751 );
752 app.platform.root_size.set_if_modified(size);
753 app.process_layout_on_next_render = false;
754 self.plugins.send(
755 PluginEvent::FinishedMeasuringLayout {
756 window: &app.window,
757 tree: &app.tree,
758 },
759 PluginHandle::new(&self.proxy),
760 );
761 }
762
763 app.driver.present(
764 app.window.inner_size().cast(),
765 &app.window,
766 |surface| {
767 self.plugins.send(
768 PluginEvent::BeforeRender {
769 window: &app.window,
770 canvas: surface.canvas(),
771 font_collection: &self.font_collection,
772 tree: &app.tree,
773 },
774 PluginHandle::new(&self.proxy),
775 );
776
777 let render_pipeline = RenderPipeline {
778 font_collection: &mut self.font_collection,
779 font_manager: &self.font_manager,
780 tree: &app.tree,
781 canvas: surface.canvas(),
782 scale_factor,
783 background: app.background,
784 };
785
786 render_pipeline.render();
787
788 self.plugins.send(
789 PluginEvent::AfterRender {
790 window: &app.window,
791 canvas: surface.canvas(),
792 font_collection: &self.font_collection,
793 tree: &app.tree,
794 animation_clock: &app.animation_clock,
795 },
796 PluginHandle::new(&self.proxy),
797 );
798 self.plugins.send(
799 PluginEvent::BeforePresenting {
800 window: &app.window,
801 font_collection: &self.font_collection,
802 tree: &app.tree,
803 },
804 PluginHandle::new(&self.proxy),
805 );
806 },
807 );
808 self.plugins.send(
809 PluginEvent::AfterPresenting {
810 window: &app.window,
811 font_collection: &self.font_collection,
812 tree: &app.tree,
813 },
814 PluginHandle::new(&self.proxy),
815 );
816
817 self.plugins.send(
818 PluginEvent::BeforeAccessibility {
819 window: &app.window,
820 font_collection: &self.font_collection,
821 tree: &app.tree,
822 },
823 PluginHandle::new(&self.proxy),
824 );
825
826 match app.accessibility_tasks_for_next_render.take() {
827 AccessibilityTask::ProcessUpdate { mode } => {
828 let update = app
829 .accessibility
830 .process_updates(&mut app.tree, &app.events_sender);
831 app.platform
832 .focused_accessibility_id
833 .set_if_modified(update.focus);
834 let node_id = app.accessibility.focused_node_id().unwrap();
835 let layout_node = app.tree.layout.get(&node_id).unwrap();
836 let focused_node =
837 AccessibilityTree::create_node(node_id, layout_node, &app.tree);
838 app.window.set_ime_allowed(is_ime_role(focused_node.role()));
839 app.platform
840 .focused_accessibility_node
841 .set_if_modified(focused_node);
842 if let Some(mode) = mode {
843 app.platform.navigation_mode.set(mode);
844 }
845
846 let area = layout_node.visible_area();
847 app.window.set_ime_cursor_area(
848 LogicalPosition::new(area.min_x(), area.min_y()),
849 LogicalSize::new(area.width(), area.height()),
850 );
851
852 app.accessibility_adapter.update_if_active(|| update);
853 }
854 AccessibilityTask::Init => {
855 let update = app.accessibility.init(&mut app.tree);
856 app.platform
857 .focused_accessibility_id
858 .set_if_modified(update.focus);
859 let node_id = app.accessibility.focused_node_id().unwrap();
860 let layout_node = app.tree.layout.get(&node_id).unwrap();
861 let focused_node =
862 AccessibilityTree::create_node(node_id, layout_node, &app.tree);
863 app.window.set_ime_allowed(is_ime_role(focused_node.role()));
864 app.platform
865 .focused_accessibility_node
866 .set_if_modified(focused_node);
867
868 let area = layout_node.visible_area();
869 app.window.set_ime_cursor_area(
870 LogicalPosition::new(area.min_x(), area.min_y()),
871 LogicalSize::new(area.width(), area.height()),
872 );
873
874 app.accessibility_adapter.update_if_active(|| update);
875 }
876 AccessibilityTask::None => {}
877 }
878
879 self.plugins.send(
880 PluginEvent::AfterAccessibility {
881 window: &app.window,
882 font_collection: &self.font_collection,
883 tree: &app.tree,
884 },
885 PluginHandle::new(&self.proxy),
886 );
887
888 if app.ticker_sender.receiver_count() > 0 {
889 app.ticker_sender.broadcast_blocking(()).unwrap();
890 }
891
892 self.plugins.send(
893 PluginEvent::AfterRedraw {
894 window: &app.window,
895 font_collection: &self.font_collection,
896 tree: &app.tree,
897 },
898 PluginHandle::new(&self.proxy),
899 );
900 });
901 }
902 WindowEvent::Resized(size) => {
903 app.driver.resize(size);
904
905 app.window.request_redraw();
906
907 app.process_layout_on_next_render = true;
908 app.tree.layout.clear_dirty();
909 app.tree.layout.invalidate(NodeId::ROOT);
910 }
911
912 WindowEvent::MouseInput { state, button, .. } => {
913 app.mouse_state = state;
914 app.platform
915 .navigation_mode
916 .set(NavigationMode::NotKeyboard);
917
918 let name = if state == ElementState::Pressed {
919 MouseEventName::MouseDown
920 } else {
921 MouseEventName::MouseUp
922 };
923 let platform_event = PlatformEvent::Mouse {
924 name,
925 cursor: (app.position.x, app.position.y).into(),
926 button: Some(map_winit_mouse_button(button)),
927 };
928 let mut events_measurer_adapter = EventsMeasurerAdapter {
929 scale_factor: app.effective_scale_factor(),
930 tree: &mut app.tree,
931 };
932 let processed_events = events_measurer_adapter.run(
933 &mut vec![platform_event],
934 &mut app.nodes_state,
935 app.accessibility.focused_node_id(),
936 );
937 app.events_sender
938 .unbounded_send(EventsChunk::Processed(processed_events))
939 .unwrap();
940 }
941
942 WindowEvent::KeyboardInput {
943 event,
944 is_synthetic,
945 ..
946 } => {
947 if is_synthetic && event.state == ElementState::Pressed {
949 return;
950 }
951
952 let name = match event.state {
953 ElementState::Pressed => KeyboardEventName::KeyDown,
954 ElementState::Released => KeyboardEventName::KeyUp,
955 };
956 let key = winit_mappings::map_winit_key(&event.logical_key);
957 let code = winit_mappings::map_winit_physical_key(&event.physical_key);
958 let modifiers = winit_mappings::map_winit_modifiers(app.modifiers_state);
959
960 #[cfg(feature = "zoom-shortcuts")]
961 if app.try_handle_zoom_shortcut(&key, modifiers, event.state.is_pressed()) {
962 return;
963 }
964
965 self.plugins.send(
966 PluginEvent::KeyboardInput {
967 window: &app.window,
968 key: key.clone(),
969 code,
970 modifiers,
971 is_pressed: event.state.is_pressed(),
972 },
973 PluginHandle::new(&self.proxy),
974 );
975
976 let platform_event = PlatformEvent::Keyboard {
977 name,
978 key,
979 code,
980 modifiers,
981 };
982 let mut events_measurer_adapter = EventsMeasurerAdapter {
983 scale_factor: app.effective_scale_factor(),
984 tree: &mut app.tree,
985 };
986 let processed_events = events_measurer_adapter.run(
987 &mut vec![platform_event],
988 &mut app.nodes_state,
989 app.accessibility.focused_node_id(),
990 );
991 app.events_sender
992 .unbounded_send(EventsChunk::Processed(processed_events))
993 .unwrap();
994 }
995
996 WindowEvent::MouseWheel { delta, phase, .. } => {
997 const WHEEL_SPEED_MODIFIER: f64 = 53.0;
998 const TOUCHPAD_SPEED_MODIFIER: f64 = 2.0;
999
1000 if TouchPhase::Moved == phase {
1001 let scroll_data = {
1002 match delta {
1003 MouseScrollDelta::LineDelta(x, y) => (
1004 (x as f64 * WHEEL_SPEED_MODIFIER),
1005 (y as f64 * WHEEL_SPEED_MODIFIER),
1006 ),
1007 MouseScrollDelta::PixelDelta(pos) => (
1008 (pos.x * TOUCHPAD_SPEED_MODIFIER),
1009 (pos.y * TOUCHPAD_SPEED_MODIFIER),
1010 ),
1011 }
1012 };
1013
1014 let platform_event = PlatformEvent::Wheel {
1015 name: WheelEventName::Wheel,
1016 scroll: scroll_data.into(),
1017 cursor: app.position,
1018 source: WheelSource::Device,
1019 };
1020 let mut events_measurer_adapter = EventsMeasurerAdapter {
1021 scale_factor: app.effective_scale_factor(),
1022 tree: &mut app.tree,
1023 };
1024 let processed_events = events_measurer_adapter.run(
1025 &mut vec![platform_event],
1026 &mut app.nodes_state,
1027 app.accessibility.focused_node_id(),
1028 );
1029 app.events_sender
1030 .unbounded_send(EventsChunk::Processed(processed_events))
1031 .unwrap();
1032 }
1033 }
1034
1035 WindowEvent::CursorLeft { .. } => {
1036 if app.mouse_state == ElementState::Released {
1037 app.position = CursorPoint::from((-1., -1.));
1038 let platform_event = PlatformEvent::Mouse {
1039 name: MouseEventName::MouseMove,
1040 cursor: app.position,
1041 button: None,
1042 };
1043 let mut events_measurer_adapter = EventsMeasurerAdapter {
1044 scale_factor: app.effective_scale_factor(),
1045 tree: &mut app.tree,
1046 };
1047 let processed_events = events_measurer_adapter.run(
1048 &mut vec![platform_event],
1049 &mut app.nodes_state,
1050 app.accessibility.focused_node_id(),
1051 );
1052 app.events_sender
1053 .unbounded_send(EventsChunk::Processed(processed_events))
1054 .unwrap();
1055 }
1056 }
1057 WindowEvent::CursorMoved { position, .. } => {
1058 app.position = CursorPoint::from((position.x, position.y));
1059
1060 let mut platform_event = vec![PlatformEvent::Mouse {
1061 name: MouseEventName::MouseMove,
1062 cursor: app.position,
1063 button: None,
1064 }];
1065
1066 for dropped_file_path in app.dropped_file_paths.drain(..) {
1067 platform_event.push(PlatformEvent::File {
1068 name: FileEventName::FileDrop,
1069 file_path: Some(dropped_file_path),
1070 cursor: app.position,
1071 });
1072 }
1073
1074 let mut events_measurer_adapter = EventsMeasurerAdapter {
1075 scale_factor: app.effective_scale_factor(),
1076 tree: &mut app.tree,
1077 };
1078 let processed_events = events_measurer_adapter.run(
1079 &mut platform_event,
1080 &mut app.nodes_state,
1081 app.accessibility.focused_node_id(),
1082 );
1083 app.events_sender
1084 .unbounded_send(EventsChunk::Processed(processed_events))
1085 .unwrap();
1086 }
1087
1088 WindowEvent::Touch(Touch {
1089 location,
1090 phase,
1091 id,
1092 force,
1093 ..
1094 }) => {
1095 app.position = CursorPoint::from((location.x, location.y));
1096
1097 let name = match phase {
1098 TouchPhase::Cancelled => TouchEventName::TouchCancel,
1099 TouchPhase::Ended => TouchEventName::TouchEnd,
1100 TouchPhase::Moved => TouchEventName::TouchMove,
1101 TouchPhase::Started => TouchEventName::TouchStart,
1102 };
1103
1104 let platform_event = PlatformEvent::Touch {
1105 name,
1106 location: app.position,
1107 finger_id: id,
1108 phase: map_winit_touch_phase(phase),
1109 force: force.map(map_winit_touch_force),
1110 };
1111 let mut events_measurer_adapter = EventsMeasurerAdapter {
1112 scale_factor: app.effective_scale_factor(),
1113 tree: &mut app.tree,
1114 };
1115 let processed_events = events_measurer_adapter.run(
1116 &mut vec![platform_event],
1117 &mut app.nodes_state,
1118 app.accessibility.focused_node_id(),
1119 );
1120 app.events_sender
1121 .unbounded_send(EventsChunk::Processed(processed_events))
1122 .unwrap();
1123 app.position = CursorPoint::from((location.x, location.y));
1124 }
1125 WindowEvent::Ime(Ime::Commit(text)) => {
1126 let platform_event = PlatformEvent::Keyboard {
1127 name: KeyboardEventName::KeyDown,
1128 key: keyboard_types::Key::Character(text),
1129 code: keyboard_types::Code::Unidentified,
1130 modifiers: winit_mappings::map_winit_modifiers(app.modifiers_state),
1131 };
1132 let mut events_measurer_adapter = EventsMeasurerAdapter {
1133 scale_factor: app.effective_scale_factor(),
1134 tree: &mut app.tree,
1135 };
1136 let processed_events = events_measurer_adapter.run(
1137 &mut vec![platform_event],
1138 &mut app.nodes_state,
1139 app.accessibility.focused_node_id(),
1140 );
1141 app.events_sender
1142 .unbounded_send(EventsChunk::Processed(processed_events))
1143 .unwrap();
1144 }
1145 WindowEvent::Ime(Ime::Preedit(text, pos)) => {
1146 let platform_event = PlatformEvent::ImePreedit {
1147 name: ImeEventName::Preedit,
1148 text,
1149 cursor: pos,
1150 };
1151 let mut events_measurer_adapter = EventsMeasurerAdapter {
1152 scale_factor: app.effective_scale_factor(),
1153 tree: &mut app.tree,
1154 };
1155 let processed_events = events_measurer_adapter.run(
1156 &mut vec![platform_event],
1157 &mut app.nodes_state,
1158 app.accessibility.focused_node_id(),
1159 );
1160 app.events_sender
1161 .unbounded_send(EventsChunk::Processed(processed_events))
1162 .unwrap();
1163 }
1164 WindowEvent::DroppedFile(file_path) => {
1165 app.dropped_file_paths.push(file_path);
1166 }
1167 WindowEvent::HoveredFile(file_path) => {
1168 let platform_event = PlatformEvent::File {
1169 name: FileEventName::FileHover,
1170 file_path: Some(file_path),
1171 cursor: app.position,
1172 };
1173 let mut events_measurer_adapter = EventsMeasurerAdapter {
1174 scale_factor: app.effective_scale_factor(),
1175 tree: &mut app.tree,
1176 };
1177 let processed_events = events_measurer_adapter.run(
1178 &mut vec![platform_event],
1179 &mut app.nodes_state,
1180 app.accessibility.focused_node_id(),
1181 );
1182 app.events_sender
1183 .unbounded_send(EventsChunk::Processed(processed_events))
1184 .unwrap();
1185 }
1186 WindowEvent::HoveredFileCancelled => {
1187 let platform_event = PlatformEvent::File {
1188 name: FileEventName::FileHoverCancelled,
1189 file_path: None,
1190 cursor: app.position,
1191 };
1192 let mut events_measurer_adapter = EventsMeasurerAdapter {
1193 scale_factor: app.effective_scale_factor(),
1194 tree: &mut app.tree,
1195 };
1196 let processed_events = events_measurer_adapter.run(
1197 &mut vec![platform_event],
1198 &mut app.nodes_state,
1199 app.accessibility.focused_node_id(),
1200 );
1201 app.events_sender
1202 .unbounded_send(EventsChunk::Processed(processed_events))
1203 .unwrap();
1204 }
1205 _ => {}
1206 }
1207 }
1208 }
1209}
1210
1211fn subscribe_preferences(proxy: EventLoopProxy<NativeEvent>) {
1212 let subscription = mundy::Preferences::subscribe(mundy::Interest::AccentColor, move |prefs| {
1213 let _ = proxy.send_event(NativeEvent::Preferences(prefs));
1214 });
1215 std::mem::forget(subscription);
1216}