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

[Windows] Adjust min_dimensions and max_dimensions using AdjustWindowRectEx. #444

Merged
merged 1 commit into from
Apr 13, 2018
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Fixed thread safety issues with input methods on X11.
- Add support for `Touch` for win32 backend.
- Fixed `Window::get_inner_size` and friends to return the size in pixels instead of points when using HIDPI displays on OSX.
- Properly calculate the minimum and maximum window size on Windows, including window decorations.

# Version 0.11.3 (2018-03-28)

Expand Down
23 changes: 13 additions & 10 deletions src/platform/windows/events_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use winapi::um::{winuser, shellapi, processthreadsapi};
use winapi::um::winnt::LONG;

use platform::platform::event;
use platform::platform::window::adjust_size;
use platform::platform::Cursor;
use platform::platform::WindowId;
use platform::platform::DEVICE_ID;
Expand Down Expand Up @@ -898,18 +899,20 @@ pub unsafe extern "system" fn callback(window: HWND, msg: UINT,
if let Some(wstash) = cstash.windows.get(&window) {
let window_state = wstash.lock().unwrap();

match window_state.attributes.min_dimensions {
Some((width, height)) => {
(*mmi).ptMinTrackSize = POINT { x: width as i32, y: height as i32 };
},
None => { }
}
if window_state.attributes.min_dimensions.is_some() ||
window_state.attributes.max_dimensions.is_some() {

let style = winuser::GetWindowLongA(window, winuser::GWL_STYLE) as DWORD;
let ex_style = winuser::GetWindowLongA(window, winuser::GWL_EXSTYLE) as DWORD;

match window_state.attributes.max_dimensions {
Some((width, height)) => {
if let Some(min_dimensions) = window_state.attributes.min_dimensions {
let (width, height) = adjust_size(min_dimensions, style, ex_style);
(*mmi).ptMinTrackSize = POINT { x: width as i32, y: height as i32 };
}
if let Some(max_dimensions) = window_state.attributes.max_dimensions {
let (width, height) = adjust_size(max_dimensions, style, ex_style);
(*mmi).ptMaxTrackSize = POINT { x: width as i32, y: height as i32 };
},
None => { }
}
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/platform/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,14 @@ pub struct WindowWrapper(HWND, HDC);
// https://github.com/retep998/winapi-rs/issues/396
unsafe impl Send for WindowWrapper {}

pub unsafe fn adjust_size(
(x, y): (u32, u32), style: DWORD, ex_style: DWORD,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, I never knew you could destructure a tuple in the function parameters. Thanks for teaching me something new today!

) -> (LONG, LONG) {
let mut rect = RECT { left: 0, right: x as LONG, top: 0, bottom: y as LONG };
winuser::AdjustWindowRectEx(&mut rect, style, 0, ex_style);
(rect.right - rect.left, rect.bottom - rect.top)
}

unsafe fn init(window: WindowAttributes, pl_attribs: PlatformSpecificWindowBuilderAttributes,
inserter: events_loop::Inserter, events_loop_proxy: events_loop::EventsLoopProxy) -> Result<Window, CreationError> {
let title = OsStr::new(&window.title).encode_wide().chain(Some(0).into_iter())
Expand Down Expand Up @@ -659,10 +667,10 @@ unsafe fn init(window: WindowAttributes, pl_attribs: PlatformSpecificWindowBuild
let real_window = {
let (width, height) = if window.dimensions.is_some() {
let min_dimensions = window.min_dimensions
.map(|d| (d.0 as raw::c_int, d.1 as raw::c_int))
.map(|d| adjust_size(d, style, ex_style))
.unwrap_or((0, 0));
let max_dimensions = window.max_dimensions
.map(|d| (d.0 as raw::c_int, d.1 as raw::c_int))
.map(|d| adjust_size(d, style, ex_style))
.unwrap_or((raw::c_int::max_value(), raw::c_int::max_value()));

(
Expand Down