dear-imgui-winit 0.18.0

Winit platform backend for dear-imgui-rs
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 winit::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size};
use winit::window::Window;

use crate::native_support::MonitorSnapshot;
use crate::sanitize;

/// Dear ImGui requires every monitor rectangle, platform window rectangle, and mouse position to
/// use one native desktop coordinate space. Winit exposes physical values everywhere, but its
/// macOS implementation encodes Cocoa desktop points using each monitor/window backing scale.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[allow(dead_code)]
enum DesktopCoordinateSpace {
    Physical,
    Logical,
}

const fn native_desktop_coordinate_space() -> DesktopCoordinateSpace {
    #[cfg(target_os = "macos")]
    {
        DesktopCoordinateSpace::Logical
    }
    #[cfg(not(target_os = "macos"))]
    {
        DesktopCoordinateSpace::Physical
    }
}

fn sanitized_scale(scale: f64) -> f64 {
    sanitize::positive_finite_or(scale, 1.0)
}

pub(super) fn validate_viewport_position(position: [f32; 2]) -> Option<[f32; 2]> {
    let position = sanitize::finite_vec2_f32(position)?;
    let minimum = f64::from(i32::MIN);
    let maximum = f64::from(i32::MAX);
    position
        .into_iter()
        .map(f64::from)
        .all(|value| value >= minimum && value <= maximum)
        .then_some(position)
}

pub(super) fn validate_viewport_size(size: [f32; 2]) -> Option<[f32; 2]> {
    let size = sanitize::finite_vec2_f32(size)?;
    let maximum = f64::from(i32::MAX);
    size.into_iter()
        .map(f64::from)
        .all(|value| value > 0.0 && value <= maximum)
        .then_some(size)
}

fn desktop_position_from_physical_values(
    space: DesktopCoordinateSpace,
    position: [f64; 2],
    scale: f64,
) -> Option<[f64; 2]> {
    if !position.into_iter().all(f64::is_finite) {
        return None;
    }

    let position = match space {
        DesktopCoordinateSpace::Physical => position,
        DesktopCoordinateSpace::Logical => {
            let scale = sanitized_scale(scale);
            [position[0] / scale, position[1] / scale]
        }
    };
    position.into_iter().all(f64::is_finite).then_some(position)
}

fn desktop_size_from_physical_values(
    space: DesktopCoordinateSpace,
    size: [f64; 2],
    scale: f64,
) -> [f32; 2] {
    let size = match space {
        DesktopCoordinateSpace::Physical => size,
        DesktopCoordinateSpace::Logical => {
            let scale = sanitized_scale(scale);
            [size[0] / scale, size[1] / scale]
        }
    };
    [
        sanitize::finite_non_negative_f64_to_f32(size[0]).unwrap_or(0.0),
        sanitize::finite_non_negative_f64_to_f32(size[1]).unwrap_or(0.0),
    ]
}

fn checked_desktop_size_from_physical_values(
    space: DesktopCoordinateSpace,
    size: [f64; 2],
    scale: f64,
) -> Option<[f32; 2]> {
    let size = match space {
        DesktopCoordinateSpace::Physical => size,
        DesktopCoordinateSpace::Logical => {
            let scale = sanitized_scale(scale);
            [size[0] / scale, size[1] / scale]
        }
    };
    if !size
        .into_iter()
        .all(|value| value.is_finite() && value >= 0.0)
    {
        return None;
    }
    Some([
        sanitize::finite_non_negative_f64_to_f32(size[0])?,
        sanitize::finite_non_negative_f64_to_f32(size[1])?,
    ])
}

pub(super) fn desktop_position_from_physical(
    position: PhysicalPosition<i32>,
    scale: f64,
) -> Option<[f32; 2]> {
    desktop_position_from_physical_values(
        native_desktop_coordinate_space(),
        [f64::from(position.x), f64::from(position.y)],
        scale,
    )
    .and_then(sanitize::finite_vec2_f64_to_f32)
}

