diff --git a/core/src/window/screenshot.rs b/core/src/window/screenshot.rs index 424168bb..5bb9334b 100644 --- a/core/src/window/screenshot.rs +++ b/core/src/window/screenshot.rs @@ -15,7 +15,7 @@ pub struct Screenshot { pub size: Size, /// The scale factor of the [`Screenshot`]. This can be useful when converting between widget /// bounds (which are in logical pixels) to crop screenshots. - pub scale_factor: f64, + pub scale_factor: f32, } impl Debug for Screenshot { @@ -35,7 +35,7 @@ impl Screenshot { pub fn new( bytes: impl Into, size: Size, - scale_factor: f64, + scale_factor: f32, ) -> Self { Self { bytes: bytes.into(), diff --git a/devtools/src/lib.rs b/devtools/src/lib.rs index 45409267..09a2a67b 100644 --- a/devtools/src/lib.rs +++ b/devtools/src/lib.rs @@ -98,7 +98,7 @@ where state.style(&self.program, theme) } - fn scale_factor(&self, state: &Self::State, window: window::Id) -> f64 { + fn scale_factor(&self, state: &Self::State, window: window::Id) -> f32 { state.scale_factor(&self.program, window) } } @@ -397,7 +397,7 @@ where program.style(self.state(), theme) } - fn scale_factor(&self, program: &P, window: window::Id) -> f64 { + fn scale_factor(&self, program: &P, window: window::Id) -> f32 { program.scale_factor(self.state(), window) } diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index b3c08879..b98b8273 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -67,7 +67,7 @@ pub fn main() -> Result<(), winit::error::EventLoopError> { let physical_size = window.inner_size(); let viewport = Viewport::with_physical_size( Size::new(physical_size.width, physical_size.height), - window.scale_factor(), + window.scale_factor() as f32, ); let clipboard = Clipboard::connect(window.clone()); @@ -212,7 +212,7 @@ pub fn main() -> Result<(), winit::error::EventLoopError> { *viewport = Viewport::with_physical_size( Size::new(size.width, size.height), - window.scale_factor(), + window.scale_factor() as f32, ); surface.configure( @@ -345,7 +345,7 @@ pub fn main() -> Result<(), winit::error::EventLoopError> { // Map window event to iced event if let Some(event) = conversion::window_event( event, - window.scale_factor(), + window.scale_factor() as f32, *modifiers, ) { events.push(event); diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 77be505b..cb5e6f43 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -26,7 +26,7 @@ struct Example { struct Window { title: String, scale_input: String, - current_scale: f64, + current_scale: f32, theme: Theme, } @@ -113,7 +113,7 @@ impl Example { Message::ScaleChanged(id, scale) => { if let Some(window) = self.windows.get_mut(&id) { window.current_scale = scale - .parse::() + .parse() .unwrap_or(window.current_scale) .clamp(0.5, 5.0); } @@ -146,7 +146,7 @@ impl Example { } } - fn scale_factor(&self, window: window::Id) -> f64 { + fn scale_factor(&self, window: window::Id) -> f32 { self.windows .get(&window) .map(|window| window.current_scale) diff --git a/graphics/src/viewport.rs b/graphics/src/viewport.rs index dc8e21d3..ea4cd8b2 100644 --- a/graphics/src/viewport.rs +++ b/graphics/src/viewport.rs @@ -5,19 +5,19 @@ use crate::core::{Size, Transformation}; pub struct Viewport { physical_size: Size, logical_size: Size, - scale_factor: f64, + scale_factor: f32, projection: Transformation, } impl Viewport { /// Creates a new [`Viewport`] with the given physical dimensions and scale /// factor. - pub fn with_physical_size(size: Size, scale_factor: f64) -> Viewport { + pub fn with_physical_size(size: Size, scale_factor: f32) -> Viewport { Viewport { physical_size: size, logical_size: Size::new( - (size.width as f64 / scale_factor) as f32, - (size.height as f64 / scale_factor) as f32, + size.width as f32 / scale_factor, + size.height as f32 / scale_factor, ), scale_factor, projection: Transformation::orthographic(size.width, size.height), @@ -45,7 +45,7 @@ impl Viewport { } /// Returns the scale factor of the [`Viewport`]. - pub fn scale_factor(&self) -> f64 { + pub fn scale_factor(&self) -> f32 { self.scale_factor } diff --git a/program/src/lib.rs b/program/src/lib.rs index e25cdb22..d479d2f8 100644 --- a/program/src/lib.rs +++ b/program/src/lib.rs @@ -94,7 +94,7 @@ pub trait Program: Sized { theme::Base::base(theme) } - fn scale_factor(&self, _state: &Self::State, _window: window::Id) -> f64 { + fn scale_factor(&self, _state: &Self::State, _window: window::Id) -> f32 { 1.0 } } @@ -171,7 +171,7 @@ pub fn with_title( self.program.style(state, theme) } - fn scale_factor(&self, state: &Self::State, window: window::Id) -> f64 { + fn scale_factor(&self, state: &Self::State, window: window::Id) -> f32 { self.program.scale_factor(state, window) } } @@ -250,7 +250,7 @@ pub fn with_subscription( self.program.style(state, theme) } - fn scale_factor(&self, state: &Self::State, window: window::Id) -> f64 { + fn scale_factor(&self, state: &Self::State, window: window::Id) -> f32 { self.program.scale_factor(state, window) } } @@ -332,7 +332,7 @@ pub fn with_theme( self.program.style(state, theme) } - fn scale_factor(&self, state: &Self::State, window: window::Id) -> f64 { + fn scale_factor(&self, state: &Self::State, window: window::Id) -> f32 { self.program.scale_factor(state, window) } } @@ -411,7 +411,7 @@ pub fn with_style( self.program.theme(state, window) } - fn scale_factor(&self, state: &Self::State, window: window::Id) -> f64 { + fn scale_factor(&self, state: &Self::State, window: window::Id) -> f32 { self.program.scale_factor(state, window) } } @@ -422,7 +422,7 @@ pub fn with_style( /// Decorates a [`Program`] with the given scale factor function. pub fn with_scale_factor( program: P, - f: impl Fn(&P::State, window::Id) -> f64, + f: impl Fn(&P::State, window::Id) -> f32, ) -> impl Program { struct WithScaleFactor { program: P, @@ -431,7 +431,7 @@ pub fn with_scale_factor( impl Program for WithScaleFactor where - F: Fn(&P::State, window::Id) -> f64, + F: Fn(&P::State, window::Id) -> f32, { type State = P::State; type Message = P::Message; @@ -490,7 +490,7 @@ pub fn with_scale_factor( self.program.style(state, theme) } - fn scale_factor(&self, state: &Self::State, window: window::Id) -> f64 { + fn scale_factor(&self, state: &Self::State, window: window::Id) -> f32 { (self.scale_factor)(state, window) } } @@ -573,7 +573,7 @@ pub fn with_executor( self.program.style(state, theme) } - fn scale_factor(&self, state: &Self::State, window: window::Id) -> f64 { + fn scale_factor(&self, state: &Self::State, window: window::Id) -> f32 { self.program.scale_factor(state, window) } } @@ -638,7 +638,7 @@ impl Instance

{ } /// Returns the current scale factor of the [`Instance`]. - pub fn scale_factor(&self, window: window::Id) -> f64 { + pub fn scale_factor(&self, window: window::Id) -> f32 { self.program.scale_factor(&self.state, window) } } diff --git a/src/application.rs b/src/application.rs index e735218b..d8f7862b 100644 --- a/src/application.rs +++ b/src/application.rs @@ -387,7 +387,7 @@ impl Application

{ /// Sets the scale factor of the [`Application`]. pub fn scale_factor( self, - f: impl Fn(&P::State) -> f64, + f: impl Fn(&P::State) -> f32, ) -> Application< impl Program, > { diff --git a/src/daemon.rs b/src/daemon.rs index 2074cf71..05d276c3 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -232,7 +232,7 @@ impl Daemon

{ /// Sets the scale factor of the [`Daemon`]. pub fn scale_factor( self, - f: impl Fn(&P::State, window::Id) -> f64, + f: impl Fn(&P::State, window::Id) -> f32, ) -> Daemon< impl Program, > { diff --git a/test/src/lib.rs b/test/src/lib.rs index 636b8173..30b08176 100644 --- a/test/src/lib.rs +++ b/test/src/lib.rs @@ -462,7 +462,7 @@ where screenshot: window::Screenshot::new( rgba, physical_size, - f64::from(scale_factor), + scale_factor, ), renderer: self.renderer.name(), }) diff --git a/tiny_skia/src/lib.rs b/tiny_skia/src/lib.rs index ca00737f..e06c7820 100644 --- a/tiny_skia/src/lib.rs +++ b/tiny_skia/src/lib.rs @@ -72,7 +72,7 @@ impl Renderer { damage: &[Rectangle], background_color: Color, ) { - let scale_factor = viewport.scale_factor() as f32; + let scale_factor = viewport.scale_factor(); self.layers.flush(); @@ -405,8 +405,7 @@ impl renderer::Headless for Renderer { scale_factor: f32, background_color: Color, ) -> Vec { - let viewport = - Viewport::with_physical_size(size, f64::from(scale_factor)); + let viewport = Viewport::with_physical_size(size, scale_factor); window::compositor::screenshot(self, &viewport, background_color) } diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index a24677bd..e55ad9a4 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -302,7 +302,7 @@ impl Renderer { encoder: &mut wgpu::CommandEncoder, viewport: &Viewport, ) { - let scale_factor = viewport.scale_factor() as f32; + let scale_factor = viewport.scale_factor(); self.text_viewport .update(&self.engine.queue, viewport.physical_size()); @@ -464,7 +464,7 @@ impl Renderer { #[cfg(any(feature = "svg", feature = "image"))] let image_cache = self.image_cache.borrow(); - let scale_factor = viewport.scale_factor() as f32; + let scale_factor = viewport.scale_factor(); let physical_bounds = Rectangle::::from(Rectangle::with_size( viewport.physical_size(), )); @@ -879,7 +879,7 @@ impl renderer::Headless for Renderer { background_color: Color, ) -> Vec { self.screenshot( - &Viewport::with_physical_size(size, f64::from(scale_factor)), + &Viewport::with_physical_size(size, scale_factor), background_color, ) } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 4b9fcc0d..6f22405d 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -13,6 +13,7 @@ use crate::core::{Event, Point, Size}; pub fn window_attributes( settings: window::Settings, title: &str, + scale_factor: f32, primary_monitor: Option, _id: Option, ) -> winit::window::WindowAttributes { @@ -21,8 +22,8 @@ pub fn window_attributes( attributes = attributes .with_title(title) .with_inner_size(winit::dpi::LogicalSize { - width: settings.size.width, - height: settings.size.height, + width: settings.size.width * scale_factor, + height: settings.size.height * scale_factor, }) .with_maximized(settings.maximized) .with_fullscreen( @@ -138,7 +139,7 @@ pub fn window_attributes( /// Converts a winit window event into an iced event. pub fn window_event( event: winit::event::WindowEvent, - scale_factor: f64, + scale_factor: f32, modifiers: winit::keyboard::ModifiersState, ) -> Option { use winit::event::Ime; @@ -146,7 +147,7 @@ pub fn window_event( match event { WindowEvent::Resized(new_size) => { - let logical_size = new_size.to_logical(scale_factor); + let logical_size = new_size.to_logical(f64::from(scale_factor)); Some(Event::Window(window::Event::Resized(Size { width: logical_size.width, @@ -157,7 +158,7 @@ pub fn window_event( Some(Event::Window(window::Event::CloseRequested)) } WindowEvent::CursorMoved { position, .. } => { - let position = position.to_logical::(scale_factor); + let position = position.to_logical::(f64::from(scale_factor)); Some(Event::Mouse(mouse::Event::CursorMoved { position: Point::new(position.x as f32, position.y as f32), @@ -313,7 +314,7 @@ pub fn window_event( } WindowEvent::Moved(position) => { let winit::dpi::LogicalPosition { x, y } = - position.to_logical(scale_factor); + position.to_logical(f64::from(scale_factor)); Some(Event::Window(window::Event::Moved(Point::new(x, y)))) } @@ -513,9 +514,9 @@ pub fn modifiers( /// Converts a physical cursor position to a logical `Point`. pub fn cursor_position( position: winit::dpi::PhysicalPosition, - scale_factor: f64, + scale_factor: f32, ) -> Point { - let logical_position = position.to_logical(scale_factor); + let logical_position = position.to_logical(f64::from(scale_factor)); Point::new(logical_position.x, logical_position.y) } @@ -526,11 +527,12 @@ pub fn cursor_position( /// [`iced`]: https://github.com/iced-rs/iced/tree/0.12 pub fn touch_event( touch: winit::event::Touch, - scale_factor: f64, + scale_factor: f32, ) -> touch::Event { let id = touch::Finger(touch.id); let position = { - let location = touch.location.to_logical::(scale_factor); + let location = + touch.location.to_logical::(f64::from(scale_factor)); Point::new(location.x as f32, location.y as f32) }; diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 0ad2c507..edfc7783 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -310,6 +310,7 @@ where id, settings, title, + scale_factor, monitor, on_open, } => { @@ -326,6 +327,7 @@ where conversion::window_attributes( settings, &title, + scale_factor, monitor .or(event_loop.primary_monitor()), self.id.clone(), @@ -480,6 +482,7 @@ enum Control { title: String, monitor: Option, on_open: oneshot::Sender, + scale_factor: f32, }, } @@ -1130,6 +1133,7 @@ fn run_action<'a, P, C>( id, settings, title: program.title(id), + scale_factor: program.scale_factor(id), monitor, on_open: channel, }) @@ -1234,7 +1238,7 @@ fn run_action<'a, P, C>( let size = window .raw .inner_size() - .to_logical(window.raw.scale_factor()); + .to_logical(f64::from(window.state.scale_factor())); let _ = channel.send(Size::new(size.width, size.height)); } diff --git a/winit/src/window/state.rs b/winit/src/window/state.rs index e17b32a3..c345a846 100644 --- a/winit/src/window/state.rs +++ b/winit/src/window/state.rs @@ -15,7 +15,7 @@ where P::Theme: theme::Base, { title: String, - scale_factor: f64, + scale_factor: f32, viewport: Viewport, viewport_version: u64, cursor_position: Option>, @@ -60,7 +60,7 @@ where Viewport::with_physical_size( Size::new(physical_size.width, physical_size.height), - window.scale_factor() * scale_factor, + window.scale_factor() as f32 * scale_factor, ) }; @@ -99,7 +99,7 @@ where } /// Returns the current scale factor of the [`Viewport`] of the [`State`]. - pub fn scale_factor(&self) -> f64 { + pub fn scale_factor(&self) -> f32 { self.viewport.scale_factor() } @@ -144,7 +144,7 @@ where self.viewport = Viewport::with_physical_size( size, - window.scale_factor() * self.scale_factor, + window.scale_factor() as f32 * self.scale_factor, ); self.viewport_version = self.viewport_version.wrapping_add(1); @@ -157,7 +157,7 @@ where self.viewport = Viewport::with_physical_size( size, - new_scale_factor * self.scale_factor, + *new_scale_factor as f32 * self.scale_factor, ); self.viewport_version = self.viewport_version.wrapping_add(1); @@ -208,7 +208,7 @@ where { self.viewport = Viewport::with_physical_size( Size::new(new_size.width, new_size.height), - window.scale_factor() * new_scale_factor, + window.scale_factor() as f32 * new_scale_factor, ); self.viewport_version = self.viewport_version.wrapping_add(1);