Skip to content

Commit ab55b32

Browse files
Desktop: Window resize handling on Windows (#3167)
* Native window resize on windows * Fix linux build * Fix windows build * try clean up * clean up * Add module comment * FIx * Review improvements * Improve
1 parent da330b6 commit ab55b32

6 files changed

Lines changed: 381 additions & 16 deletions

File tree

desktop/Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,25 @@ vello = { workspace = true }
4141
derivative = { workspace = true }
4242
rfd = { workspace = true }
4343
open = { workspace = true }
44-
rand = { workspace = true }
44+
rand = { workspace = true, features = ["thread_rng"] }
4545
serde = { workspace = true }
4646

4747
# Hardware acceleration dependencies
4848
ash = { version = "0.38", optional = true }
4949

5050
# Windows-specific dependencies
5151
[target.'cfg(windows)'.dependencies]
52-
windows = { version = "0.58", features = [
52+
windows = { version = "0.58.0", features = [
53+
"Win32_Foundation",
5354
"Win32_Graphics_Direct3D11",
5455
"Win32_Graphics_Direct3D12",
5556
"Win32_Graphics_Dxgi",
5657
"Win32_Graphics_Dxgi_Common",
57-
"Win32_Foundation"
58+
"Win32_Graphics_Dwm",
59+
"Win32_Graphics_Gdi",
60+
"Win32_System_LibraryLoader",
61+
"Win32_UI_Controls",
62+
"Win32_UI_WindowsAndMessaging",
5863
], optional = true }
5964

6065
# macOS-specific dependencies

desktop/src/app.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ use winit::window::Window;
2323
use winit::window::WindowId;
2424

2525
use crate::cef;
26+
use crate::native_window;
2627

2728
pub(crate) struct WinitApp {
2829
cef_context: Box<dyn cef::CefContext>,
2930
window: Option<Arc<Window>>,
31+
native_window: native_window::NativeWindowHandle,
3032
cef_schedule: Option<Instant>,
3133
window_size_sender: Sender<WindowSize>,
3234
graphics_state: Option<GraphicsState>,
@@ -71,6 +73,7 @@ impl WinitApp {
7173
web_communication_initialized: false,
7274
web_communication_startup_buffer: Vec::new(),
7375
persistent_data,
76+
native_window: Default::default(),
7477
}
7578
}
7679

@@ -277,22 +280,15 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
277280
.with_title(APP_NAME)
278281
.with_min_inner_size(winit::dpi::LogicalSize::new(400, 300))
279282
.with_inner_size(winit::dpi::LogicalSize::new(1200, 800))
280-
.with_decorations(false)
281283
.with_resizable(true);
282284

283-
#[cfg(target_os = "linux")]
284-
{
285-
use crate::consts::APP_ID;
286-
use winit::platform::wayland::ActiveEventLoopExtWayland;
285+
window = self.native_window.build(window, event_loop);
287286

288-
window = if event_loop.is_wayland() {
289-
winit::platform::wayland::WindowAttributesExtWayland::with_name(window, APP_ID, "")
290-
} else {
291-
winit::platform::x11::WindowAttributesExtX11::with_name(window, APP_ID, APP_NAME)
292-
}
293-
}
287+
let window = event_loop.create_window(window).unwrap();
288+
289+
self.native_window.setup(&window);
294290

295-
let window = Arc::new(event_loop.create_window(window).unwrap());
291+
let window = Arc::new(window);
296292
let graphics_state = GraphicsState::new(window.clone(), self.wgpu_context.clone());
297293

298294
self.window = Some(window);

desktop/src/cef/texture_import/d3d11.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ impl D3D11Importer {
265265

266266
fn import_d3d11_handle_to_d3d12(&self, hal_device: &<wgpu::hal::api::Dx12 as wgpu::hal::Api>::Device) -> Result<windows::Win32::Graphics::Direct3D12::ID3D12Resource, TextureImportError> {
267267
use windows::Win32::Graphics::Direct3D12::*;
268-
use windows::core::*;
269268

270269
// Get D3D12 device from wgpu-hal
271270
let d3d12_device = hal_device.raw_device();

desktop/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub(crate) mod consts;
99

1010
mod cef;
1111

12+
mod native_window;
13+
1214
mod render;
1315

1416
mod app;

desktop/src/native_window.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use winit::event_loop::ActiveEventLoop;
2+
use winit::window::{Window, WindowAttributes};
3+
4+
#[cfg(target_os = "windows")]
5+
mod windows;
6+
7+
pub(crate) enum NativeWindowHandle {
8+
#[cfg(target_os = "windows")]
9+
#[expect(private_interfaces, dead_code)]
10+
Windows(windows::WindowsNativeWindowHandle),
11+
None,
12+
}
13+
impl Default for NativeWindowHandle {
14+
fn default() -> Self {
15+
Self::None
16+
}
17+
}
18+
impl NativeWindowHandle {
19+
#[allow(unused_variables)]
20+
pub(super) fn build(&mut self, window: WindowAttributes, event_loop: &ActiveEventLoop) -> WindowAttributes {
21+
#[cfg(target_os = "linux")]
22+
{
23+
use crate::consts::{APP_ID, APP_NAME};
24+
use winit::platform::wayland::ActiveEventLoopExtWayland;
25+
if event_loop.is_wayland() {
26+
winit::platform::wayland::WindowAttributesExtWayland::with_name(window, APP_ID, "")
27+
} else {
28+
winit::platform::x11::WindowAttributesExtX11::with_name(window, APP_ID, APP_NAME)
29+
}
30+
}
31+
#[cfg(not(target_os = "linux"))]
32+
{
33+
window
34+
}
35+
}
36+
37+
#[allow(unused_variables)]
38+
pub(crate) fn setup(&mut self, window: &Window) {
39+
#[cfg(target_os = "windows")]
40+
{
41+
*self = NativeWindowHandle::Windows(windows::WindowsNativeWindowHandle::new(window));
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)