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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
use crate::{
    convert::{winit_key_code_to_code, winit_key_to_key},
    window::{WinState, Window},
    window_modifiers::WindowModifiers,
};
use hashbrown::HashMap;
use std::{error::Error, fmt::Display, sync::Arc};

// #[cfg(feature = "accesskit")]
// use accesskit::{Action, NodeBuilder, NodeId, TreeUpdate};
// #[cfg(feature = "accesskit")]
// use accesskit_winit;
// use std::cell::RefCell;
use vizia_core::context::EventProxy;
use vizia_core::prelude::*;
use vizia_core::{backend::*, events::EventManager};
use winit::{
    application::ApplicationHandler,
    dpi::{LogicalPosition, LogicalSize},
    error::EventLoopError,
    event::ElementState,
    event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy},
    keyboard::{NativeKeyCode, PhysicalKey},
    window::{CursorIcon, CustomCursor, WindowAttributes, WindowId, WindowLevel},
};

#[cfg(target_os = "windows")]
use winit::{
    platform::windows::WindowAttributesExtWindows,
    raw_window_handle::{HasWindowHandle, RawWindowHandle},
};
// #[cfg(all(
//     feature = "clipboard",
//     feature = "wayland",
//     any(
//         target_os = "linux",
//         target_os = "dragonfly",
//         target_os = "freebsd",
//         target_os = "netbsd",
//         target_os = "openbsd"
//     )
// ))]
// use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle};
use vizia_window::WindowPosition;

#[derive(Debug)]
pub enum UserEvent {
    Event(Event),
    #[cfg(feature = "accesskit")]
    AccessKitActionRequest(accesskit_winit::ActionRequestEvent),
}

#[cfg(feature = "accesskit")]
impl From<accesskit_winit::ActionRequestEvent> for UserEvent {
    fn from(action_request_event: accesskit_winit::ActionRequestEvent) -> Self {
        UserEvent::AccessKitActionRequest(action_request_event)
    }
}

impl From<vizia_core::events::Event> for UserEvent {
    fn from(event: vizia_core::events::Event) -> Self {
        UserEvent::Event(event)
    }
}

type IdleCallback = Option<Box<dyn Fn(&mut Context)>>;

#[derive(Debug)]
pub enum ApplicationError {
    EventLoopError(EventLoopError),
    LogError,
}

impl Display for ApplicationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ApplicationError::EventLoopError(ele) => write!(f, "{}", ele),
            ApplicationError::LogError => write!(f, "log error"),
        }
    }
}

impl std::error::Error for ApplicationError {}

///Creating a new application creates a root `Window` and a `Context`. Views declared within the closure passed to `Application::new()` are added to the context and rendered into the root window.
///
/// # Example
/// ```no_run
/// # use vizia_core::prelude::*;
/// # use vizia_winit::application::Application;
/// Application::new(|cx|{
///    // Content goes here
/// })
/// .run();
///```
/// Calling `run()` on the `Application` causes the program to enter the event loop and for the main window to display.
pub struct Application {
    cx: BackendContext,
    event_manager: EventManager,
    pub(crate) event_loop: Option<EventLoop<UserEvent>>,
    on_idle: IdleCallback,
    window_description: WindowDescription,
    control_flow: ControlFlow,
    event_loop_proxy: EventLoopProxy<UserEvent>,
    windows: HashMap<WindowId, WinState>,
    window_ids: HashMap<Entity, WindowId>,
}

pub struct WinitEventProxy(EventLoopProxy<UserEvent>);

impl EventProxy for WinitEventProxy {
    fn send(&self, event: Event) -> Result<(), ()> {
        self.0.send_event(UserEvent::Event(event)).map_err(|_| ())
    }

    fn make_clone(&self) -> Box<dyn EventProxy> {
        Box::new(WinitEventProxy(self.0.clone()))
    }
}

