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
use crate::conv::{from_imgui_cursor, to_imgui_button, to_imgui_key};
use cgmath::Matrix3;
use easy_imgui::{self as imgui, cgmath, mint, Vector2};
use easy_imgui_renderer::Renderer;
use easy_imgui_sys::*;
use glutin::{
    context::PossiblyCurrentContext,
    prelude::*,
    surface::{Surface, WindowSurface},
};
use std::num::NonZeroU32;
use std::time::{Duration, Instant};
use winit::{
    dpi::{LogicalSize, PhysicalSize},
    event::{Event, Ime::Commit},
    keyboard::PhysicalKey,
    window::{CursorIcon, Window},
};

// Only used with the main-window feature
#[allow(unused_imports)]
use winit::dpi::{LogicalPosition, PhysicalPosition, Pixel};

/// This struct maintains basic window info to be kept across events.
#[derive(Debug, Clone)]
pub struct MainWindowStatus {
    last_frame: Instant,
    current_cursor: Option<CursorIcon>,
}

impl Default for MainWindowStatus {
    fn default() -> MainWindowStatus {
        let now = Instant::now();
        MainWindowStatus {
            last_frame: now,
            current_cursor: Some(CursorIcon::Default),
        }
    }
}

/// This struct handles the main loop going to idle when there is no user input for a while.
pub struct MainWindowIdler {
    idle_time: Duration,
    idle_frame_count: u32,
    last_input_time: Instant,
    last_input_frame: u32,
}

impl Default for MainWindowIdler {
    fn default() -> MainWindowIdler {
        let now = Instant::now();
        MainWindowIdler {
            idle_time: Duration::from_secs(1),
            idle_frame_count: 60,
            last_input_time: now,
            last_input_frame: 0,
        }
    }
}

impl MainWindowIdler {
    /// Sets the maximum time that the window will be rendered without user input.
    pub fn set_idle_time(&mut self, time: Duration) {
        self.idle_time = time;
    }
    /// Sets the maximum number of frames time that the window will be rendered without user input.
    pub fn set_idle_frame_count(&mut self, frame_count: u32) {
        self.idle_frame_count = frame_count;
    }
    /// Call this when the window is renderer.
    pub fn incr_frame(&mut self) {
        // An u32 incrementing 60 values/second would overflow after about 2 years, better safe
        // than sorry.
        self.last_input_frame = self.last_input_frame.saturating_add(1);
    }
    /// Check whether the window should go to idle or keep on rendering.
    pub fn has_to_render(&self) -> bool {
        self.last_input_frame < self.idle_frame_count
            || Instant::now().duration_since(self.last_input_time) < self.idle_time
    }
    /// Notify this struct that user input happened.
    pub fn ping_user_input(&mut self) {
        self.last_input_time = Instant::now();
        self.last_input_frame = 0;
    }
}

/// This traits grants access to a Window.
///
/// Usually you will have a [`MainWindow`], but if you create the `Window` with an external
/// crate, maybe you don't own it.
pub trait MainWindowRef {
    /// Gets the [`Window`].
    fn window(&self) -> &Window;
    /// This runs just before rendering.
    ///
    /// The intended use is to make the GL context current, if needed.
    /// Clearing the background is usually done in [`easy_imgui::UiBuilder::pre_render`], or by the renderer if it has a background color.
    fn pre_render(&mut self) {}
    /// This runs just after rendering.
    ///
    /// The intended use is to present the screen buffer.
    fn post_render(&mut self) {}
    /// Notifies of a user interaction, for idling purposes.
    fn ping_user_input(&mut self) {}
    /// There are no more messages, going to idle.
    fn about_to_wait(&mut self, _pinged: bool) {}
    fn transform_position(&self, pos: Vector2) -> Vector2 {
        pos / self.scale_factor()
    }
    /// Gets the scale factor of the window, (HiDPI).
    fn scale_factor(&self) -> f32 {
        self.window().scale_factor() as f32
    }
    /// Changes the scale factor.
    ///
    /// Normally there is nothing to be done here, unless you are doing something fancy with HiDPI.
    ///
    /// It returns the real applied scale factor, as it would returned by
    /// `self.scale_factor()` after this change has been applied.
    fn set_scale_factor(&self, scale: f32) -> f32 {
        scale
    }
    /// The window has been resized.
    ///
    /// Takes the new physical size. It should return the new logical size.
    fn resize(&mut self, size: PhysicalSize<u32>) -> LogicalSize<f32> {
        let scale = self.scale_factor();
        size.to_logical(scale as f64)
    }
    /// Changes the mouse cursor.
    fn set_cursor(&mut self, cursor: Option<CursorIcon>) {
        let w = self.window();
        match cursor {
            None => w.set_cursor_visible(false),
            Some(c) => {
                w.set_cursor_icon(c);
                w.set_cursor_visible(true);
            }
        }
    }
}

