Use f32 for scale_factor

This commit is contained in:
Héctor Ramón Jiménez 2025-09-02 23:29:22 +02:00
parent ad0e4c53cf
commit 74b792b608
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
14 changed files with 56 additions and 51 deletions

View file

@ -15,7 +15,7 @@ pub struct Screenshot {
pub size: Size<u32>,
/// 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<Bytes>,
size: Size<u32>,
scale_factor: f64,
scale_factor: f32,
) -> Self {
Self {
bytes: bytes.into(),

View file

@ -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)
}

View file

@ -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);

View file

@ -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::<f64>()
.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)

View file

@ -5,19 +5,19 @@ use crate::core::{Size, Transformation};
pub struct Viewport {
physical_size: Size<u32>,
logical_size: Size<f32>,
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<u32>, scale_factor: f64) -> Viewport {
pub fn with_physical_size(size: Size<u32>, 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
}

View file

@ -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<P: Program>(
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<P: Program>(
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<P: Program>(
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<P: Program>(
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<P: Program>(
/// Decorates a [`Program`] with the given scale factor function.
pub fn with_scale_factor<P: Program>(
program: P,
f: impl Fn(&P::State, window::Id) -> f64,
f: impl Fn(&P::State, window::Id) -> f32,
) -> impl Program<State = P::State, Message = P::Message, Theme = P::Theme> {
struct WithScaleFactor<P, F> {
program: P,
@ -431,7 +431,7 @@ pub fn with_scale_factor<P: Program>(
impl<P: Program, F> Program for WithScaleFactor<P, F>
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<P: Program>(
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<P: Program, E: 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<P: Program> Instance<P> {
}
/// 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)
}
}

View file

@ -387,7 +387,7 @@ impl<P: Program> Application<P> {
/// 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<State = P::State, Message = P::Message, Theme = P::Theme>,
> {

View file

@ -232,7 +232,7 @@ impl<P: Program> Daemon<P> {
/// 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<State = P::State, Message = P::Message, Theme = P::Theme>,
> {

View file

@ -462,7 +462,7 @@ where
screenshot: window::Screenshot::new(
rgba,
physical_size,
f64::from(scale_factor),
scale_factor,
),
renderer: self.renderer.name(),
})

View file

@ -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<u8> {
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)
}

View file

@ -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::<f32>::from(Rectangle::with_size(
viewport.physical_size(),
));
@ -879,7 +879,7 @@ impl renderer::Headless for Renderer {
background_color: Color,
) -> Vec<u8> {
self.screenshot(
&Viewport::with_physical_size(size, f64::from(scale_factor)),
&Viewport::with_physical_size(size, scale_factor),
background_color,
)
}

View file

@ -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<winit::monitor::MonitorHandle>,
_id: Option<String>,
) -> 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<Event> {
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::<f64>(scale_factor);
let position = position.to_logical::<f64>(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<f64>,
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::<f64>(scale_factor);
let location =
touch.location.to_logical::<f64>(f64::from(scale_factor));
Point::new(location.x as f32, location.y as f32)
};

View file

@ -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<winit::monitor::MonitorHandle>,
on_open: oneshot::Sender<window::Id>,
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));
}

View file

@ -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<winit::dpi::PhysicalPosition<f64>>,
@ -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);