Skip to content

Commit 1f18774

Browse files
authored
Merge pull request #2861 from tigerros/master
Add `ratio` method to `Size`
2 parents 8bfd099 + da9aaf4 commit 1f18774

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

core/src/size.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Size {
5252
}
5353
}
5454

55-
/// Rotates the given [`Size`] and returns the minimum [`Size`]
55+
/// Rotates this [`Size`] and returns the minimum [`Size`]
5656
/// containing it.
5757
pub fn rotate(self, rotation: Radians) -> Size {
5858
let radians = f32::from(rotation);
@@ -64,6 +64,15 @@ impl Size {
6464
+ (self.height * radians.cos()).abs(),
6565
}
6666
}
67+
68+
/// Applies an aspect ratio to this [`Size`] without
69+
/// exceeding its bounds.
70+
pub const fn ratio(self, aspect_ratio: f32) -> Size {
71+
Size {
72+
width: (self.height * aspect_ratio).min(self.width),
73+
height: (self.width / aspect_ratio).min(self.height),
74+
}
75+
}
6776
}
6877

6978
impl Size<Length> {

examples/layout/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ edition = "2024"
66
publish = false
77

88
[dependencies]
9-
iced = { path = "../..", features = ["canvas"] }
9+
iced = { path = "../..", features = ["canvas", "debug"] }

examples/layout/src/main.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use iced::keyboard;
33
use iced::mouse;
44
use iced::widget::{
55
button, canvas, center, center_y, checkbox, column, container, pick_list,
6-
pin, row, rule, scrollable, space, stack, text,
6+
pin, responsive, row, rule, scrollable, space, stack, text,
77
};
88
use iced::{
99
Center, Element, Fill, Font, Length, Point, Rectangle, Renderer, Shrink,
@@ -153,6 +153,10 @@ impl Example {
153153
title: "Pinning",
154154
view: pinning,
155155
},
156+
Self {
157+
title: "Responsive",
158+
view: responsive_,
159+
},
156160
];
157161

158162
fn is_first(self) -> bool {
@@ -333,6 +337,42 @@ fn pinning<'a>() -> Element<'a, Message> {
333337
.into()
334338
}
335339

340+
fn responsive_<'a>() -> Element<'a, Message> {
341+
column![
342+
responsive(|size| {
343+
container(center(
344+
text!("{}x{}px", size.width, size.width).font(Font::MONOSPACE),
345+
))
346+
.clip(true)
347+
.width(size.width / 4.0)
348+
.height(size.width / 4.0)
349+
.style(container::bordered_box)
350+
.into()
351+
})
352+
.width(Shrink)
353+
.height(Shrink),
354+
responsive(|size| {
355+
let size = size.ratio(16.0 / 9.0);
356+
357+
container(center(
358+
text!("{:.0}x{:.0}px (16:9)", size.width, size.height)
359+
.font(Font::MONOSPACE),
360+
))
361+
.clip(true)
362+
.width(size.width)
363+
.height(size.height)
364+
.style(container::bordered_box)
365+
.into()
366+
})
367+
.width(Shrink)
368+
.height(Shrink)
369+
]
370+
.align_x(Center)
371+
.spacing(10)
372+
.padding(10)
373+
.into()
374+
}
375+
336376
fn square<'a>(size: impl Into<Length> + Copy) -> Element<'a, Message> {
337377
struct Square;
338378

0 commit comments

Comments
 (0)