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 scale_factor,
749 &self.fallback_fonts,
750 );
751 app.platform.root_size.set_if_modified(size);
752 app.process_layout_on_next_render = false;
753 self.plugins.send(
754 PluginEvent::FinishedMeasuringLayout {
755 window: &app.window,
756 tree: &app.tree,
757 },
758 PluginHandle::new(&self.proxy),
759 );
760 }
761
762 app.driver.present(
763 app.window.inner_size().cast(),
764 &app.window,
765 |surface| {
766 self.plugins.send(
767 PluginEvent::BeforeRender {
768 window: &app.window,
769 canvas: surface.canvas(),
770 font_collection: &self.font_collection,
771 tree: &app.tree,
772 },
773 PluginHandle::new(&self.proxy),
774 );
775
776 let render_pipeline = RenderPipeline {
777 font_collection: &mut self.font_collection,
778 font_manager: &self.font_manager,
779 tree: &app.tree,
780 canvas: surface.canvas(),
781 scale_factor,
782 background: app.background,
783 };
784
785 render_pipeline.render();
786
787 self.plugins.send(
788 PluginEvent::AfterRender {
789 window: &app.window,
790 canvas: surface.canvas(),
791 font_collection: &self.font_collection,
792 tree: &app.tree,
793 animation_clock: &app.animation_clock,
794 },
795 PluginHandle::new(&self.proxy),
796 );
797 self.plugins.send(
798 PluginEvent::BeforePresenting {
799 window: &app.window,
800 font_collection: &self.font_collection,
801 tree: &app.tree,
802 },
803 PluginHandle::new(&self.proxy),
804 );
805 },
806 );
807 self.plugins.send(
808 PluginEvent::AfterPresenting {
809 window: &app.window,
810 font_collection: &self.font_collection,
811 tree: &app.tree,
812 },
813 PluginHandle::new(&self.proxy),
814 );
815
816 self.plugins.send(
817 PluginEvent::BeforeAccessibility {
818 window: &app.window,
819 font_collection: &self.font_collection,
820 tree: &app.tree,
821 },
822 PluginHandle::new(&self.proxy),
823 );
824
825 match app.accessibility_tasks_for_next_render.take() {
826 AccessibilityTask::ProcessUpdate { mode } => {
827 let update = app
828 .accessibility
829 .process_updates(&mut app.tree, &app.events_sender);
830 app.platform
831 .focused_accessibility_id
832 .set_if_modified(update.focus);
833 let node_id = app.accessibility.focused_node_id().unwrap();
834 let layout_node = app.tree.layout.get(&node_id).unwrap();
835 let focused_node =
836 AccessibilityTree::create_node(node_id, layout_node, &app.tree);
837 app.window.set_ime_allowed(is_ime_role(focused_node.role()));
838 app.platform
839 .focused_accessibility_node
840 .set_if_modified(focused_node);
841 if let Some(mode) = mode {
842 app.platform.navigation_mode.set(mode);
843 }
844
845 let area = layout_node.visible_area();
846 app.window.set_ime_cursor_area(
847 LogicalPosition::new(area.min_x(), area.min_y()),
848 LogicalSize::new(area.width(), area.height()),
849 );
850
851 app.accessibility_adapter.update_if_active(|| update);
852 }
853 AccessibilityTask::Init => {
854 let update = app.accessibility.init(&mut app.tree);
855 app.platform
856 .focused_accessibility_id
857 .set_if_modified(update.focus);
858 let node_id = app.accessibility.focused_node_id().unwrap();
859 let layout_node = app.tree.layout.get(&node_id).unwrap();
860 let focused_node =
861 AccessibilityTree::create_node(node_id, layout_node, &app.tree);
862 app.window.set_ime_allowed(is_ime_role(focused_node.role()));
863 app.platform
864 .focused_accessibility_node
865 .set_if_modified(focused_node);
866
867 let area = layout_node.visible_area();
868 app.window.set_ime_cursor_area(
869 LogicalPosition::new(area.min_x(), area.min_y()),
870 LogicalSize::new(area.width(), area.height()),
871 );
872
873 app.accessibility_adapter.update_if_active(|| update);
874 }
875 AccessibilityTask::None => {}
876 }
877
878 self.plugins.send(
879 PluginEvent::AfterAccessibility {
880 window: &app.window,
881 font_collection: &self.font_collection,
882 tree: &app.tree,
883 },
884 PluginHandle::new(&self.proxy),
885 );
886
887 if app.ticker_sender.receiver_count() > 0 {
888 app.ticker_sender.broadcast_blocking(()).unwrap();
889 }
890
891 self.plugins.send(
892 PluginEvent::AfterRedraw {
893 window: &app.window,
894 font_collection: &self.font_collection,
895 tree: &app.tree,
896 },
897 PluginHandle::new(&self.proxy),
898 );
899 });
900 }
901 WindowEvent::Resized(size) => {
902 app.driver.resize(size);
903
904 app.window.request_redraw();
905
906 app.process_layout_on_next_render = true;
907 app.tree.layout.clear_dirty();
908 app.tree.layout.invalidate(NodeId::ROOT);
909 }
910
911 WindowEvent::MouseInput { state, button, .. } => {
912 app.mouse_state = state;
913 app.platform
914 .navigation_mode
915 .set(NavigationMode::NotKeyboard);
916
917 let name = if state == ElementState::Pressed {
918 MouseEventName::MouseDown
919 } else {
920 MouseEventName::MouseUp
921 };
922 let platform_event = PlatformEvent::Mouse {
923 name,
924 cursor: (app.position.x, app.position.y).into(),
925 button: Some(map_winit_mouse_button(button)),
926 };
927 let mut events_measurer_adapter = EventsMeasurerAdapter {
928 scale_factor: app.effective_scale_factor(),
929 tree: &mut app.tree,
930 };
931 let processed_events = events_measurer_adapter.run(
932 &mut vec![platform_event],
933 &mut app.nodes_state,
934 app.accessibility.focused_node_id(),
935 );
936 app.events_sender
937 .unbounded_send(EventsChunk::Processed(processed_events))
938 .unwrap();
939 }
940
941 WindowEvent::KeyboardInput {
942 event,
943 is_synthetic,
944 ..
945 } => {
946 if is_synthetic && event.state == ElementState::Pressed {
948 return;
949 }
950
951 let name = match event.state {
952 ElementState::Pressed => KeyboardEventName::KeyDown,
953 ElementState::Released => KeyboardEventName::KeyUp,
954 };
955 let key = winit_mappings::map_winit_key(&event.logical_key);
956 let code = winit_mappings::map_winit_physical_key(&event.physical_key);
957 let modifiers = winit_mappings::map_winit_modifiers(app.modifiers_state);
958
959 #[cfg(feature = "zoom-shortcuts")]
960 if app.try_handle_zoom_shortcut(&key, modifiers, event.state.is_pressed()) {
961 return;
962 }
963
964 self.plugins.send(
965 PluginEvent::KeyboardInput {
966 window: &app.window,
967 key: key.clone(),
968 code,
969 modifiers,
970 is_pressed: event.state.is_pressed(),
971 },
972 PluginHandle::new(&self.proxy),
973 );
974
975 let platform_event = PlatformEvent::Keyboard {
976 name,
977 key,
978 code,
979 modifiers,
980 };
981 let mut events_measurer_adapter = EventsMeasurerAdapter {
982 scale_factor: app.effective_scale_factor(),
983 tree: &mut app.tree,
984 };
985 let processed_events = events_measurer_adapter.run(
986 &mut vec![platform_event],
987 &mut app.nodes_state,
988 app.accessibility.focused_node_id(),
989 );
990 app.events_sender
991 .unbounded_send(EventsChunk::Processed(processed_events))
992 .unwrap();
993 }
994
995 WindowEvent::MouseWheel { delta, phase, .. } => {
996 const WHEEL_SPEED_MODIFIER: f64 = 53.0;
997 const TOUCHPAD_SPEED_MODIFIER: f64 = 2.0;
998
999 if TouchPhase::Moved == phase {
1000 let scroll_data = {
1001 match delta {
1002 MouseScrollDelta::LineDelta(x, y) => (
1003 (x as f64 * WHEEL_SPEED_MODIFIER),
1004 (y as f64 * WHEEL_SPEED_MODIFIER),
1005 ),
1006 MouseScrollDelta::PixelDelta(pos) => (
1007 (pos.x * TOUCHPAD_SPEED_MODIFIER),
1008 (pos.y * TOUCHPAD_SPEED_MODIFIER),
1009 ),
1010 }
1011 };
1012
1013 let platform_event = PlatformEvent::Wheel {
1014 name: WheelEventName::Wheel,
1015 scroll: scroll_data.into(),
1016 cursor: app.position,
1017 source: WheelSource::Device,
1018 };
1019 let mut events_measurer_adapter = EventsMeasurerAdapter {
1020 scale_factor: app.effective_scale_factor(),
1021 tree: &mut app.tree,
1022 };
1023 let processed_events = events_measurer_adapter.run(
1024 &mut vec![platform_event],
1025 &mut app.nodes_state,
1026 app.accessibility.focused_node_id(),
1027 );
1028 app.events_sender
1029 .unbounded_send(EventsChunk::Processed(processed_events))
1030 .unwrap();
1031 }
1032 }
1033
1034 WindowEvent::CursorLeft { .. } => {
1035 if app.mouse_state == ElementState::Released {
1036 app.position = CursorPoint::from((-1., -1.));
1037 let platform_event = PlatformEvent::Mouse {
1038 name: MouseEventName::MouseMove,
1039 cursor: app.position,
1040 button: None,
1041 };
1042 let mut events_measurer_adapter = EventsMeasurerAdapter {
1043 scale_factor: app.effective_scale_factor(),
1044 tree: &mut app.tree,
1045 };
1046 let processed_events = events_measurer_adapter.run(
1047 &mut vec![platform_event],
1048 &mut app.nodes_state,
1049 app.accessibility.focused_node_id(),
1050 );
1051 app.events_sender
1052 .unbounded_send(EventsChunk::Processed(processed_events))
1053 .unwrap();
1054 }
1055 }
1056 WindowEvent::CursorMoved { position, .. } => {
1057 app.position = CursorPoint::from((position.x, position.y));
1058
1059 let mut platform_event = vec![PlatformEvent::Mouse {
1060 name: MouseEventName::MouseMove,
1061 cursor: app.position,
1062 button: None,
1063 }];
1064
1065 for dropped_file_path in app.dropped_file_paths.drain(..) {
1066 platform_event.push(PlatformEvent::File {
1067 name: FileEventName::FileDrop,
1068 file_path: Some(dropped_file_path),
1069 cursor: app.position,
1070 });
1071 }
1072
1073 let mut events_measurer_adapter = EventsMeasurerAdapter {
1074 scale_factor: app.effective_scale_factor(),
1075 tree: &mut app.tree,
1076 };
1077 let processed_events = events_measurer_adapter.run(
1078 &mut platform_event,
1079 &mut app.nodes_state,
1080 app.accessibility.focused_node_id(),
1081 );
1082 app.events_sender
1083 .unbounded_send(EventsChunk::Processed(processed_events))
1084 .unwrap();
1085 }
1086
1087 WindowEvent::Touch(Touch {
1088 location,
1089 phase,
1090 id,
1091 force,
1092 ..
1093 }) => {
1094 app.position = CursorPoint::from((location.x, location.y));
1095
1096 let name = match phase {
1097 TouchPhase::Cancelled => TouchEventName::TouchCancel,
1098 TouchPhase::Ended => TouchEventName::TouchEnd,
1099 TouchPhase::Moved => TouchEventName::TouchMove,
1100 TouchPhase::Started => TouchEventName::TouchStart,
1101 };
1102
1103 let platform_event = PlatformEvent::Touch {
1104 name,
1105 location: app.position,
1106 finger_id: id,
1107 phase: map_winit_touch_phase(phase),
1108 force: force.map(map_winit_touch_force),
1109 };
1110 let mut events_measurer_adapter = EventsMeasurerAdapter {
1111 scale_factor: app.effective_scale_factor(),
1112 tree: &mut app.tree,
1113 };
1114 let processed_events = events_measurer_adapter.run(
1115 &mut vec![platform_event],
1116 &mut app.nodes_state,
1117 app.accessibility.focused_node_id(),
1118 );
1119 app.events_sender
1120 .unbounded_send(EventsChunk::Processed(processed_events))
1121 .unwrap();
1122 app.position = CursorPoint::from((location.x, location.y));
1123 }
1124 WindowEvent::Ime(Ime::Commit(text)) => {
1125 let platform_event = PlatformEvent::Keyboard {
1126 name: KeyboardEventName::KeyDown,
1127 key: keyboard_types::Key::Character(text),
1128 code: keyboard_types::Code::Unidentified,
1129 modifiers: winit_mappings::map_winit_modifiers(app.modifiers_state),
1130 };
1131 let mut events_measurer_adapter = EventsMeasurerAdapter {
1132 scale_factor: app.effective_scale_factor(),
1133 tree: &mut app.tree,
1134 };
1135 let processed_events = events_measurer_adapter.run(
1136 &mut vec![platform_event],
1137 &mut app.nodes_state,
1138 app.accessibility.focused_node_id(),
1139 );
1140 app.events_sender
1141 .unbounded_send(EventsChunk::Processed(processed_events))
1142 .unwrap();
1143 }
1144 WindowEvent::Ime(Ime::Preedit(text, pos)) => {
1145 let platform_event = PlatformEvent::ImePreedit {
1146 name: ImeEventName::Preedit,
1147 text,
1148 cursor: pos,
1149 };
1150 let mut events_measurer_adapter = EventsMeasurerAdapter {
1151 scale_factor: app.effective_scale_factor(),
1152 tree: &mut app.tree,
1153 };
1154 let processed_events = events_measurer_adapter.run(
1155 &mut vec![platform_event],
1156 &mut app.nodes_state,
1157 app.accessibility.focused_node_id(),
1158 );
1159 app.events_sender
1160 .unbounded_send(EventsChunk::Processed(processed_events))
1161 .unwrap();
1162 }
1163 WindowEvent::DroppedFile(file_path) => {
1164 app.dropped_file_paths.push(file_path);
1165 }
1166 WindowEvent::HoveredFile(file_path) => {
1167 let platform_event = PlatformEvent::File {
1168 name: FileEventName::FileHover,
1169 file_path: Some(file_path),
1170 cursor: app.position,
1171 };
1172 let mut events_measurer_adapter = EventsMeasurerAdapter {
1173 scale_factor: app.effective_scale_factor(),
1174 tree: &mut app.tree,
1175 };
1176 let processed_events = events_measurer_adapter.run(
1177 &mut vec![platform_event],
1178 &mut app.nodes_state,
1179 app.accessibility.focused_node_id(),
1180 );
1181 app.events_sender
1182 .unbounded_send(EventsChunk::Processed(processed_events))
1183 .unwrap();
1184 }
1185 WindowEvent::HoveredFileCancelled => {
1186 let platform_event = PlatformEvent::File {
1187 name: FileEventName::FileHoverCancelled,
1188 file_path: None,
1189 cursor: app.position,
1190 };
1191 let mut events_measurer_adapter = EventsMeasurerAdapter {
1192 scale_factor: app.effective_scale_factor(),
1193 tree: &mut app.tree,
1194 };
1195 let processed_events = events_measurer_adapter.run(
1196 &mut vec![platform_event],
1197 &mut app.nodes_state,
1198 app.accessibility.focused_node_id(),
1199 );
1200 app.events_sender
1201 .unbounded_send(EventsChunk::Processed(processed_events))
1202 .unwrap();
1203 }
1204 _ => {}
1205 }
1206 }
1207 }
1208}
1209
1210fn subscribe_preferences(proxy: EventLoopProxy<NativeEvent>) {
1211 let subscription = mundy::Preferences::subscribe(mundy::Interest::AccentColor, move |prefs| {
1212 let _ = proxy.send_event(NativeEvent::Preferences(prefs));
1213 });
1214 std::mem::forget(subscription);
1215}