baseview 0.3.1

Low-level windowing system geared towards making audio plugin UIs.
Documentation
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
use windows_core::{ComObject, HSTRING};
use windows_sys::Win32::{
    Foundation::{LPARAM, LRESULT, RECT, WPARAM},
    UI::{Controls::WM_MOUSELEAVE, WindowsAndMessaging::*},
};

use crate::{warn, EventStatus, HandlerError, WindowHandler};
use dpi::{PhysicalPosition, PhysicalSize, Size};
use std::cell::{Cell, OnceCell};
use std::num::NonZeroUsize;
use windows_sys::Win32::Foundation::POINT;

pub(crate) const BV_WINDOW_MUST_CLOSE: u32 = WM_USER + 1;

use super::drop_target::DropTarget;
use super::*;
use crate::handler::WindowHandlerBuilder;
use crate::host::Host;
use crate::platform::win::window_state::{WindowSharedState, WindowState};
use crate::platform::Error;
use crate::window::WindowInitializer;
use crate::wrappers::win32::cursor::SystemCursor;
use crate::wrappers::win32::window::*;
use crate::wrappers::win32::{
    ole_initialize, run_thread_message_loop_until, Dpi, DpiAwarenessContext, ExtendedUser32, Rect,
    WindowStyle,
};
use crate::{Event, MouseButton, MouseEvent, ScrollDelta, WindowEvent, WindowSize};

#[allow(non_snake_case)]
fn HIWORD(wparam: WPARAM) -> u16 {
    ((wparam >> 16) & 0xffff) as u16
}

#[allow(non_snake_case)]
fn LOWORD(lparam: LPARAM) -> u16 {
    (lparam & 0xffff) as u16
}

const WIN_FRAME_TIMER: NonZeroUsize = match NonZeroUsize::new(4242) {
    Some(x) => x,
    None => unreachable!(),
};

pub struct WindowHandle {
    init: Cell<Option<WindowInitializer>>,
    hwnd: Cell<Option<HWnd>>,
    state: Rc<WindowSharedState>,
}

impl WindowHandle {
    pub fn run_until_closed(self) -> Result<()> {
        self.show()?;

        run_thread_message_loop_until(|| !self.is_open())?;
        Ok(())
    }

    pub fn is_open(&self) -> bool {
        self.state.is_alive.get()
    }

    pub fn is_resizable(&self) -> bool {
        self.state.sizing_strategy.is_resizable()
    }

    pub fn min_size(&self) -> Option<Size> {
        self.state.sizing_strategy.min_size()
    }

    pub fn max_size(&self) -> Option<Size> {
        self.state.sizing_strategy.max_size()
    }

    pub fn size(&self) -> WindowSize {
        self.state.size()
    }

    pub fn resize(&self, new_size: Size) -> Result<()> {
        let new_size = new_size.to_physical(self.state.scale_factor());
        let hwnd = match self.hwnd.get() {
            Some(hwnd) => hwnd,
            None => {
                self.state.current_size.set(new_size);
                return Ok(());
            }
        };

        let _guard = self.state.originate_host_resize();
        hwnd.resize_and_activate(new_size, self.state.current_dpi.get(), &self.state.user32)?;

        if self.state.current_size.get() == new_size {
            Ok(())
        } else {
            Err(Error::ResizeFailed)
        }
    }

    pub fn suggest_scale_factor(&self, scale_factor: f64) -> Result<()> {
        let current_scale_factor = self.state.scale_factor();
        self.state.fallback_scale_factor.set(Some(scale_factor));

        if self.state.current_dpi.get().is_some() {
            return Ok(());
        }

        let Some(hwnd) = self.hwnd.get() else { return Ok(()) };

        let current_size = self.state.current_size.get();
        let new_size = self
            .state
            .current_size
            .get()
            .to_logical::<f64>(current_scale_factor)
            .to_physical(self.state.scale_factor());

        // This call doesn't meaningfully change the scaling factor, ignore the result
        if current_size == new_size {
            return Ok(());
        }

        let _guard = self.state.originate_host_resize();

        hwnd.resize_and_activate(new_size, None, &self.state.user32)?;

        if self.state.current_size.get() == new_size {
            Ok(())
        } else {
            Err(Error::ResizeFailed)
        }
    }