/// Converts detached native monitor facts into the coordinate model used by Winit viewports.
///
/// Main and work rectangles are converted independently. The conversion is fallible so native
/// values that cannot be represented as finite ImGui `f32` values are rejected instead of being
/// silently clamped into a different geometry.
pub(super) fn monitor_from_snapshot(
    snapshot: &MonitorSnapshot,
) -> Option<dear_imgui_rs::sys::ImGuiPlatformMonitor> {
    let scale = sanitized_scale(snapshot.scale_factor());
    let main = snapshot.main();
    let work = snapshot.work();
    let main_position = desktop_position_from_physical_values(
        native_desktop_coordinate_space(),
        main.position(),
        scale,
    )
    .and_then(sanitize::finite_vec2_f64_to_f32)?;
    let work_position = desktop_position_from_physical_values(
        native_desktop_coordinate_space(),
        work.position(),
        scale,
    )
    .and_then(sanitize::finite_vec2_f64_to_f32)?;
    let main_size = checked_desktop_size_from_physical_values(
        native_desktop_coordinate_space(),
        main.size(),
        scale,
    )?;
    let work_size = checked_desktop_size_from_physical_values(
        native_desktop_coordinate_space(),
        work.size(),
        scale,
    )?;

    Some(dear_imgui_rs::sys::ImGuiPlatformMonitor {
        MainPos: dear_imgui_rs::sys::ImVec2 {
            x: main_position[0],
            y: main_position[1],
        },
        MainSize: dear_imgui_rs::sys::ImVec2 {
            x: main_size[0],
            y: main_size[1],
        },
        WorkPos: dear_imgui_rs::sys::ImVec2 {
            x: work_position[0],
            y: work_position[1],
        },
        WorkSize: dear_imgui_rs::sys::ImVec2 {
            x: work_size[0],
            y: work_size[1],
        },
        DpiScale: sanitize::positive_finite_f32_or(scale as f32, 1.0),
        PlatformHandle: std::ptr::null_mut(),
    })
}

pub(super) unsafe fn viewport_target_dpi_scale(
    viewport: *mut dear_imgui_rs::sys::ImGuiViewport,
) -> f32 {
    let Some(viewport_ref) = (unsafe { viewport.as_ref() }) else {
        return 1.0;
    };
    let fallback = sanitize::positive_finite_f32_or(viewport_ref.DpiScale, 1.0);
    let monitor = unsafe { dear_imgui_rs::sys::igGetViewportPlatformMonitor(viewport) };
    unsafe { monitor.as_ref() }
        .map(|monitor| sanitize::positive_finite_f32_or(monitor.DpiScale, fallback))
        .unwrap_or(fallback)
}

pub(crate) fn desktop_size_for_window(window: &Window) -> [f32; 2] {
    desktop_size_from_physical(window.inner_size(), window.scale_factor())
}

pub(crate) fn single_window_display_metrics(window: &Window) -> ([f32; 2], [f32; 2]) {
    single_window_display_metrics_from_physical(window.inner_size(), window.scale_factor())
}

fn single_window_display_metrics_from_physical(
    size: PhysicalSize<u32>,
    scale: f64,
) -> ([f32; 2], [f32; 2]) {
    let scale = sanitized_scale(scale);
    let logical_size: LogicalSize<f64> = size.to_logical(scale);
    (
        sanitize::finite_non_negative_size(logical_size),
        sanitize::framebuffer_scale(scale, 1.0),
    )
}

pub(super) fn desktop_size_from_physical(size: PhysicalSize<u32>, scale: f64) -> [f32; 2] {
    desktop_size_from_physical_values(
        native_desktop_coordinate_space(),
        [f64::from(size.width), f64::from(size.height)],
        scale,
    )
}

pub(crate) fn framebuffer_scale_for_window(window: &Window) -> [f32; 2] {
    framebuffer_scale_for_space(native_desktop_coordinate_space(), window.scale_factor())
}

pub(crate) fn scale_factor_inner_size_override(
    scale_viewports: bool,
    current_size: PhysicalSize<u32>,
) -> Option<PhysicalSize<u32>> {
    scale_factor_inner_size_override_for_space(
        native_desktop_coordinate_space(),
        scale_viewports,
        current_size,
    )
}

fn scale_factor_inner_size_override_for_space(
    space: DesktopCoordinateSpace,
    scale_viewports: bool,
    current_size: PhysicalSize<u32>,
) -> Option<PhysicalSize<u32>> {
    (matches!(space, DesktopCoordinateSpace::Physical) && !scale_viewports).then_some(current_size)
}

