Remove Option from HiDpiFactorChanged in favor of a bare PhysicalSize (#1346)

* Remove Option from HiDpiFactorChanged in favor of a bare PhysicalSize

* Fix macos and ios builds
This commit is contained in:
Osspial 2020-01-03 00:28:41 -05:00
parent 777d9edeaa
commit 55166da437
6 changed files with 40 additions and 41 deletions

View file

@ -302,13 +302,12 @@ pub enum WindowEvent<'a> {
/// ///
/// After this event callback has been processed, the window will be resized to whatever value /// After this event callback has been processed, the window will be resized to whatever value
/// is pointed to by the `new_inner_size` reference. By default, this will contain the size suggested /// is pointed to by the `new_inner_size` reference. By default, this will contain the size suggested
/// by the OS, but it can be changed to any value. If `new_inner_size` is set to `None`, no resizing /// by the OS, but it can be changed to any value.
/// will occur.
/// ///
/// For more information about DPI in general, see the [`dpi`](dpi/index.html) module. /// For more information about DPI in general, see the [`dpi`](dpi/index.html) module.
HiDpiFactorChanged { HiDpiFactorChanged {
hidpi_factor: f64, hidpi_factor: f64,
new_inner_size: &'a mut Option<PhysicalSize<u32>>, new_inner_size: &'a mut PhysicalSize<u32>,
}, },
/// The system window theme has changed. /// The system window theme has changed.

View file

@ -861,8 +861,8 @@ fn handle_hidpi_proxy(
hidpi_factor: f64, hidpi_factor: f64,
window_id: id, window_id: id,
) { ) {
let size = suggested_size.to_physical(hidpi_factor); let mut size = suggested_size.to_physical(hidpi_factor);
let new_inner_size = &mut Some(size); let new_inner_size = &mut size;
let event = Event::WindowEvent { let event = Event::WindowEvent {
window_id: RootWindowId(window_id.into()), window_id: RootWindowId(window_id.into()),
event: WindowEvent::HiDpiFactorChanged { event: WindowEvent::HiDpiFactorChanged {
@ -872,7 +872,7 @@ fn handle_hidpi_proxy(
}; };
event_handler.handle_nonuser_event(event, &mut control_flow); event_handler.handle_nonuser_event(event, &mut control_flow);
let (view, screen_frame) = get_view_and_screen_frame(window_id); let (view, screen_frame) = get_view_and_screen_frame(window_id);
if let Some(physical_size) = new_inner_size { let physical_size = *new_inner_size;
let logical_size = physical_size.to_logical(hidpi_factor); let logical_size = physical_size.to_logical(hidpi_factor);
let size = CGSize::new(logical_size); let size = CGSize::new(logical_size);
let new_frame: CGRect = CGRect::new(screen_frame.origin, size); let new_frame: CGRect = CGRect::new(screen_frame.origin, size);
@ -880,7 +880,6 @@ fn handle_hidpi_proxy(
let () = msg_send![view, setFrame: new_frame]; let () = msg_send![view, setFrame: new_frame];
} }
} }
}
fn get_view_and_screen_frame(window_id: id) -> (id, CGRect) { fn get_view_and_screen_frame(window_id: id) -> (id, CGRect) {
unsafe { unsafe {

View file

@ -730,7 +730,7 @@ impl<T> EventLoop<T> {
if let Some(dpi) = window.new_dpi { if let Some(dpi) = window.new_dpi {
let dpi = dpi as f64; let dpi = dpi as f64;
let logical_size = LogicalSize::<f64>::from(*window.size); let logical_size = LogicalSize::<f64>::from(*window.size);
let mut new_inner_size = Some(logical_size.to_physical(dpi)); let mut new_inner_size = logical_size.to_physical(dpi);
callback(Event::WindowEvent { callback(Event::WindowEvent {
window_id, window_id,
@ -740,13 +740,11 @@ impl<T> EventLoop<T> {
}, },
}); });
if let Some(new_size) = new_inner_size { let (w, h) = new_inner_size.to_logical::<u32>(dpi).into();
let (w, h) = new_size.to_logical::<u32>(dpi).into();
frame.resize(w, h); frame.resize(w, h);
*window.size = (w, h); *window.size = (w, h);
} }
} }
}
if window.closed { if window.closed {
callback(Event::WindowEvent { callback(Event::WindowEvent {
window_id, window_id,

View file

@ -422,7 +422,8 @@ impl<T: 'static> EventProcessor<T> {
height, height,
); );
let mut new_inner_size = Some(PhysicalSize::new(new_width, new_height)); let old_inner_size = PhysicalSize::new(width, height);
let mut new_inner_size = PhysicalSize::new(new_width, new_height);
callback(Event::WindowEvent { callback(Event::WindowEvent {
window_id, window_id,
@ -432,9 +433,12 @@ impl<T: 'static> EventProcessor<T> {
}, },
}); });
if let Some(new_size) = new_inner_size { if new_inner_size != old_inner_size {
window.set_inner_size_physical(new_size.width, new_size.height); window.set_inner_size_physical(
shared_state_lock.dpi_adjusted = Some(new_size.into()); new_inner_size.width,
new_inner_size.height,
);
shared_state_lock.dpi_adjusted = Some(new_inner_size.into());
// if the DPI factor changed, force a resize event to ensure the logical // if the DPI factor changed, force a resize event to ensure the logical
// size is computed with the right DPI factor // size is computed with the right DPI factor
resized = true; resized = true;
@ -1135,9 +1139,10 @@ impl<T: 'static> EventProcessor<T> {
*window_id, *window_id,
), ),
); );
let mut new_inner_size = Some( let old_inner_size =
PhysicalSize::new(new_width, new_height), PhysicalSize::new(width, height);
); let mut new_inner_size =
PhysicalSize::new(new_width, new_height);
callback(Event::WindowEvent { callback(Event::WindowEvent {
window_id, window_id,
@ -1147,9 +1152,9 @@ impl<T: 'static> EventProcessor<T> {
}, },
}); });
if let Some(new_size) = new_inner_size { if new_inner_size != old_inner_size {
let (new_width, new_height) = let (new_width, new_height) =
new_size.into(); new_inner_size.into();
window.set_inner_size_physical( window.set_inner_size_physical(
new_width, new_height, new_width, new_height,
); );

View file

@ -188,8 +188,8 @@ impl Handler {
suggested_size: LogicalSize<f64>, suggested_size: LogicalSize<f64>,
hidpi_factor: f64, hidpi_factor: f64,
) { ) {
let size = suggested_size.to_physical(hidpi_factor); let mut size = suggested_size.to_physical(hidpi_factor);
let new_inner_size = &mut Some(size); let new_inner_size = &mut size;
let event = Event::WindowEvent { let event = Event::WindowEvent {
window_id: WindowId(get_window_id(*ns_window)), window_id: WindowId(get_window_id(*ns_window)),
event: WindowEvent::HiDpiFactorChanged { event: WindowEvent::HiDpiFactorChanged {
@ -200,7 +200,7 @@ impl Handler {
callback.handle_nonuser_event(event, &mut *self.control_flow.lock().unwrap()); callback.handle_nonuser_event(event, &mut *self.control_flow.lock().unwrap());
let physical_size = new_inner_size.unwrap_or(size); let physical_size = *new_inner_size;
let logical_size = physical_size.to_logical(hidpi_factor); let logical_size = physical_size.to_logical(hidpi_factor);
let size = NSSize::new(logical_size.width, logical_size.height); let size = NSSize::new(logical_size.width, logical_size.height);
unsafe { NSWindow::setContentSize_(*ns_window, size) }; unsafe { NSWindow::setContentSize_(*ns_window, size) };

View file

@ -1486,27 +1486,25 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
(old_physical_inner_rect.bottom - old_physical_inner_rect.top) as u32, (old_physical_inner_rect.bottom - old_physical_inner_rect.top) as u32,
); );
// We calculate our own size because the default suggested rect doesn't do a great job
// of preserving the window's logical size.
let suggested_physical_inner_size = old_physical_inner_size
.to_logical::<f64>(old_dpi_factor)
.to_physical::<u32>(new_dpi_factor);
// `allow_resize` prevents us from re-applying DPI adjustment to the restored size after // `allow_resize` prevents us from re-applying DPI adjustment to the restored size after
// exiting fullscreen (the restored size is already DPI adjusted). // exiting fullscreen (the restored size is already DPI adjusted).
let mut new_inner_size_opt = let mut new_physical_inner_size = match allow_resize {
Some(suggested_physical_inner_size).filter(|_| allow_resize); // We calculate our own size because the default suggested rect doesn't do a great job
// of preserving the window's logical size.
true => old_physical_inner_size
.to_logical::<f64>(old_dpi_factor)
.to_physical::<u32>(new_dpi_factor),
false => old_physical_inner_size,
};
let _ = subclass_input.send_event_unbuffered(Event::WindowEvent { let _ = subclass_input.send_event_unbuffered(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)), window_id: RootWindowId(WindowId(window)),
event: HiDpiFactorChanged { event: HiDpiFactorChanged {
hidpi_factor: new_dpi_factor, hidpi_factor: new_dpi_factor,
new_inner_size: &mut new_inner_size_opt, new_inner_size: &mut new_physical_inner_size,
}, },
}); });
let new_physical_inner_size = new_inner_size_opt.unwrap_or(old_physical_inner_size);
// Unset maximized if we're changing the window's size. // Unset maximized if we're changing the window's size.
if new_physical_inner_size != old_physical_inner_size { if new_physical_inner_size != old_physical_inner_size {
WindowState::set_window_flags(subclass_input.window_state.lock(), window, |f| { WindowState::set_window_flags(subclass_input.window_state.lock(), window, |f| {