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
use super::super::coordinates::monitor_from_snapshot;
use super::*;
use crate::multi_viewport::{
    WinitMonitorCollectionFailure, WinitMonitorPublicationReport, WinitMonitorPublicationState,
};
use crate::native_support::{MonitorSnapshot, collect_monitor_snapshot_set};
use std::cmp::Ordering;

#[derive(Clone, Copy)]
struct MonitorVectorState {
    data: *mut dear_imgui_rs::sys::ImGuiPlatformMonitor,
    size: i32,
    capacity: i32,
}

impl MonitorVectorState {
    unsafe fn from_platform_io(raw: *mut dear_imgui_rs::sys::ImGuiPlatformIO) -> Self {
        let monitors = unsafe { &(*raw).Monitors };
        Self {
            data: monitors.Data,
            size: monitors.Size,
            capacity: monitors.Capacity,
        }
    }

    unsafe fn install_into(self, raw: *mut dear_imgui_rs::sys::ImGuiPlatformIO) {
        let monitors = unsafe { &mut (*raw).Monitors };
        monitors.Data = self.data;
        monitors.Size = self.size;
        monitors.Capacity = self.capacity;
    }

    unsafe fn matches(self, raw: *mut dear_imgui_rs::sys::ImGuiPlatformIO) -> bool {
        let monitors = unsafe { &(*raw).Monitors };
        monitors.Data == self.data
            && monitors.Size == self.size
            && monitors.Capacity == self.capacity
    }

    unsafe fn free(self) {
        if !self.data.is_null() {
            unsafe { dear_imgui_rs::sys::igMemFree(self.data.cast()) };
        }
    }
}

pub(in super::super) struct PreparedMonitors {
    storage: Option<MonitorVectorState>,
    facts: Option<Vec<MonitorSnapshot>>,
    values: Vec<dear_imgui_rs::sys::ImGuiPlatformMonitor>,
    state: WinitMonitorPublicationState,
}

impl PreparedMonitors {
    fn allocate(
        context: &Context,
        facts: Option<Vec<MonitorSnapshot>>,
        monitors: &[dear_imgui_rs::sys::ImGuiPlatformMonitor],
        state: WinitMonitorPublicationState,
    ) -> Result<Self, WinitPlatformError> {
        validate_monitors(monitors)?;
        let count =
            i32::try_from(monitors.len()).map_err(|_| WinitPlatformError::MonitorCountOverflow)?;
        let byte_len = std::mem::size_of_val(monitors);
        let data = context.binding().with_bound_context(|| unsafe {
            dear_imgui_rs::sys::igMemAlloc(byte_len)
                .cast::<dear_imgui_rs::sys::ImGuiPlatformMonitor>()
        });
        if data.is_null() {
            return Err(WinitPlatformError::MonitorStorageAllocationFailed);
        }
        unsafe { data.copy_from_nonoverlapping(monitors.as_ptr(), monitors.len()) };
        Ok(Self {
            storage: Some(MonitorVectorState {
                data,
                size: count,
                capacity: count,
            }),
            facts,
            values: monitors.to_vec(),
            state,
        })
    }

    fn take_storage(&mut self) -> MonitorVectorState {
        self.storage
            .take()
            .expect("prepared monitor storage can only be published once")
    }

    fn take_publication(
        &mut self,
    ) -> (
        MonitorVectorState,
        Option<Vec<MonitorSnapshot>>,
        Vec<dear_imgui_rs::sys::ImGuiPlatformMonitor>,
        WinitMonitorPublicationState,
    ) {
        (
            self.take_storage(),
            self.facts.take(),
            std::mem::take(&mut self.values),
            self.state,
        )
    }
}

impl Drop for PreparedMonitors {
    fn drop(&mut self) {
        if let Some(storage) = self.storage.take() {
            unsafe { storage.free() };
        }
    }
}