fn framebuffer_scale_for_space(space: DesktopCoordinateSpace, scale: f64) -> [f32; 2] {
    match space {
        // The ImGui coordinate unit already is a framebuffer pixel on Windows and X11.
        DesktopCoordinateSpace::Physical => [1.0, 1.0],
        DesktopCoordinateSpace::Logical => {
            let scale = sanitize::positive_finite_f32_or(sanitized_scale(scale) as f32, 1.0);
            [scale, scale]
        }
    }
}

pub(super) fn framebuffer_scale_for_dpi_scale(scale: f64) -> [f32; 2] {
    framebuffer_scale_for_space(native_desktop_coordinate_space(), scale)
}

pub(crate) fn window_position_from_desktop(position: [f32; 2]) -> Position {
    match native_desktop_coordinate_space() {
        DesktopCoordinateSpace::Physical => Position::Physical(
            PhysicalPosition::new(f64::from(position[0]), f64::from(position[1])).cast(),
        ),
        DesktopCoordinateSpace::Logical => Position::Logical(LogicalPosition::new(
            f64::from(position[0]),
            f64::from(position[1]),
        )),
    }
}

pub(crate) fn window_size_from_desktop(size: [f32; 2]) -> Size {
    match native_desktop_coordinate_space() {
        DesktopCoordinateSpace::Physical => {
            Size::Physical(PhysicalSize::new(f64::from(size[0]), f64::from(size[1])).cast())
        }
        DesktopCoordinateSpace::Logical => {
            Size::Logical(LogicalSize::new(f64::from(size[0]), f64::from(size[1])))
        }
    }
}

pub(super) fn request_client_geometry(
    window: &Window,
    position: [f32; 2],
    size: [f32; 2],
    dpi_scale: f32,
) {
    let _ = window.request_inner_size(window_size_from_desktop(size));
    let outer_position = outer_position_from_client(window, position, dpi_scale);
    window.set_outer_position(window_position_from_desktop(outer_position));
}

pub(crate) fn client_physical_to_screen_pos(
    window: &Window,
    client_position: [f64; 2],
) -> Option<[f32; 2]> {
    let scale = window.scale_factor();
    let client = desktop_position_from_physical_values(
        native_desktop_coordinate_space(),
        client_position,
        scale,
    )?;
    let origin = window.inner_position().ok()?;
    let origin = desktop_position_from_physical_values(
        native_desktop_coordinate_space(),
        [f64::from(origin.x), f64::from(origin.y)],
        scale,
    )?;
    sanitize::finite_vec2_f64_to_f32([origin[0] + client[0], origin[1] + client[1]])
}

#[derive(Clone, Copy, Debug)]
pub(super) struct ObservedClientGeometry {
    pub(super) position: [f32; 2],
    pub(super) size: [f32; 2],
    pub(super) decoration_offset: Option<[f32; 2]>,
}

pub(super) fn observe_client_geometry(window: &Window) -> Option<ObservedClientGeometry> {
    let scale = window.scale_factor();
    let inner = window.inner_position().ok()?;
    let position = desktop_position_from_physical(inner, scale)?;
    let size = desktop_size_from_physical(window.inner_size(), scale);
    let decoration_offset = window
        .outer_position()
        .ok()
        .and_then(|outer| decoration_offset_from_positions(inner, outer, scale));
    Some(ObservedClientGeometry {
        position,
        size,
        decoration_offset,
    })
}

pub(super) fn decoration_offset(window: &Window) -> Option<[f32; 2]> {
    let scale = window.scale_factor();
    let inner = window.inner_position().ok()?;
    let outer = window.outer_position().ok()?;
    decoration_offset_from_positions(inner, outer, scale)
}

fn decoration_offset_from_positions(
    inner: PhysicalPosition<i32>,
    outer: PhysicalPosition<i32>,
    scale: f64,
) -> Option<[f32; 2]> {
    let inner = desktop_position_from_physical_values(
        native_desktop_coordinate_space(),
        [f64::from(inner.x), f64::from(inner.y)],
        scale,
    )?;
    let outer = desktop_position_from_physical_values(
        native_desktop_coordinate_space(),
        [f64::from(outer.x), f64::from(outer.y)],
        scale,
    )?;
    sanitize::finite_vec2_f64_to_f32([inner[0] - outer[0], inner[1] - outer[1]])
}