    pub fn set_parent(&self, new_parent: ParentWindowHandle) -> Result<()> {
        let hwnd = match self.hwnd.get() {
            Some(hwnd) => hwnd,
            None => {
                let Some(mut init) = self.init.take() else { return Ok(()) };
                init.settings.parent = Some(new_parent.into());

                let window = BaseviewWindow::create(self.state.clone(), init)?;
                self.hwnd.set(Some(window));

                return Ok(());
            }
        };

        if !self.state.parented.get() {
            panic!("Called set_parent on a floating window")
        }

        hwnd.set_parent(&new_parent.handle)?;

        Ok(())
    }

    #[inline]
    pub fn handle_main_thread_callback(&self) {
        // No-op
    }

    pub fn show(&self) -> Result<()> {
        let hwnd = match self.hwnd.get() {
            Some(hwnd) => hwnd,
            None => {
                let Some(init) = self.init.take() else { return Ok(()) };

                let window = BaseviewWindow::create(self.state.clone(), init)?;
                self.hwnd.set(Some(window));

                return Ok(());
            }
        };

        hwnd.show_and_activate();

        Ok(())
    }

    pub fn hide(&self) -> Result<()> {
        let Some(hwnd) = self.hwnd.get() else { return Ok(()) };
        hwnd.hide();

        Ok(())
    }
}

impl Drop for WindowHandle {
    fn drop(&mut self) {
        if !self.state.is_alive.get() {
            return;
        }

        if let Some(hwnd) = self.hwnd.take() {
            let _guard = self.state.originate_host_destroy();
            if let Err(e) = hwnd.destroy() {
                warn!("Failed to destroy window: {}", e);
            }
        }
    }
}

pub struct BaseviewWindow {
    window_state: Rc<WindowState>,
    shared_state: Rc<WindowSharedState>,
    initial_size: Size,

    handler_builder: Cell<Option<WindowHandlerBuilder>>,
    handler: OnceCell<Box<dyn WindowHandler>>,
    host: Host,

    // Things not directly used, but kept so their Drop impl runs when the window is destroyed
    _keyboard_hook: Cell<Option<hook::KeyboardHookHandle>>,
    _drop_target: Cell<Option<ComObject<DropTarget>>>,

    #[cfg(feature = "opengl")]
    pub gl_config: Option<crate::gl::GlConfig>,
}

impl BaseviewWindow {
    pub fn create(shared_state: Rc<WindowSharedState>, init: WindowInitializer) -> Result<HWnd> {
        let dpi_ctx = DpiAwarenessContext::new(&shared_state.user32)?;

        let style = WindowStyle::from_settings(&init.settings);

        let window_size = shared_state.current_size.get();

        let initializer = {
            let shared_state = shared_state.clone();

            move |hwnd: HWnd| {
                let window_state = Rc::new(WindowState::new(
                    hwnd,
                    shared_state.user32.clone(),
                    shared_state.clone(),
                ));

                BaseviewWindow {
                    window_state,
                    initial_size: init.settings.size,
                    handler_builder: Cell::new(Some(init.builder)),
                    handler: OnceCell::new(),
                    shared_state,
                    host: init.host,

                    _drop_target: None.into(),
                    _keyboard_hook: None.into(),

                    #[cfg(feature = "opengl")]
                    gl_config: init.settings.gl_config,
                }
            }
        };

        let parent = init.settings.parent.map(|p| p.inner.handle);
        let rect = dpi_ctx.client_area_to_nc_area(window_size.into(), style, None)?;
        let title = HSTRING::from(init.settings.title);
        let window = create_window(&title, style, rect.size(), parent, &dpi_ctx, initializer)?;

        // FIXME: this SetTimer call could be in after_create, but for some reason it changes the ordering
        // for a parent+child window situation, which results in the parent drawing over the child.
        // This timer should be replaced by proper window redrawing/damage/vsync handling, but this
        // would be a breaking change, so we'll do that later.
        // TODO: create a new timer instead of hard-coding a specific ID
        window.set_timer(WIN_FRAME_TIMER, 15)?;

        Ok(window)
    }