pub(in super::super) struct MonitorOwnership {
    prior: MonitorVectorState,
    installed: MonitorVectorState,
    facts: Option<Vec<MonitorSnapshot>>,
    values: Vec<dear_imgui_rs::sys::ImGuiPlatformMonitor>,
    state: WinitMonitorPublicationState,
}

impl MonitorOwnership {
    pub(in super::super) unsafe fn installed_matches(
        &self,
        raw: *mut dear_imgui_rs::sys::ImGuiPlatformIO,
    ) -> bool {
        unsafe { self.installed.matches(raw) }
    }

    unsafe fn replace_installed(
        &mut self,
        raw: *mut dear_imgui_rs::sys::ImGuiPlatformIO,
        mut prepared: PreparedMonitors,
    ) -> Result<(), WinitPlatformError> {
        if !unsafe { self.installed.matches(raw) } {
            return Err(WinitPlatformError::PlatformStateReplaced {
                field: "PlatformIO.Monitors",
            });
        }
        let (replacement, facts, values, state) = prepared.take_publication();
        unsafe { replacement.install_into(raw) };
        let previous = std::mem::replace(&mut self.installed, replacement);
        self.facts = facts;
        self.values = values;
        self.state = state;
        unsafe { previous.free() };
        Ok(())
    }

    pub(in super::super) fn report(&self) -> WinitMonitorPublicationReport {
        WinitMonitorPublicationReport::new(self.state, self.facts.clone())
    }

    fn retain_after_failure(&mut self, reason: WinitMonitorCollectionFailure) {
        self.state = WinitMonitorPublicationState::RetainedAfterCollectionFailure { reason };
    }

    pub(in super::super) unsafe fn restore_if_owned(
        self,
        raw: *mut dear_imgui_rs::sys::ImGuiPlatformIO,
    ) {
        if unsafe { self.installed.matches(raw) } {
            unsafe { self.prior.install_into(raw) };
            unsafe { self.installed.free() };
        } else if unsafe { self.prior.matches(raw) } {
            // An allocator-aware foreign replacement may have freed Winit's allocation before
            // reproducing the prior state (most commonly the empty vector). It is therefore not
            // safe to free the detached pointer again. A direct raw replacement can leak Winit's
            // allocation, but never turns uncertain ownership into a double free.
        } else {
            // A foreign owner replaced the vector through the allocator-aware API. That operation
            // released our installed allocation, so only the detached prior allocation remains.
            unsafe { self.prior.free() };
        }
    }

    pub(in super::super) unsafe fn context_destroyed(self) {
        // Dear ImGui released whichever vector remained installed. The prior allocation was
        // detached from native ownership when Winit published its monitor list.
        unsafe { self.prior.free() };
    }
}

pub(in super::super) fn prepare_monitors(
    context: &Context,
    window: &winit::window::Window,
) -> Result<PreparedMonitors, WinitPlatformError> {
    let publication = match collect_monitor_publication(window) {
        MonitorCollection::Available(publication) => publication,
        MonitorCollection::Unavailable(reason) => {
            return Err(WinitPlatformError::MonitorCollectionUnavailable { reason });
        }
    };
    PreparedMonitors::allocate(
        context,
        publication.facts,
        &publication.values,
        WinitMonitorPublicationState::NativeSnapshot,
    )
}

#[derive(Clone, Debug, PartialEq)]
struct MonitorPublication {
    facts: Option<Vec<MonitorSnapshot>>,
    values: Vec<dear_imgui_rs::sys::ImGuiPlatformMonitor>,
}

enum MonitorCollection {
    Available(MonitorPublication),
    Unavailable(WinitMonitorCollectionFailure),
}

fn snapshot_order(left: &MonitorSnapshot, right: &MonitorSnapshot) -> Ordering {
    left.identity()
        .cmp(right.identity())
        .then_with(|| compare_f64_pair(left.main().position(), right.main().position()))
        .then_with(|| compare_f64_pair(left.main().size(), right.main().size()))
        .then_with(|| left.scale_factor().total_cmp(&right.scale_factor()))
}