fn transform_position_with_optional_matrix(
    w: &impl MainWindowRef,
    pos: Vector2,
    mx: &Option<Matrix3<f32>>,
) -> Vector2 {
    use cgmath::{EuclideanSpace as _, Transform};
    match mx {
        Some(mx) => mx.transform_point(cgmath::Point2::from_vec(pos)).to_vec(),
        None => pos / w.scale_factor(),
    }
}

bitflags::bitflags! {
    /// These flags can be used to customize the [`do_event`] function.
    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
    pub struct EventFlags: u32 {
        /// Do not render the UI
        const DoNotRender = 1;
        /// Do not run the focus gained workaround.
        ///
        /// https://github.com/rust-windowing/winit/issues/2841
        const DoNotFixFocusGainedBug = 2;
        /// Do not change the size or scale of the UI
        const DoNotResize = 4;
        /// Do not send mouse positions
        const DoNotMouse = 8;
    }
}

/// Helper struct to call [`do_event`] without owning the Window.
pub struct MainWindowPieces<'a> {
    window: &'a Window,
    surface: &'a Surface<WindowSurface>,
    gl_context: &'a PossiblyCurrentContext,
    matrix: Option<Matrix3<f32>>,
}

impl<'a> MainWindowPieces<'a> {
    pub fn new(
        window: &'a Window,
        surface: &'a Surface<WindowSurface>,
        gl_context: &'a PossiblyCurrentContext,
    ) -> Self {
        MainWindowPieces {
            window,
            surface,
            gl_context,
            matrix: None,
        }
    }
    /// Sets the matrix that transforms the input mouse coordinates into UI space.
    ///
    /// If none, it uses the default transformation.
    pub fn set_matrix(&mut self, matrix: Option<Matrix3<f32>>) {
        self.matrix = matrix;
    }
}

/// Default implementation if you have all the pieces.
impl<'a> MainWindowRef for MainWindowPieces<'a> {
    fn window(&self) -> &Window {
        self.window
    }
    fn pre_render(&mut self) {
        self.gl_context.make_current(self.surface).unwrap();
    }
    fn post_render(&mut self) {
        self.window.pre_present_notify();
        self.surface.swap_buffers(self.gl_context).unwrap();
    }
    fn resize(&mut self, size: PhysicalSize<u32>) -> LogicalSize<f32> {
        let width = NonZeroU32::new(size.width.max(1)).unwrap();
        let height = NonZeroU32::new(size.height.max(1)).unwrap();
        self.surface.resize(self.gl_context, width, height);
        let scale = self.scale_factor();
        size.to_logical(scale as f64)
    }
    fn transform_position(&self, pos: Vector2) -> Vector2 {
        transform_position_with_optional_matrix(self, pos, &self.matrix)
    }
}

/// Simple implementation if you only have a window, no pre/post render, no resize.
impl<'a> MainWindowRef for &'a Window {
    fn window(&self) -> &Window {
        self
    }
}

