1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! `impl Platform for WindowsPlatform` — the main trait implementation.
use crate::core::{ObjectId, PlatformFamily};
use crate::platform::accessibility::AccessibilityBridge;
use crate::platform::clipboard::RichClipboardBackend;
use crate::platform::ime::ImeBridge;
use crate::platform::{
EmbeddedCapabilityContract, NativeCapabilityContract, Platform, PlatformCapabilities,
WindowStateFlag,
};
use crate::platform::windows::notify;
use crate::platform::windows::types::*;
use crate::platform::DropEvent;
use std::sync::atomic::Ordering;
/// SAFETY: All Win32 FFI calls in this module follow standard Windows API safety patterns:
/// - `CreateWindowExW` return values are checked for null (via `hwnd.is_null()`) before use.
/// - `GetLastError` is implicitly checked via the null-return convention; a null HWND indicates
/// that the caller should inspect `GetLastError` for the specific failure code.
/// - Pointers passed to Win32 functions must remain valid for the duration of the call; wide
/// strings (`to_wide`) are kept alive via local variables that live across the `unsafe` block.
/// - `ShowWindow` / `UpdateWindow` / `MoveWindow` operate on previously-validated HWNDs.
impl Platform for WindowsPlatform {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn set_window_state(&self, widget_id: ObjectId, flag: WindowStateFlag, on: bool) -> bool {
let Some(kind) = self.state.kind_of(widget_id) else {
return false;
};
if !matches!(kind, super::types::WindowsHandleKind::Window) {
return false;
}
self.state.set_window_state(widget_id, flag, on);
#[cfg(target_os = "windows")]
{
use winapi::um::winuser::{
GetWindowLongW, SetWindowLongW, ShowWindow, GWL_STYLE, SW_MAXIMIZE, SW_MINIMIZE,
SW_RESTORE, WS_CAPTION, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_THICKFRAME,
};
if let Some(hwnd) = self.get_native_handle(widget_id) {
match flag {
WindowStateFlag::Maximized => unsafe {
let cmd = if on { SW_MAXIMIZE } else { SW_RESTORE };
ShowWindow(hwnd, cmd);
},
WindowStateFlag::Minimized => unsafe {
let cmd = if on { SW_MINIMIZE } else { SW_RESTORE };
ShowWindow(hwnd, cmd);
},
WindowStateFlag::Fullscreen => {
// Win32 has no "full screen" window flag: it is achieved by
// dropping the frame styles and filling the monitor. Reapply
// the styles to leave it, which is what the WM would do.
unsafe {
let style = GetWindowLongW(hwnd, GWL_STYLE) as u32;
let frame =
WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
let new_style = if on { style & !frame } else { style | frame };
SetWindowLongW(hwnd, GWL_STYLE, new_style as i32);
ShowWindow(hwnd, SW_MAXIMIZE);
}
}
WindowStateFlag::Resizable => unsafe {
let style = GetWindowLongW(hwnd, GWL_STYLE) as u32;
let new_style = if on {
style | WS_THICKFRAME | WS_MAXIMIZEBOX
} else {
style & !(WS_THICKFRAME | WS_MAXIMIZEBOX)
};
SetWindowLongW(hwnd, GWL_STYLE, new_style as i32);
},
WindowStateFlag::Decorated => unsafe {
let style = GetWindowLongW(hwnd, GWL_STYLE) as u32;
let decorative = WS_CAPTION | WS_THICKFRAME;
let new_style = if on { style | decorative } else { style & !decorative };
SetWindowLongW(hwnd, GWL_STYLE, new_style as i32);
},
}
}
}
true
}
fn is_window_in_state(&self, widget_id: ObjectId, flag: WindowStateFlag) -> Option<bool> {
let kind = self.state.kind_of(widget_id)?;
if !matches!(kind, super::types::WindowsHandleKind::Window) {
return None;
}
#[cfg(target_os = "windows")]
{
use winapi::um::winuser::{
GetWindowLongW, IsIconic, IsZoomed, GWL_STYLE, WS_CAPTION, WS_MAXIMIZEBOX,
WS_MINIMIZEBOX, WS_THICKFRAME,
};
if let Some(hwnd) = self.get_native_handle(widget_id) {
let value = match flag {
WindowStateFlag::Maximized => unsafe { IsZoomed(hwnd) != 0 },
WindowStateFlag::Minimized => unsafe { IsIconic(hwnd) != 0 },
// Full screen is "no frame styles left", the inverse of the
// Decorated computation below.
WindowStateFlag::Fullscreen => unsafe {
let style = GetWindowLongW(hwnd, GWL_STYLE) as u32;
style & (WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX) == 0
},
WindowStateFlag::Resizable => unsafe {
let style = GetWindowLongW(hwnd, GWL_STYLE) as u32;
style & (WS_THICKFRAME | WS_MAXIMIZEBOX) != 0
},
WindowStateFlag::Decorated => unsafe {
let style = GetWindowLongW(hwnd, GWL_STYLE) as u32;
style & (WS_CAPTION | WS_THICKFRAME) != 0
},
};
return Some(value);
}
}
self.state.window_state(widget_id, flag)
}
fn set_window_min_size(&self, widget_id: ObjectId, width: u32, height: u32) -> bool {
let Some(kind) = self.state.kind_of(widget_id) else {
return false;
};
if !matches!(kind, super::types::WindowsHandleKind::Window) {
return false;
}
// Win32 has no setter: the value is consumed by the `WM_GETMINMAXINFO`
// handler in `rw_wnd_proc`, which reads it back from this state model. That
// is why the write is recorded even for a state-only window.
self.state.set_window_min_size(widget_id, width, height);
// Force a recompute so a *shrink* of the constraint takes effect now
// rather than at the next user resize.
#[cfg(target_os = "windows")]
{
use winapi::um::winuser::{SetWindowPos, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOZORDER};
if let Some(hwnd) = self.get_native_handle(widget_id) {
unsafe {
SetWindowPos(
hwnd,
std::ptr::null_mut(),
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE,
);
}
}
}
true
}
fn window_min_size(&self, widget_id: ObjectId) -> Option<(u32, u32)> {
let kind = self.state.kind_of(widget_id)?;
if !matches!(kind, super::types::WindowsHandleKind::Window) {
return None;
}
self.state.window_min_size(widget_id)
}
fn set_window_icon(&self, widget_id: ObjectId, path: &str) -> bool {
let Some(kind) = self.state.kind_of(widget_id) else {
return false;
};
if !matches!(kind, super::types::WindowsHandleKind::Window) {
return false;
}
#[cfg(target_os = "windows")]
{
use winapi::um::winuser::{
LoadImageW, SendMessageW, ICON_BIG, ICON_SMALL, IMAGE_ICON, LR_DEFAULTSIZE,
LR_LOADFROMFILE, WM_SETICON,
};
if let Some(hwnd) = self.get_native_handle(widget_id) {
let wide = Self::to_wide(path);
// SAFETY: `wide` is a NUL-terminated UTF-16 buffer that outlives
// the call; LoadImageW returns a fresh HICON or null.
let icon = unsafe {
LoadImageW(
std::ptr::null_mut(),
wide.as_ptr(),
IMAGE_ICON,
0,
0,
LR_LOADFROMFILE | LR_DEFAULTSIZE,
)
};
if icon.is_null() {
log::warn!("[rust_widgets][windows] set_window_icon: could not load '{path}'");
return false;
}
// Set both the small (taskbar) and big (alt-tab) icons, which is
// what the shell reads.
unsafe {
SendMessageW(hwnd, WM_SETICON, ICON_SMALL as usize, icon as isize);
SendMessageW(hwnd, WM_SETICON, ICON_BIG as usize, icon as isize);
}
}
}
self.state.set_window_icon(widget_id, path);
true
}
fn window_icon(&self, widget_id: ObjectId) -> Option<String> {
let kind = self.state.kind_of(widget_id)?;
if !matches!(kind, super::types::WindowsHandleKind::Window) {
return None;
}
// Win32 stores an HICON, not a path, so the recorded request is the answer.
self.state.window_icon(widget_id)
}
fn backend_name(&self) -> &'static str {
"WindowsPlatform"
}
/// Reads installed physical memory via `GlobalMemoryStatusEx`.
fn total_memory_mb(&self) -> Option<u64> {
#[cfg(target_os = "windows")]
{
use winapi::um::sysinfoapi::{GlobalMemoryStatusEx, MEMORYSTATUSEX};
// SAFETY: `MEMORYSTATUSEX` is a plain C struct; zeroing it and setting
// `dwLength` is exactly what the API contract requires. The call only
// writes into our own stack value.
let mut status: MEMORYSTATUSEX = unsafe { std::mem::zeroed() };
status.dwLength = std::mem::size_of::<MEMORYSTATUSEX>() as u32;
let ok = unsafe { GlobalMemoryStatusEx(&mut status) };
if ok != 0 {
return Some(status.ullTotalPhys / (1024 * 1024));
}
}
None
}
/// Reports `true` when the system is running on battery power.
///
/// `GetSystemPowerStatus` sets `ACLineStatus` to 0 while discharging; 1 means
/// AC, and 255 means "unknown", which is treated as AC so a desktop is never
/// mistaken for a laptop on battery.
fn is_on_battery(&self) -> bool {
#[cfg(target_os = "windows")]
{
use winapi::um::winbase::{GetSystemPowerStatus, SYSTEM_POWER_STATUS};
// SAFETY: `SYSTEM_POWER_STATUS` is a plain C struct filled by the call
// from our own stack value.
let mut status: SYSTEM_POWER_STATUS = unsafe { std::mem::zeroed() };
let ok = unsafe { GetSystemPowerStatus(&mut status) };
if ok != 0 {
return status.ACLineStatus == 0;
}
}
false
}
/// Samples this process's working set against total physical memory.
fn process_memory_utilization(&self) -> Option<f32> {
#[cfg(target_os = "windows")]
{
use winapi::um::processthreadsapi::GetCurrentProcess;
use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
// SAFETY: both structs are plain C layouts owned by this stack frame.
let mut counters: PROCESS_MEMORY_COUNTERS = unsafe { std::mem::zeroed() };
counters.cb = std::mem::size_of::<PROCESS_MEMORY_COUNTERS>() as u32;
let ok =
unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut counters, counters.cb) };
if ok != 0 {
let total = self.total_memory_mb()? as f64 * 1024.0 * 1024.0;
if total > 0.0 {
let ratio = (counters.WorkingSetSize as f64 / total) as f32;
return Some(ratio.clamp(0.0, 1.0));
}
}
}
None
}
/// CPU load has no cheap, stable Win32 query here, so this backend reports
/// `None` and the adaptive monitor keeps its default.
fn process_cpu_utilization(&self) -> Option<f32> {
None
}
/// Hands the job file to the shell's `Print` verb via PowerShell.
fn spawn_print_job(&self, job_file: &std::path::Path) -> Result<(), String> {
let status = std::process::Command::new("powershell")
.arg("-NoProfile")
.arg("-Command")
.arg(format!(
"Start-Process -FilePath '{}' -Verb Print -PassThru | Out-Null",
job_file.display()
))
.status();
if let Ok(status) = status {
if status.success() {
return Ok(());
}
}
Err("system print command failed on windows".to_string())
}
/// The shell `print` verb is always available on Windows.
fn has_print_support(&self) -> bool {
std::process::Command::new("cmd")
.args(["/C", "print /? 2>NUL"])
.output()
.map(|out| out.status.success())
.unwrap_or(false)
}
/// A library-painted widget gets a child `HWND` of its own class; `WM_PAINT`
/// blits a frame from `widget::runtime`. See `windows/canvas.rs`.
///
/// Gated on the same profile conditions as `canvas.rs`: without a widget
/// registry there is no frame to render, so the trait defaults apply and
/// `supports_surfaces()` reports `false`.
#[cfg(widgets_unstripped)]
fn mount_surface(&self, parent: ObjectId, id: ObjectId, rect: crate::core::Rect) -> bool {
let Some(parent_hwnd) = self.get_native_handle(parent) else {
log::error!("[windows] mount_surface: unknown parent window {parent}");
return false;
};
let Some(hwnd) = super::canvas::mount_canvas(parent_hwnd, id, rect) else {
return false;
};
self.bind_native_handle(id, hwnd);
crate::widget::runtime::set_geometry(id, rect);
true
}
#[cfg(widgets_unstripped)]
fn resize_surface(&self, id: ObjectId, rect: crate::core::Rect) -> bool {
let Some(hwnd) = super::canvas::hwnd_for_widget(id) else {
log::error!("[windows] resize_surface: id={id} is not mounted");
return false;
};
if !super::canvas::resize_canvas(hwnd, rect) {
return false;
}
crate::widget::runtime::set_geometry(id, rect);
true
}
#[cfg(widgets_unstripped)]
fn unmount_surface(&self, id: ObjectId) -> bool {
let Some(hwnd) = super::canvas::hwnd_for_widget(id) else {
log::error!("[windows] unmount_surface: id={id} is not mounted");
return false;
};
super::canvas::unmount_canvas(hwnd)
}
/// `true` only when the widget surface exists for this profile.
#[cfg(widgets_unstripped)]
fn supports_surfaces(&self) -> bool {
true
}
/// Invalidate the canvas window so the OS sends a fresh `WM_PAINT`.
#[cfg(widgets_unstripped)]
fn invalidate_surface(&self, id: ObjectId) -> bool {
match super::canvas::hwnd_for_widget(id) {
Some(hwnd) => {
super::canvas::invalidate_canvas(hwnd);
true
}
None => false,
}
}
fn family(&self) -> PlatformFamily {
PlatformFamily::Desktop
}
fn capabilities(&self) -> PlatformCapabilities {
PlatformCapabilities {
dpi_scaling: true,
ime: true,
accessibility: true,
native_menu: true,
typed_widget_trigger: true,
}
}
fn native_capability_contract(&self) -> Option<NativeCapabilityContract> {
Some(NativeCapabilityContract::from_platform_caps(self.capabilities()))
}
fn embedded_capability_contract(&self) -> Option<EmbeddedCapabilityContract> {
None
}
fn dpi_scale_factor(&self) -> f32 {
#[cfg(target_os = "windows")]
{
// Query the actual DPI of the primary monitor.
unsafe {
let hdc = winapi::um::winuser::GetDC(std::ptr::null_mut());
if hdc.is_null() {
return 1.0;
}
let dpi = winapi::um::wingdi::GetDeviceCaps(hdc, winapi::um::wingdi::LOGPIXELSX);
winapi::um::winuser::ReleaseDC(std::ptr::null_mut(), hdc);
dpi as f32 / 96.0
}
}
#[cfg(not(target_os = "windows"))]
{
1.0
}
}
fn init(&self) {
self.runtime_initialized.store(true, Ordering::SeqCst);
#[cfg(target_os = "windows")]
{
// SAFETY: The platform instance is stored in a `OnceLock<Box<dyn Platform>>`
// (see `runtime.rs`), so it lives for the entire program duration (`'static`).
let static_self: &'static WindowsPlatform =
unsafe { std::mem::transmute::<&WindowsPlatform, &'static WindowsPlatform>(self) };
notify::register_active_platform(static_self);
}
#[cfg(target_os = "windows")]
unsafe {
use winapi::um::commctrl::InitCommonControls;
InitCommonControls();
}
}
fn run(&self) {
#[cfg(target_os = "windows")]
unsafe {
use std::ptr::null_mut;
use std::thread;
use std::time::Duration;
use winapi::um::winuser::{
DispatchMessageW, PeekMessageW, TranslateMessage, MSG, PM_REMOVE, WM_QUIT,
};
self.runtime_running.store(true, Ordering::SeqCst);
while self.runtime_running.load(Ordering::SeqCst) {
let mut msg: MSG = std::mem::zeroed();
while PeekMessageW(&mut msg, null_mut(), 0, 0, PM_REMOVE) != 0 {
if msg.message == WM_QUIT {
self.runtime_running.store(false, Ordering::SeqCst);
break;
}
// Accelerators must be offered to every window that has an
// HACCEL table before normal dispatch: TranslateAcceleratorW
// turns a matching key press into the item's WM_COMMAND, and
// returns 0 for everything else so the message falls through
// to TranslateMessage/DispatchMessageW unchanged.
let translated = self.try_translate_accelerator(&msg);
if !translated {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
if self.runtime_running.load(Ordering::SeqCst) {
thread::sleep(Duration::from_millis(10));
}
}
self.runtime_running.store(false, Ordering::SeqCst);
}
}
fn quit(&self) {
#[cfg(target_os = "windows")]
unsafe {
use winapi::um::winuser::PostQuitMessage;
self.runtime_running.store(false, Ordering::SeqCst);
PostQuitMessage(0);
}
}
/// Release every registry entry the backend holds for `widget_id`.
///
/// Beyond the authoritative `BackendState` record, the Win32 backend keeps
/// per-widget entries in three side tables: the native handle map (`handles`),
/// the accelerator/owner bookkeeping (`menu_owner_window`) and the
/// native-dialog metadata (`dialog_data`). All of them must be purged,
/// otherwise a UI rebuilt in a create/destroy loop would leak one entry per
/// discarded widget. Every lock is scoped to its own statement so no two
/// guards are ever held at the same time.
///
/// Only the library's own bookkeeping is released here: no Win32 message is
/// sent and no window is destroyed — the process-wide HWND may still be owned
/// elsewhere, so `DestroyWindow` is deliberately not called.
///
/// A window's accelerator table is released with it, otherwise each
/// create/destroy cycle would leak an `HACCEL`.
fn destroy_widget(&self, widget_id: ObjectId) -> bool {
#[cfg(target_os = "windows")]
{
crate::platform::windows::accel::release_accelerator_table(widget_id);
if let Ok(mut handles) = self.menu_state.handles.lock() {
handles.remove(&widget_id);
} else {
log::error!("[rust_widgets][windows] destroy_widget: handles mutex poisoned");
}
if let Ok(mut owners) = self.menu_state.menu_owner_window.lock() {
owners.remove(&widget_id);
} else {
log::error!(
"[rust_widgets][windows] destroy_widget: menu_owner_window mutex poisoned"
);
}
if let Ok(mut data) = self.dialog_data.lock() {
data.remove(&widget_id);
} else {
log::error!("[rust_widgets][windows] destroy_widget: dialog_data mutex poisoned");
}
}
// The state record is the authority on whether the widget existed.
self.state.destroy_widget(widget_id)
}
fn create_window(&self, title: &str, x: i32, y: i32, width: u32, height: u32) -> ObjectId {
#[cfg(target_os = "windows")]
{
unsafe extern "system" {
fn GetModuleHandleW(lpModuleName: *const u16) -> *mut std::ffi::c_void;
}
use std::ptr::null_mut;
use winapi::um::winuser::{
CreateWindowExW, ShowWindow, UpdateWindow, SW_SHOW, WS_OVERLAPPEDWINDOW, WS_VISIBLE,
};
notify::ensure_window_class_registered();
let class_name = Self::to_wide("RustWidgetsWindowClass");
let title_wide = Self::to_wide(title);
// SAFETY: GetModuleHandleW with a null module name returns the handle to
// the calling process's executable (HINSTANCE). This is safe per MSDN and
// the return value is only used as the hInstance parameter for CreateWindowExW.
// A null module name is explicitly documented to be valid.
let hinstance = unsafe { GetModuleHandleW(std::ptr::null()) };
let hwnd = unsafe {
CreateWindowExW(
0,
class_name.as_ptr(),
title_wide.as_ptr(),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
x,
y,
width as i32,
height as i32,
null_mut(),
null_mut(),
hinstance as _,
null_mut(),
)
};
if hwnd.is_null() {
log::error!(
"[rust_widgets][windows] create_window failed for title='{}' (GetLastError={})",
title,
unsafe { winapi::um::errhandlingapi::GetLastError() }
);
return 0;
}
let widget_id =
self.state.create_widget(WindowsHandleKind::Window, title, x, y, width, height);
self.bind_native_handle(widget_id, hwnd);
// `WS_OVERLAPPEDWINDOW` is titled + resizable, so a fresh Win32 window
// starts restored, windowed, resizable and decorated.
self.state.init_window_state(
widget_id,
crate::platform::state::WindowStateRecord::new_window(),
);
unsafe {
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
}
widget_id
}
#[cfg(not(target_os = "windows"))]
{
let widget_id =
self.state.create_widget(WindowsHandleKind::Window, title, x, y, width, height);
self.state.init_window_state(
widget_id,
crate::platform::state::WindowStateRecord::new_window(),
);
widget_id
}
}
fn set_clipboard_text(&self, _text: &str) -> bool {
#[cfg(target_os = "windows")]
{
use winapi::um::winbase::GlobalAlloc;
use winapi::um::winbase::{GlobalLock, GlobalUnlock, GHND};
use winapi::um::winuser::{
CloseClipboard, EmptyClipboard, OpenClipboard, SetClipboardData, CF_UNICODETEXT,
};
let text_utf16: Vec<u16> = _text.encode_utf16().chain(std::iter::once(0)).collect();
let byte_size = text_utf16.len() * 2;
// SAFETY: Win32 clipboard API calls with proper error checking.
unsafe {
if OpenClipboard(std::ptr::null_mut()) == 0 {
return false;
}
if EmptyClipboard() == 0 {
CloseClipboard();
return false;
}
let h_mem = GlobalAlloc(GHND, byte_size);
if h_mem.is_null() {
CloseClipboard();
return false;
}
let p_dest = GlobalLock(h_mem) as *mut u16;
if p_dest.is_null() {
GlobalUnlock(h_mem);
CloseClipboard();
return false;
}
std::ptr::copy_nonoverlapping(text_utf16.as_ptr(), p_dest, text_utf16.len());
GlobalUnlock(h_mem);
let ret = SetClipboardData(CF_UNICODETEXT, h_mem as _);
CloseClipboard();
ret as isize != 0
}
}
#[cfg(not(target_os = "windows"))]
{
let _ = _text;
false
}
}
fn get_clipboard_text(&self) -> String {
#[cfg(target_os = "windows")]
{
use winapi::um::winbase::GlobalLock;
use winapi::um::winuser::{
CloseClipboard, GetClipboardData, OpenClipboard, CF_UNICODETEXT,
};
// SAFETY: Win32 clipboard API calls with proper error checking.
let result = unsafe {
if OpenClipboard(std::ptr::null_mut()) == 0 {
return String::new();
}
let h_mem = GetClipboardData(CF_UNICODETEXT);
if h_mem.is_null() {
CloseClipboard();
return String::new();
}
let p_src = GlobalLock(h_mem) as *const u16;
if p_src.is_null() {
CloseClipboard();
return String::new();
}
let mut len = 0;
while *p_src.add(len) != 0 {
len += 1;
}
let slice = std::slice::from_raw_parts(p_src, len);
let text = String::from_utf16_lossy(slice);
CloseClipboard();
text
};
result
}
#[cfg(not(target_os = "windows"))]
{
String::new()
}
}
fn begin_drag(&self, source_widget_id: ObjectId, mime: &str, payload: &[u8]) -> bool {
#[cfg(target_os = "windows")]
{
// State-backed drag-drop (platform native OLE pending)
// Full OLE implementation (IDropSource/IDropTarget) requires:
// DoDragDrop, OleInitialize, RegisterDragDrop, RevokeDragDrop
// See: https://learn.microsoft.com/en-us/windows/win32/shell/dragdrop
self.state.begin_drag(source_widget_id, mime, payload)
}
#[cfg(not(target_os = "windows"))]
{
let _ = (source_widget_id, mime, payload);
false
}
}
fn poll_drop_event(&self) -> Option<DropEvent> {
#[cfg(target_os = "windows")]
{
// State-backed drop event polling (OLE polling pending)
self.state.pop_drop_event()
}
#[cfg(not(target_os = "windows"))]
{
None
}
}
fn inject_drop_event(&self, event: DropEvent) -> bool {
#[cfg(target_os = "windows")]
{
// State-backed drop event injection (OLE injection pending)
self.state.inject_drop_event(event)
}
#[cfg(not(target_os = "windows"))]
{
let _ = event;
false
}
}
// ── IME support ─────────────────────────────────────────────────────────
fn set_widget_ime_enabled(&self, widget_id: ObjectId, enabled: bool) -> bool {
self.state.set_ime_enabled(widget_id, enabled)
}
fn is_widget_ime_enabled(&self, widget_id: ObjectId) -> bool {
self.state.ime_enabled(widget_id)
}
fn ime_bridge(&self) -> Option<&dyn ImeBridge> {
Some(&self.ime_bridge)
}
fn clipboard_backend(&self) -> Option<&dyn RichClipboardBackend> {
Some(&self.clipboard)
}
#[cfg(target_os = "windows")]
fn accessibility_bridge(&self) -> Option<&dyn AccessibilityBridge> {
Some(&self.a11y_bridge)
}
}
#[cfg(target_os = "windows")]
impl WindowsPlatform {
/// Offers a message to the accelerator table of the currently active window.
///
/// Returns `true` when an accelerator matched, in which case the message has
/// been consumed and Win32 has posted the item's `WM_COMMAND` instead.
///
/// This is an inherent method rather than a `Platform` trait method: it is
/// called only from [`Platform::run`]'s message pump, and putting it on the
/// trait would invite other backends to implement a concept that does not
/// exist on them.
///
/// Only the active window is consulted. Accelerators belong to the focused
/// window, so a background window must not swallow a chord typed into the
/// foreground one.
pub(crate) fn try_translate_accelerator(&self, msg: &winapi::um::winuser::MSG) -> bool {
use winapi::um::winuser::{GetActiveWindow, TranslateAcceleratorW};
// SAFETY: GetActiveWindow is a side-effect-free query; it returns a live
// HWND for this thread or null.
let active = unsafe { GetActiveWindow() };
if active.is_null() {
return false;
}
let Some(window_id) = self.widget_id_by_native_handle(active) else {
return false;
};
let Some(table) = crate::platform::windows::accel::accel_table_for(window_id) else {
return false;
};
// SAFETY: `active` is a live window for this process, `table` is an
// HACCEL created by `accel::install_accelerator`, and `msg` is the message
// being dispatched. TranslateAcceleratorW only reads it and posts
// WM_COMMAND, so `msg` does not need to outlive the call.
let translated = unsafe { TranslateAcceleratorW(active, table, msg as *const _ as *mut _) };
translated != 0
}
}