From b0c73429f54603f84976cb5e84058f6db89d7993 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Sat, 2 Sep 2023 09:11:48 -0700 Subject: [PATCH] Updated inner size handling --- src/lib.rs | 8 +++++++- src/private.rs | 14 +++++++------- src/window.rs | 49 +++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4b22627..f4c1a13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ #![warn(missing_docs, clippy::pedantic)] #![deny(unsafe_code)] #![allow(clippy::module_name_repetitions)] +#![allow(clippy::missing_panics_doc)] // https://github.com/rust-lang/rust-clippy/issues/11436 mod private; mod window; @@ -78,9 +79,14 @@ where } } - /// Begins running the application. This function will never return. + /// Begins running the application. /// /// Internally this runs the [`EventLoop`]. + /// + /// # Errors + /// + /// Returns an [`EventLoopError`] upon the loop exiting due to an error. See + /// [`EventLoop::run`] for more information. pub fn run(mut self) -> Result<(), EventLoopError> { self.event_loop.run(move |event, target, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/src/private.rs b/src/private.rs index 7b3102f..636616c 100644 --- a/src/private.rs +++ b/src/private.rs @@ -4,8 +4,8 @@ use std::sync::{mpsc, Arc}; use winit::dpi::{PhysicalPosition, PhysicalSize}; use winit::error::OsError; use winit::event::{ - AxisId, DeviceId, ElementState, Ime, InnerSizeWriter, KeyEvent, Modifiers, MouseButton, - MouseScrollDelta, Touch, TouchPhase, + AxisId, DeviceId, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, Touch, + TouchPhase, }; use winit::event_loop::AsyncRequestSerial; use winit::window::{ActivationToken, Theme, WindowId}; @@ -197,7 +197,6 @@ pub enum WindowEvent { /// For more information about DPI in general, see the [`dpi`](crate::dpi) module. ScaleFactorChanged { scale_factor: f64, - inner_size_writer: InnerSizeWriter, }, /// The system window theme has changed. @@ -320,10 +319,11 @@ impl From for WindowEvent { winit::event::WindowEvent::Touch(touch) => Self::Touch(touch), winit::event::WindowEvent::ScaleFactorChanged { scale_factor, - inner_size_writer, - } => Self::ScaleFactorChanged { - scale_factor, - inner_size_writer, + .. // TODO use the suggested size from the writer + } => { + Self::ScaleFactorChanged { + scale_factor, + } }, winit::event::WindowEvent::ThemeChanged(theme) => Self::ThemeChanged(theme), winit::event::WindowEvent::Occluded(occluded) => Self::Occluded(occluded), diff --git a/src/window.rs b/src/window.rs index 620a437..e89bb90 100644 --- a/src/window.rs +++ b/src/window.rs @@ -474,20 +474,33 @@ where self.occluded = occluded; behavior.occlusion_changed(self); } - WindowEvent::ScaleFactorChanged { - scale_factor, - inner_size_writer: _, - } => { + WindowEvent::ScaleFactorChanged { scale_factor } => { + let factor_changed = scale_factor - self.scale; + let new_inner_size = if factor_changed.abs() >= f64::EPSILON { + // TODO use the suggested size from the writer + PhysicalSize { + width: self.inner_size.width + + lossy_f64_to_u32( + ((f64::from(self.inner_size.width)) * factor_changed).round(), + ), + height: self.inner_size.height + + lossy_f64_to_u32( + (f64::from(self.inner_size.height) * factor_changed).round(), + ), + } + } else { + self.inner_size + }; // Ensure both values are updated before any behavior // callbacks are invoked. self.scale = scale_factor; // TODO not sure how to implement now - // let inner_size_changed = self.inner_size != new_inner_size; - // self.inner_size = new_inner_size; - // behavior.scale_factor_changed(self); - // if inner_size_changed { - // behavior.resized(self); - // } + let inner_size_changed = self.inner_size != new_inner_size; + self.inner_size = new_inner_size; + behavior.scale_factor_changed(self); + if inner_size_changed { + behavior.resized(self); + } } WindowEvent::Resized(new_inner_size) => { if self.inner_size != new_inner_size { @@ -649,6 +662,12 @@ where } } +/// Performs `f64 as u32` but avoids clippy's lints. +#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] +fn lossy_f64_to_u32(value: f64) -> u32 { + value as u32 +} + impl Application for RunningWindow where AppMessage: Message, @@ -771,6 +790,11 @@ where /// Messages can be sent to the application's main thread using /// [`Application::send`]. Each time a message is received by the main event /// loop, `app_callback` will be invoked. + /// + /// # Errors + /// + /// Returns an [`EventLoopError`] upon the loop exiting due to an error. See + /// [`EventLoop::run`] for more information. fn run_with_event_callback( app_callback: impl FnMut(AppMessage, &Windows) -> AppMessage::Response + 'static, @@ -791,6 +815,11 @@ where /// Messages can be sent to the application's main thread using /// [`Application::send`]. Each time a message is received by the main event /// loop, `app_callback` will be invoked. + /// + /// # Errors + /// + /// Returns an [`EventLoopError`] upon the loop exiting due to an error. See + /// [`EventLoop::run`] for more information. fn run_with_context_and_event_callback( context: Self::Context, app_callback: impl FnMut(AppMessage, &Windows) -> AppMessage::Response