fn compare_f64_pair(left: [f64; 2], right: [f64; 2]) -> Ordering {
    left[0]
        .total_cmp(&right[0])
        .then_with(|| left[1].total_cmp(&right[1]))
}

fn normalize_snapshots(
    mut snapshots: Vec<MonitorSnapshot>,
    primary: Option<&crate::native_support::MonitorIdentity>,
) -> Vec<MonitorSnapshot> {
    snapshots.sort_by(snapshot_order);
    // Only remove exact duplicate facts. Detached fallback identities can collide for identical
    // displays; dropping a distinct work rectangle would silently lose native evidence.
    snapshots.dedup_by(|left, right| left == right);
    if let Some(primary) = primary
        && let Some(index) = snapshots
            .iter()
            .position(|snapshot| snapshot.identity() == primary)
    {
        let primary = snapshots.remove(index);
        snapshots.insert(0, primary);
    }
    snapshots
}

fn collect_monitor_publication(window: &winit::window::Window) -> MonitorCollection {
    let publication = match collect_monitor_snapshot_set(window) {
        Ok(publication) => publication,
        Err(error) => {
            return MonitorCollection::Unavailable(WinitMonitorCollectionFailure::Native(error));
        }
    };
    let (snapshots, primary) = publication.into_parts();
    monitor_collection_from_snapshots(snapshots, primary.as_ref())
}

fn monitor_collection_from_snapshots(
    snapshots: Vec<MonitorSnapshot>,
    primary: Option<&crate::native_support::MonitorIdentity>,
) -> MonitorCollection {
    if snapshots.is_empty() {
        return MonitorCollection::Unavailable(WinitMonitorCollectionFailure::EmptyCollection);
    }
    if snapshots.len() > 1 && primary.is_none() {
        return MonitorCollection::Unavailable(
            WinitMonitorCollectionFailure::PrimaryIdentityUnproven,
        );
    }
    let snapshots = normalize_snapshots(snapshots, primary);
    if snapshots.is_empty() {
        return MonitorCollection::Unavailable(WinitMonitorCollectionFailure::EmptyCollection);
    }
    let Some(values) = snapshots
        .iter()
        .map(monitor_from_snapshot)
        .collect::<Option<Vec<_>>>()
    else {
        return MonitorCollection::Unavailable(WinitMonitorCollectionFailure::ProjectionInvalid);
    };
    if validate_monitors(&values).is_err() {
        return MonitorCollection::Unavailable(WinitMonitorCollectionFailure::ProjectionInvalid);
    }
    MonitorCollection::Available(MonitorPublication {
        facts: Some(snapshots),
        values,
    })
}

pub(in super::super) fn refresh_monitors(
    context: &Context,
    window: &winit::window::Window,
    ownership: &mut MonitorOwnership,
) -> Result<bool, WinitPlatformError> {
    refresh_monitor_collection(context, collect_monitor_publication(window), ownership)
}

fn refresh_monitor_collection(
    context: &Context,
    collection: MonitorCollection,
    ownership: &mut MonitorOwnership,
) -> Result<bool, WinitPlatformError> {
    let publication = match collection {
        MonitorCollection::Available(publication) => publication,
        MonitorCollection::Unavailable(reason) => {
            ownership.retain_after_failure(reason);
            return Ok(false);
        }
    };
    refresh_published_monitors(context, publication, ownership)
}