// NewType to disable the HiDPI scaling.
pub struct NoScale<'a>(pub &'a Window);

impl<'a> MainWindowRef for NoScale<'a> {
    fn window(&self) -> &Window {
        self.0
    }
    fn scale_factor(&self) -> f32 {
        1.0
    }
    fn set_scale_factor(&self, _scale: f32) -> f32 {
        1.0
    }
}

/// The result of processing an event in the ImGui loop.
#[derive(Debug, Default, Clone)]
pub struct EventResult {
    /// The user requested to close the window. You can break the loop or ignore it, at will.
    pub window_closed: bool,
    /// ImGui requests handling the mouse, your application should ignore mouse events.
    pub want_capture_mouse: bool,
    /// ImGui requests handling the keyboard, your application should ignore keyboard events.
    pub want_capture_keyboard: bool,
    /// ImGui requests handling text input, your application should ignore text events.
    pub want_text_input: bool,
}

/// Just like [`MainWindowWithRenderer::do_event`] but using all the pieces separately.
pub fn do_event<EventUserType>(
    main_window: &mut impl MainWindowRef,
    renderer: &mut Renderer,
    status: &mut MainWindowStatus,
    app: &mut impl imgui::UiBuilder,
    event: &Event<EventUserType>,
    flags: EventFlags,
) -> EventResult {
    let mut window_closed = false;
    match event {
        Event::NewEvents(x) => {
            if x == &winit::event::StartCause::Init
                && !flags.contains(EventFlags::DoNotFixFocusGainedBug)
            {
                // This fixes "keyboard non-responsive on startup because it doesn't detect FocusGained...":
                // https://github.com/rust-windowing/winit/issues/2841
                use winit::raw_window_handle::{
                    HasWindowHandle,
                    RawWindowHandle::{Xcb, Xlib},
                };
                if let Ok(h) = main_window.window().window_handle() {
                    if matches!(h.as_raw(), Xcb(_) | Xlib(_)) {
                        main_window.window().set_visible(false);
                        main_window.window().set_visible(true);
                    }
                }
            }
            let now = Instant::now();
            let mut imgui = unsafe { renderer.imgui().set_current() };
            let io = imgui.io_mut();
            io.DeltaTime = now.duration_since(status.last_frame).as_secs_f32();
            status.last_frame = now;
        }
        Event::AboutToWait => {
            let imgui = unsafe { renderer.imgui().set_current() };
            let io = imgui.io();
            if io.WantSetMousePos {
                let pos = io.MousePos;
                let pos = winit::dpi::LogicalPosition { x: pos.x, y: pos.y };
                let _ = main_window.window().set_cursor_position(pos);
            }
            // If the mouse is down, redraw all the time, maybe the user is dragging.
            let mouse = unsafe { ImGui_IsAnyMouseDown() };
            main_window.about_to_wait(mouse);
        }
        Event::WindowEvent { window_id, event } if main_window.window().id() == *window_id => {
            use winit::event::WindowEvent::*;
            match event {
                CloseRequested => {
                    window_closed = true;
                }
                RedrawRequested => unsafe {
                    let imgui = renderer.imgui().set_current();
                    let io = imgui.io();
                    let config_flags = imgui::ConfigFlags::from_bits_truncate(io.ConfigFlags);
                    if !config_flags.contains(imgui::ConfigFlags::NoMouseCursorChange) {
                        let cursor = if io.MouseDrawCursor {
                            None
                        } else {
                            let cursor = imgui::MouseCursor::from_bits(ImGui_GetMouseCursor())
                                .unwrap_or(imgui::MouseCursor::Arrow);
                            from_imgui_cursor(cursor)
                        };
                        if cursor != status.current_cursor {
                            main_window.set_cursor(cursor);
                            status.current_cursor = cursor;
                        }
                    }
                    if !flags.contains(EventFlags::DoNotRender) {
                        main_window.pre_render();
                        renderer.do_frame(app);
                        main_window.post_render();
                    }
                },
                Resized(size) => {
                    if !flags.contains(EventFlags::DoNotResize) {
                        main_window.ping_user_input();
                        // GL surface in physical pixels, imgui in logical
                        let size = main_window.resize(*size);
                        let mut imgui = unsafe { renderer.imgui().set_current() };
                        let io = imgui.io_mut();
                        let size = Vector2::from(mint::Vector2::from(size));
                        io.DisplaySize = imgui::v2_to_im(size);
                    }
                }
                ScaleFactorChanged { scale_factor, .. } => {
                    if !flags.contains(EventFlags::DoNotResize) {
                        main_window.ping_user_input();
                        let scale_factor = main_window.set_scale_factor(*scale_factor as f32);
                        let mut imgui = unsafe { renderer.imgui().set_current() };
                        let io = imgui.io_mut();
                        // Keep the mouse in the same relative position: maybe it is wrong, but it is
                        // the best guess we can do.
                        let old_scale_factor = io.DisplayFramebufferScale.x;
                        if io.MousePos.x.is_finite() && io.MousePos.y.is_finite() {
                            io.MousePos.x *= scale_factor / old_scale_factor;
                            io.MousePos.y *= scale_factor / old_scale_factor;
                        }
                        let size = renderer.size();
                        renderer.set_size(size, scale_factor);
                    }
                }
                ModifiersChanged(mods) => {
                    main_window.ping_user_input();
                    unsafe {
                        let mut imgui = renderer.imgui().set_current();
                        let io = imgui.io_mut();
                        ImGuiIO_AddKeyEvent(
                            io,
                            imgui::Key::ModCtrl.bits(),
                            mods.state().control_key(),
                        );
                        ImGuiIO_AddKeyEvent(
                            io,
                            imgui::Key::ModShift.bits(),
                            mods.state().shift_key(),
                        );
                        #[rustfmt::skip]
                        ImGuiIO_AddKeyEvent(
                            io,
                            imgui::Key::ModAlt.bits(),
                            mods.state().alt_key(),
                        );
                        ImGuiIO_AddKeyEvent(
                            io,
                            imgui::Key::ModSuper.bits(),
                            mods.state().super_key(),
                        );
                    }
                }
                KeyboardInput {
                    event:
                        winit::event::KeyEvent {
                            physical_key,
                            text,
                            state,
                            ..
                        },
                    is_synthetic: false,
                    ..
                } => {
                    main_window.ping_user_input();
                    let pressed = *state == winit::event::ElementState::Pressed;
                    if let Some(key) = to_imgui_key(*physical_key) {
                        unsafe {
                            let mut imgui = renderer.imgui().set_current();
                            let io = imgui.io_mut();
                            ImGuiIO_AddKeyEvent(io, key.bits(), pressed);

                            use winit::keyboard::KeyCode::*;
                            if let PhysicalKey::Code(keycode) = physical_key {
                                let kmod = match keycode {
                                    ControlLeft | ControlRight => Some(imgui::Key::ModCtrl),
                                    ShiftLeft | ShiftRight => Some(imgui::Key::ModShift),
                                    AltLeft | AltRight => Some(imgui::Key::ModAlt),
                                    SuperLeft | SuperRight => Some(imgui::Key::ModSuper),
                                    _ => None,
                                };
                                if let Some(kmod) = kmod {
                                    ImGuiIO_AddKeyEvent(io, kmod.bits(), pressed);
                                }
                            }
                        }
                    }
                    if pressed {
                        if let Some(text) = text {
                            unsafe {
                                let mut imgui = renderer.imgui().set_current();
                                let io = imgui.io_mut();
                                for c in text.chars() {
                                    ImGuiIO_AddInputCharacter(io, c as u32);
                                }
                            }
                        }
                    }
                }
                Ime(Commit(text)) => {
                    main_window.ping_user_input();
                    unsafe {
                        let mut imgui = renderer.imgui().set_current();
                        let io = imgui.io_mut();
                        for c in text.chars() {
                            ImGuiIO_AddInputCharacter(io, c as u32);
                        }
                    }
                }
                CursorMoved { position, .. } => {
                    main_window.ping_user_input();
                    unsafe {
                        let mut imgui = renderer.imgui().set_current();
                        let io = imgui.io_mut();
                        let position = main_window
                            .transform_position(Vector2::new(position.x as f32, position.y as f32));
                        ImGuiIO_AddMousePosEvent(io, position.x, position.y);
                    }
                }
                MouseWheel {
                    delta,
                    phase: winit::event::TouchPhase::Moved,
                    ..
                } => {
                    main_window.ping_user_input();
                    let mut imgui = unsafe { renderer.imgui().set_current() };
                    let io = imgui.io_mut();
                    let (h, v) = match delta {
                        winit::event::MouseScrollDelta::LineDelta(h, v) => (*h, *v),
                        winit::event::MouseScrollDelta::PixelDelta(d) => {
                            let scale = io.DisplayFramebufferScale.x;
                            let f_scale = unsafe { ImGui_GetFontSize() };
                            let scale = scale * f_scale;
                            (d.x as f32 / scale, d.y as f32 / scale)
                        }
                    };
                    unsafe {
                        ImGuiIO_AddMouseWheelEvent(io, h, v);
                    }
                }
                MouseInput { state, button, .. } => {
                    main_window.ping_user_input();
                    unsafe {
                        let mut imgui = renderer.imgui().set_current();
                        let io = imgui.io_mut();
                        if let Some(btn) = to_imgui_button(*button) {
                            let pressed = *state == winit::event::ElementState::Pressed;
                            ImGuiIO_AddMouseButtonEvent(io, btn.bits(), pressed);
                        }
                    }
                }
                CursorLeft { .. } => {
                    main_window.ping_user_input();
                    unsafe {
                        let mut imgui = renderer.imgui().set_current();
                        let io = imgui.io_mut();
                        ImGuiIO_AddMousePosEvent(io, f32::MAX, f32::MAX);
                    }
                }
                Focused(focused) => {
                    main_window.ping_user_input();
                    unsafe {
                        let mut imgui = renderer.imgui().set_current();
                        let io = imgui.io_mut();
                        ImGuiIO_AddFocusEvent(io, *focused);
                    }
                }
                _ => {}
            }
        }
        _ => {}
    }
    let res = unsafe {
        let imgui = renderer.imgui().set_current();
        EventResult {
            window_closed,
            want_capture_mouse: imgui.want_capture_mouse(),
            want_capture_keyboard: imgui.want_capture_keyboard(),
            want_text_input: imgui.want_text_input(),
        }
    };
    res
}