impl Application {
    pub fn new<F>(content: F) -> Self
    where
        F: 'static + FnOnce(&mut Context),
    {
        let context = Context::new();

        let event_loop =
            EventLoop::<UserEvent>::with_user_event().build().expect("Failed to create event loop");

        let mut cx = BackendContext::new(context);
        let event_proxy_obj = event_loop.create_proxy();
        cx.set_event_proxy(Box::new(WinitEventProxy(event_proxy_obj)));

        cx.renegotiate_language();
        cx.0.remove_user_themes();
        (content)(cx.context());

        let proxy = event_loop.create_proxy();

        Self {
            cx,
            event_manager: EventManager::new(),
            event_loop: Some(event_loop),
            on_idle: None,
            window_description: WindowDescription::new(),
            control_flow: ControlFlow::Wait,
            event_loop_proxy: proxy,
            windows: HashMap::new(),
            window_ids: HashMap::new(),
        }
    }

    fn create_window(
        &mut self,
        event_loop: &ActiveEventLoop,
        window_entity: Entity,
        window_description: &WindowDescription,
        #[allow(unused_variables)] owner: Option<Arc<winit::window::Window>>,
    ) -> Result<Arc<winit::window::Window>, Box<dyn Error>> {
        #[allow(unused_mut)]
        let mut window_attributes = apply_window_description(window_description);

        let window = {
            #[cfg(target_os = "windows")]
            {
                if let Some(owner) = owner {
                    let RawWindowHandle::Win32(handle) = owner.window_handle().unwrap().as_raw()
                    else {
                        unreachable!();
                    };
                    window_attributes = window_attributes.with_owner_window(handle.hwnd.get());
                }

                // The current version of winit spawns new windows with unspecified position/size.
                // As a workaround, we'll hide the window during creation and reveal it afterward.
                let visible = window_attributes.visible;
                let window_attributes = window_attributes.with_visible(false);

                let window = event_loop.create_window(window_attributes)?;

                // Another problem is the white background that briefly flashes on window creation.
                // To avoid this one we must wait until the first draw is complete before revealing
                // our window. The visible property won't work in this case as it prevents drawing.
                // Instead we use the "cloak" attribute, which hides the window without that issue.
                set_cloak(&window, true);
                window.set_visible(visible);
                window
            }

            #[cfg(not(target_os = "windows"))]
            {
                event_loop.create_window(window_attributes)?
            }
        };

        let window = Arc::new(window);
        let window_state = WinState::new(event_loop, window.clone(), window_entity)?;

        let window_id = window_state.window.id();
        self.windows.insert(window_id, window_state);
        self.window_ids.insert(window_entity, window_id);
        Ok(window)
    }

    /// Sets the default built-in theming to be ignored.
    pub fn ignore_default_theme(mut self) -> Self {
        self.cx.context().ignore_default_theme = true;
        self
    }

    pub fn should_poll(mut self) -> Self {
        self.control_flow = ControlFlow::Poll;

        self
    }

    /// Takes a closure which will be called at the end of every loop of the application.
    ///
    /// The callback provides a place to run 'idle' processing and happens at the end of each loop but before drawing.
    /// If the callback pushes events into the queue in state then the event loop will re-run. Care must be taken not to
    /// push events into the queue every time the callback runs unless this is intended.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use vizia_core::prelude::*;
    /// # use vizia_winit::application::Application;
    /// #
    /// Application::new(|cx| {
    ///     // Build application here
    /// })
    /// .on_idle(|cx| {
    ///     // Code here runs at the end of every event loop after OS and vizia events have been handled
    /// })
    /// .run();
    /// ```
    pub fn on_idle<F: 'static + Fn(&mut Context)>(mut self, callback: F) -> Self {
        self.on_idle = Some(Box::new(callback));

        self
    }

    /// Returns a `ContextProxy` which can be used to send events from another thread.
    pub fn get_proxy(&self) -> ContextProxy {
        self.cx.0.get_proxy()
    }

    pub fn run(mut self) -> Result<(), ApplicationError> {
        self.event_loop.take().unwrap().run_app(&mut self).map_err(ApplicationError::EventLoopError)
    }
}

