incular-windows 0.1.0

Windows platform integration for Incular
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
//! Windows desktop adapter for Incular's shared desktop shell.
#[cfg(target_os = "windows")]
mod application_shell;
#[cfg(target_os = "windows")]
mod crash_reporter;
#[cfg(target_os = "windows")]
mod environment;
#[cfg(target_os = "windows")]
mod platform_menus;
#[cfg(target_os = "windows")]
mod pointer;

pub use incular_desktop::RunError;

/// Returns the static application-shell capability matrix advertised by the
/// Windows adapter for a concrete native window system.
#[doc(hidden)]
#[must_use]
#[cfg(target_os = "windows")]
pub fn application_shell_capabilities(
    system: incular_platform::NativeWindowSystem,
) -> incular_platform::ApplicationServiceCapabilities {
    let shell = application_shell::WindowsApplicationShell::default();
    let mut capabilities = incular_platform::PlatformCapabilities::default();
    shell.refine_capabilities(system, &mut capabilities);
    capabilities.application_services
}

/// Returns the advanced-input capability matrix after the Windows facade has
/// refined the shared desktop capabilities.
#[doc(hidden)]
#[must_use]
#[cfg(target_os = "windows")]
pub fn advanced_input_capabilities(
    system: incular_platform::NativeWindowSystem,
) -> incular_platform::AdvancedInputCapabilities {
    use incular_desktop::DesktopPlatformServices as _;

    let services = WindowsDesktopPlatformServices::default();
    let mut capabilities = incular_platform::PlatformCapabilities::unsupported();
    capabilities.advanced_input = incular_desktop::desktop_advanced_input_capabilities(system);
    services.refine_advanced_input_capabilities(system, &mut capabilities);
    capabilities.advanced_input
}

#[cfg(target_os = "windows")]
#[derive(Clone, Default)]
struct WindowsDesktopPlatformServices {
    application_shell: application_shell::WindowsApplicationShell,
    menus: std::rc::Rc<platform_menus::WindowsPlatformMenuDelegate>,
    environment: environment::WindowsEnvironmentState,
    pointer: pointer::WindowsPointerState,
}

#[cfg(target_os = "windows")]
impl incular_desktop::DesktopPlatformServices for WindowsDesktopPlatformServices {
    fn application_shell(&self) -> &dyn incular_desktop::DesktopApplicationShellServices {
        self
    }
    fn refine_advanced_input_capabilities(
        &self,
        system: incular_platform::NativeWindowSystem,
        capabilities: &mut incular_platform::PlatformCapabilities,
    ) {
        capabilities.advanced_input.stylus =
            if system == incular_platform::NativeWindowSystem::Win32 {
                incular_platform::CapabilitySupport::Supported
            } else {
                incular_platform::CapabilitySupport::Unsupported
            };
    }

    fn system_environment_preferences(&self) -> incular_platform::SystemEnvironmentPreferences {
        self.environment.preferences()
    }

    fn application_active(&self) -> Option<bool> {
        Some(self.environment.application_active())
    }

    fn take_system_environment_change(&self) -> bool {
        self.environment.take_settings_change()
    }

    fn take_application_lifecycle_events(&self) -> Vec<incular_platform::PlatformLifecycle> {
        self.environment.take_lifecycle_events()
    }

    fn start_system_environment_watch(
        &self,
        _tokio: &dyn Fn() -> incular_runtime::TokioHandle,
        wake: std::sync::Arc<dyn Fn() + Send + Sync>,
    ) {
        self.environment.start_watch(wake);
    }