#[cfg(feature = "clipboard")]
pub mod clipboard {
    use easy_imgui as imgui;
    use std::ffi::{c_char, c_void, CStr, CString};

    /// Sets up the ImGui clipboard using the `arboard` crate.
    pub fn setup(imgui: &mut imgui::CurrentContext<'_>) {
        if let Ok(ctx) = arboard::Clipboard::new() {
            let clip = MyClipboard {
                ctx,
                text: CString::default(),
            };
            let io = imgui.io_mut();
            io.ClipboardUserData = Box::into_raw(Box::new(clip)) as *mut c_void;
            io.SetClipboardTextFn = Some(set_clipboard_text);
            io.GetClipboardTextFn = Some(get_clipboard_text);
        }
    }
    unsafe extern "C" fn set_clipboard_text(user: *mut c_void, text: *const c_char) {
        let clip = &mut *(user as *mut MyClipboard);
        if text.is_null() {
            let _ = clip.ctx.clear();
        } else {
            let cstr = CStr::from_ptr(text);
            let str = String::from_utf8_lossy(cstr.to_bytes()).to_string();
            let _ = clip.ctx.set_text(str);
        }
    }

    // The returned pointer should be valid for a while...
    unsafe extern "C" fn get_clipboard_text(user: *mut c_void) -> *const c_char {
        let clip = &mut *(user as *mut MyClipboard);
        let Ok(text) = clip.ctx.get_text() else {
            return std::ptr::null();
        };
        let Ok(text) = CString::new(text) else {
            return std::ptr::null();
        };
        clip.text = text;
        clip.text.as_ptr()
    }