impl ApplicationHandler<UserEvent> for Application {
    fn user_event(&mut self, _event_loop: &ActiveEventLoop, user_event: UserEvent) {
        match user_event {
            UserEvent::Event(event) => {
                self.cx.send_event(event);
            }

            #[cfg(feature = "accesskit")]
            UserEvent::AccessKitActionRequest(action_request_event) => {
                let node_id = action_request_event.request.target;

                if action_request_event.request.action != Action::ScrollIntoView {
                    let entity = Entity::new(node_id.0 as u64, 0);

                    // Handle focus action from screen reader
                    if action_request_event.request.action == Action::Focus {
                        cx.0.with_current(entity, |cx| {
                            cx.focus();
                        });
                    }

                    cx.send_event(
                        Event::new(WindowEvent::ActionRequest(action_request_event.request))
                            .direct(entity),
                    );
                }
            }
        }
    }

    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        let main_window: Arc<winit::window::Window> = self
            .create_window(event_loop, Entity::root(), &self.window_description.clone(), None)
            .expect("failed to create initial window");
        let custom_cursors = Arc::new(load_default_cursors(event_loop));
        self.cx.add_main_window(Entity::root(), &self.window_description, 1.0);
        self.cx.add_window(Window {
            window: Some(main_window.clone()),
            on_close: None,
            on_create: None,
            should_close: false,
            custom_cursors: custom_cursors.clone(),
        });

        self.cx.0.windows.insert(
            Entity::root(),
            WindowState {
                window_description: self.window_description.clone(),
                ..Default::default()
            },
        );

        self.cx.0.remove_user_themes();