    fn notify_destroyed_to_host(&self) {
        if self.shared_state.destroy_host_originated.get() {
            return;
        };

        self.host.notify_destroyed()
    }

    fn request_resize_from_host(
        &self, new_size: WindowSize,
    ) -> core::result::Result<(), HandlerError> {
        if self.shared_state.resize_host_originated.get() {
            return Ok(());
        };

        self.host.request_resize(new_size)
    }

    pub(crate) fn handle_on_frame(&self) {
        let Some(handler) = self.handler.get() else { return };

        if let Err(e) = handler.on_frame() {
            warn!("Error while rendering frame: {}", e);
            self.window_state.request_close();
        }
    }

    pub(crate) fn handle_event(&self, event: Event) -> EventStatus {
        let Some(handler) = self.handler.get() else {
            return EventStatus::Ignored;
        };

        handler.on_event(event)
    }
}

impl Drop for BaseviewWindow {
    fn drop(&mut self) {
        self.shared_state.is_alive.set(false);
        self.notify_destroyed_to_host();
    }
}

impl WindowImpl for BaseviewWindow {
    fn after_create(&self, window: HWnd) -> core::result::Result<(), Error> {
        let hwnd = window.as_raw();
        let window_state = &self.window_state;

        self._keyboard_hook.set(Some(hook::init_keyboard_hook(hwnd)));

        // Now we can get the actual dpi of the window.
        let dpi = window.get_dpi(&self.window_state.user32)?;

        if let Some(dpi) = dpi {
            if Some(dpi) != window_state.shared.current_dpi.get() {
                window_state.shared.current_dpi.set(Some(dpi));

                // We cannot create a window in "logical" pixels, and we can't DPI-scale to physical pixels because we
                // have no way to know where the window will end up.
                // So, at window creation, we assume a DPI=96, and if it ends up wrong, we resize the window
                // to the actual logical size the user desired.
                let new_size = self.initial_size.to_physical(dpi.scale_factor());

                // Preemptively update so a synchronous WM_SIZE from SetWindowPos below
                // doesn't also emit Resized.
                window_state.shared.current_size.set(new_size);
                window.resize_and_activate(new_size, Some(dpi), &window_state.user32)?;
            }
        }

        let drop_target = ComObject::new(DropTarget::new(Rc::downgrade(window_state), window));
        self._drop_target.set(Some(drop_target.clone()));

        ole_initialize()?;
        window.register_drag_drop(drop_target.as_interface())?;

        #[cfg(feature = "opengl")]
        if let Some(gl_config) = self.gl_config.clone() {
            let gl_context = gl::GlContextInner::create(window, gl_config)
                .expect("Could not create OpenGL context");

            let Ok(()) = self.window_state.gl_context.set(Rc::new(gl_context)) else {
                unreachable!();
            };
        };

        let handler = {
            let context = crate::WindowContext::new(Rc::clone(&self.window_state));
            self.handler_builder.take().unwrap().build(context)?
        };
        let Ok(()) = self.handler.set(handler) else { unreachable!() };

        Ok(())
    }

    unsafe fn handle_message(
        &self, window: HWnd, msg: u32, wparam: WPARAM, lparam: LPARAM,
    ) -> Option<LRESULT> {
        unsafe { wnd_proc_inner(window, msg, wparam, lparam, self) }
    }

    fn before_destroy(&self, window: HWnd) {
        let _ = window.revoke_drag_drop();
    }
}

