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 3 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
64 changes: 64 additions & 0 deletions examples/undecorated_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![allow(unused)]

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, WindowId};

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

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

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

impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let mut attrs = Window::default_attributes().with_decorations(false);
#[cfg(windows)]
{
attrs = attrs.with_undecorated_shadow(false);
}

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

fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
match event {
#[cfg(windows)]
WindowEvent::KeyboardInput {
event: KeyEvent { logical_key: Key::Character(c), state: ElementState::Pressed, .. },
..
} if c.as_ref() == "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);
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(all(feature = "rwh_05", 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 @@ -68,7 +67,31 @@ mod platform {
}
}

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

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

pub fn fill_window_with_border(window: &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<&Window, &Window>)>(
window: &Window,
f: F,
) {
GC.with(|gc| {
let size = window.inner_size();
let (Some(width), Some(height)) =
Expand All @@ -82,13 +105,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 @@ -110,6 +135,10 @@ mod platform {
// No-op on mobile platforms.
}

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

#[allow(dead_code)]
pub fn cleanup_window(_window: &winit::window::Window) {
// No-op on mobile platforms.
Expand Down
4 changes: 4 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ changelog entry.
- Remove `EventLoop::run`.
- Remove `EventLoopExtRunOnDemand::run_on_demand`.
- Remove `EventLoopExtPumpEvents::pump_events`.

### Fixed

- 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 @@ -1136,6 +1136,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
43 changes: 32 additions & 11 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,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 tracing::warn;
Expand Down Expand Up @@ -213,13 +213,34 @@ impl Window {
#[inline]
pub fn inner_size(&self) -> PhysicalSize<u32> {
let mut rect: RECT = unsafe { mem::zeroed() };

if unsafe { GetClientRect(self.hwnd(), &mut rect) } == false.into() {
panic!(
"Unexpected GetClientRect failure: please report this error to \
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;

if self.window_state_lock().window_flags.contains(WindowFlags::MARKER_UNDECORATED_SHADOW) {
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)
}

#[inline]
Expand Down
Loading