Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Windows, fix undecorated shadow reported client size is bigger than what's visible #3712

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions examples/undecorated_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#![allow(unused)]

use std::sync::Arc;

use winit::application::ApplicationHandler;
use winit::event::{ElementState, KeyEvent, WindowEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::keyboard::Key;
#[cfg(windows)]
use winit::platform::windows::{WindowAttributesExtWindows, WindowExtWindows};
use winit::window::{Window, WindowAttributes, WindowId};

#[path = "util/fill.rs"]
mod fill;

struct App {
window: Option<Box<dyn Window>>,
shadow: bool,
}

impl Default for App {
fn default() -> Self {
Self { window: None, shadow: true }
}
}

impl ApplicationHandler for App {
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
let mut attrs = WindowAttributes::default().with_decorations(false);
#[cfg(windows)]
{
attrs = attrs.with_undecorated_shadow(true);
}

self.window = Some(event_loop.create_window(attrs).unwrap());
}

fn window_event(
&mut self,
event_loop: &dyn ActiveEventLoop,
_id: WindowId,
event: WindowEvent,
) {
match event {
#[cfg(windows)]
WindowEvent::KeyboardInput {
event: KeyEvent { logical_key: Key::Character(c), state: ElementState::Pressed, .. },
..
} if c == "x" => {
self.shadow = !self.shadow;
self.window.as_ref().unwrap().set_undecorated_shadow(self.shadow);
},
WindowEvent::CloseRequested => {
event_loop.exit();
},
WindowEvent::RedrawRequested => {
let window = self.window.as_ref().unwrap();
fill::fill_window_with_border(window.as_ref());
window.request_redraw();
},
_ => (),
}
}
}

fn main() {
let event_loop = EventLoop::new().unwrap();
event_loop.set_control_flow(ControlFlow::Wait);
let mut app = App::default();
event_loop.run_app(&mut app).unwrap()
}
45 changes: 37 additions & 8 deletions examples/util/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
//! The `softbuffer` crate is used, largely because of its ease of use. `glutin` or `wgpu` could
//! also be used to fill the window buffer, but they are more complicated to use.

#[allow(unused_imports)]
pub use platform::cleanup_window;
pub use platform::fill_window;
#![allow(unused)]
pub use platform::{cleanup_window, fill_window, fill_window_with_border};

