forked from neovide/neovide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimensions.rs
132 lines (113 loc) · 3.41 KB
/
dimensions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::{
fmt::Display,
ops::{Div, Mul},
str::FromStr,
};
use serde::{Deserialize, Serialize};
use winit::dpi::PhysicalSize;
use crate::settings;
// Maybe this should be independent from serialization?
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub struct Dimensions {
pub width: u64,
pub height: u64,
}
impl Default for Dimensions {
fn default() -> Self {
settings::DEFAULT_WINDOW_GEOMETRY
}
}
impl FromStr for Dimensions {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let invalid_parse_err = format!("Invalid geometry: {s}\nValid format: <width>x<height>");
s.split('x')
.map(|dimension| {
dimension
.parse::<u64>()
.map_err(|_| invalid_parse_err.as_str())
.and_then(|dimension| {
if dimension > 0 {
Ok(dimension)
} else {
Err("Invalid Dimensions: Window dimensions should be greater than 0.")
}
})
})
.collect::<Result<Vec<_>, &str>>()
.and_then(|dimensions| {
if let [width, height] = dimensions[..] {
Ok(Dimensions { width, height })
} else {
Err(invalid_parse_err.as_str())
}
})
.map_err(|msg| msg.to_owned())
}
}
impl Display for Dimensions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}x{}", self.width, self.height)
}
}
macro_rules! impl_from_tuple_to_dimensions {
($type:ty) => {
impl From<($type, $type)> for Dimensions {
fn from((width, height): ($type, $type)) -> Self {
Dimensions {
width: width as u64,
height: height as u64,
}
}
}
};
}
impl_from_tuple_to_dimensions!(u64);
impl_from_tuple_to_dimensions!(f32);
macro_rules! impl_from_dimensions_to_tuple {
($type:ty) => {
impl From<Dimensions> for ($type, $type) {
fn from(dimensions: Dimensions) -> Self {
(dimensions.width as $type, dimensions.height as $type)
}
}
};
}
impl_from_dimensions_to_tuple!(u64);
impl_from_dimensions_to_tuple!(u32);
impl_from_dimensions_to_tuple!(i32);
impl From<PhysicalSize<u32>> for Dimensions {
fn from(PhysicalSize { width, height }: PhysicalSize<u32>) -> Self {
Dimensions {
width: width as u64,
height: height as u64,
}
}
}
impl From<Dimensions> for PhysicalSize<u32> {
fn from(Dimensions { width, height }: Dimensions) -> Self {
PhysicalSize {
width: width as u32,
height: height as u32,
}
}
}
impl Mul for Dimensions {
type Output = Self;
fn mul(self, other: Self) -> Self {
Dimensions::from((self.width * other.width, self.height * other.height))
}
}
impl Div for Dimensions {
type Output = Self;
fn div(self, other: Self) -> Self {
Dimensions::from((self.width / other.width, self.height / other.height))
}
}
impl Mul<Dimensions> for (u64, u64) {
type Output = Self;
fn mul(self, other: Dimensions) -> Self {
let (x, y) = self;
(x * other.width, y * other.height)
}
}