Skip to content

Commit 6740831

Browse files
committed
Use actual floats for logical coordinates
1 parent 9f29aec commit 6740831

File tree

12 files changed

+165
-136
lines changed

12 files changed

+165
-136
lines changed

core/src/point.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
use crate::Vector;
22

3+
use num_traits::{Float, Num};
4+
use std::fmt;
5+
36
/// A 2D point.
4-
#[derive(Debug, Clone, Copy, PartialEq, Default)]
5-
pub struct Point {
7+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8+
pub struct Point<T = f32> {
69
/// The X coordinate.
7-
pub x: f32,
10+
pub x: T,
811

912
/// The Y coordinate.
10-
pub y: f32,
13+
pub y: T,
1114
}
1215

1316
impl Point {
1417
/// The origin (i.e. a [`Point`] at (0, 0)).
15-
pub const ORIGIN: Point = Point::new(0.0, 0.0);
18+
pub const ORIGIN: Self = Self::new(0.0, 0.0);
19+
}
1620

21+
impl<T: Num> Point<T> {
1722
/// Creates a new [`Point`] with the given coordinates.
18-
pub const fn new(x: f32, y: f32) -> Self {
23+
pub const fn new(x: T, y: T) -> Self {
1924
Self { x, y }
2025
}
2126

2227
/// Computes the distance to another [`Point`].
23-
pub fn distance(&self, to: Point) -> f32 {
28+
pub fn distance(&self, to: Self) -> T
29+
where
30+
T: Float,
31+
{
2432
let a = self.x - to.x;
2533
let b = self.y - to.y;
2634

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

37-
impl From<[u16; 2]> for Point {
45+
impl From<[u16; 2]> for Point<u16> {
3846
fn from([x, y]: [u16; 2]) -> Self {
39-
Point::new(x.into(), y.into())
47+
Point::new(x, y)
4048
}
4149
}
4250

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

49-
impl std::ops::Add<Vector> for Point {
57+
impl<T> std::ops::Add<Vector<T>> for Point<T>
58+
where
59+
T: std::ops::Add<Output = T>,
60+
{
5061
type Output = Self;
5162

52-
fn add(self, vector: Vector) -> Self {
63+
fn add(self, vector: Vector<T>) -> Self {
5364
Self {
5465
x: self.x + vector.x,
5566
y: self.y + vector.y,
5667
}
5768
}
5869
}
5970

60-
impl std::ops::Sub<Vector> for Point {
71+
impl<T> std::ops::Sub<Vector<T>> for Point<T>
72+
where
73+
T: std::ops::Sub<Output = T>,
74+
{
6175
type Output = Self;
6276

63-
fn sub(self, vector: Vector) -> Self {
77+
fn sub(self, vector: Vector<T>) -> Self {
6478
Self {
6579
x: self.x - vector.x,
6680
y: self.y - vector.y,
6781
}
6882
}
6983
}
7084

71-
impl std::ops::Sub<Point> for Point {
72-
type Output = Vector;
85+
impl<T> std::ops::Sub<Point<T>> for Point<T>
86+
where
87+
T: std::ops::Sub<Output = T>,
88+
{
89+
type Output = Vector<T>;
7390

74-
fn sub(self, point: Point) -> Vector {
91+
fn sub(self, point: Self) -> Vector<T> {
7592
Vector::new(self.x - point.x, self.y - point.y)
7693
}
7794
}
95+
96+
impl<T> fmt::Display for Point<T>
97+
where
98+
T: fmt::Display,
99+
{
100+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101+
write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
102+
}
103+
}

core/src/window/event.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::time::Instant;
2-
use crate::Size;
2+
use crate::{Point, Size};
33

44
use std::path::PathBuf;
55

66
/// A window-related event.
7-
#[derive(PartialEq, Eq, Clone, Debug)]
7+
#[derive(PartialEq, Clone, Debug)]
88
pub enum Event {
99
/// A window was moved.
1010
Moved {
@@ -30,22 +30,22 @@ pub enum Event {
3030
/// The user has requested for the window to close.
3131
CloseRequested,
3232

33-
/// A window was destroyed by the runtime.
34-
Destroyed,
35-
3633
/// A window was created.
37-
///
38-
/// **Note:** this event is not supported on Wayland.
3934
Created {
4035
/// The position of the created window. This is relative to the top-left corner of the desktop
4136
/// the window is on, including virtual desktops. Refers to window's "inner" position,
4237
/// or the client area, in logical pixels.
43-
position: (i32, i32),
38+
///
39+
/// **Note**: Not available in Wayland.
40+
position: Option<Point>,
4441
/// The size of the created window. This is its "inner" size, or the size of the
4542
/// client area, in logical pixels.
46-
size: Size<u32>,
43+
size: Size,
4744
},
4845

46+
/// A window was destroyed by the runtime.
47+
Destroyed,
48+
4949
/// A window was focused.
5050
Focused,
5151

core/src/window/position.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::Point;
2+
13
/// The position of a window in a given screen.
2-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4+
#[derive(Debug, Clone, Copy, PartialEq)]
35
pub enum Position {
46
/// The platform-specific default position for a new window.
57
Default,
@@ -12,7 +14,7 @@ pub enum Position {
1214
/// position. So if you have decorations enabled and want the window to be
1315
/// at (0, 0) you would have to set the position to
1416
/// `(PADDING_X, PADDING_Y)`.
15-
Specific(i32, i32),
17+
Specific(Point),
1618
}
1719

1820
impl Default for Position {

core/src/window/settings.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,23 @@ mod platform;
2525
mod platform;
2626

2727
use crate::window::{Icon, Level, Position};
28+
use crate::Size;
2829

2930
pub use platform::PlatformSpecific;
3031
/// The window settings of an application.
3132
#[derive(Debug, Clone)]
3233
pub struct Settings {
33-
/// The initial size of the window.
34-
pub size: (u32, u32),
34+
/// The initial logical dimensions of the window.
35+
pub size: Size,
3536

3637
/// The initial position of the window.
3738
pub position: Position,
3839

3940
/// The minimum size of the window.
40-
pub min_size: Option<(u32, u32)>,
41+
pub min_size: Option<Size>,
4142

4243
/// The maximum size of the window.
43-
pub max_size: Option<(u32, u32)>,
44+
pub max_size: Option<Size>,
4445

4546
/// Whether the window should be visible or not.
4647
pub visible: bool,
@@ -77,7 +78,7 @@ pub struct Settings {
7778
impl Default for Settings {
7879
fn default() -> Self {
7980
Self {
80-
size: (1024, 768),
81+
size: Size::new(1024.0, 768.0),
8182
position: Position::default(),
8283
min_size: None,
8384
max_size: None,

examples/multi_window/src/main.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use iced::multi_window::{self, Application};
44
use iced::widget::{button, column, container, scrollable, text, text_input};
55
use iced::window;
66
use iced::{
7-
Alignment, Command, Element, Length, Settings, Subscription, Theme,
7+
Alignment, Command, Element, Length, Point, Settings, Subscription, Theme,
8+
Vector,
89
};
10+
911
use std::collections::HashMap;
1012

1113
fn main() -> iced::Result {
@@ -33,8 +35,8 @@ enum Message {
3335
ScaleChanged(window::Id, String),
3436
TitleChanged(window::Id, String),
3537
CloseWindow(window::Id),
38+
WindowCreated(window::Id, Option<Point>),
3639
WindowDestroyed(window::Id),
37-
WindowCreated(window::Id, (i32, i32)),
3840
NewWindow,
3941
}
4042

@@ -90,10 +92,11 @@ impl multi_window::Application for Example {
9092
self.windows.remove(&id);
9193
}
9294
Message::WindowCreated(id, position) => {
93-
self.next_window_pos = window::Position::Specific(
94-
position.0 + 20,
95-
position.1 + 20,
96-
);
95+
if let Some(position) = position {
96+
self.next_window_pos = window::Position::Specific(
97+
position + Vector::new(20.0, 20.0),
98+
);
99+
}
97100

98101
if let Some(window) = self.windows.get(&id) {
99102
return text_input::focus(window.input_id.clone());

examples/solar_system/src/main.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ impl State {
114114

115115
pub fn new() -> State {
116116
let now = Instant::now();
117-
let (width, height) = window::Settings::default().size;
117+
let size = window::Settings::default().size;
118118

119119
State {
120120
space_cache: canvas::Cache::default(),
121121
system_cache: canvas::Cache::default(),
122122
start: now,
123123
now,
124-
stars: Self::generate_stars(width, height),
124+
stars: Self::generate_stars(size.width, size.height),
125125
}
126126
}
127127

@@ -130,7 +130,7 @@ impl State {
130130
self.system_cache.clear();
131131
}
132132

133-
fn generate_stars(width: u32, height: u32) -> Vec<(Point, f32)> {
133+
fn generate_stars(width: f32, height: f32) -> Vec<(Point, f32)> {
134134
use rand::Rng;
135135

136136
let mut rng = rand::thread_rng();
@@ -139,12 +139,8 @@ impl State {
139139
.map(|_| {
140140
(
141141
Point::new(
142-
rng.gen_range(
143-
(-(width as f32) / 2.0)..(width as f32 / 2.0),
144-
),
145-
rng.gen_range(
146-
(-(height as f32) / 2.0)..(height as f32 / 2.0),
147-
),
142+
rng.gen_range((-width / 2.0)..(width / 2.0)),
143+
rng.gen_range((-height / 2.0)..(height / 2.0)),
148144
),
149145
rng.gen_range(0.5..1.0),
150146
)

examples/todos/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use iced::widget::{
88
};
99
use iced::window;
1010
use iced::{Application, Element};
11-
use iced::{Color, Command, Length, Settings, Subscription};
11+
use iced::{Color, Command, Length, Settings, Size, Subscription};
1212

1313
use once_cell::sync::Lazy;
1414
use serde::{Deserialize, Serialize};
@@ -22,7 +22,7 @@ pub fn main() -> iced::Result {
2222

2323
Todos::run(Settings {
2424
window: window::Settings {
25-
size: (500, 800),
25+
size: Size::new(500.0, 800.0),
2626
..window::Settings::default()
2727
},
2828
..Settings::default()

runtime/src/window.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub use screenshot::Screenshot;
1010
use crate::command::{self, Command};
1111
use crate::core::time::Instant;
1212
use crate::core::window::{self, Event, Icon, Level, Mode, UserAttention};
13-
use crate::core::Size;
13+
use crate::core::{Point, Size};
1414
use crate::futures::event;
1515
use crate::futures::Subscription;
1616

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

5050
/// Resizes the window to the given logical dimensions.
51-
pub fn resize<Message>(
52-
id: window::Id,
53-
new_size: Size<u32>,
54-
) -> Command<Message> {
51+
pub fn resize<Message>(id: window::Id, new_size: Size) -> Command<Message> {
5552
Command::single(command::Action::Window(id, Action::Resize(new_size)))
5653
}
5754

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

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

8178
/// Changes the [`Mode`] of the window.

0 commit comments

Comments
 (0)