    struct MyClipboard {
        ctx: arboard::Clipboard,
        text: CString,
    }
}

#[cfg(feature = "main-window")]
mod main_window {
    use super::*;
    use anyhow::{anyhow, Result};
    use easy_imgui_renderer::glow;
    use glutin::{
        config::{Config, ConfigTemplateBuilder},
        context::{ContextApi, ContextAttributesBuilder},
        display::GetGlDisplay,
        surface::SurfaceAttributesBuilder,
    };
    use glutin_winit::DisplayBuilder;
    use raw_window_handle::HasRawWindowHandle;
    use winit::{event_loop::EventLoopWindowTarget, window::WindowBuilder};

    /// This type represents a `winit` window and an OpenGL context.
    pub struct MainWindow {
        gl_context: PossiblyCurrentContext,
        // The surface must be dropped before the window.
        surface: Surface<WindowSurface>,
        window: Window,
        matrix: Option<Matrix3<f32>>,
        idler: MainWindowIdler,
    }

    /// This is a [`MainWindow`] plus a [`Renderer`]. It is the ultimate `easy-imgui` object.
    /// Instead of a literal `MainWindow` you can use any type that implements [`MainWindowRef`].
    pub struct MainWindowWithRenderer {
        main_window: MainWindow,
        renderer: Renderer,
        status: MainWindowStatus,
    }