/// Our custom `wnd_proc` handler. If the result contains a value, then this is returned after
/// handling any deferred tasks. otherwise the default window procedure is invoked.
unsafe fn wnd_proc_inner(
    window: HWnd, msg: u32, wparam: WPARAM, lparam: LPARAM, window_bv: &BaseviewWindow,
) -> Option<LRESULT> {
    let window_state = &window_bv.window_state;
    match msg {
        WM_MOUSEMOVE => {
            if window_state.mouse_was_outside_window.get() {
                // this makes Windows track whether the mouse leaves the window.
                // When the mouse leaves it results in a `WM_MOUSELEAVE` event.
                // Couldn't find a good way to track whether the mouse enters,
                // but if `WM_MOUSEMOVE` happens, the mouse must have entered.
                let _ = window.start_cursor_leave_tracking();
                window_state.mouse_was_outside_window.set(false);

                let enter_event = Event::Mouse(MouseEvent::CursorEntered);
                window_bv.handle_event(enter_event);
            }

            let x = (lparam & 0xFFFF) as i16 as i32;
            let y = ((lparam >> 16) & 0xFFFF) as i16 as i32;

            let move_event = Event::Mouse(MouseEvent::CursorMoved {
                position: PhysicalPosition { x, y }.cast(),
                modifiers: window_state
                    .keyboard_state
                    .borrow()
                    .get_modifiers_from_mouse_wparam(wparam),
            });

            window_bv.handle_event(move_event);
            Some(0)
        }

        WM_MOUSELEAVE => {
            window_bv.handle_event(Event::Mouse(MouseEvent::CursorLeft));

            window_state.mouse_was_outside_window.set(true);
            Some(0)
        }
        WM_MOUSEWHEEL | WM_MOUSEHWHEEL => {
            let value = (wparam >> 16) as i16;
            let value = value as i32;
            let value = value as f32 / WHEEL_DELTA as f32;

            let event = Event::Mouse(MouseEvent::WheelScrolled {
                delta: if msg == WM_MOUSEWHEEL {
                    ScrollDelta::Lines { x: 0.0, y: value }
                } else {
                    ScrollDelta::Lines { x: value, y: 0.0 }
                },
                modifiers: window_state
                    .keyboard_state
                    .borrow()
                    .get_modifiers_from_mouse_wparam(wparam),
            });

            window_bv.handle_event(event);
            Some(0)
        }
        WM_LBUTTONDOWN | WM_LBUTTONUP | WM_MBUTTONDOWN | WM_MBUTTONUP | WM_RBUTTONDOWN
        | WM_RBUTTONUP | WM_XBUTTONDOWN | WM_XBUTTONUP => {
            let mut mouse_button_counter = window_state.mouse_button_counter.get();

            #[allow(non_snake_case)]
            fn GET_XBUTTON_WPARAM(wparam: WPARAM) -> u16 {
                HIWORD(wparam)
            }

            const XBUTTON1: u16 = 0x1;
            const XBUTTON2: u16 = 0x2;

            let button = match msg {
                WM_LBUTTONDOWN | WM_LBUTTONUP => Some(MouseButton::Left),
                WM_MBUTTONDOWN | WM_MBUTTONUP => Some(MouseButton::Middle),
                WM_RBUTTONDOWN | WM_RBUTTONUP => Some(MouseButton::Right),
                WM_XBUTTONDOWN | WM_XBUTTONUP => match GET_XBUTTON_WPARAM(wparam) {
                    XBUTTON1 => Some(MouseButton::Back),
                    XBUTTON2 => Some(MouseButton::Forward),
                    _ => None,
                },
                _ => None,
            };

            if let Some(button) = button {
                let event = match msg {
                    WM_LBUTTONDOWN | WM_MBUTTONDOWN | WM_RBUTTONDOWN | WM_XBUTTONDOWN => {
                        // Capture the mouse cursor on button down
                        mouse_button_counter = mouse_button_counter.saturating_add(1);
                        window.set_capture();
                        MouseEvent::ButtonPressed {
                            button,
                            modifiers: window_state
                                .keyboard_state
                                .borrow()
                                .get_modifiers_from_mouse_wparam(wparam),
                        }
                    }
                    WM_LBUTTONUP | WM_MBUTTONUP | WM_RBUTTONUP | WM_XBUTTONUP => {
                        // Release the mouse cursor capture when all buttons are released
                        mouse_button_counter = mouse_button_counter.saturating_sub(1);
                        if mouse_button_counter == 0 {
                            HWnd::release_capture();
                        }

                        MouseEvent::ButtonReleased {
                            button,
                            modifiers: window_state
                                .keyboard_state
                                .borrow()
                                .get_modifiers_from_mouse_wparam(wparam),
                        }
                    }
                    _ => {
                        unreachable!()
                    }
                };

                window_state.mouse_button_counter.set(mouse_button_counter);
                window_bv.handle_event(Event::Mouse(event));
            }

            None
        }
        WM_TIMER => {
            if wparam == WIN_FRAME_TIMER.get() {
                window_bv.handle_on_frame()
            }

            Some(0)
        }
        WM_CLOSE => {
            window_bv.handle_event(Event::Window(WindowEvent::WillClose));

            None
        }
        WM_CHAR | WM_SYSCHAR | WM_KEYDOWN | WM_SYSKEYDOWN | WM_KEYUP | WM_SYSKEYUP
        | WM_INPUTLANGCHANGE => {
            let opt_event = window_state.keyboard_state.borrow_mut().process_message(
                window.as_raw(),
                msg,
                wparam,
                lparam,
            );

            if let Some(event) = opt_event {
                window_bv.handle_event(Event::Keyboard(event));
            }

            if msg != WM_SYSKEYDOWN {
                Some(0)
            } else {
                None
            }
        }
        WM_SETFOCUS => {
            window_bv.handle_event(Event::Window(WindowEvent::Focused));

            None
        }
        WM_KILLFOCUS => {
            window_bv.handle_event(Event::Window(WindowEvent::Unfocused));

            None
        }
        WM_SIZE => {
            let width = (lparam & 0xFFFF) as u16 as u32;
            let height = ((lparam >> 16) & 0xFFFF) as u16 as u32;

            let new_size = PhysicalSize { width, height };
            let current_size = window_state.shared.current_size.get();

            // Only send the event if anything changed
            if current_size == new_size {
                return None;
            }

            let previous = window_state.shared.current_size.replace(new_size);
            let new_size = WindowSize::from_physical(new_size, window_state.shared.scale_factor());

            let handler = window_bv.handler.get()?;
            if let Err(e) = handler.resized(new_size) {
                warn!("Window Handler failed to resize: {}", e);
                window_state.shared.current_size.set(previous);

                if let Err(e) = window_state.resize(previous.into()) {
                    warn!("Failed to resize back to previous window size: {}", e);
                }

                return Some(-1);
            }

            if let Err(e) = window_bv.request_resize_from_host(new_size) {
                warn!("Resize request from Host failed: {}. Reverting to previous size.", e);

                if let Err(e) = handler.resized(new_size) {
                    warn!("Window Handler failed to resize to previous window size: {}", e);
                }

                window_state.shared.current_size.set(previous);
                if let Err(e) = window_state.resize(previous.into()) {
                    warn!("Failed to resize back to previous window size: {}", e);
                }

                return Some(-1);
            }

            None
        }
        WM_DPICHANGED => {
            let suggested_nc_rect = Rect((lparam as *const RECT).read());
            let dpi = Dpi((wparam & 0xFFFF) as u16 as u32);

            let dpi_ctx = DpiAwarenessContext::new(&window_state.user32).unwrap();
            let style = window.get_style().unwrap();
            let suggested_rect =
                dpi_ctx.nc_area_to_client_area(suggested_nc_rect, style, Some(dpi)).unwrap();

            let new_size = suggested_rect.size();

            let changed = window_state.shared.current_size.get() != new_size
                || window_state.shared.current_dpi.get() != Some(dpi);

            window_state.shared.current_dpi.replace(Some(dpi));
            let previous_size = window_state.shared.current_size.replace(new_size);

            // Windows makes us resize the window manually. This however will not send a WM_SIZE event,
            // hence why we are notifying the window handler manually below.
            let _ = window.set_nc_rect(suggested_nc_rect);

            if changed {
                let handler = window_bv.handler.get()?;
                let new_size = WindowSize::from_physical(new_size, dpi.scale_factor());

                if let Err(e) = handler.resized(new_size) {
                    warn!("Window Handler failed to resize: {}", e);
                    window_state.shared.current_size.set(previous_size);

                    if let Err(e) = window_state.resize(previous_size.into()) {
                        warn!("Failed to resize back to previous window size: {}", e);
                    }
                }

                if let Err(e) = window_bv.request_resize_from_host(new_size) {
                    warn!("Resize request from Host failed: {}. Reverting to previous size.", e);

                    if let Err(e) = handler.resized(new_size) {
                        warn!("Window Handler failed to resize to previous window size: {}", e);
                    }

                    window_state.shared.current_size.set(previous_size);
                    if let Err(e) = window_state.resize(previous_size.into()) {
                        warn!("Failed to resize back to previous window size: {}", e);
                    }

                    return Some(-1);
                }
            }

            None
        }
        // If WM_SETCURSOR returns `None`, WM_SETCURSOR continues to get handled by the outer window(s),
        // If it returns `Some(1)`, the current window decides what the cursor is
        WM_SETCURSOR => {
            let low_word = LOWORD(lparam) as u32;
            let mouse_in_window = low_word == HTCLIENT;
            if mouse_in_window {
                // Here we need to set the cursor back to what the state says, since it can have changed when outside the window
                if let Ok(cursor) = SystemCursor::load(window_state.cursor_icon.get()) {
                    cursor.set()
                }
                Some(1)
            } else {
                // Cursor is being changed by some other window, e.g. when having mouse on the borders to resize it
                None
            }
        }
        WM_GETMINMAXINFO => {
            let sizing = window_state.shared.sizing_strategy;

            // Only implement this message if we actually need to specify a min/max size
            if let (None, None) = (sizing.min_size(), sizing.max_size()) {
                return None;
            }

            let info = lparam as *mut MINMAXINFO;

            let ctx = DpiAwarenessContext::new(&window_state.user32).unwrap();
            let style = window.get_style().unwrap();
            let dpi = window_state.shared.current_dpi.get();

            if let Some(size) = sizing.min_size() {
                let size = size.to_physical(window_state.shared.scale_factor());
                let size =
                    ctx.client_area_to_nc_area(size.into(), style, dpi).unwrap().size().cast();
                let pt = POINT { x: size.width, y: size.height };
                (&raw mut (*info).ptMinTrackSize).write(pt);
            }

            if let Some(size) = sizing.max_size() {
                let size = size.to_physical(window_state.shared.scale_factor());
                let size =
                    ctx.client_area_to_nc_area(size.into(), style, dpi).unwrap().size().cast();
                let pt = POINT { x: size.width, y: size.height };
                (&raw mut (*info).ptMaxTrackSize).write(pt);
            }

            Some(0)
        }
        // NOTE: `WM_NCDESTROY` is handled in the outer function because this deallocates the window
        //        state
        BV_WINDOW_MUST_CLOSE => {
            let _ = window.destroy();
            Some(0)
        }
        _ => None,
    }
}

impl WindowHandle {
    pub fn create_window(init: WindowInitializer) -> Result<WindowHandle> {
        let extended_user_32 = ExtendedUser32::load()?;

        let shared_state = WindowSharedState::new(extended_user_32, &init.settings);

        if init.settings.wait_for_parent && init.settings.parent.is_none() {
            return Ok(WindowHandle {
                hwnd: None.into(),
                state: shared_state,
                init: Some(init).into(),
            });
        }

        let window = BaseviewWindow::create(shared_state.clone(), init)?;

        Ok(WindowHandle { hwnd: Some(window).into(), state: shared_state, init: None.into() })
    }
}

pub fn copy_to_clipboard(_data: &str) {
    todo!()
}