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
//! Main platform implementation for Dear ImGui winit backend
//!
//! This module contains the core `WinitPlatform` struct and its implementation
//! for integrating Dear ImGui with winit windowing.
use std::ffi::c_void;
use dear_imgui_rs::{BackendFlags, ConfigFlags, Context};
use winit::dpi::{LogicalPosition, LogicalSize};
use winit::event::{Event, WindowEvent};
use winit::window::{Window, WindowAttributes};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;
use crate::cursor::CursorSettings;
use crate::events;
type SetImeDataCallback = unsafe extern "C" fn(
*mut dear_imgui_rs::sys::ImGuiContext,
*mut dear_imgui_rs::sys::ImGuiViewport,
*mut dear_imgui_rs::sys::ImGuiPlatformImeData,
);
/// IME hook: Dear ImGui calls this when the text input caret moves. We forward
/// the position to winit so platforms that support it can position the IME
/// candidate/composition window near the caret.
unsafe extern "C" fn imgui_winit_set_ime_data(
ctx: *mut dear_imgui_rs::sys::ImGuiContext,
viewport: *mut dear_imgui_rs::sys::ImGuiViewport,
data: *mut dear_imgui_rs::sys::ImGuiPlatformImeData,
) {
use dear_imgui_rs::sys::{ImGuiPlatformImeData, ImGuiViewport};
let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| unsafe {
if viewport.is_null() || data.is_null() {
return;
}
// Retrieve the window pointer we stored in Platform_ImeUserData.
let pio = platform_io_for_ime_context(ctx);
if pio.is_null() {
return;
}
let user_data = (*pio).Platform_ImeUserData as *const Window;
if user_data.is_null() {
return;
}
// Safety: we rely on the application to keep the Window alive while the
// ImGui context is active. This matches typical winit usage.
let window: &Window = &*user_data;
let ime: &ImGuiPlatformImeData = &*data;
let vp: &ImGuiViewport = &*viewport;
// If IME is not visible and not expecting text input, there's nothing to do.
if !ime.WantVisible && !ime.WantTextInput {
return;
}
// Dear ImGui gives InputPos in the same coordinate space as the viewport's
// Pos. Convert to client-area coordinates by subtracting viewport origin.
let rel_x = (ime.InputPos.x - vp.Pos.x) as f64;
let rel_y = (ime.InputPos.y - vp.Pos.y) as f64;
let pos = LogicalPosition::new(rel_x, rel_y);
// Use the reported line height as a reasonable IME region height. Width is
// not very important for most IME implementations.
let line_h = if ime.InputLineHeight > 0.0 {
ime.InputLineHeight as f64
} else {
16.0
};
let size = LogicalSize::new(line_h, line_h);
window.set_ime_cursor_area(pos, size);
}));
if res.is_err() {
eprintln!("dear-imgui-winit: panic in Platform_SetImeDataFn");
std::process::abort();
}
}
fn is_winit_set_ime_data(callback: Option<SetImeDataCallback>) -> bool {
callback.is_some_and(|callback| {
std::ptr::fn_addr_eq(callback, imgui_winit_set_ime_data as SetImeDataCallback)
})
}
unsafe fn platform_io_for_ime_context(
ctx: *mut dear_imgui_rs::sys::ImGuiContext,
) -> *mut dear_imgui_rs::sys::ImGuiPlatformIO {
if ctx.is_null() {
unsafe { dear_imgui_rs::sys::igGetPlatformIO_Nil() }
} else {
unsafe { dear_imgui_rs::sys::igGetPlatformIO_ContextPtr(ctx) }
}
}
/// DPI scaling mode for the platform
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub enum HiDpiMode {
/// Use the default DPI scaling
#[default]
Default,
/// Use a custom scale factor
Locked(f64),
/// Round the scale factor to the nearest integer
Rounded,
}
/// Main platform backend for Dear ImGui with winit integration
pub struct WinitPlatform {
hidpi_mode: HiDpiMode,
hidpi_factor: f64,
cursor_cache: Option<CursorSettings>,
ime_enabled: bool,
ime_auto_manage: bool,
last_frame: Instant,
}
impl WinitPlatform {
/// Create a new winit platform backend
///
/// # Example
///
/// ```
/// use dear_imgui_rs::Context;
/// use dear_imgui_winit::WinitPlatform;
///
/// let mut imgui_ctx = Context::create();
/// let mut platform = WinitPlatform::new(&mut imgui_ctx);
/// ```
pub fn new(imgui_ctx: &mut Context) -> Self {
// Set backend platform name for diagnostics before borrowing Io
let _ = imgui_ctx.set_platform_name(Some(format!(
"dear-imgui-winit {}",
env!("CARGO_PKG_VERSION")
)));
let io = imgui_ctx.io_mut();
// Set backend flags
let mut backend_flags = io.backend_flags();
backend_flags.insert(BackendFlags::HAS_MOUSE_CURSORS | BackendFlags::HAS_SET_MOUSE_POS);
#[cfg(feature = "multi-viewport")]
{
// Mark that this platform backend is capable of handling viewports.
// Note: we intentionally DO NOT enable `ConfigFlags::VIEWPORTS_ENABLE` here.
// Multi-viewport is an opt-in feature and should be enabled explicitly via:
//
// imgui_ctx.enable_multi_viewport();
//
// This matches Dear ImGui's guidance and avoids partially-enabled viewport
// behavior when the renderer/platform callbacks are not fully wired.
backend_flags.insert(BackendFlags::PLATFORM_HAS_VIEWPORTS);
// Backend can also report hovered viewport ids via `Io::add_mouse_viewport_event`.
backend_flags.insert(BackendFlags::HAS_MOUSE_HOVERED_VIEWPORT);
// We keep `HAS_SET_MOUSE_POS` flag: `prepare_render()` will avoid using it
// whenever `ConfigFlags::VIEWPORTS_ENABLE` is actually set.
}
io.set_backend_flags(backend_flags);
Self {
hidpi_mode: HiDpiMode::default(),
hidpi_factor: 1.0,
cursor_cache: None,
ime_enabled: false,
ime_auto_manage: true,
last_frame: Instant::now(),
}
}
/// Set the DPI scaling mode
pub fn set_hidpi_mode(&mut self, hidpi_mode: HiDpiMode) {
self.hidpi_mode = hidpi_mode;
}
/// Enable or disable IME events for the attached window.
///
/// Winit does not deliver `WindowEvent::Ime` events unless IME is explicitly
/// allowed on the window. When `ime_auto_manage` is enabled (default), the
/// backend will override this based on `io.want_text_input()` every frame.
/// Use this helper for immediate overrides (e.g. when auto-management is
/// disabled or you want to force a specific state for a while).
pub fn set_ime_allowed(&mut self, window: &Window, allowed: bool) {
window.set_ime_allowed(allowed);
self.ime_enabled = allowed;
}
/// Returns whether IME is currently allowed for the attached window.
///
/// This reflects the last state set via `set_ime_allowed` or IME
/// `WindowEvent::Ime(Enabled/Disabled)` notifications.
pub fn ime_enabled(&self) -> bool {
self.ime_enabled
}
/// Enable or disable automatic IME management.
///
/// When enabled (default), the backend will call `set_ime_allowed` based on
/// Dear ImGui's `io.want_text_input()` flag each frame, turning IME on
/// while text widgets are active and off otherwise. When disabled, IME
/// state is left entirely under application control.
pub fn set_ime_auto_management(&mut self, enabled: bool) {
self.ime_auto_manage = enabled;
}
/// Get the current DPI scaling factor
pub fn hidpi_factor(&self) -> f64 {
self.hidpi_factor
}
/// Attach the platform to a window
pub fn attach_window(
&mut self,
window: &Window,
hidpi_mode: HiDpiMode,
imgui_ctx: &mut Context,
) {
self.hidpi_mode = hidpi_mode;
self.hidpi_factor = match hidpi_mode {
HiDpiMode::Default => window.scale_factor(),
HiDpiMode::Locked(factor) => factor,
HiDpiMode::Rounded => window.scale_factor().round(),
};
// Convert via winit scale then adapt to our active HiDPI mode
let logical_size = window.inner_size().to_logical(window.scale_factor());
let logical_size = self.scale_size_from_winit(window, logical_size);
let io = imgui_ctx.io_mut();
io.set_display_size([logical_size.width as f32, logical_size.height as f32]);
io.set_display_framebuffer_scale([self.hidpi_factor as f32, self.hidpi_factor as f32]);
// Enable IME by default so WindowEvent::Ime events and IME composition
// are available on desktop platforms. Auto-management (when enabled)
// will further refine this for text widgets.
self.set_ime_allowed(window, true);
// Register Dear ImGui -> winit IME bridge so text input widgets can
// move the platform IME candidate/composition window near the caret.
unsafe {
let pio = imgui_ctx.platform_io_mut().as_raw_mut();
if !pio.is_null() {
if (*pio).Platform_SetImeDataFn.is_none() {
(*pio).Platform_SetImeDataFn = Some(imgui_winit_set_ime_data);
(*pio).Platform_ImeUserData = window as *const Window as *mut c_void;
} else if is_winit_set_ime_data((*pio).Platform_SetImeDataFn) {
(*pio).Platform_ImeUserData = window as *const Window as *mut c_void;
}
}
}
}
/// Detach the platform from a window and clear winit-owned IME hooks.
///
/// Call this before destroying a window when the Dear ImGui context will outlive it. The
/// method only clears the IME callback/userdata pair if it is still owned by this backend and
/// still points at `window`.
pub fn detach_window(&mut self, window: &Window, imgui_ctx: &mut Context) {
window.set_ime_allowed(false);
self.ime_enabled = false;
unsafe {
let pio = imgui_ctx.platform_io_mut().as_raw_mut();
if pio.is_null() || !is_winit_set_ime_data((*pio).Platform_SetImeDataFn) {
return;
}
let window_ptr = window as *const Window as *mut c_void;
if (*pio).Platform_ImeUserData == window_ptr {
(*pio).Platform_ImeUserData = std::ptr::null_mut();
(*pio).Platform_SetImeDataFn = None;
}
}
}
/// Handle a winit event.
///
/// This is the most general entry point: pass the full `Event<T>` from
/// your event loop and the backend will dispatch to the appropriate
/// handlers. For `ApplicationHandler::window_event`, where you already
/// receive a `WindowEvent` for a specific window, you can use
/// `handle_window_event` instead and avoid constructing a synthetic
/// `Event::WindowEvent`.
pub fn handle_event<T>(
&mut self,
imgui_ctx: &mut Context,
window: &Window,
event: &Event<T>,
) -> bool {
match event {
Event::WindowEvent { event, .. } => {
self.handle_window_event_internal(imgui_ctx, window, event)
}
Event::DeviceEvent { event, .. } => {
events::handle_device_event(event);
false
}
_ => false,
}
}
/// Handle a single window event for a given window.
///
/// This is a convenience wrapper for frameworks that already route
/// window-local events, such as winit's `ApplicationHandler::window_event`,
/// and don't need to build a full `Event::WindowEvent` value.
pub fn handle_window_event(
&mut self,
imgui_ctx: &mut Context,
window: &Window,
event: &WindowEvent,
) -> bool {
self.handle_window_event_internal(imgui_ctx, window, event)
}
/// Internal implementation for window event handling.
fn handle_window_event_internal(
&mut self,
imgui_ctx: &mut Context,
window: &Window,
event: &WindowEvent,
) -> bool {
match event {
WindowEvent::Resized(physical_size) => {
let logical_size = physical_size.to_logical(window.scale_factor());
let logical_size = self.scale_size_from_winit(window, logical_size);
imgui_ctx
.io_mut()
.set_display_size([logical_size.width as f32, logical_size.height as f32]);
false
}
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
let new_hidpi = match self.hidpi_mode {
HiDpiMode::Default => *scale_factor,
HiDpiMode::Locked(factor) => factor,
HiDpiMode::Rounded => scale_factor.round(),
};
// Adjust mouse position proportionally when DPI factor changes
{
let io = imgui_ctx.io_mut();
let mouse = io.mouse_pos();
if mouse[0].is_finite() && mouse[1].is_finite() && self.hidpi_factor > 0.0 {
let scale = (new_hidpi / self.hidpi_factor) as f32;
io.set_mouse_pos([mouse[0] * scale, mouse[1] * scale]);
}
}
self.hidpi_factor = new_hidpi;
let logical_size = window.inner_size().to_logical(window.scale_factor());
let logical_size = self.scale_size_from_winit(window, logical_size);
let io = imgui_ctx.io_mut();
io.set_display_size([logical_size.width as f32, logical_size.height as f32]);
io.set_display_framebuffer_scale([
self.hidpi_factor as f32,
self.hidpi_factor as f32,
]);
false
}
WindowEvent::KeyboardInput { event, .. } => {
events::handle_keyboard_input(event, imgui_ctx)
}
WindowEvent::CursorMoved { position, .. } => {
// With multi-viewports enabled, feed absolute/screen coordinates like upstream backends
#[cfg(feature = "multi-viewport")]
{
if imgui_ctx
.io()
.config_flags()
.contains(dear_imgui_rs::ConfigFlags::VIEWPORTS_ENABLE)
{
// Main window always maps to the main Dear ImGui viewport.
let main_viewport_id = imgui_ctx.main_viewport().id();
imgui_ctx
.io_mut()
.add_mouse_viewport_event(main_viewport_id.into());
// Feed absolute/screen coordinates in logical pixels, matching io.DisplaySize.
let scale = window.scale_factor();
let pos_logical = position.to_logical::<f64>(scale);
if let Ok(base_phys) = window.inner_position() {
let base_logical = base_phys.to_logical::<f64>(scale);
let sx = base_logical.x + pos_logical.x;
let sy = base_logical.y + pos_logical.y;
return events::handle_cursor_moved([sx, sy], imgui_ctx);
} else if let Ok(base_phys) = window.outer_position() {
let base_logical = base_phys.to_logical::<f64>(scale);
let sx = base_logical.x + pos_logical.x;
let sy = base_logical.y + pos_logical.y;
return events::handle_cursor_moved([sx, sy], imgui_ctx);
}
}
}
// Fallback: local logical coordinates
let position = position.to_logical(window.scale_factor());
let position = self.scale_pos_from_winit(window, position);
events::handle_cursor_moved([position.x, position.y], imgui_ctx)
}
WindowEvent::MouseInput { button, state, .. } => {
events::handle_mouse_button(*button, *state, imgui_ctx)
}
WindowEvent::MouseWheel { delta, .. } => events::handle_mouse_wheel(*delta, imgui_ctx),
// When cursor leaves the window, tell ImGui the mouse is unavailable so
// software cursor (if enabled) won’t be drawn at the last position.
WindowEvent::CursorLeft { .. } => {
{
let io = imgui_ctx.io_mut();
io.add_mouse_pos_event([-f32::MAX, -f32::MAX]);
// No Dear ImGui viewport is hovered anymore.
io.add_mouse_viewport_event(dear_imgui_rs::Id::default());
}
false
}
WindowEvent::ModifiersChanged(modifiers) => {
events::handle_modifiers_changed(modifiers, imgui_ctx);
false
}
WindowEvent::Ime(ime) => {
events::handle_ime_event(ime, imgui_ctx);
// Track IME enabled/disabled state based on winit notifications.
self.ime_enabled = !matches!(ime, winit::event::Ime::Disabled);
imgui_ctx.io().want_capture_keyboard()
}
WindowEvent::Touch(touch) => {
events::handle_touch_event(touch, window, imgui_ctx);
imgui_ctx.io().want_capture_mouse()
}
WindowEvent::Focused(focused) => events::handle_focused(*focused, imgui_ctx),
_ => false,
}
}
/// Prepare for rendering - should be called before Dear ImGui rendering
pub fn prepare_render(&mut self, imgui_ctx: &mut Context, window: &Window) {
let now = Instant::now();
let delta = now - self.last_frame;
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
self.last_frame = now;
imgui_ctx.io_mut().set_delta_time(delta_s);
// In multi-viewport mode, keep DisplaySize/FramebufferScale in sync every frame.
// This matches upstream backends (SDL/GLFW) and avoids stale or spurious DPI
// changes affecting the main viewport after platform windows are moved.
#[cfg(feature = "multi-viewport")]
{
if imgui_ctx
.io()
.config_flags()
.contains(ConfigFlags::VIEWPORTS_ENABLE)
{
let winit_scale = window.scale_factor();
let hidpi = match self.hidpi_mode {
HiDpiMode::Default => winit_scale,
HiDpiMode::Locked(factor) => factor,
HiDpiMode::Rounded => winit_scale.round(),
};
self.hidpi_factor = hidpi;
let logical_size = window.inner_size().to_logical(winit_scale);
let logical_size = self.scale_size_from_winit(window, logical_size);
let io = imgui_ctx.io_mut();
io.set_display_size([logical_size.width as f32, logical_size.height as f32]);
io.set_display_framebuffer_scale([hidpi as f32, hidpi as f32]);
}
}
// If backend supports setting mouse pos and ImGui requests it, honor it
// Skip when multi-viewports are enabled (no global cursor set in winit)
if imgui_ctx.io().want_set_mouse_pos()
&& !imgui_ctx
.io()
.config_flags()
.contains(ConfigFlags::VIEWPORTS_ENABLE)
{
let pos = imgui_ctx.io().mouse_pos();
let logical_pos = self
.scale_pos_for_winit(window, LogicalPosition::new(pos[0] as f64, pos[1] as f64));
let _ = window.set_cursor_position(logical_pos);
}
// Note: cursor shape update is exposed via prepare_render_with_ui()
}
/// Prepare frame - alias for prepare_render for compatibility
pub fn prepare_frame(&mut self, window: &Window, imgui_ctx: &mut Context) {
self.prepare_render(imgui_ctx, window);
}
/// Toggle Dear ImGui software-drawn cursor.
/// When enabled, the OS cursor is hidden and ImGui draws the cursor in draw data.
pub fn set_software_cursor_enabled(&mut self, imgui_ctx: &mut Context, enabled: bool) {
imgui_ctx.io_mut().set_mouse_draw_cursor(enabled);
// Invalidate cursor cache so next prepare_render_with_ui applies visibility change
self.cursor_cache = None;
}
/// Update cursor given a Ui reference (preferred, matches upstream)
pub fn prepare_render_with_ui(&mut self, ui: &dear_imgui_rs::Ui, window: &Window) {
// Auto-manage IME allowed state based on Dear ImGui's intent. This lets
// the platform show/hide IME (and soft keyboards on mobile) only when
// text input widgets are active.
if self.ime_auto_manage {
let want_text = ui.io().want_text_input();
if want_text && !self.ime_enabled {
window.set_ime_allowed(true);
self.ime_enabled = true;
} else if !want_text && self.ime_enabled {
window.set_ime_allowed(false);
self.ime_enabled = false;
}
}
// Only change OS cursor if not disabled by config flags
if !ui
.io()
.config_flags()
.contains(ConfigFlags::NO_MOUSE_CURSOR_CHANGE)
{
// Our Io wrapper does not currently expose MouseDrawCursor, assume false (OS cursor)
let cursor = CursorSettings {
cursor: ui.mouse_cursor(),
draw_cursor: ui.io().mouse_draw_cursor(),
};
if self.cursor_cache != Some(cursor) {
cursor.apply(window);
self.cursor_cache = Some(cursor);
}
}
}
/// Scale a logical size from winit to our active HiDPI mode
pub fn scale_size_from_winit(
&self,
window: &Window,
logical_size: LogicalSize<f64>,
) -> LogicalSize<f64> {
match self.hidpi_mode {
HiDpiMode::Default => logical_size,
// Convert to physical using winit scale, then back to logical with our factor
_ => logical_size
.to_physical::<f64>(window.scale_factor())
.to_logical(self.hidpi_factor),
}
}
/// Scale a logical position from winit to our active HiDPI mode
pub fn scale_pos_from_winit(
&self,
window: &Window,
logical_pos: LogicalPosition<f64>,
) -> LogicalPosition<f64> {
match self.hidpi_mode {
HiDpiMode::Default => logical_pos,
_ => logical_pos
.to_physical::<f64>(window.scale_factor())
.to_logical(self.hidpi_factor),
}
}
/// Scale a logical position for winit based on our active HiDPI mode
pub fn scale_pos_for_winit(
&self,
window: &Window,
logical_pos: LogicalPosition<f64>,
) -> LogicalPosition<f64> {
match self.hidpi_mode {
HiDpiMode::Default => logical_pos,
_ => logical_pos
.to_physical::<f64>(self.hidpi_factor)
.to_logical(window.scale_factor()),
}
}
/// Create window attributes with Dear ImGui defaults
pub fn create_window_attributes() -> WindowAttributes {
WindowAttributes::default()
.with_title("Dear ImGui Window")
.with_inner_size(LogicalSize::new(1024.0, 768.0))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::test_sync::lock_context;
#[test]
fn test_hidpi_mode_default() {
assert_eq!(HiDpiMode::default(), HiDpiMode::Default);
}
#[test]
fn test_platform_creation() {
let _guard = lock_context();
let mut ctx = Context::create();
let platform = WinitPlatform::new(&mut ctx);
assert_eq!(platform.hidpi_mode, HiDpiMode::Default);
assert_eq!(platform.hidpi_factor, 1.0);
assert_eq!(platform.cursor_cache, None);
assert!(!platform.ime_enabled);
}
#[test]
fn test_hidpi_mode_setting() {
let _guard = lock_context();
let mut ctx = Context::create();
let mut platform = WinitPlatform::new(&mut ctx);
platform.set_hidpi_mode(HiDpiMode::Locked(2.0));
assert_eq!(platform.hidpi_mode, HiDpiMode::Locked(2.0));
platform.set_hidpi_mode(HiDpiMode::Rounded);
assert_eq!(platform.hidpi_mode, HiDpiMode::Rounded);
}
#[test]
fn test_ime_callback_ownership_detection() {
unsafe extern "C" fn other_ime_callback(
_ctx: *mut dear_imgui_rs::sys::ImGuiContext,
_viewport: *mut dear_imgui_rs::sys::ImGuiViewport,
_data: *mut dear_imgui_rs::sys::ImGuiPlatformImeData,
) {
}
assert!(is_winit_set_ime_data(Some(imgui_winit_set_ime_data)));
assert!(!is_winit_set_ime_data(Some(other_ime_callback)));
assert!(!is_winit_set_ime_data(None));
}
#[test]
fn ime_platform_io_lookup_uses_passed_context() {
let _guard = lock_context();
let ctx_a = Context::create();
let raw_a = ctx_a.as_raw();
let marker_a = std::ptr::NonNull::<Window>::dangling().as_ptr();
unsafe {
let platform_io_a = dear_imgui_rs::sys::igGetPlatformIO_ContextPtr(raw_a);
(*platform_io_a).Platform_ImeUserData = marker_a.cast();
dear_imgui_rs::sys::igSetCurrentContext(std::ptr::null_mut());
}
let ctx_b = Context::create();
let raw_b = ctx_b.as_raw();
let marker_b = std::ptr::NonNull::<u8>::dangling().as_ptr();
unsafe {
let platform_io_b = dear_imgui_rs::sys::igGetPlatformIO_ContextPtr(raw_b);
(*platform_io_b).Platform_ImeUserData = marker_b.cast();
let selected = platform_io_for_ime_context(raw_a);
assert_eq!((*selected).Platform_ImeUserData, marker_a.cast());
assert_ne!((*selected).Platform_ImeUserData, marker_b.cast());
dear_imgui_rs::sys::igSetCurrentContext(raw_a);
}
drop(ctx_a);
unsafe {
dear_imgui_rs::sys::igSetCurrentContext(raw_b);
}
drop(ctx_b);
}
#[test]
fn test_window_attributes_creation() {
let attrs = WinitPlatform::create_window_attributes();
// Just test that it doesn't panic - actual values depend on winit defaults
let _ = attrs;
}
}