pub(super) fn outer_position_from_client(
    window: &Window,
    client_position: [f32; 2],
    dpi_scale: f32,
) -> [f32; 2] {
    #[cfg(target_os = "windows")]
    if !window.is_decorated() {
        // Undecorated Winit windows retain resize-capable Win32 style bits while WM_NCCALCSIZE
        // makes the client area cover the window. Inferring decorations from those styles would
        // invent a title-bar offset and desynchronize ImGui hit testing from the native client
        // origin.
        return client_position;
    }

    // Decorated Windows windows need the destination monitor DPI, which is not necessarily the
    // current live decoration offset. Other platforms use Winit's observed client/outer geometry.
    #[cfg(target_os = "windows")]
    if let Some(position) =
        windows::outer_position_from_client_style(window, client_position, dpi_scale)
    {
        return position;
    }

    let _ = dpi_scale;
    decoration_offset(window)
        .and_then(|offset| outer_position_from_decoration_offset(client_position, offset))
        .unwrap_or(client_position)
}

fn outer_position_from_decoration_offset(
    client_position: [f32; 2],
    decoration_offset: [f32; 2],
) -> Option<[f32; 2]> {
    sanitize::finite_vec2_f32([
        client_position[0] - decoration_offset[0],
        client_position[1] - decoration_offset[1],
    ])
}

pub(crate) fn ime_cursor_area_for_viewport(
    window: &Window,
    input_position: [f32; 2],
    viewport_position: [f32; 2],
    line_height: f32,
) -> Option<(LogicalPosition<f64>, LogicalSize<f64>)> {
    let input_position = sanitize::finite_vec2_f32(input_position)?;
    let viewport_position = sanitize::finite_vec2_f32(viewport_position)?;
    let line_height = if line_height.is_finite() && line_height > 0.0 {
        line_height
    } else {
        16.0
    };
    let mut client_position = [
        f64::from(input_position[0] - viewport_position[0]),
        f64::from(input_position[1] - viewport_position[1]),
    ];
    let mut line_height = f64::from(line_height);
    if native_desktop_coordinate_space() == DesktopCoordinateSpace::Physical {
        let scale = sanitized_scale(window.scale_factor());
        client_position[0] /= scale;
        client_position[1] /= scale;
        line_height /= scale;
    }
    if !client_position.into_iter().all(f64::is_finite)
        || !line_height.is_finite()
        || line_height <= 0.0
    {
        return None;
    }
    Some((
        LogicalPosition::new(client_position[0], client_position[1]),
        LogicalSize::new(line_height, line_height),
    ))
}

#[cfg(target_os = "windows")]
mod windows {
    use windows_sys::Win32::Foundation::RECT;
    use windows_sys::Win32::UI::HiDpi::AdjustWindowRectExForDpi;
    use windows_sys::Win32::UI::WindowsAndMessaging::{GWL_EXSTYLE, GWL_STYLE, GetWindowLongW};
    use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
    use winit::window::Window;

    use crate::sanitize;