    impl MainWindow {
        /// Creates a `MainWindow` with default values.
        pub fn new<EventUserType>(
            event_loop: &EventLoopWindowTarget<EventUserType>,
            title: &str,
        ) -> Result<MainWindow> {
            // For standard UI, we need as few fancy things as available
            let score = |c: &Config| (c.num_samples(), c.depth_size(), c.stencil_size());
            Self::with_gl_chooser(event_loop, title, |cfg1, cfg2| {
                if score(&cfg2) < score(&cfg1) {
                    cfg2
                } else {
                    cfg1
                }
            })
        }
        /// Creates a `MainWindow` with your own OpenGL context chooser.
        ///
        /// If you don't have specific OpenGL needs, prefer using [`MainWindow::new`]. If you do,
        /// consider using a _FramebufferObject_ and do an offscreen rendering instead.
        pub fn with_gl_chooser<EventUserType>(
            event_loop: &EventLoopWindowTarget<EventUserType>,
            title: &str,
            f_choose_cfg: impl FnMut(Config, Config) -> Config,
        ) -> Result<MainWindow> {
            let window_builder = WindowBuilder::new();
            let template = ConfigTemplateBuilder::new()
                .prefer_hardware_accelerated(Some(true))
                .with_depth_size(0)
                .with_stencil_size(0);

            let display_builder = DisplayBuilder::new().with_window_builder(Some(window_builder));

            let (window, gl_config) = display_builder
                .build(event_loop, template, |configs| {
                    configs.reduce(f_choose_cfg).unwrap()
                })
                .map_err(|e| anyhow!("{:#?}", e))?;
            let window = window.unwrap();
            window.set_title(title);
            window.set_ime_allowed(true);
            let raw_window_handle = Some(window.raw_window_handle());
            let gl_display = gl_config.display();
            let context_attributes = ContextAttributesBuilder::new().build(raw_window_handle);
            let fallback_context_attributes = ContextAttributesBuilder::new()
                .with_context_api(ContextApi::Gles(None))
                .build(raw_window_handle);

            let mut not_current_gl_context = Some(unsafe {
                gl_display
                    .create_context(&gl_config, &context_attributes)
                    .or_else(|_| {
                        gl_display.create_context(&gl_config, &fallback_context_attributes)
                    })?
            });

            let size = window.inner_size();

            let (width, height): (u32, u32) = size.into();
            let raw_window_handle = window.raw_window_handle();
            let attrs = SurfaceAttributesBuilder::<WindowSurface>::new().build(
                raw_window_handle,
                NonZeroU32::new(width).unwrap(),
                NonZeroU32::new(height).unwrap(),
            );

            let surface = unsafe {
                gl_config
                    .display()
                    .create_window_surface(&gl_config, &attrs)?
            };
            let gl_context = not_current_gl_context
                .take()
                .unwrap()
                .make_current(&surface)?;

            // Enable v-sync to avoid consuming too much CPU
            let _ = surface.set_swap_interval(
                &gl_context,
                glutin::surface::SwapInterval::Wait(NonZeroU32::new(1).unwrap()),
            );

            Ok(MainWindow {
                gl_context,
                window,
                surface,
                matrix: None,
                idler: MainWindowIdler::default(),
            })
        }
        /// Sets a custom matrix that converts physical mouse coordinates into logical ones.
        pub fn set_matrix(&mut self, matrix: Option<Matrix3<f32>>) {
            self.matrix = matrix;
        }