    fn windows_message_hook(
        &self,
    ) -> Option<Box<dyn FnMut(*const std::ffi::c_void) -> bool + 'static>> {
        let mut environment = self.environment.message_hook();
        let mut pointer = self.pointer.message_hook();
        Some(Box::new(move |message| {
            // Both hooks are observation-only. Evaluate both even if one ever
            // grows a consuming case so input/settings capture cannot starve.
            let pointer_handled = pointer(message);
            let environment_handled = environment(message);
            pointer_handled || environment_handled
        }))
    }

    fn take_native_pointer_sample(
        &self,
        pointer: u64,
    ) -> Option<incular_platform::NativePointerSample> {
        self.pointer.take(pointer)
    }

    fn external_file_drag_support(
        &self,
        system: incular_platform::NativeWindowSystem,
    ) -> incular_platform::CapabilitySupport {
        if system == incular_platform::NativeWindowSystem::Win32 {
            incular_platform::CapabilitySupport::Supported
        } else {
            incular_platform::CapabilitySupport::Unsupported
        }
    }

    fn external_drag_position(
        &self,
        system: incular_platform::NativeWindowSystem,
        window: &winit::window::Window,
    ) -> Option<winit::dpi::PhysicalPosition<f64>> {
        if system != incular_platform::NativeWindowSystem::Win32 {
            return None;
        }
        use raw_window_handle::RawWindowHandle;
        use windows_sys::Win32::{
            Foundation::POINT, Graphics::Gdi::ScreenToClient, UI::WindowsAndMessaging::GetCursorPos,
        };

        let RawWindowHandle::Win32(handle) =
            incular_desktop::winit_adapter::raw_window_handles(window).window
        else {
            return None;
        };
        let mut point = POINT { x: 0, y: 0 };
        // SAFETY: `point` is writable and `handle.hwnd` is the live Winit HWND.
        if unsafe { GetCursorPos(&mut point) } == 0
            || unsafe { ScreenToClient(handle.hwnd.get(), &mut point) } == 0
        {
            return None;
        }
        Some(winit::dpi::PhysicalPosition::new(
            f64::from(point.x),
            f64::from(point.y),
        ))
    }

    fn platform_menu_delegate(&self) -> std::rc::Rc<dyn incular_widgets::PlatformMenuDelegate> {
        self.menus.clone()
    }

    fn register_platform_menu_window(
        &self,
        system: incular_platform::NativeWindowSystem,
        window: &winit::window::Window,
    ) -> incular_platform::PlatformOperationResult {
        if system != incular_platform::NativeWindowSystem::Win32 {
            return Ok(());
        }
        self.menus.register_window(window)
    }

    fn unregister_platform_menu_window(
        &self,
        system: incular_platform::NativeWindowSystem,
        window: &winit::window::Window,
    ) {
        if system == incular_platform::NativeWindowSystem::Win32 {
            self.menus.unregister_window(window);
        }
    }

    fn flush_platform_menu_events(&self) {
        self.menus.flush_events();
    }

    fn wait_for_destroyed_event_after_window_drop(
        &self,
        system: incular_platform::NativeWindowSystem,
    ) -> bool {
        system == incular_platform::NativeWindowSystem::Win32
    }

    fn work_area_support(
        &self,
        system: incular_platform::NativeWindowSystem,
    ) -> incular_platform::CapabilitySupport {
        if system == incular_platform::NativeWindowSystem::Win32 {
            incular_platform::CapabilitySupport::Supported
        } else {
            incular_platform::CapabilitySupport::Unsupported
        }
    }

    fn work_area(
        &self,
        monitor: &winit::monitor::MonitorHandle,
    ) -> Option<incular_platform::PhysicalScreenRect> {
        use windows_sys::Win32::{
            Foundation::RECT,
            Graphics::Gdi::{GetMonitorInfoW, MONITORINFO},
        };
        use winit::platform::windows::MonitorHandleExtWindows;

        let zero = RECT {
            left: 0,
            top: 0,
            right: 0,
            bottom: 0,
        };
        let mut info = MONITORINFO {
            cbSize: u32::try_from(std::mem::size_of::<MONITORINFO>()).ok()?,
            rcMonitor: zero,
            rcWork: zero,
            dwFlags: 0,
        };
        // SAFETY: `monitor.hmonitor()` is a live HMONITOR borrowed from Winit
        // and `info` is a correctly sized writable MONITORINFO for the duration
        // of the synchronous Win32 call.
        if unsafe { GetMonitorInfoW(monitor.hmonitor(), &mut info) } == 0 {
            return None;
        }
        let width = u32::try_from(info.rcWork.right.checked_sub(info.rcWork.left)?).ok()?;
        let height = u32::try_from(info.rcWork.bottom.checked_sub(info.rcWork.top)?).ok()?;
        Some(incular_platform::PhysicalScreenRect::new(
            info.rcWork.left,
            info.rcWork.top,
            width,
            height,
        ))
    }

    fn transient_support(
        &self,
        system: incular_platform::NativeWindowSystem,
        _role: incular_config::TransientRole,
    ) -> incular_platform::CapabilitySupport {
        if system == incular_platform::NativeWindowSystem::Win32 {
            incular_platform::CapabilitySupport::Supported
        } else {
            incular_platform::CapabilitySupport::Unsupported
        }
    }

    fn configure_transient_attributes(
        &self,
        system: incular_platform::NativeWindowSystem,
        parent: &winit::window::Window,
        _role: incular_config::TransientRole,
        attributes: winit::window::WindowAttributes,
    ) -> winit::window::WindowAttributes {
        if system != incular_platform::NativeWindowSystem::Win32 {
            return attributes;
        }
        use raw_window_handle::RawWindowHandle;
        use winit::platform::windows::WindowAttributesExtWindows;

        let RawWindowHandle::Win32(handle) =
            incular_desktop::winit_adapter::raw_window_handles(parent).window
        else {
            return attributes;
        };
        attributes
            .with_owner_window(handle.hwnd.get())
            .with_skip_taskbar(true)
            .with_active(false)
    }

    fn attach_transient(
        &self,
        system: incular_platform::NativeWindowSystem,
        parent: &winit::window::Window,
        popup: &winit::window::Window,
        _role: incular_config::TransientRole,
    ) -> incular_platform::PlatformOperationResult {
        if system != incular_platform::NativeWindowSystem::Win32 {
            return Ok(());
        }
        use raw_window_handle::RawWindowHandle;
        use windows_sys::Win32::UI::WindowsAndMessaging::{GW_OWNER, GetWindow};

        let RawWindowHandle::Win32(parent_handle) =
            incular_desktop::winit_adapter::raw_window_handles(parent).window
        else {
            return Err(incular_platform::PlatformOperationError::unavailable());
        };
        let RawWindowHandle::Win32(popup_handle) =
            incular_desktop::winit_adapter::raw_window_handles(popup).window
        else {
            return Err(incular_platform::PlatformOperationError::unavailable());
        };
        // SAFETY: both HWNDs are live Winit windows on this event-loop thread.
        // Querying GW_OWNER does not retain or mutate either handle.
        if unsafe { GetWindow(popup_handle.hwnd.get(), GW_OWNER) } != parent_handle.hwnd.get() {
            return Err(incular_platform::PlatformOperationError::with_context(
                incular_platform::PlatformOperationErrorKind::NativeFailure,
                "Win32 transient window was not created with the requested owner",
            ));
        }
        Ok(())
    }

    fn show_transient(
        &self,
        system: incular_platform::NativeWindowSystem,
        popup: &winit::window::Window,
        _role: incular_config::TransientRole,
    ) -> incular_platform::PlatformOperationResult {
        if system != incular_platform::NativeWindowSystem::Win32 {
            popup.set_visible(true);
            return Ok(());
        }
        use raw_window_handle::RawWindowHandle;
        use windows_sys::Win32::UI::WindowsAndMessaging::{
            GWL_EXSTYLE, GetWindowLongPtrW, SW_SHOWNOACTIVATE, SWP_FRAMECHANGED, SWP_NOACTIVATE,
            SWP_NOMOVE, SWP_NOSIZE, SWP_NOZORDER, SetWindowLongPtrW, SetWindowPos, ShowWindow,
            WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
        };

        let RawWindowHandle::Win32(handle) =
            incular_desktop::winit_adapter::raw_window_handles(popup).window
        else {
            return Err(incular_platform::PlatformOperationError::unavailable());
        };
        let hwnd = handle.hwnd.get();
        // SAFETY: the HWND is owned by this live Winit window and every call is
        // synchronous on the window's event-loop thread. Showing first with
        // SW_SHOWNOACTIVATE avoids an activation edge; applying the persistent
        // NOACTIVATE/TOOLWINDOW styles afterwards prevents later mouse
        // activation and task-switcher participation. The desktop shell never
        // calls Winit's visibility mutator for this host after this point.
        unsafe {
            ShowWindow(hwnd, SW_SHOWNOACTIVATE);
            let current = GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
            let required = isize::try_from(WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW)
                .expect("Win32 extended style flags fit isize");
            SetWindowLongPtrW(hwnd, GWL_EXSTYLE, current | required);
            let flags = SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER;
            if SetWindowPos(hwnd, 0, 0, 0, 0, 0, flags) == 0 {
                return Err(incular_platform::PlatformOperationError::with_context(
                    incular_platform::PlatformOperationErrorKind::NativeFailure,
                    "Win32 could not apply transient extended-window styles",
                ));
            }
            let applied = GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
            if applied & required != required {
                return Err(incular_platform::PlatformOperationError::with_context(
                    incular_platform::PlatformOperationErrorKind::NativeFailure,
                    "Win32 transient window is missing WS_EX_NOACTIVATE/WS_EX_TOOLWINDOW after native show",
                ));
            }
        }
        Ok(())
    }
}