fn refresh_published_monitors(
    context: &Context,
    publication: MonitorPublication,
    ownership: &mut MonitorOwnership,
) -> Result<bool, WinitPlatformError> {
    validate_monitors(&publication.values)?;
    let raw = unsafe { dear_imgui_rs::sys::igGetPlatformIO_Nil() };
    if raw.is_null() {
        return Err(WinitPlatformError::ContextMismatch);
    }
    if !unsafe { ownership.installed.matches(raw) } {
        return Err(WinitPlatformError::PlatformStateReplaced {
            field: "PlatformIO.Monitors",
        });
    }
    if ownership.facts == publication.facts && ownership.values == publication.values {
        ownership.state = WinitMonitorPublicationState::NativeSnapshot;
        return Ok(false);
    }
    let prepared = PreparedMonitors::allocate(
        context,
        publication.facts,
        &publication.values,
        WinitMonitorPublicationState::NativeSnapshot,
    )?;
    unsafe { ownership.replace_installed(raw, prepared)? };
    Ok(true)
}

#[cfg(test)]
pub(in super::super) fn refresh_monitors_for_test(
    context: &Context,
    monitors: &[dear_imgui_rs::sys::ImGuiPlatformMonitor],
    ownership: &mut MonitorOwnership,
) -> Result<bool, WinitPlatformError> {
    refresh_published_monitors(
        context,
        MonitorPublication {
            facts: None,
            values: monitors.to_vec(),
        },
        ownership,
    )
}

#[cfg(test)]
pub(in super::super) fn refresh_monitor_snapshots_for_test(
    context: &Context,
    snapshots: Option<Vec<MonitorSnapshot>>,
    ownership: &mut MonitorOwnership,
) -> Result<bool, WinitPlatformError> {
    let collection = snapshots
        .map(|snapshots| monitor_collection_from_snapshots(snapshots, None))
        .unwrap_or(MonitorCollection::Unavailable(
            WinitMonitorCollectionFailure::Native(
                crate::native_support::MonitorCollectionError::MainFactsUnavailable { monitor: 0 },
            ),
        ));
    refresh_monitor_collection(context, collection, ownership)
}

#[cfg(test)]
pub(in super::super) fn prepare_monitors_for_test(
    context: &Context,
    monitors: Vec<dear_imgui_rs::sys::ImGuiPlatformMonitor>,
) -> Result<PreparedMonitors, WinitPlatformError> {
    PreparedMonitors::allocate(
        context,
        None,
        &monitors,
        WinitMonitorPublicationState::NativeSnapshot,
    )
}

pub(in super::super) fn publish_monitors(
    context: &mut Context,
    mut prepared: PreparedMonitors,
) -> MonitorOwnership {
    context.binding().with_bound_context(|| unsafe {
        let raw = context.platform_io_mut().as_raw_mut();
        let prior = MonitorVectorState::from_platform_io(raw);
        let (installed, facts, values, state) = prepared.take_publication();
        installed.install_into(raw);
        MonitorOwnership {
            prior,
            installed,
            facts,
            values,
            state,
        }
    })
}