        /// Splits this window into its parts.
        ///
        /// # Safety
        /// Do not drop the `window` without dropping the `surface` first.
        pub unsafe fn into_pieces(
            self,
        ) -> (PossiblyCurrentContext, Surface<WindowSurface>, Window) {
            (self.gl_context, self.surface, self.window)
        }
        pub fn glutin_context(&self) -> &PossiblyCurrentContext {
            &self.gl_context
        }
        /// Creates a new `glow` OpenGL context for this window and the selected configuration.
        pub fn create_gl_context(&self) -> glow::Context {
            let dsp = self.gl_context.display();
            unsafe { glow::Context::from_loader_function_cstr(|s| dsp.get_proc_address(s)) }
        }
        /// Gets a reference to the `winit` window.
        pub fn window(&self) -> &Window {
            &self.window
        }
        pub fn surface(&self) -> &Surface<WindowSurface> {
            &self.surface
        }
        /// Converts the given physical size to a logical size, using the window scale factor.
        pub fn to_logical_size<X: Pixel, Y: Pixel>(&self, size: PhysicalSize<X>) -> LogicalSize<Y> {
            let scale = self.window.scale_factor();
            size.to_logical(scale)
        }
        /// Converts the given logical size to a physical size, using the window scale factor.
        pub fn to_physical_size<X: Pixel, Y: Pixel>(
            &self,
            size: LogicalSize<X>,
        ) -> PhysicalSize<Y> {
            let scale = self.window.scale_factor();
            size.to_physical(scale)
        }
        /// Converts the given physical position to a logical position, using the window scale factor.
        pub fn to_logical_pos<X: Pixel, Y: Pixel>(
            &self,
            pos: PhysicalPosition<X>,
        ) -> LogicalPosition<Y> {
            let scale = self.window.scale_factor();
            pos.to_logical(scale)
        }
        /// Converts the given logical position to a physical position, using the window scale factor.
        pub fn to_physical_pos<X: Pixel, Y: Pixel>(
            &self,
            pos: LogicalPosition<X>,
        ) -> PhysicalPosition<Y> {
            let scale = self.window.scale_factor();
            pos.to_physical(scale)
        }
    }