pub fn run_application(application: incular_runtime::Application) -> Result<(), RunError> {
    #[cfg(target_os = "windows")]
    let _crash_handler = crash_reporter::install();
    #[cfg(target_os = "windows")]
    {
        incular_desktop::run_application_with_services(
            application,
            WindowsDesktopPlatformServices::default(),
        )
    }
    #[cfg(not(target_os = "windows"))]
    incular_desktop::run_application(application)
}

pub fn run_window(
    runtime: incular_runtime::Runtime,
    on_action: impl FnMut(incular_widgets::internal::ActionId) + 'static,
) -> Result<(), RunError> {
    #[cfg(target_os = "windows")]
    let _crash_handler = crash_reporter::install();
    #[cfg(target_os = "windows")]
    {
        incular_desktop::run_window_with_services(
            runtime,
            on_action,
            WindowsDesktopPlatformServices::default(),
        )
    }
    #[cfg(not(target_os = "windows"))]
    incular_desktop::run_window(runtime, on_action)
}

#[cfg(feature = "devtools")]
pub use incular_desktop::{
    DevToolsLaunchMode, devtools_launch_mode_from, devtools_runner, devtools_ui_candidates,
};

#[cfg(target_os = "windows")]
impl incular_desktop::DesktopApplicationShellServices for WindowsDesktopPlatformServices {
    fn refine_application_shell_capabilities(
        &self,
        system: incular_platform::NativeWindowSystem,
        capabilities: &mut incular_platform::PlatformCapabilities,
    ) {
        self.application_shell
            .refine_capabilities(system, capabilities);
    }
    fn start_application_shell_watch(
        &self,
        deliver: std::sync::Arc<dyn Fn(incular_runtime::NativeApplicationShellEvent) + Send + Sync>,
        _complete: std::sync::Arc<
            dyn Fn(incular_runtime::NativeApplicationShellCompletion) + Send + Sync,
        >,
    ) {
        self.application_shell.start_watch(deliver);
    }
    fn set_application_shell_notification_identity(&self, identity: Option<String>) {
        self.application_shell.set_notification_identity(identity);
    }
    fn apply_application_shell_request(
        &self,
        system: incular_platform::NativeWindowSystem,
        request: incular_runtime::NativeApplicationShellRequest,
        target_window: Option<&winit::window::Window>,
    ) -> incular_runtime::NativeApplicationShellApplyResult {
        self.application_shell
            .apply(system, request, target_window)
            .into()
    }
}