        for (window_entity, window_state) in self.cx.0.windows.clone().into_iter() {
            if window_entity == Entity::root() {
                continue;
            }
            let owner = window_state.owner.and_then(|entity| {
                self.window_ids
                    .get(&entity)
                    .and_then(|id| self.windows.get(id).map(|ws| ws.window.clone()))
            });

            let window = self
                .create_window(event_loop, window_entity, &window_state.window_description, owner)
                .expect("Failed to create window");
            self.cx.add_main_window(window_entity, &window_state.window_description, 1.0);
            self.cx.mutate_window(window_entity, |cx, win: &mut Window| {
                win.window = Some(window.clone());
                win.custom_cursors = custom_cursors.clone();
                if let Some(callback) = &win.on_create {
                    (callback)(&mut EventContext::new_with_current(cx.context(), window_entity));
                }
            });
            self.cx.needs_refresh(window_entity);
        }
    }

    fn window_event(
        &mut self,
        _event_loop: &ActiveEventLoop,
        window_id: WindowId,
        event: winit::event::WindowEvent,
    ) {
        let window = match self.windows.get_mut(&window_id) {
            Some(window) => window,
            None => return,
        };

        match event {
            winit::event::WindowEvent::Resized(size) => {
                window.resize(size);
                self.cx.set_window_size(window.entity, size.width as f32, size.height as f32);
                self.cx.needs_refresh(window.entity);
                window.window().request_redraw();

                #[cfg(target_os = "windows")]
                {
                    while self.event_manager.flush_events(self.cx.context()) {}

                    self.cx.process_style_updates();

                    if self.cx.process_animations() {
                        window.window().request_redraw();
                    }

                    self.cx.process_visual_updates();

                    #[cfg(feature = "accesskit")]
                    self.cx.process_tree_updates(|tree_updates| {
                        for update in tree_updates.iter_mut() {
                            accesskit.update_if_active(|| update.take().unwrap());
                        }
                    });

                    window.window().request_redraw();
                }
            }

            winit::event::WindowEvent::Moved(position) => {
                self.cx.set_window_position(window.entity, position.x as f32, position.y as f32);
            }

            winit::event::WindowEvent::CloseRequested | winit::event::WindowEvent::Destroyed => {
                let window_entity = window.entity;
                self.cx.emit_window_event(window_entity, WindowEvent::WindowClose);
            }
            winit::event::WindowEvent::DroppedFile(path) => {
                self.cx.emit_window_event(window.entity, WindowEvent::Drop(DropData::File(path)));
            }

            winit::event::WindowEvent::HoveredFile(_) => {}
            winit::event::WindowEvent::HoveredFileCancelled => {}
            winit::event::WindowEvent::Focused(is_focused) => {
                self.cx.0.window_has_focus = is_focused;
                // #[cfg(feature = "accesskit")]
                // accesskit.update_if_active(|| TreeUpdate {
                //     nodes: vec![],
                //     tree: None,
                //     focus: is_focused.then_some(self.cx.focused().accesskit_id()).unwrap_or(NodeId(0)),
                // });
            }
            winit::event::WindowEvent::KeyboardInput { device_id: _, event, is_synthetic: _ } => {
                let code = match event.physical_key {
                    PhysicalKey::Code(code) => winit_key_code_to_code(code),
                    PhysicalKey::Unidentified(native) => match native {
                        NativeKeyCode::Windows(_scancode) => return,
                        _ => return,
                    },
                };

                let key = match event.logical_key {
                    winit::keyboard::Key::Named(named_key) => winit_key_to_key(named_key),
                    _ => None,
                };

                if let winit::keyboard::Key::Character(character) = event.logical_key {
                    if event.state == ElementState::Pressed {
                        self.cx.emit_window_event(
                            window.entity,
                            WindowEvent::CharInput(character.as_str().chars().next().unwrap()),
                        );
                    }
                }

                let event = match event.state {
                    winit::event::ElementState::Pressed => WindowEvent::KeyDown(code, key),
                    winit::event::ElementState::Released => WindowEvent::KeyUp(code, key),
                };

                self.cx.emit_window_event(window.entity, event);
                window.window().request_redraw();
            }
            winit::event::WindowEvent::ModifiersChanged(modifiers) => {
                self.cx.modifiers().set(Modifiers::SHIFT, modifiers.state().shift_key());

                self.cx.modifiers().set(Modifiers::ALT, modifiers.state().alt_key());

                self.cx.modifiers().set(Modifiers::CTRL, modifiers.state().control_key());

                self.cx.modifiers().set(Modifiers::SUPER, modifiers.state().super_key());

                window.window().request_redraw();
            }
            winit::event::WindowEvent::Ime(_) => {}
            winit::event::WindowEvent::CursorMoved { device_id: _, position } => {
                self.cx.emit_window_event(
                    window.entity,
                    WindowEvent::MouseMove(position.x as f32, position.y as f32),
                );
                window.window().request_redraw();
            }
            winit::event::WindowEvent::CursorEntered { device_id: _ } => {
                self.cx.emit_window_event(window.entity, WindowEvent::MouseEnter);
                window.window().request_redraw();
            }
            winit::event::WindowEvent::CursorLeft { device_id: _ } => {
                self.cx.emit_window_event(window.entity, WindowEvent::MouseLeave);
                window.window().request_redraw();
            }
            winit::event::WindowEvent::MouseWheel { device_id: _, delta, phase: _ } => {
                let out_event = match delta {
                    winit::event::MouseScrollDelta::LineDelta(x, y) => {
                        WindowEvent::MouseScroll(x, y)
                    }
                    winit::event::MouseScrollDelta::PixelDelta(pos) => {
                        WindowEvent::MouseScroll(
                            pos.x as f32 / 20.0,
                            pos.y as f32 / 20.0, // this number calibrated for wayland
                        )
                    }
                };

                self.cx.emit_window_event(window.entity, out_event);
                window.window().request_redraw();
            }
            winit::event::WindowEvent::MouseInput { device_id: _, state, button } => {
                let button = match button {
                    winit::event::MouseButton::Left => MouseButton::Left,
                    winit::event::MouseButton::Right => MouseButton::Right,
                    winit::event::MouseButton::Middle => MouseButton::Middle,
                    winit::event::MouseButton::Other(val) => MouseButton::Other(val),
                    winit::event::MouseButton::Back => MouseButton::Back,
                    winit::event::MouseButton::Forward => MouseButton::Forward,
                };

                let event = match state {
                    winit::event::ElementState::Pressed => WindowEvent::MouseDown(button),
                    winit::event::ElementState::Released => WindowEvent::MouseUp(button),
                };

                self.cx.emit_window_event(window.entity, event);
                window.window().request_redraw();
            }

            winit::event::WindowEvent::ScaleFactorChanged {
                scale_factor,
                inner_size_writer: _,
            } => {
                self.cx.set_scale_factor(scale_factor);
                self.cx.needs_refresh(window.entity);
            }
            winit::event::WindowEvent::ThemeChanged(theme) => {
                let theme = match theme {
                    winit::window::Theme::Light => ThemeMode::LightMode,
                    winit::window::Theme::Dark => ThemeMode::DarkMode,
                };
                self.cx.emit_window_event(window.entity, WindowEvent::ThemeChanged(theme));
            }
            winit::event::WindowEvent::Occluded(_) => {}
            winit::event::WindowEvent::RedrawRequested => {
                for window in self.windows.values_mut() {
                    window.make_current();
                    //self.cx.needs_refresh(window.entity);
                    if self.cx.draw(window.entity, &mut window.surface, &mut window.dirty_surface) {
                        window.swap_buffers();
                    }

                    // Un-cloak
                    #[cfg(target_os = "windows")]
                    if window.is_initially_cloaked {
                        window.is_initially_cloaked = false;
                        set_cloak(window.window(), false);
                    }
                }
            }

            _ => {}
        }
    }

    fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
        if self.windows.is_empty() {
            event_loop.exit();
            return;
        }

        event_loop.set_control_flow(self.control_flow);

        while self.event_manager.flush_events(self.cx.context()) {}

        self.cx.process_style_updates();

        if self.cx.process_animations() {
            for window in self.windows.values() {
                window.window().request_redraw();
            }
        }

        self.cx.process_visual_updates();

        #[cfg(feature = "accesskit")]
        cx.process_tree_updates(|tree_updates| {
            for update in tree_updates.iter_mut() {
                accesskit.update_if_active(|| update.take().unwrap());
            }
        });

        if let Some(idle_callback) = &self.on_idle {
            self.cx.set_current(Entity::root());
            (idle_callback)(self.cx.context());
        }

        if self.cx.has_queued_events() {
            self.event_loop_proxy
                .send_event(UserEvent::Event(Event::new(())))
                .expect("Failed to send event");
        }

        if self.cx.0.windows.iter().any(|(_, window_state)| !window_state.redraw_list.is_empty()) {
            for window in self.windows.values() {
                window.window().request_redraw();
            }
        }

        if self.control_flow != ControlFlow::Poll {
            if let Some(timer_time) = self.cx.get_next_timer_time() {
                event_loop.set_control_flow(ControlFlow::WaitUntil(timer_time));
            } else {
                event_loop.set_control_flow(ControlFlow::Wait);
            }
        }

        let window_entities = self
            .cx
            .0
            .windows
            .iter()
            .filter_map(|(entity, state)| state.should_close.then_some(*entity))
            .collect::<Vec<_>>();

        for window_entity in window_entities {
            self.cx.0.remove(window_entity);
        }

        // Sync window state with context
        self.windows.retain(|_, win| self.cx.0.windows.contains_key(&win.entity));
        self.window_ids.retain(|e, _| self.cx.0.windows.contains_key(e));

        if self.windows.len() != self.cx.0.windows.len() {
            for (window_entity, window_state) in self.cx.0.windows.clone().iter() {
                if !self.window_ids.contains_key(window_entity) {
                    self.cx.add_main_window(*window_entity, &window_state.window_description, 1.0);

                    let owner = window_state.owner.and_then(|entity| {
                        self.window_ids
                            .get(&entity)
                            .and_then(|id| self.windows.get(id).map(|ws| ws.window.clone()))
                    });

                    let window = self
                        .create_window(
                            event_loop,
                            *window_entity,
                            &window_state.window_description,
                            owner,
                        )
                        .expect("Failed to create window");

                    self.cx.mutate_window(*window_entity, |cx, win: &mut Window| {
                        win.window = Some(window.clone());
                        if let Some(callback) = &win.on_create {
                            (callback)(&mut EventContext::new_with_current(
                                cx.context(),
                                *window_entity,
                            ));
                        }
                    });
                }
            }
        }

        if self.windows.is_empty() {
            event_loop.exit();
        }
    }

    fn new_events(&mut self, _event_loop: &ActiveEventLoop, _cause: winit::event::StartCause) {
        self.cx.process_timers();
        self.cx.emit_scheduled_events();
    }

    fn exiting(&mut self, _event_loop: &ActiveEventLoop) {}
}

