Skip to content

Commit e128ff9

Browse files
committed
wip
1 parent dd09546 commit e128ff9

1 file changed

Lines changed: 63 additions & 18 deletions

File tree

‎src/utils.rs‎

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ use dpi::{LogicalSize, PhysicalSize};
44

55
#[derive(Copy, Clone)]
66
pub(crate) enum SizingStrategy {
7-
Fixed,
7+
Fixed { size: Size },
88
Resizable { min_size: Option<Size>, max_size: Option<Size>, aspect_ratio: Option<AspectRatio> },
99
}
1010

1111
impl SizingStrategy {
1212
pub fn from_settings(settings: &WindowSettings) -> Self {
1313
if !settings.resizable {
14-
return Self::Fixed;
14+
return Self::Fixed { size: settings.size };
1515
}
1616

17-
if let (Some(min_size), Some(max_size)) = (settings.min_size, settings.max_size) {
17+
let mut min_size = settings.min_size;
18+
let mut max_size = settings.max_size;
19+
20+
if let (Some(min_size), Some(max_size)) = (min_size, max_size) {
1821
if min_size == max_size {
19-
return Self::Fixed;
22+
return Self::Fixed { size: min_size };
2023
}
2124
}
2225

23-
let mut min_size = settings.min_size;
24-
let mut max_size = settings.max_size;
25-
2626
if let Some(ratio) = settings.aspect_ratio {
2727
min_size = min_size.map(|s| adjust_min_size(s, ratio));
2828
max_size = max_size.map(|s| adjust_max_size(s, ratio));
@@ -37,14 +37,14 @@ impl SizingStrategy {
3737

3838
pub fn min_size(&self) -> Option<Size> {
3939
match self {
40-
Self::Fixed => None,
40+
Self::Fixed { .. } => None,
4141
Self::Resizable { min_size, .. } => *min_size,
4242
}
4343
}
4444

4545
pub fn max_size(&self) -> Option<Size> {
4646
match self {
47-
Self::Fixed => None,
47+
Self::Fixed { .. } => None,
4848
Self::Resizable { max_size, .. } => *max_size,
4949
}
5050
}
@@ -94,10 +94,49 @@ impl SizingStrategy {
9494

9595
pub fn aspect_ratio(&self) -> Option<AspectRatio> {
9696
match self {
97-
Self::Fixed => None,
97+
Self::Fixed { .. } => None,
9898
Self::Resizable { aspect_ratio, .. } => *aspect_ratio,
9999
}
100100
}
101+
102+
pub fn adjust_size(&self, size: Size, scale_factor: f64) -> Size {
103+
let (min, max, ratio) = match *self {
104+
SizingStrategy::Resizable { min_size, max_size, aspect_ratio } => {
105+
(min_size, max_size, aspect_ratio)
106+
}
107+
SizingStrategy::Fixed { size: fixed_size } => {
108+
return match size {
109+
Size::Physical(_) => Size::Physical(fixed_size.to_physical(scale_factor)),
110+
Size::Logical(_) => Size::Logical(fixed_size.to_logical(scale_factor)),
111+
}
112+
}
113+
};
114+
115+
match size {
116+
Size::Physical(mut size) => {
117+
if let Some(max_size) = max {
118+
let max_size = max_size.to_physical(scale_factor);
119+
size.width = size.width.min(max_size.width);
120+
size.height = size.height.min(max_size.height);
121+
}
122+
123+
if let Some(min_size) = min {
124+
let min_size = min_size.to_physical(scale_factor);
125+
size.width = size.width.max(min_size.width);
126+
size.height = size.height.max(min_size.height);
127+
}
128+
129+
if let Some(ratio) = ratio {
130+
todo!()
131+
}
132+
133+
size.into()
134+
}
135+
Size::Logical(size) => {
136+
todo!()
137+
}
138+
}
139+
}
101140
}
102141

103142
impl Default for SizingStrategy {
@@ -106,6 +145,19 @@ impl Default for SizingStrategy {
106145
}
107146
}
108147

148+
macro_rules! map_size {
149+
($size:expr, $ratio:expr, $inner:expr) => {
150+
match $size {
151+
Size::Physical(PhysicalSize { width, height }) => {
152+
Size::Physical(PhysicalSize::from($inner(width, height, $ratio)))
153+
}
154+
Size::Logical(LogicalSize { width, height }) => {
155+
Size::Logical(LogicalSize::from($inner(width, height, $ratio)))
156+
}
157+
}
158+
};
159+
}
160+
109161
fn adjust_min_size(size: Size, ratio: AspectRatio) -> Size {
110162
fn inner<P: Pixel>(x: P, y: P, ratio: AspectRatio) -> (P, P) {
111163
if ratio.numerator < ratio.denominator {
@@ -158,14 +210,7 @@ fn adjust_max_size(size: Size, ratio: AspectRatio) -> Size {
158210
}
159211
}
160212

161-
match size {
162-
Size::Physical(PhysicalSize { width, height }) => {
163-
Size::Physical(PhysicalSize::from(inner(width, height, ratio)))
164-
}
165-
Size::Logical(LogicalSize { width, height }) => {
166-
Size::Logical(LogicalSize::from(inner(width, height, ratio)))
167-
}
168-
}
213+
map_size!(size, ratio, inner)
169214
}
170215

171216
#[cfg(test)]

0 commit comments

Comments
 (0)