Use actual floats for logical coordinates

This commit is contained in:
Héctor Ramón Jiménez 2023-11-30 23:40:33 +01:00
parent 9f29aec128
commit 67408311f4
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
12 changed files with 165 additions and 136 deletions

View file

@ -732,7 +732,8 @@ pub fn run_command<A, C, E>(
});
}
window::Action::FetchSize(callback) => {
let size = window.inner_size();
let size =
window.inner_size().to_logical(window.scale_factor());
proxy
.send_event(callback(Size::new(
@ -747,10 +748,10 @@ pub fn run_command<A, C, E>(
window::Action::Minimize(minimized) => {
window.set_minimized(minimized);
}
window::Action::Move { x, y } => {
window::Action::Move(position) => {
window.set_outer_position(winit::dpi::LogicalPosition {
x,
y,
x: position.x,
y: position.y,
});
}
window::Action::ChangeMode(mode) => {

View file

@ -6,7 +6,7 @@ use crate::core::keyboard;
use crate::core::mouse;
use crate::core::touch;
use crate::core::window;
use crate::core::{Event, Point};
use crate::core::{Event, Point, Size};
/// Converts some [`window::Settings`] into a `WindowBuilder` from `winit`.
pub fn window_settings(
@ -17,11 +17,12 @@ pub fn window_settings(
) -> winit::window::WindowBuilder {
let mut window_builder = winit::window::WindowBuilder::new();
let (width, height) = settings.size;
window_builder = window_builder
.with_title(title)
.with_inner_size(winit::dpi::LogicalSize { width, height })
.with_inner_size(winit::dpi::LogicalSize {
width: settings.size.width,
height: settings.size.height,
})
.with_resizable(settings.resizable)
.with_enabled_buttons(if settings.resizable {
winit::window::WindowButtons::all()
@ -41,14 +42,20 @@ pub fn window_settings(
window_builder = window_builder.with_position(position);
}
if let Some((width, height)) = settings.min_size {
window_builder = window_builder
.with_min_inner_size(winit::dpi::LogicalSize { width, height });
if let Some(min_size) = settings.min_size {
window_builder =
window_builder.with_min_inner_size(winit::dpi::LogicalSize {
width: min_size.width,
height: min_size.height,
});
}
if let Some((width, height)) = settings.max_size {
window_builder = window_builder
.with_max_inner_size(winit::dpi::LogicalSize { width, height });
if let Some(max_size) = settings.max_size {
window_builder =
window_builder.with_max_inner_size(winit::dpi::LogicalSize {
width: max_size.width,
height: max_size.height,
});
}
#[cfg(any(
@ -277,15 +284,15 @@ pub fn window_level(level: window::Level) -> winit::window::WindowLevel {
/// [`winit`]: https://github.com/rust-windowing/winit
pub fn position(
monitor: Option<&winit::monitor::MonitorHandle>,
(width, height): (u32, u32),
size: Size,
position: window::Position,
) -> Option<winit::dpi::Position> {
match position {
window::Position::Default => None,
window::Position::Specific(x, y) => {
window::Position::Specific(position) => {
Some(winit::dpi::Position::Logical(winit::dpi::LogicalPosition {
x: f64::from(x),
y: f64::from(y),
x: f64::from(position.x),
y: f64::from(position.y),
}))
}
window::Position::Centered => {
@ -297,8 +304,8 @@ pub fn position(
let centered: winit::dpi::PhysicalPosition<i32> =
winit::dpi::LogicalPosition {
x: (resolution.width - f64::from(width)) / 2.0,
y: (resolution.height - f64::from(height)) / 2.0,
x: (resolution.width - f64::from(size.width)) / 2.0,
y: (resolution.height - f64::from(size.height)) / 2.0,
}
.to_physical(monitor.scale_factor());

View file

@ -5,8 +5,12 @@ mod windows;
pub use state::State;
use crate::conversion;
use crate::core;
use crate::core::mouse;
use crate::core::renderer;
use crate::core::widget::operation;
use crate::core::{self, mouse, renderer, window, Size};
use crate::core::window;
use crate::core::{Point, Size};
use crate::futures::futures::channel::mpsc;
use crate::futures::futures::{task, Future, StreamExt};
use crate::futures::{Executor, Runtime, Subscription};
@ -350,18 +354,18 @@ async fn run_instance<A, E, C>(
let mut mouse_interaction = mouse::Interaction::default();
let mut events =
if let Some((position, size)) = logical_bounds_of(windows.main()) {
vec![(
Some(window::Id::MAIN),
core::Event::Window(
window::Id::MAIN,
window::Event::Created { position, size },
),
)]
} else {
Vec::new()
};
let mut events = {
let (position, size) = logical_bounds_of(windows.main());
vec![(
Some(window::Id::MAIN),
core::Event::Window(
window::Id::MAIN,
window::Event::Created { position, size },
),
)]
};
let mut messages = Vec::new();
let mut redraw_pending = false;
@ -374,7 +378,7 @@ async fn run_instance<A, E, C>(
window,
exit_on_close_request,
} => {
let bounds = logical_bounds_of(&window);
let (position, size) = logical_bounds_of(&window);
let (inner_size, i) = windows.add(
&application,
@ -394,18 +398,13 @@ async fn run_instance<A, E, C>(
));
ui_caches.push(user_interface::Cache::default());
if let Some(bounds) = bounds {
events.push((
Some(id),
core::Event::Window(
id,
window::Event::Created {
position: bounds.0,
size: bounds.1,
},
),
));
}
events.push((
Some(id),
core::Event::Window(
id,
window::Event::Created { position, size },
),
));
}
Event::EventLoopAwakened(event) => {
match event {
@ -925,7 +924,8 @@ fn run_command<A, C, E>(
}
window::Action::FetchSize(callback) => {
let window = windows.with_raw(id);
let size = window.inner_size();
let size =
window.inner_size().to_logical(window.scale_factor());
proxy
.send_event(callback(Size::new(
@ -940,9 +940,12 @@ fn run_command<A, C, E>(
window::Action::Minimize(minimized) => {
windows.with_raw(id).set_minimized(minimized);
}
window::Action::Move { x, y } => {
window::Action::Move(position) => {
windows.with_raw(id).set_outer_position(
winit::dpi::LogicalPosition { x, y },
winit::dpi::LogicalPosition {
x: position.x,
y: position.y,
},
);
}
window::Action::ChangeMode(mode) => {
@ -1145,25 +1148,23 @@ pub fn user_force_quit(
}
}
fn logical_bounds_of(
window: &winit::window::Window,
) -> Option<((i32, i32), Size<u32>)> {
let scale = window.scale_factor();
let pos = window
fn logical_bounds_of(window: &winit::window::Window) -> (Option<Point>, Size) {
let position = window
.inner_position()
.map(|pos| {
((pos.x as f64 / scale) as i32, (pos.y as f64 / scale) as i32)
})
.ok()?;
.ok()
.map(|position| position.to_logical(window.scale_factor()))
.map(|position| Point {
x: position.x,
y: position.y,
});
let size = {
let size = window.inner_size();
Size::new(
(size.width as f64 / scale) as u32,
(size.height as f64 / scale) as u32,
)
let size = window.inner_size().to_logical(window.scale_factor());
Size::new(size.width, size.height)
};
Some((pos, size))
(position, size)
}
#[cfg(not(target_arch = "wasm32"))]