    impl MainWindowWithRenderer {
        /// Creates a new [`Renderer`] and attaches it to the given window.
        pub fn new(main_window: MainWindow) -> Self {
            let gl = main_window.create_gl_context();
            let renderer = Renderer::new(std::rc::Rc::new(gl)).unwrap();
            Self::new_with_renderer(main_window, renderer)
        }
        /// Sets the time after which the UI will stop rendering, if there is no user input.
        pub fn set_idle_time(&mut self, time: Duration) {
            self.main_window.idler.set_idle_time(time);
        }
        /// Sets the frame count after which the UI will stop rendering, if there is no user input.
        ///
        /// Note that by default V-Sync is enabled, and that will affect the frame rate.
        pub fn set_idle_frame_count(&mut self, frame_count: u32) {
            self.main_window.idler.set_idle_frame_count(frame_count);
        }
        /// Forces a rebuild of the UI.
        ///
        /// By default the window will stop rendering the UI after a while without user input. Use this
        /// function to force a redraw because of some external factor.
        pub fn ping_user_input(&mut self) {
            self.main_window.idler.ping_user_input();
        }
        /// Gets a reference to the inner renderer.
        pub fn renderer(&mut self) -> &mut Renderer {
            &mut self.renderer
        }
        /// Gets a reference to the inner window.
        pub fn main_window(&mut self) -> &mut MainWindow {
            &mut self.main_window
        }
        /// Attaches the given window and renderer together.
        pub fn new_with_renderer(main_window: MainWindow, mut renderer: Renderer) -> Self {
            let w = main_window.window();
            let size = w.inner_size();
            let scale = w.scale_factor();
            let size = size.to_logical::<f32>(scale);
            renderer.set_size(Vector2::from(mint::Vector2::from(size)), scale as f32);

            let mut imgui = unsafe { renderer.imgui().set_current() };

            #[cfg(feature = "clipboard")]
            clipboard::setup(&mut imgui);

            MainWindowWithRenderer {
                main_window,
                renderer,
                status: MainWindowStatus::default(),
            }
        }
        /// The main event function, to be called from your event loop.
        ///
        /// It returns [`EventResult`]. You can use it to break the main loop, or ignore it, as you see fit.
        /// It also informs of whether ImGui want the monopoly of the user input.
        pub fn do_event<EventUserType>(
            &mut self,
            app: &mut impl imgui::UiBuilder,
            event: &Event<EventUserType>,
        ) -> EventResult {
            self.do_event_with_flags(app, event, EventFlags::empty())
        }
        /// Just like `do_event` but with flags.
        pub fn do_event_with_flags<EventUserType>(
            &mut self,
            app: &mut impl imgui::UiBuilder,
            event: &Event<EventUserType>,
            flags: EventFlags,
        ) -> EventResult {
            do_event(
                &mut self.main_window,
                &mut self.renderer,
                &mut self.status,
                app,
                event,
                flags,
            )
        }
    }

    /// Main implementation of the `MainWindowRef` trait for an owned `MainWindow`.
    impl MainWindowRef for MainWindow {
        fn window(&self) -> &Window {
            &self.window
        }
        fn pre_render(&mut self) {
            self.idler.incr_frame();
            self.gl_context.make_current(&self.surface).unwrap();
        }
        fn post_render(&mut self) {
            self.window.pre_present_notify();
            self.surface.swap_buffers(&self.gl_context).unwrap();
        }
        fn resize(&mut self, size: PhysicalSize<u32>) -> LogicalSize<f32> {
            let width = NonZeroU32::new(size.width.max(1)).unwrap();
            let height = NonZeroU32::new(size.height.max(1)).unwrap();
            self.surface.resize(&self.gl_context, width, height);
            self.to_logical_size::<_, f32>(size)
        }
        fn ping_user_input(&mut self) {
            self.idler.ping_user_input();
        }
        fn about_to_wait(&mut self, pinged: bool) {
            if pinged || self.idler.has_to_render() {
                // No need to call set_control_flow(): doing a redraw will force extra Poll.
                // Not doing it will default to Wait.
                self.window.request_redraw();
            }
        }
        fn transform_position(&self, pos: Vector2) -> Vector2 {
            transform_position_with_optional_matrix(self, pos, &self.matrix)
        }
    }
}

#[cfg(feature = "main-window")]
pub use main_window::*;