Skip to content

Commit

Permalink
Fix incorrect size on window creation
Browse files Browse the repository at this point in the history
This is a followup to #1037 that fixes the problem where a window
created with default size ends up too big in hi-dpi contexts. It is a
minimal change, and doesn't address the flashing issue.
  • Loading branch information
raphlinus committed Sep 11, 2020
1 parent b631667 commit 440d201
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions druid-shell/src/platform/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub(crate) struct WindowBuilder {
present_strategy: PresentStrategy,
resizable: bool,
show_titlebar: bool,
size: Size,
size: Option<Size>,
min_size: Option<Size>,
position: Point,
state: window::WindowState,
Expand Down Expand Up @@ -1109,7 +1109,7 @@ impl WindowBuilder {
resizable: true,
show_titlebar: true,
present_strategy: Default::default(),
size: Size::new(CW_USEDEFAULT as f64, CW_USEDEFAULT as f64),
size: None,
min_size: None,
position: Point::new(CW_USEDEFAULT as f64, CW_USEDEFAULT as f64),
state: window::WindowState::RESTORED,
Expand All @@ -1122,7 +1122,7 @@ impl WindowBuilder {
}

pub fn set_size(&mut self, size: Size) {
self.size = size;
self.size = Some(size);
}

pub fn set_min_size(&mut self, size: Size) {
Expand Down Expand Up @@ -1168,8 +1168,12 @@ impl WindowBuilder {
};

let scale = Scale::new(1.0, 1.0);
let area = ScaledArea::from_dp(self.size, scale);
let size_px = area.size_px();
let mut area = ScaledArea::default();
let (width, height) = self.size.map(|size| {
area = ScaledArea::from_dp(size, scale);
let size_px = area.size_px();
(size_px.width as i32, size_px.height as i32)
}).unwrap_or((CW_USEDEFAULT, CW_USEDEFAULT));

let (hmenu, accels, has_menu) = match self.menu {
Some(menu) => {
Expand Down Expand Up @@ -1231,8 +1235,8 @@ impl WindowBuilder {
dwStyle,
self.position.x as i32,
self.position.y as i32,
size_px.width as i32,
size_px.height as i32,
width,
height,
0 as HWND,
hmenu,
0 as HINSTANCE,
Expand All @@ -1247,7 +1251,12 @@ impl WindowBuilder {
register_accel(hwnd, &accels);
}

handle.set_size(handle.get_size());
if let Some(size) = self.size {
// TODO: because this is deferred, it causes some flashing.
// Investigate a proper fix that gets the window created with
// the correct size.
handle.set_size(size);
}
handle.set_window_state(self.state);

Ok(handle)
Expand Down

0 comments on commit 440d201

Please sign in to comment.