impl WindowModifiers for Application {
    fn title<T: ToString>(mut self, title: impl Res<T>) -> Self {
        self.window_description.title = title.get(&self.cx.0).to_string();

        title.set_or_bind(&mut self.cx.0, Entity::root(), |cx, title| {
            cx.emit(WindowEvent::SetTitle(title.get(cx).to_string()));
        });

        self
    }

    fn inner_size<S: Into<WindowSize>>(mut self, size: impl Res<S>) -> Self {
        self.window_description.inner_size = size.get(&self.cx.0).into();

        size.set_or_bind(&mut self.cx.0, Entity::root(), |cx, size| {
            cx.emit(WindowEvent::SetSize(size.get(cx).into()));
        });

        self
    }

    fn min_inner_size<S: Into<WindowSize>>(mut self, size: impl Res<Option<S>>) -> Self {
        self.window_description.min_inner_size = size.get(&self.cx.0).map(|s| s.into());

        size.set_or_bind(&mut self.cx.0, Entity::root(), |cx, size| {
            cx.emit(WindowEvent::SetMinSize(size.get(cx).map(|s| s.into())));
        });

        self
    }

    fn max_inner_size<S: Into<WindowSize>>(mut self, size: impl Res<Option<S>>) -> Self {
        self.window_description.max_inner_size = size.get(&self.cx.0).map(|s| s.into());

        size.set_or_bind(&mut self.cx.0, Entity::root(), |cx, size| {
            cx.emit(WindowEvent::SetMaxSize(size.get(cx).map(|s| s.into())));
        });
        self
    }

