Skip to content

Commit 52d5529

Browse files
authored
Merge pull request #2297 from xStrom/dpi-fixes
Unify window size rounding strategy
2 parents 666ca5f + 30537d1 commit 52d5529

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

druid-shell/src/backend/gtk/window.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,9 @@ impl WindowBuilder {
428428
if let Some(min_size_dp) = self.min_size {
429429
let min_area = ScaledArea::from_dp(min_size_dp, scale);
430430
let min_size_px = min_area.size_px();
431-
win_state.drawing_area.set_size_request(
432-
min_size_px.width.round() as i32,
433-
min_size_px.height.round() as i32,
434-
);
431+
win_state
432+
.drawing_area
433+
.set_size_request(min_size_px.width as i32, min_size_px.height as i32);
435434
}
436435

437436
win_state
@@ -1052,15 +1051,18 @@ impl WindowHandle {
10521051
}
10531052
}
10541053

1054+
/// Sets the size of the window in display points
10551055
pub fn set_size(&self, size: Size) {
10561056
if let Some(state) = self.state.upgrade() {
1057-
let px = size.to_px(state.scale.get());
1057+
let area = ScaledArea::from_dp(size, state.scale.get());
1058+
let size_px = area.size_px();
10581059
state
10591060
.window
1060-
.resize(px.width.round() as i32, px.height.round() as i32)
1061+
.resize(size_px.width as i32, size_px.height as i32);
10611062
}
10621063
}
10631064

1065+
/// Gets the size of the window in display points
10641066
pub fn get_size(&self) -> Size {
10651067
if let Some(state) = self.state.upgrade() {
10661068
let (x, y) = state.window.size();

druid-shell/src/backend/mac/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,8 +1415,8 @@ impl WindowHandle {
14151415

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

druid-shell/src/backend/windows/window.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -560,14 +560,15 @@ impl MyWndProc {
560560
if let Some(hwnd) = self.handle.borrow().get_hwnd() {
561561
match op {
562562
DeferredOp::SetSize(size_dp) => unsafe {
563-
let size_px = size_dp.to_px(self.scale());
563+
let area = ScaledArea::from_dp(size_dp, self.scale());
564+
let size_px = area.size_px();
564565
if SetWindowPos(
565566
hwnd,
566567
HWND_TOPMOST,
567568
0,
568569
0,
569-
size_px.width.round() as i32,
570-
size_px.height.round() as i32,
570+
size_px.width as i32,
571+
size_px.height as i32,
571572
SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE,
572573
) == 0
573574
{
@@ -1248,9 +1249,10 @@ impl WndProc for MyWndProc {
12481249
let min_max_info = unsafe { &mut *(lparam as *mut MINMAXINFO) };
12491250
self.with_wnd_state(|s| {
12501251
if let Some(min_size_dp) = s.min_size {
1251-
let min_size_px = min_size_dp.to_px(self.scale());
1252-
min_max_info.ptMinTrackSize.x = min_size_px.width.round() as i32;
1253-
min_max_info.ptMinTrackSize.y = min_size_px.height.round() as i32;
1252+
let min_area = ScaledArea::from_dp(min_size_dp, self.scale());
1253+
let min_size_px = min_area.size_px();
1254+
min_max_info.ptMinTrackSize.x = min_size_px.width as i32;
1255+
min_max_info.ptMinTrackSize.y = min_size_px.height as i32;
12541256
}
12551257
});
12561258
Some(0)
@@ -1938,7 +1940,7 @@ impl WindowHandle {
19381940
}
19391941
}
19401942

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

1995-
// Sets the size of the window in DP
1997+
/// Sets the size of the window in display points
19961998
pub fn set_size(&self, size: Size) {
19971999
self.defer(DeferredOp::SetSize(size));
19982000
}
19992001

2000-
// Gets the size of the window in pixels
2002+
/// Gets the size of the window in display points
20012003
pub fn get_size(&self) -> Size {
20022004
if let Some(w) = self.state.upgrade() {
20032005
let hwnd = w.hwnd.get();
@@ -2016,7 +2018,7 @@ impl WindowHandle {
20162018
};
20172019
let width = rect.right - rect.left;
20182020
let height = rect.bottom - rect.top;
2019-
return Size::new(width as f64, height as f64);
2021+
return Size::new(width as f64, height as f64).to_dp(w.scale.get());
20202022
}
20212023
}
20222024
Size::new(0.0, 0.0)
@@ -2026,12 +2028,12 @@ impl WindowHandle {
20262028
self.defer(DeferredOp::SetResizable(resizable));
20272029
}
20282030

2029-
// Sets the window state.
2031+
/// Sets the window state.
20302032
pub fn set_window_state(&self, state: window::WindowState) {
20312033
self.defer(DeferredOp::SetWindowState(state));
20322034
}
20332035

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

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

0 commit comments

Comments
 (0)