Skip to content

Commit

Permalink
Use actual floats for logical coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Nov 30, 2023
1 parent 9f29aec commit 6740831
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 136 deletions.
58 changes: 42 additions & 16 deletions core/src/point.rs
Original file line number Diff line number Diff line change
@@ -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<T = f32> {
/// 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<T: Num> Point<T> {
/// 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;

Expand All @@ -34,9 +42,9 @@ impl From<[f32; 2]> for Point {
}
}

impl From<[u16; 2]> for Point {
impl From<[u16; 2]> for Point<u16> {
fn from([x, y]: [u16; 2]) -> Self {
Point::new(x.into(), y.into())
Point::new(x, y)
}
}

Expand All @@ -46,32 +54,50 @@ impl From<Point> for [f32; 2] {
}
}

impl std::ops::Add<Vector> for Point {
impl<T> std::ops::Add<Vector<T>> for Point<T>
where
T: std::ops::Add<Output = T>,
{
type Output = Self;

fn add(self, vector: Vector) -> Self {
fn add(self, vector: Vector<T>) -> Self {
Self {
x: self.x + vector.x,
y: self.y + vector.y,
}
}
}

impl std::ops::Sub<Vector> for Point {
impl<T> std::ops::Sub<Vector<T>> for Point<T>
where
T: std::ops::Sub<Output = T>,
{
type Output = Self;

fn sub(self, vector: Vector) -> Self {
fn sub(self, vector: Vector<T>) -> Self {
Self {
x: self.x - vector.x,
y: self.y - vector.y,
}
}
}

impl std::ops::Sub<Point> for Point {
type Output = Vector;
impl<T> std::ops::Sub<Point<T>> for Point<T>
where
T: std::ops::Sub<Output = T>,
{
type Output = Vector<T>;

fn sub(self, point: Point) -> Vector {
fn sub(self, point: Self) -> Vector<T> {
Vector::new(self.x - point.x, self.y - point.y)
}
}

impl<T> fmt::Display for Point<T>
where
T: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
}
}
18 changes: 9 additions & 9 deletions core/src/window/event.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<Point>,
/// The size of the created window. This is its "inner" size, or the size of the
/// client area, in logical pixels.
size: Size<u32>,
size: Size,
},

/// A window was destroyed by the runtime.
Destroyed,

/// A window was focused.
Focused,

Expand Down
6 changes: 4 additions & 2 deletions core/src/window/position.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions core/src/window/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Size>,

/// The maximum size of the window.
pub max_size: Option<(u32, u32)>,
pub max_size: Option<Size>,

/// Whether the window should be visible or not.
pub visible: bool,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions examples/multi_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -33,8 +35,8 @@ enum Message {
ScaleChanged(window::Id, String),
TitleChanged(window::Id, String),
CloseWindow(window::Id),
WindowCreated(window::Id, Option<Point>),
WindowDestroyed(window::Id),
WindowCreated(window::Id, (i32, i32)),
NewWindow,
}

Expand Down Expand Up @@ -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());
Expand Down
14 changes: 5 additions & 9 deletions examples/solar_system/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand All @@ -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();
Expand All @@ -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),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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()
Expand Down
13 changes: 5 additions & 8 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,17 +48,14 @@ pub fn drag<Message>(id: window::Id) -> Command<Message> {
}

/// Resizes the window to the given logical dimensions.
pub fn resize<Message>(
id: window::Id,
new_size: Size<u32>,
) -> Command<Message> {
pub fn resize<Message>(id: window::Id, new_size: Size) -> Command<Message> {
Command::single(command::Action::Window(id, Action::Resize(new_size)))
}

/// Fetches the window's size in logical dimensions.
pub fn fetch_size<Message>(
id: window::Id,
f: impl FnOnce(Size<u32>) -> Message + 'static,
f: impl FnOnce(Size) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Window(id, Action::FetchSize(Box::new(f))))
}
Expand All @@ -74,8 +71,8 @@ pub fn minimize<Message>(id: window::Id, minimized: bool) -> Command<Message> {
}

/// Moves the window to the given logical coordinates.
pub fn move_to<Message>(id: window::Id, x: i32, y: i32) -> Command<Message> {
Command::single(command::Action::Window(id, Action::Move { x, y }))
pub fn move_to<Message>(id: window::Id, position: Point) -> Command<Message> {
Command::single(command::Action::Window(id, Action::Move(position)))
}

/// Changes the [`Mode`] of the window.
Expand Down
Loading

0 comments on commit 6740831

Please sign in to comment.