#[cfg(not(any(target_os = "android", target_os = "ios")))]
mod platform {
Expand All @@ -19,7 +18,7 @@ mod platform {
use std::mem::ManuallyDrop;
use std::num::NonZeroU32;

use softbuffer::{Context, Surface};
use softbuffer::{Buffer, Context, Surface};
use winit::window::{Window, WindowId};

thread_local! {
Expand Down Expand Up @@ -70,7 +69,31 @@ mod platform {
}
}

const DARK_GRAY: u32 = 0xff181818;
const LEMON: u32 = 0xffd1ffbd;

pub fn fill_window(window: &dyn Window) {
fill_window_ex(window, |_, _, buffer| buffer.fill(DARK_GRAY))
}

pub fn fill_window_with_border(window: &dyn Window) {
fill_window_ex(window, |width, height, buffer| {
for y in 0..height {
for x in 0..width {
let color = if (x == 0 || y == 0 || x == width - 1 || y == height - 1) {
LEMON
} else {
DARK_GRAY
};
buffer[y * width + x] = color;
}
}
})
}
pub fn fill_window_ex<F: Fn(usize, usize, &mut Buffer<&dyn Window, &dyn Window>)>(
window: &dyn Window,
f: F,
) {
GC.with(|gc| {
let size = window.surface_size();
let (Some(width), Some(height)) =
Expand All @@ -84,13 +107,15 @@ mod platform {
let surface =
gc.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window);

// Fill a buffer with a solid color.
const DARK_GRAY: u32 = 0xff181818;

surface.resize(width, height).expect("Failed to resize the softbuffer surface");

let mut buffer = surface.buffer_mut().expect("Failed to get the softbuffer buffer");
buffer.fill(DARK_GRAY);

let width = width.get() as usize;
let height = height.get() as usize;

f(width, height, &mut buffer);

buffer.present().expect("Failed to present the softbuffer buffer");
})
}
Expand All @@ -112,6 +137,10 @@ mod platform {
// No-op on mobile platforms.
}

pub fn fill_window_with_border(_window: &dyn winit::window::Window) {
// No-op on mobile platforms.
}

#[allow(dead_code)]
pub fn cleanup_window(_window: &dyn winit::window::Window) {
// No-op on mobile platforms.
Expand Down
1 change: 1 addition & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,4 @@ changelog entry.
- On macOS, fixed redundant `SurfaceResized` event at window creation.
- On Windows, fixed the event loop not waking on accessibility requests.
- On X11, fixed cursor grab mode state tracking on error.
- On Windows, fix `Window::inner_size` of undecorated window with shadows, reporting a size bigger than what's visible.
2 changes: 2 additions & 0 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,8 @@ unsafe fn public_window_callback_inner(
// with the Windows API.
params.rgrc[0].top += 1;
params.rgrc[0].bottom += 1;
params.rgrc[0].left += 1;
params.rgrc[0].right += 1;
}

result = ProcResult::Value(0);
Expand Down
73 changes: 61 additions & 12 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ use windows_sys::Win32::UI::Input::KeyboardAndMouse::{
use windows_sys::Win32::UI::Input::Touch::{RegisterTouchWindow, TWF_WANTPALM};
use windows_sys::Win32::UI::WindowsAndMessaging::{
CreateWindowExW, EnableMenuItem, FlashWindowEx, GetClientRect, GetCursorPos,
GetForegroundWindow, GetSystemMenu, GetSystemMetrics, GetWindowPlacement, GetWindowTextLengthW,
GetWindowTextW, IsWindowVisible, LoadCursorW, PeekMessageW, PostMessageW, RegisterClassExW,
SetCursor, SetCursorPos, SetForegroundWindow, SetMenuDefaultItem, SetWindowDisplayAffinity,
SetWindowPlacement, SetWindowPos, SetWindowTextW, TrackPopupMenu, CS_HREDRAW, CS_VREDRAW,
CW_USEDEFAULT, FLASHWINFO, FLASHW_ALL, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY,
GWLP_HINSTANCE, HTBOTTOM, HTBOTTOMLEFT, HTBOTTOMRIGHT, HTCAPTION, HTLEFT, HTRIGHT, HTTOP,
HTTOPLEFT, HTTOPRIGHT, MENU_ITEM_STATE, MFS_DISABLED, MFS_ENABLED, MF_BYCOMMAND, NID_READY,
PM_NOREMOVE, SC_CLOSE, SC_MAXIMIZE, SC_MINIMIZE, SC_MOVE, SC_RESTORE, SC_SIZE, SM_DIGITIZER,
SWP_ASYNCWINDOWPOS, SWP_NOACTIVATE, SWP_NOSIZE, SWP_NOZORDER, TPM_LEFTALIGN, TPM_RETURNCMD,
WDA_EXCLUDEFROMCAPTURE, WDA_NONE, WM_NCLBUTTONDOWN, WM_SYSCOMMAND, WNDCLASSEXW,
GetForegroundWindow, GetSystemMenu, GetSystemMetrics, GetWindowPlacement, GetWindowRect,
GetWindowTextLengthW, GetWindowTextW, IsWindowVisible, LoadCursorW, PeekMessageW, PostMessageW,
RegisterClassExW, SetCursor, SetCursorPos, SetForegroundWindow, SetMenuDefaultItem,
SetWindowDisplayAffinity, SetWindowPlacement, SetWindowPos, SetWindowTextW, TrackPopupMenu,
CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, FLASHWINFO, FLASHW_ALL, FLASHW_STOP, FLASHW_TIMERNOFG,
FLASHW_TRAY, GWLP_HINSTANCE, HTBOTTOM, HTBOTTOMLEFT, HTBOTTOMRIGHT, HTCAPTION, HTLEFT, HTRIGHT,
HTTOP, HTTOPLEFT, HTTOPRIGHT, MENU_ITEM_STATE, MFS_DISABLED, MFS_ENABLED, MF_BYCOMMAND,
NID_READY, PM_NOREMOVE, SC_CLOSE, SC_MAXIMIZE, SC_MINIMIZE, SC_MOVE, SC_RESTORE, SC_SIZE,
SM_DIGITIZER, SWP_ASYNCWINDOWPOS, SWP_NOACTIVATE, SWP_NOSIZE, SWP_NOZORDER, TPM_LEFTALIGN,
TPM_RETURNCMD, WDA_EXCLUDEFROMCAPTURE, WDA_NONE, WM_NCLBUTTONDOWN, WM_SYSCOMMAND, WNDCLASSEXW,
};

use crate::cursor::Cursor;
Expand Down Expand Up @@ -461,7 +461,30 @@ impl CoreWindow for Window {
rust-windowing/winit"
)
}
PhysicalSize::new((rect.right - rect.left) as u32, (rect.bottom - rect.top) as u32)

let mut width = rect.right - rect.left;
let mut height = rect.bottom - rect.top;

let window_flags = self.window_state_lock().window_flags;
if window_flags.contains(WindowFlags::MARKER_UNDECORATED_SHADOW)
&& !window_flags.contains(WindowFlags::MARKER_DECORATIONS)
{
let mut pt: POINT = unsafe { mem::zeroed() };
if unsafe { ClientToScreen(self.hwnd(), &mut pt) } == true.into() {
let mut window_rc: RECT = unsafe { mem::zeroed() };
if unsafe { GetWindowRect(self.hwnd(), &mut window_rc) } == true.into() {
let left_b = pt.x - window_rc.left;
let right_b = pt.x + width - window_rc.right;
let top_b = pt.y - window_rc.top;
let bottom_b = pt.y + height - window_rc.bottom;

width = width - left_b - right_b;
height = height - top_b - bottom_b;
}
}
}

PhysicalSize::new(width as u32, height as u32)
}

fn outer_size(&self) -> PhysicalSize<u32> {
Expand All @@ -475,9 +498,35 @@ impl CoreWindow for Window {

fn request_surface_size(&self, size: Size) -> Option<PhysicalSize<u32>> {
let scale_factor = self.scale_factor();
let physical_size = size.to_physical::<u32>(scale_factor);
let mut physical_size = size.to_physical::<u32>(scale_factor);

let window_flags = self.window_state_lock().window_flags;
if window_flags.contains(WindowFlags::MARKER_UNDECORATED_SHADOW)
&& !window_flags.contains(WindowFlags::MARKER_DECORATIONS)
{
let mut pt: POINT = unsafe { mem::zeroed() };
if unsafe { ClientToScreen(self.hwnd(), &mut pt) } == true.into() {
let mut window_rc: RECT = unsafe { mem::zeroed() };
if unsafe { GetWindowRect(self.hwnd(), &mut window_rc) } == true.into() {
let mut client_rc: RECT = unsafe { mem::zeroed() };
if unsafe { GetClientRect(self.hwnd(), &mut client_rc) } == true.into() {
let curr_width = client_rc.right - client_rc.left;
let curr_height = client_rc.bottom - client_rc.top;

let left_b = pt.x - window_rc.left;
let right_b: i32 = (pt.x + curr_width) - window_rc.right;
let top_b = pt.y - window_rc.top;
let bottom_b: i32 = (pt.y + curr_height) - window_rc.bottom;

physical_size.width =
(physical_size.width as i32 + (left_b - right_b)) as u32;
physical_size.height =
(physical_size.height as i32 + (top_b - bottom_b)) as u32;
}
}
}
}

window_flags.set_size(self.hwnd(), physical_size);

if physical_size != self.surface_size() {
Expand Down
Loading