Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/src/platform/windows/mod.rs
1use itertools::Itertools as _;
2use std::os::windows::ffi::OsStrExt as _;
3 
4// Re-export a couple winit types and modules as the concrete implementations
5// for Windows.
6pub use crate::windowing::winit::app::App;
7 
8pub(crate) static DXC_PATH: std::sync::OnceLock<Option<DXCPath>> = std::sync::OnceLock::new();
9 
10/// Path to the DXC DLLs to be used to compile DirectX shaders using DXC.
11/// See https://github.com/microsoft/DirectXShaderCompiler.
12#[derive(Debug)]
13pub struct DXCPath {
14 pub dxc_path: String,
15 pub dxil_path: String,
16}
17 
18pub trait AppBuilderExt {
19 /// Set the AppUserModel ID, which Windows uses to attribute notifications to
20 /// our correct application.
21 fn set_app_user_model_id(&mut self, app_id: String);
22 
23 /// Use DXC (the newer DirectX Shader Compiler) to compile DirectX shaders.
24 /// Using DXC requires the dlls within [`DXCPath`] to be available and shipped
25 /// alongside the application.=
26 fn use_dxc_for_directx_shader_compilation(&mut self, dxc_path: DXCPath);
27}
28 
29impl AppBuilderExt for super::AppBuilder {
30 fn set_app_user_model_id(&mut self, app_id: String) {
31 let set_id = unsafe { set_app_user_model_id(app_id) };
32 if let Err(err) = set_id {
33 log::error!("Unable to set Windows AppUserModel ID: {err:?}");
34 }
35 }
36 
37 fn use_dxc_for_directx_shader_compilation(&mut self, dxc_path: DXCPath) {
38 if let Err(e) = DXC_PATH.set(Some(dxc_path)) {
39 log::warn!("Failed to set DXC path {e:?}");
40 }
41 }
42}
43 
44unsafe fn set_app_user_model_id(app_id: String) -> Result<(), windows::core::Error> {
45 let wide_string = std::ffi::OsStr::new(&app_id)
46 .encode_wide()
47 .chain(std::iter::once(0))
48 .collect_vec();
49 windows::Win32::UI::Shell::SetCurrentProcessExplicitAppUserModelID(windows::core::PCWSTR(
50 wide_string.as_ptr(),
51 ))
52}
53