fn validate_monitors(
    monitors: &[dear_imgui_rs::sys::ImGuiPlatformMonitor],
) -> Result<(), WinitPlatformError> {
    if monitors.is_empty() {
        return Err(WinitPlatformError::NoMonitors);
    }
    for (monitor, value) in monitors.iter().enumerate() {
        let values = [
            value.MainPos.x,
            value.MainPos.y,
            value.MainSize.x,
            value.MainSize.y,
            value.WorkPos.x,
            value.WorkPos.y,
            value.WorkSize.x,
            value.WorkSize.y,
            value.DpiScale,
        ];
        if !values.iter().all(|value| value.is_finite()) {
            return Err(WinitPlatformError::InvalidMonitorGeometry {
                monitor,
                reason: "geometry and DPI values must be finite",
            });
        }
        if value.MainSize.x <= 0.0 || value.MainSize.y <= 0.0 {
            return Err(WinitPlatformError::InvalidMonitorGeometry {
                monitor,
                reason: "MainSize must be positive",
            });
        }
        if value.WorkSize.x < 0.0 || value.WorkSize.y < 0.0 {
            return Err(WinitPlatformError::InvalidMonitorGeometry {
                monitor,
                reason: "WorkSize must not be negative",
            });
        }

        let main_max = [
            value.MainPos.x + value.MainSize.x,
            value.MainPos.y + value.MainSize.y,
        ];
        let work_max = [
            value.WorkPos.x + value.WorkSize.x,
            value.WorkPos.y + value.WorkSize.y,
        ];
        if !main_max
            .iter()
            .chain(work_max.iter())
            .all(|value| value.is_finite())
        {
            return Err(WinitPlatformError::InvalidMonitorGeometry {
                monitor,
                reason: "geometry bounds must not overflow",
            });
        }
        if value.WorkPos.x < value.MainPos.x
            || value.WorkPos.y < value.MainPos.y
            || work_max[0] > main_max[0]
            || work_max[1] > main_max[1]
        {
            return Err(WinitPlatformError::InvalidMonitorGeometry {
                monitor,
                reason: "work area must be contained within the main area",
            });
        }
        if value.DpiScale <= 0.0 || value.DpiScale >= 99.0 {
            return Err(WinitPlatformError::InvalidMonitorGeometry {
                monitor,
                reason: "DpiScale must be greater than 0 and less than 99",
            });
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{
        MonitorCollection, MonitorSnapshot, monitor_collection_from_snapshots, normalize_snapshots,
    };
    use crate::multi_viewport::WinitMonitorCollectionFailure;
    use crate::native_support::{
        MonitorIdentity, PhysicalMonitorRect, WorkAreaFallback, WorkAreaProvenance,
    };

    #[test]
    fn detached_primary_identity_is_promoted_without_fabrication() {
        let main = PhysicalMonitorRect::new([0.0, 0.0], [1920.0, 1080.0]).unwrap();
        let primary = MonitorSnapshot::from_test(
            MonitorIdentity::from_test_key("primary"),
            main,
            main,
            1.0,
            WorkAreaProvenance::FullMain(WorkAreaFallback::SourceUnavailable),
        );
        let secondary_main = PhysicalMonitorRect::new([1920.0, 0.0], [1920.0, 1080.0]).unwrap();
        let secondary = MonitorSnapshot::from_test(
            MonitorIdentity::from_test_key("secondary"),
            secondary_main,
            secondary_main,
            1.0,
            WorkAreaProvenance::FullMain(WorkAreaFallback::SourceUnavailable),
        );

        let primary_identity = MonitorIdentity::from_test_key("primary");
        let snapshots = normalize_snapshots(vec![secondary, primary], Some(&primary_identity));
        assert_eq!(snapshots[0].identity(), &primary_identity);
        assert_eq!(snapshots.len(), 2);
    }

    #[test]
    fn multiple_monitors_require_a_proven_primary_but_one_monitor_does_not() {
        let main = PhysicalMonitorRect::new([0.0, 0.0], [1920.0, 1080.0]).unwrap();
        let primary = MonitorSnapshot::from_test(
            MonitorIdentity::from_test_key("primary"),
            main,
            main,
            1.0,
            WorkAreaProvenance::FullMain(WorkAreaFallback::SourceUnavailable),
        );
        let secondary_main = PhysicalMonitorRect::new([1920.0, 0.0], [1920.0, 1080.0]).unwrap();
        let secondary = MonitorSnapshot::from_test(
            MonitorIdentity::from_test_key("secondary"),
            secondary_main,
            secondary_main,
            1.0,
            WorkAreaProvenance::FullMain(WorkAreaFallback::SourceUnavailable),
        );

        assert!(matches!(
            monitor_collection_from_snapshots(vec![primary.clone(), secondary], None),
            MonitorCollection::Unavailable(WinitMonitorCollectionFailure::PrimaryIdentityUnproven),
        ));
        assert!(matches!(
            monitor_collection_from_snapshots(vec![primary], None),
            MonitorCollection::Available(_),
        ));
    }
}