diff --git a/core/src/point.rs b/core/src/point.rs index 9bf7726b8e..ef42852f1b 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -1,26 +1,34 @@ use crate::Vector; +use num_traits::{Float, Num}; +use std::fmt; + /// A 2D point. -#[derive(Debug, Clone, Copy, PartialEq, Default)] -pub struct Point { +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Point { /// The X coordinate. - pub x: f32, + pub x: T, /// The Y coordinate. - pub y: f32, + pub y: T, } impl Point { /// The origin (i.e. a [`Point`] at (0, 0)). - pub const ORIGIN: Point = Point::new(0.0, 0.0); + pub const ORIGIN: Self = Self::new(0.0, 0.0); +} +impl Point { /// Creates a new [`Point`] with the given coordinates. - pub const fn new(x: f32, y: f32) -> Self { + pub const fn new(x: T, y: T) -> Self { Self { x, y } } /// Computes the distance to another [`Point`]. - pub fn distance(&self, to: Point) -> f32 { + pub fn distance(&self, to: Self) -> T + where + T: Float, + { let a = self.x - to.x; let b = self.y - to.y; @@ -34,9 +42,9 @@ impl From<[f32; 2]> for Point { } } -impl From<[u16; 2]> for Point { +impl From<[u16; 2]> for Point { fn from([x, y]: [u16; 2]) -> Self { - Point::new(x.into(), y.into()) + Point::new(x, y) } } @@ -46,10 +54,13 @@ impl From for [f32; 2] { } } -impl std::ops::Add for Point { +impl std::ops::Add> for Point +where + T: std::ops::Add, +{ type Output = Self; - fn add(self, vector: Vector) -> Self { + fn add(self, vector: Vector) -> Self { Self { x: self.x + vector.x, y: self.y + vector.y, @@ -57,10 +68,13 @@ impl std::ops::Add for Point { } } -impl std::ops::Sub for Point { +impl std::ops::Sub> for Point +where + T: std::ops::Sub, +{ type Output = Self; - fn sub(self, vector: Vector) -> Self { + fn sub(self, vector: Vector) -> Self { Self { x: self.x - vector.x, y: self.y - vector.y, @@ -68,10 +82,22 @@ impl std::ops::Sub for Point { } } -impl std::ops::Sub for Point { - type Output = Vector; +impl std::ops::Sub> for Point +where + T: std::ops::Sub, +{ + type Output = Vector; - fn sub(self, point: Point) -> Vector { + fn sub(self, point: Self) -> Vector { Vector::new(self.x - point.x, self.y - point.y) } } + +impl fmt::Display for Point +where + T: fmt::Display, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y) + } +} diff --git a/core/src/window/event.rs b/core/src/window/event.rs index f7759435f1..3ab7cd810a 100644 --- a/core/src/window/event.rs +++ b/core/src/window/event.rs @@ -1,10 +1,10 @@ use crate::time::Instant; -use crate::Size; +use crate::{Point, Size}; use std::path::PathBuf; /// A window-related event. -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(PartialEq, Clone, Debug)] pub enum Event { /// A window was moved. Moved { @@ -30,22 +30,22 @@ pub enum Event { /// The user has requested for the window to close. CloseRequested, - /// A window was destroyed by the runtime. - Destroyed, - /// A window was created. - /// - /// **Note:** this event is not supported on Wayland. Created { /// The position of the created window. This is relative to the top-left corner of the desktop /// the window is on, including virtual desktops. Refers to window's "inner" position, /// or the client area, in logical pixels. - position: (i32, i32), + /// + /// **Note**: Not available in Wayland. + position: Option, /// The size of the created window. This is its "inner" size, or the size of the /// client area, in logical pixels. - size: Size, + size: Size, }, + /// A window was destroyed by the runtime. + Destroyed, + /// A window was focused. Focused, diff --git a/core/src/window/position.rs b/core/src/window/position.rs index c260c29eb1..73391e75eb 100644 --- a/core/src/window/position.rs +++ b/core/src/window/position.rs @@ -1,5 +1,7 @@ +use crate::Point; + /// The position of a window in a given screen. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum Position { /// The platform-specific default position for a new window. Default, @@ -12,7 +14,7 @@ pub enum Position { /// position. So if you have decorations enabled and want the window to be /// at (0, 0) you would have to set the position to /// `(PADDING_X, PADDING_Y)`. - Specific(i32, i32), + Specific(Point), } impl Default for Position { diff --git a/core/src/window/settings.rs b/core/src/window/settings.rs index 25df815908..fbbf86abd8 100644 --- a/core/src/window/settings.rs +++ b/core/src/window/settings.rs @@ -25,22 +25,23 @@ mod platform; mod platform; use crate::window::{Icon, Level, Position}; +use crate::Size; pub use platform::PlatformSpecific; /// The window settings of an application. #[derive(Debug, Clone)] pub struct Settings { - /// The initial size of the window. - pub size: (u32, u32), + /// The initial logical dimensions of the window. + pub size: Size, /// The initial position of the window. pub position: Position, /// The minimum size of the window. - pub min_size: Option<(u32, u32)>, + pub min_size: Option, /// The maximum size of the window. - pub max_size: Option<(u32, u32)>, + pub max_size: Option, /// Whether the window should be visible or not. pub visible: bool, @@ -77,7 +78,7 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { Self { - size: (1024, 768), + size: Size::new(1024.0, 768.0), position: Position::default(), min_size: None, max_size: None, diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 7d1f1e9134..16beb46e24 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -4,8 +4,10 @@ use iced::multi_window::{self, Application}; use iced::widget::{button, column, container, scrollable, text, text_input}; use iced::window; use iced::{ - Alignment, Command, Element, Length, Settings, Subscription, Theme, + Alignment, Command, Element, Length, Point, Settings, Subscription, Theme, + Vector, }; + use std::collections::HashMap; fn main() -> iced::Result { @@ -33,8 +35,8 @@ enum Message { ScaleChanged(window::Id, String), TitleChanged(window::Id, String), CloseWindow(window::Id), + WindowCreated(window::Id, Option), WindowDestroyed(window::Id), - WindowCreated(window::Id, (i32, i32)), NewWindow, } @@ -90,10 +92,11 @@ impl multi_window::Application for Example { self.windows.remove(&id); } Message::WindowCreated(id, position) => { - self.next_window_pos = window::Position::Specific( - position.0 + 20, - position.1 + 20, - ); + if let Some(position) = position { + self.next_window_pos = window::Position::Specific( + position + Vector::new(20.0, 20.0), + ); + } if let Some(window) = self.windows.get(&id) { return text_input::focus(window.input_id.clone()); diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 8295ddeded..82421a8684 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -114,14 +114,14 @@ impl State { pub fn new() -> State { let now = Instant::now(); - let (width, height) = window::Settings::default().size; + let size = window::Settings::default().size; State { space_cache: canvas::Cache::default(), system_cache: canvas::Cache::default(), start: now, now, - stars: Self::generate_stars(width, height), + stars: Self::generate_stars(size.width, size.height), } } @@ -130,7 +130,7 @@ impl State { self.system_cache.clear(); } - fn generate_stars(width: u32, height: u32) -> Vec<(Point, f32)> { + fn generate_stars(width: f32, height: f32) -> Vec<(Point, f32)> { use rand::Rng; let mut rng = rand::thread_rng(); @@ -139,12 +139,8 @@ impl State { .map(|_| { ( Point::new( - rng.gen_range( - (-(width as f32) / 2.0)..(width as f32 / 2.0), - ), - rng.gen_range( - (-(height as f32) / 2.0)..(height as f32 / 2.0), - ), + rng.gen_range((-width / 2.0)..(width / 2.0)), + rng.gen_range((-height / 2.0)..(height / 2.0)), ), rng.gen_range(0.5..1.0), ) diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index a7ba69b93e..4dac032c83 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -8,7 +8,7 @@ use iced::widget::{ }; use iced::window; use iced::{Application, Element}; -use iced::{Color, Command, Length, Settings, Subscription}; +use iced::{Color, Command, Length, Settings, Size, Subscription}; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; @@ -22,7 +22,7 @@ pub fn main() -> iced::Result { Todos::run(Settings { window: window::Settings { - size: (500, 800), + size: Size::new(500.0, 800.0), ..window::Settings::default() }, ..Settings::default() diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 375ce8896e..f46ac1b80e 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -10,7 +10,7 @@ pub use screenshot::Screenshot; use crate::command::{self, Command}; use crate::core::time::Instant; use crate::core::window::{self, Event, Icon, Level, Mode, UserAttention}; -use crate::core::Size; +use crate::core::{Point, Size}; use crate::futures::event; use crate::futures::Subscription; @@ -48,17 +48,14 @@ pub fn drag(id: window::Id) -> Command { } /// Resizes the window to the given logical dimensions. -pub fn resize( - id: window::Id, - new_size: Size, -) -> Command { +pub fn resize(id: window::Id, new_size: Size) -> Command { Command::single(command::Action::Window(id, Action::Resize(new_size))) } /// Fetches the window's size in logical dimensions. pub fn fetch_size( id: window::Id, - f: impl FnOnce(Size) -> Message + 'static, + f: impl FnOnce(Size) -> Message + 'static, ) -> Command { Command::single(command::Action::Window(id, Action::FetchSize(Box::new(f)))) } @@ -74,8 +71,8 @@ pub fn minimize(id: window::Id, minimized: bool) -> Command { } /// Moves the window to the given logical coordinates. -pub fn move_to(id: window::Id, x: i32, y: i32) -> Command { - Command::single(command::Action::Window(id, Action::Move { x, y })) +pub fn move_to(id: window::Id, position: Point) -> Command { + Command::single(command::Action::Window(id, Action::Move(position))) } /// Changes the [`Mode`] of the window. diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index 2a31bbd617..5afe038983 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -1,5 +1,5 @@ use crate::core::window::{Icon, Level, Mode, Settings, UserAttention}; -use crate::core::Size; +use crate::core::{Point, Size}; use crate::futures::MaybeSend; use crate::window::Screenshot; @@ -20,23 +20,18 @@ pub enum Action { /// The settings of the window. settings: Settings, }, - /// Resize the window. - Resize(Size), - /// Fetch the current size of the window. - FetchSize(Box) -> T + 'static>), + /// Resize the window to the given logical dimensions. + Resize(Size), + /// Fetch the current logical dimensions of the window. + FetchSize(Box T + 'static>), /// Set the window to maximized or back Maximize(bool), /// Set the window to minimized or back Minimize(bool), - /// Move the window. + /// Move the window to the given logical coordinates. /// /// Unsupported on Wayland. - Move { - /// The new logical x location of the window - x: i32, - /// The new logical y location of the window - y: i32, - }, + Move(Point), /// Change the [`Mode`] of the window. ChangeMode(Mode), /// Fetch the current [`Mode`] of the window. @@ -114,7 +109,7 @@ impl Action { Self::FetchSize(o) => Action::FetchSize(Box::new(move |s| f(o(s)))), Self::Maximize(maximized) => Action::Maximize(maximized), Self::Minimize(minimized) => Action::Minimize(minimized), - Self::Move { x, y } => Action::Move { x, y }, + Self::Move(position) => Action::Move(position), Self::ChangeMode(mode) => Action::ChangeMode(mode), Self::FetchMode(o) => Action::FetchMode(Box::new(move |s| f(o(s)))), Self::ToggleMaximize => Action::ToggleMaximize, @@ -151,8 +146,8 @@ impl fmt::Debug for Action { Self::Minimize(minimized) => { write!(f, "Action::Minimize({minimized}") } - Self::Move { x, y } => { - write!(f, "Action::Move {{ x: {x}, y: {y} }}") + Self::Move(position) => { + write!(f, "Action::Move({position})") } Self::ChangeMode(mode) => write!(f, "Action::SetMode({mode:?})"), Self::FetchMode(_) => write!(f, "Action::FetchMode"), diff --git a/winit/src/application.rs b/winit/src/application.rs index b197c4ed78..4e6a879fc1 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -732,7 +732,8 @@ pub fn run_command( }); } 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( 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) => { diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 68c2b905ae..7e51a2d400 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -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 { 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 = 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()); diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index f8cedcb831..7347645227 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -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( 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( 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( )); 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( } 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( 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)> { - let scale = window.scale_factor(); - let pos = window +fn logical_bounds_of(window: &winit::window::Window) -> (Option, 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"))]