Skip to content

Commit

Permalink
Merge pull request #2297 from xStrom/dpi-fixes
Browse files Browse the repository at this point in the history
Unify window size rounding strategy
  • Loading branch information
xStrom committed Dec 1, 2022
2 parents 666ca5f + 30537d1 commit 52d5529
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
14 changes: 8 additions & 6 deletions druid-shell/src/backend/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,9 @@ impl WindowBuilder {
if let Some(min_size_dp) = self.min_size {
let min_area = ScaledArea::from_dp(min_size_dp, scale);
let min_size_px = min_area.size_px();
win_state.drawing_area.set_size_request(
min_size_px.width.round() as i32,
min_size_px.height.round() as i32,
);
win_state
.drawing_area
.set_size_request(min_size_px.width as i32, min_size_px.height as i32);
}

win_state
Expand Down Expand Up @@ -1052,15 +1051,18 @@ impl WindowHandle {
}
}

/// Sets the size of the window in display points
pub fn set_size(&self, size: Size) {
if let Some(state) = self.state.upgrade() {
let px = size.to_px(state.scale.get());
let area = ScaledArea::from_dp(size, state.scale.get());
let size_px = area.size_px();
state
.window
.resize(px.width.round() as i32, px.height.round() as i32)
.resize(size_px.width as i32, size_px.height as i32);
}
}

/// Gets the size of the window in display points
pub fn get_size(&self) -> Size {
if let Some(state) = self.state.upgrade() {
let (x, y) = state.window.size();
Expand Down
4 changes: 2 additions & 2 deletions druid-shell/src/backend/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,8 +1415,8 @@ impl WindowHandle {

/// Get the `Scale` of the window.
pub fn get_scale(&self) -> Result<Scale, Error> {
// TODO: Get actual Scale
Ok(Scale::new(1.0, 1.0))
let scale_factor: CGFloat = unsafe { msg_send![*self.nsview.load(), backingScaleFactor] };
Ok(Scale::new(scale_factor, scale_factor))
}
}

Expand Down
28 changes: 15 additions & 13 deletions druid-shell/src/backend/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,14 +560,15 @@ impl MyWndProc {
if let Some(hwnd) = self.handle.borrow().get_hwnd() {
match op {
DeferredOp::SetSize(size_dp) => unsafe {
let size_px = size_dp.to_px(self.scale());
let area = ScaledArea::from_dp(size_dp, self.scale());
let size_px = area.size_px();
if SetWindowPos(
hwnd,
HWND_TOPMOST,
0,
0,
size_px.width.round() as i32,
size_px.height.round() as i32,
size_px.width as i32,
size_px.height as i32,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE,
) == 0
{
Expand Down Expand Up @@ -1248,9 +1249,10 @@ impl WndProc for MyWndProc {
let min_max_info = unsafe { &mut *(lparam as *mut MINMAXINFO) };
self.with_wnd_state(|s| {
if let Some(min_size_dp) = s.min_size {
let min_size_px = min_size_dp.to_px(self.scale());
min_max_info.ptMinTrackSize.x = min_size_px.width.round() as i32;
min_max_info.ptMinTrackSize.y = min_size_px.height.round() as i32;
let min_area = ScaledArea::from_dp(min_size_dp, self.scale());
let min_size_px = min_area.size_px();
min_max_info.ptMinTrackSize.x = min_size_px.width as i32;
min_max_info.ptMinTrackSize.y = min_size_px.height as i32;
}
});
Some(0)
Expand Down Expand Up @@ -1938,7 +1940,7 @@ impl WindowHandle {
}
}

// Gets the position of the window in virtual screen coordinates
/// Gets the position of the window in virtual screen coordinates
pub fn get_position(&self) -> Point {
if let Some(w) = self.state.upgrade() {
let hwnd = w.hwnd.get();
Expand Down Expand Up @@ -1992,12 +1994,12 @@ impl WindowHandle {
Insets::ZERO
}

// Sets the size of the window in DP
/// Sets the size of the window in display points
pub fn set_size(&self, size: Size) {
self.defer(DeferredOp::SetSize(size));
}

// Gets the size of the window in pixels
/// Gets the size of the window in display points
pub fn get_size(&self) -> Size {
if let Some(w) = self.state.upgrade() {
let hwnd = w.hwnd.get();
Expand All @@ -2016,7 +2018,7 @@ impl WindowHandle {
};
let width = rect.right - rect.left;
let height = rect.bottom - rect.top;
return Size::new(width as f64, height as f64);
return Size::new(width as f64, height as f64).to_dp(w.scale.get());
}
}
Size::new(0.0, 0.0)
Expand All @@ -2026,12 +2028,12 @@ impl WindowHandle {
self.defer(DeferredOp::SetResizable(resizable));
}

// Sets the window state.
/// Sets the window state.
pub fn set_window_state(&self, state: window::WindowState) {
self.defer(DeferredOp::SetWindowState(state));
}

// Gets the window state.
/// Gets the window state.
pub fn get_window_state(&self) -> window::WindowState {
// We can not store state internally because it could be modified externally.
if let Some(w) = self.state.upgrade() {
Expand All @@ -2057,7 +2059,7 @@ impl WindowHandle {
}
}

// Allows windows to handle a custom titlebar like it was the default one.
/// Allows windows to handle a custom titlebar like it was the default one.
pub fn handle_titlebar(&self, val: bool) {
if let Some(w) = self.state.upgrade() {
w.handle_titlebar.set(val);
Expand Down

0 comments on commit 52d5529

Please sign in to comment.