    fn position<P: Into<WindowPosition>>(mut self, position: impl Res<P>) -> Self {
        self.window_description.position = Some(position.get(&self.cx.0).into());

        position.set_or_bind(&mut self.cx.0, Entity::root(), |cx, size| {
            cx.emit(WindowEvent::SetPosition(size.get(cx).into()));
        });

        self
    }

    fn resizable(mut self, flag: impl Res<bool>) -> Self {
        self.window_description.resizable = flag.get(&self.cx.0);

        flag.set_or_bind(&mut self.cx.0, Entity::root(), |cx, flag| {
            cx.emit(WindowEvent::SetResizable(flag.get(cx)));
        });

        self
    }

    fn minimized(mut self, flag: impl Res<bool>) -> Self {
        self.window_description.minimized = flag.get(&self.cx.0);

        flag.set_or_bind(&mut self.cx.0, Entity::root(), |cx, flag| {
            cx.emit(WindowEvent::SetMinimized(flag.get(cx)));
        });
        self
    }

    fn maximized(mut self, flag: impl Res<bool>) -> Self {
        self.window_description.maximized = flag.get(&self.cx.0);

        flag.set_or_bind(&mut self.cx.0, Entity::root(), |cx, flag| {
            cx.emit(WindowEvent::SetMaximized(flag.get(cx)));
        });

        self
    }

    fn visible(mut self, flag: bool) -> Self {
        self.window_description.visible = flag;

        self
    }

    fn transparent(mut self, flag: bool) -> Self {
        self.window_description.transparent = flag;

        self
    }

    fn decorations(mut self, flag: bool) -> Self {
        self.window_description.decorations = flag;

        self
    }

    fn always_on_top(mut self, flag: bool) -> Self {
        self.window_description.always_on_top = flag;
        self
    }

    fn vsync(mut self, flag: bool) -> Self {
        self.window_description.vsync = flag;

        self
    }

    fn icon(mut self, width: u32, height: u32, image: Vec<u8>) -> Self {
        self.window_description.icon = Some(image);
        self.window_description.icon_width = width;
        self.window_description.icon_height = height;

        self
    }

    fn on_close(self, _callback: impl Fn(&mut EventContext)) -> Self {
        self
    }

    fn on_create(self, _callback: impl Fn(&mut EventContext)) -> Self {
        self
    }

    fn enabled_window_buttons(mut self, window_buttons: WindowButtons) -> Self {
        self.window_description.enabled_window_buttons = window_buttons;

        self
    }
}