    pub(super) fn outer_position_from_client_style(
        window: &Window,
        client_position: [f32; 2],
        dpi_scale: f32,
    ) -> Option<[f32; 2]> {
        let handle = window.window_handle().ok()?.as_raw();
        let RawWindowHandle::Win32(handle) = handle else {
            return None;
        };
        let hwnd = handle.hwnd.get() as windows_sys::Win32::Foundation::HWND;
        let style = unsafe { GetWindowLongW(hwnd, GWL_STYLE) } as u32;
        let ex_style = unsafe { GetWindowLongW(hwnd, GWL_EXSTYLE) } as u32;
        let dpi_scale = sanitize::positive_finite_f32_or(
            dpi_scale,
            sanitize::positive_finite_f32_or(window.scale_factor() as f32, 1.0),
        );
        let dpi = (dpi_scale * 96.0).round().clamp(1.0, u32::MAX as f32) as u32;
        let mut rect = RECT {
            left: client_position[0].round() as i32,
            top: client_position[1].round() as i32,
            right: client_position[0].round() as i32,
            bottom: client_position[1].round() as i32,
        };
        let adjusted = unsafe { AdjustWindowRectExForDpi(&mut rect, style, 0, ex_style, dpi) } != 0;
        adjusted.then_some([rect.left as f32, rect.top as f32])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn monitor_from_values(
        space: DesktopCoordinateSpace,
        position: [f64; 2],
        size: [f64; 2],
        scale: f64,
    ) -> dear_imgui_rs::sys::ImGuiPlatformMonitor {
        let position =
            desktop_position_from_physical_values(space, position, scale).unwrap_or([0.0, 0.0]);
        let size = desktop_size_from_physical_values(space, size, scale);
        dear_imgui_rs::sys::ImGuiPlatformMonitor {
            MainPos: dear_imgui_rs::sys::ImVec2 {
                x: position[0] as f32,
                y: position[1] as f32,
            },
            MainSize: dear_imgui_rs::sys::ImVec2 {
                x: size[0],
                y: size[1],
            },
            WorkPos: dear_imgui_rs::sys::ImVec2 {
                x: position[0] as f32,
                y: position[1] as f32,
            },
            WorkSize: dear_imgui_rs::sys::ImVec2 {
                x: size[0],
                y: size[1],
            },
            DpiScale: sanitize::positive_finite_f32_or(sanitized_scale(scale) as f32, 1.0),
            PlatformHandle: std::ptr::null_mut(),
        }
    }

    #[test]
    fn physical_desktop_monitors_keep_a_mixed_dpi_layout_contiguous() {
        let primary = monitor_from_values(
            DesktopCoordinateSpace::Physical,
            [0.0, 0.0],
            [1920.0, 1080.0],
            1.0,
        );
        let secondary = monitor_from_values(
            DesktopCoordinateSpace::Physical,
            [1920.0, 0.0],
            [2560.0, 1440.0],
            1.5,
        );

        assert_eq!(primary.MainPos.x, 0.0);
        assert_eq!(primary.MainSize.x, 1920.0);
        assert_eq!(secondary.MainPos.x, 1920.0);
        assert_eq!(secondary.MainSize.x, 2560.0);
        assert_eq!(secondary.DpiScale, 1.5);
    }

    #[test]
    fn monitor_conversion_preserves_negative_origins_and_sanitizes_scale() {
        let monitor = monitor_from_values(
            DesktopCoordinateSpace::Physical,
            [-1600.0, -200.0],
            [1600.0, 900.0],
            f64::NAN,
        );

        assert_eq!(monitor.MainPos.x, -1600.0);
        assert_eq!(monitor.MainPos.y, -200.0);
        assert_eq!(monitor.WorkPos, monitor.MainPos);
        assert_eq!(monitor.WorkSize, monitor.MainSize);
        assert_eq!(monitor.DpiScale, 1.0);
    }

    #[test]
    fn snapshot_conversion_preserves_distinct_main_and_work_rectangles() {
        use crate::native_support::{
            MonitorIdentity, MonitorSnapshot, PhysicalMonitorRect, WorkAreaProvenance,
        };

        let main = PhysicalMonitorRect::new([-1920.0, 0.0], [1920.0, 1080.0]).unwrap();
        let work = PhysicalMonitorRect::new([-1920.0, 45.0], [1920.0, 1035.0]).unwrap();
        let snapshot = MonitorSnapshot::from_test(
            MonitorIdentity::from_test_key("secondary"),
            main,
            work,
            1.5,
            WorkAreaProvenance::WindowsRcWork,
        );

        let monitor = monitor_from_snapshot(&snapshot).unwrap();
        let divisor = match native_desktop_coordinate_space() {
            DesktopCoordinateSpace::Physical => 1.0,
            DesktopCoordinateSpace::Logical => 1.5,
        };
        assert_eq!(monitor.MainPos.x, (-1920.0 / divisor) as f32);
        assert_eq!(monitor.MainPos.y, 0.0);
        assert_eq!(monitor.MainSize.x, (1920.0 / divisor) as f32);
        assert_eq!(monitor.MainSize.y, (1080.0 / divisor) as f32);
        assert_eq!(monitor.WorkPos.x, (-1920.0 / divisor) as f32);
        assert_eq!(monitor.WorkPos.y, (45.0 / divisor) as f32);
        assert_eq!(monitor.WorkSize.x, (1920.0 / divisor) as f32);
        assert_eq!(monitor.WorkSize.y, (1035.0 / divisor) as f32);
        assert_eq!(monitor.DpiScale, 1.5);
    }

    #[test]
    fn physical_client_input_uses_the_same_desktop_units_as_window_origins() {
        let position = client_to_screen_for_space(
            DesktopCoordinateSpace::Physical,
            [320.5, 72.0],
            [1920.0, -140.0],
            1.5,
        );
        assert_eq!(position, Some([2240.5, -68.0]));
    }

    #[test]
    fn logical_client_input_converts_both_origin_and_local_position_once() {
        let position = client_to_screen_for_space(
            DesktopCoordinateSpace::Logical,
            [300.0, 150.0],
            [2880.0, 600.0],
            1.5,
        );
        assert_eq!(position, Some([2120.0, 500.0]));
    }

    #[test]
    fn framebuffer_scale_is_independent_from_monitor_dpi_in_physical_space() {
        assert_eq!(
            framebuffer_scale_for_space(DesktopCoordinateSpace::Physical, 1.5),
            [1.0, 1.0]
        );
        assert_eq!(
            framebuffer_scale_for_space(DesktopCoordinateSpace::Logical, 2.0),
            [2.0, 2.0]
        );
    }

    #[test]
    fn single_window_metrics_restore_logical_units_after_native_desktop_units() {
        assert_eq!(
            single_window_display_metrics_from_physical(PhysicalSize::new(1200_u32, 900_u32), 1.5,),
            ([800.0, 600.0], [1.5, 1.5])
        );
    }

    #[test]
    fn physical_desktop_ime_area_converts_back_to_winit_client_logical_units() {
        let (position, size) = ime_cursor_area_for_space(
            DesktopCoordinateSpace::Physical,
            [2220.0, 360.0],
            [1920.0, 0.0],
            30.0,
            1.5,
        )
        .unwrap();

        assert_eq!(position, LogicalPosition::new(200.0, 240.0));
        assert_eq!(size, LogicalSize::new(20.0, 20.0));
    }

    #[test]
    fn undecorated_client_origin_needs_no_outer_position_offset() {
        assert_eq!(
            outer_position_from_decoration_offset([2447.0, 802.0], [0.0, 0.0]),
            Some([2447.0, 802.0])
        );
    }

    #[test]
    fn observed_decoration_offset_positions_the_client_origin() {
        assert_eq!(
            outer_position_from_decoration_offset([2447.0, 802.0], [11.0, 45.0]),
            Some([2436.0, 757.0])
        );
    }

    #[test]
    fn scale_factor_change_preserves_the_backends_desktop_size_unit() {
        let current = PhysicalSize::new(800, 600);
        assert_eq!(
            scale_factor_inner_size_override_for_space(
                DesktopCoordinateSpace::Physical,
                false,
                current,
            ),
            Some(current)
        );
        assert_eq!(
            scale_factor_inner_size_override_for_space(
                DesktopCoordinateSpace::Physical,
                true,
                current,
            ),
            None
        );
        assert_eq!(
            scale_factor_inner_size_override_for_space(
                DesktopCoordinateSpace::Logical,
                false,
                current,
            ),
            None
        );
    }

    fn client_to_screen_for_space(
        space: DesktopCoordinateSpace,
        client: [f64; 2],
        origin: [f64; 2],
        scale: f64,
    ) -> Option<[f32; 2]> {
        let client = desktop_position_from_physical_values(space, client, scale)?;
        let origin = desktop_position_from_physical_values(space, origin, scale)?;
        sanitize::finite_vec2_f64_to_f32([origin[0] + client[0], origin[1] + client[1]])
    }

    fn ime_cursor_area_for_space(
        space: DesktopCoordinateSpace,
        input_position: [f32; 2],
        viewport_position: [f32; 2],
        line_height: f32,
        scale: f64,
    ) -> Option<(LogicalPosition<f64>, LogicalSize<f64>)> {
        let input_position = sanitize::finite_vec2_f32(input_position)?;
        let viewport_position = sanitize::finite_vec2_f32(viewport_position)?;
        let mut client_position = [
            f64::from(input_position[0] - viewport_position[0]),
            f64::from(input_position[1] - viewport_position[1]),
        ];
        let mut line_height = f64::from(line_height);
        if space == DesktopCoordinateSpace::Physical {
            let scale = sanitized_scale(scale);
            client_position[0] /= scale;
            client_position[1] /= scale;
            line_height /= scale;
        }
        Some((
            LogicalPosition::new(client_position[0], client_position[1]),
            LogicalSize::new(line_height, line_height),
        ))
    }
}