fn apply_window_description(description: &WindowDescription) -> WindowAttributes {
    let mut window_attributes = winit::window::Window::default_attributes();

    window_attributes = window_attributes.with_title(&description.title).with_inner_size(
        LogicalSize::new(description.inner_size.width, description.inner_size.height),
    );

    if let Some(min_inner_size) = description.min_inner_size {
        window_attributes = window_attributes
            .with_min_inner_size(LogicalSize::new(min_inner_size.width, min_inner_size.height));
    }

    if let Some(max_inner_size) = description.max_inner_size {
        window_attributes = window_attributes
            .with_max_inner_size(LogicalSize::new(max_inner_size.width, max_inner_size.height));
    }

    if let Some(position) = description.position {
        window_attributes =
            window_attributes.with_position(LogicalPosition::new(position.x, position.y));
    }

    window_attributes
        .with_resizable(description.resizable)
        .with_maximized(description.maximized)
        // Accesskit requires that the window start invisible until accesskit is initialized.
        //.with_visible(false)
        .with_window_level(if description.always_on_top {
            WindowLevel::AlwaysOnTop
        } else {
            WindowLevel::Normal
        })
        .with_transparent(description.transparent)
        .with_decorations(description.decorations)
        .with_window_icon(description.icon.as_ref().map(|icon| {
            winit::window::Icon::from_rgba(
                icon.clone(),
                description.icon_width,
                description.icon_height,
            )
            .unwrap()
        }))
        .with_enabled_buttons(
            winit::window::WindowButtons::from_bits(description.enabled_window_buttons.bits())
                .unwrap(),
        )
}

/// Cloaks the window such that it is not visible to the user, but will still be composited.
///
/// <https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute>
///
#[cfg(target_os = "windows")]
fn set_cloak(window: &winit::window::Window, state: bool) -> bool {
    use windows_sys::Win32::{
        Foundation::{BOOL, FALSE, HWND, TRUE},
        Graphics::Dwm::{DwmSetWindowAttribute, DWMWA_CLOAK},
    };

    let RawWindowHandle::Win32(handle) = window.window_handle().unwrap().as_raw() else {
        unreachable!();
    };

    let value = if state { TRUE } else { FALSE };

    let result = unsafe {
        DwmSetWindowAttribute(
            handle.hwnd.get() as HWND,
            DWMWA_CLOAK as u32,
            std::ptr::from_ref(&value).cast(),
            std::mem::size_of::<BOOL>() as u32,
        )
    };

    result == 0 // success
}

#[allow(unused_variables)]
pub fn load_default_cursors(event_loop: &ActiveEventLoop) -> HashMap<CursorIcon, CustomCursor> {
    #[allow(unused_mut)]
    let mut custom_cursors = HashMap::new();

    #[cfg(target_os = "windows")]
    {
        let mut load_cursor = |cursor, bytes, x, y| {
            custom_cursors.insert(
                cursor,
                event_loop.create_custom_cursor(
                    CustomCursor::from_rgba(bytes, 32, 32, x, y)
                        .expect("Failed to create custom cursor"),
                ),
            );
        };

        load_cursor(
            CursorIcon::Alias, //
            include_bytes!("../resources/cursors/windows/aliasb"),
            0,
            0,
        );
        load_cursor(
            CursorIcon::Cell, //
            include_bytes!("../resources/cursors/windows/cell"),
            7,
            7,
        );
        load_cursor(
            CursorIcon::ColResize,
            include_bytes!("../resources/cursors/windows/col_resize"),
            10,
            8,
        );
        load_cursor(
            CursorIcon::Copy, //
            include_bytes!("../resources/cursors/windows/copy"),
            0,
            0,
        );
        load_cursor(
            CursorIcon::Grab, //
            include_bytes!("../resources/cursors/windows/grab"),
            6,
            0,
        );
        load_cursor(
            CursorIcon::Grabbing, //
            include_bytes!("../resources/cursors/windows/grabbing"),
            6,
            0,
        );
        load_cursor(
            CursorIcon::RowResize, //
            include_bytes!("../resources/cursors/windows/row_resize"),
            9,
            10,
        );
        load_cursor(
            CursorIcon::VerticalText, //
            include_bytes!("../resources/cursors/windows/vertical_text"),
            9,
            3,
        );
        load_cursor(
            CursorIcon::ZoomIn, //
            include_bytes!("../resources/cursors/windows/zoom_in"),
            6,
            6,
        );
        load_cursor(
            CursorIcon::ZoomOut, //
            include_bytes!("../resources/cursors/windows/zoom_out"),
            6,
            6,
        );
    }

    custom_cursors
}