Skip to content

Commit 7dc1fb4

Browse files
authored
Merge pull request #1711 from iced-rs/feature/generic-pixel-units
Generic pixel units
2 parents f75e020 + fd14086 commit 7dc1fb4

57 files changed

Lines changed: 505 additions & 482 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/src/length.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// The strategy used to fill space in a specific dimension.
2-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2+
#[derive(Debug, Clone, Copy, PartialEq)]
33
pub enum Length {
44
/// Fill all the remaining space
55
Fill,
@@ -17,7 +17,7 @@ pub enum Length {
1717
Shrink,
1818

1919
/// Fill a fixed amount of space
20-
Units(u16),
20+
Fixed(f32),
2121
}
2222

2323
impl Length {
@@ -31,13 +31,19 @@ impl Length {
3131
Length::Fill => 1,
3232
Length::FillPortion(factor) => *factor,
3333
Length::Shrink => 0,
34-
Length::Units(_) => 0,
34+
Length::Fixed(_) => 0,
3535
}
3636
}
3737
}
3838

39+
impl From<f32> for Length {
40+
fn from(amount: f32) -> Self {
41+
Length::Fixed(amount)
42+
}
43+
}
44+
3945
impl From<u16> for Length {
4046
fn from(units: u16) -> Self {
41-
Length::Units(units)
47+
Length::Fixed(f32::from(units))
4248
}
4349
}

core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod content_fit;
3535
mod font;
3636
mod length;
3737
mod padding;
38+
mod pixels;
3839
mod point;
3940
mod rectangle;
4041
mod size;
@@ -47,6 +48,7 @@ pub use content_fit::ContentFit;
4748
pub use font::Font;
4849
pub use length::Length;
4950
pub use padding::Padding;
51+
pub use pixels::Pixels;
5052
pub use point::Point;
5153
pub use rectangle::Rectangle;
5254
pub use size::Size;

core/src/padding.rs

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,29 @@ use crate::Size;
3333
/// let widget = Widget::new().padding([10, 20]); // top/bottom, left/right
3434
/// let widget = Widget::new().padding([5, 10, 15, 20]); // top, right, bottom, left
3535
/// ```
36-
#[derive(Debug, Hash, Copy, Clone)]
36+
#[derive(Debug, Copy, Clone)]
3737
pub struct Padding {
3838
/// Top padding
39-
pub top: u16,
39+
pub top: f32,
4040
/// Right padding
41-
pub right: u16,
41+
pub right: f32,
4242
/// Bottom padding
43-
pub bottom: u16,
43+
pub bottom: f32,
4444
/// Left padding
45-
pub left: u16,
45+
pub left: f32,
4646
}
4747

4848
impl Padding {
4949
/// Padding of zero
5050
pub const ZERO: Padding = Padding {
51-
top: 0,
52-
right: 0,
53-
bottom: 0,
54-
left: 0,
51+
top: 0.0,
52+
right: 0.0,
53+
bottom: 0.0,
54+
left: 0.0,
5555
};
5656

5757
/// Create a Padding that is equal on all sides
58-
pub const fn new(padding: u16) -> Padding {
58+
pub const fn new(padding: f32) -> Padding {
5959
Padding {
6060
top: padding,
6161
right: padding,
@@ -65,12 +65,12 @@ impl Padding {
6565
}
6666

6767
/// Returns the total amount of vertical [`Padding`].
68-
pub fn vertical(self) -> u16 {
68+
pub fn vertical(self) -> f32 {
6969
self.top + self.bottom
7070
}
7171

7272
/// Returns the total amount of horizontal [`Padding`].
73-
pub fn horizontal(self) -> u16 {
73+
pub fn horizontal(self) -> f32 {
7474
self.left + self.right
7575
}
7676

@@ -79,16 +79,49 @@ impl Padding {
7979
let available = (outer - inner).max(Size::ZERO);
8080

8181
Padding {
82-
top: self.top.min((available.height as u16) / 2),
83-
right: self.right.min((available.width as u16) / 2),
84-
bottom: self.bottom.min((available.height as u16) / 2),
85-
left: self.left.min((available.width as u16) / 2),
82+
top: self.top.min(available.height / 2.0),
83+
right: self.right.min(available.width / 2.0),
84+
bottom: self.bottom.min(available.height / 2.0),
85+
left: self.left.min(available.width / 2.0),
8686
}
8787
}
8888
}
8989

9090
impl From<u16> for Padding {
9191
fn from(p: u16) -> Self {
92+
Padding {
93+
top: f32::from(p),
94+
right: f32::from(p),
95+
bottom: f32::from(p),
96+
left: f32::from(p),
97+
}
98+
}
99+
}
100+
101+
impl From<[u16; 2]> for Padding {
102+
fn from(p: [u16; 2]) -> Self {
103+
Padding {
104+
top: f32::from(p[0]),
105+
right: f32::from(p[1]),
106+
bottom: f32::from(p[0]),
107+
left: f32::from(p[1]),
108+
}
109+
}
110+
}
111+
112+
impl From<[u16; 4]> for Padding {
113+
fn from(p: [u16; 4]) -> Self {
114+
Padding {
115+
top: f32::from(p[0]),
116+
right: f32::from(p[1]),
117+
bottom: f32::from(p[2]),
118+
left: f32::from(p[3]),
119+
}
120+
}
121+
}
122+
123+
impl From<f32> for Padding {
124+
fn from(p: f32) -> Self {
92125
Padding {
93126
top: p,
94127
right: p,
@@ -98,8 +131,8 @@ impl From<u16> for Padding {
98131
}
99132
}
100133

101-
impl From<[u16; 2]> for Padding {
102-
fn from(p: [u16; 2]) -> Self {
134+
impl From<[f32; 2]> for Padding {
135+
fn from(p: [f32; 2]) -> Self {
103136
Padding {
104137
top: p[0],
105138
right: p[1],
@@ -109,8 +142,8 @@ impl From<[u16; 2]> for Padding {
109142
}
110143
}
111144

112-
impl From<[u16; 4]> for Padding {
113-
fn from(p: [u16; 4]) -> Self {
145+
impl From<[f32; 4]> for Padding {
146+
fn from(p: [f32; 4]) -> Self {
114147
Padding {
115148
top: p[0],
116149
right: p[1],

core/src/pixels.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// An amount of logical pixels.
2+
///
3+
/// Normally used to represent an amount of space, or the size of something.
4+
///
5+
/// This type is normally asked as an argument in a generic way
6+
/// (e.g. `impl Into<Pixels>`) and, since `Pixels` implements `From` both for
7+
/// `f32` and `u16`, you should be able to provide both integers and float
8+
/// literals as needed.
9+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
10+
pub struct Pixels(pub f32);
11+
12+
impl From<f32> for Pixels {
13+
fn from(amount: f32) -> Self {
14+
Self(amount)
15+
}
16+
}
17+
18+
impl From<u16> for Pixels {
19+
fn from(amount: u16) -> Self {
20+
Self(f32::from(amount))
21+
}
22+
}

core/src/size.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ impl Size {
2929
/// Increments the [`Size`] to account for the given padding.
3030
pub fn pad(&self, padding: Padding) -> Self {
3131
Size {
32-
width: self.width + padding.horizontal() as f32,
33-
height: self.height + padding.vertical() as f32,
32+
width: self.width + padding.horizontal(),
33+
height: self.height + padding.vertical(),
3434
}
3535
}
3636

examples/color_palette/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ impl<C: ColorSpace + Copy> ColorPicker<C> {
301301
}
302302

303303
row![
304-
text(C::LABEL).width(Length::Units(50)),
304+
text(C::LABEL).width(50),
305305
slider(cr1, c1, move |v| C::new(v, c2, c3)),
306306
slider(cr2, c2, move |v| C::new(c1, v, c3)),
307307
slider(cr3, c3, move |v| C::new(c1, c2, v)),
308-
text(color.to_string()).width(Length::Units(185)).size(14),
308+
text(color.to_string()).width(185).size(14),
309309
]
310310
.spacing(10)
311311
.align_items(Alignment::Center)

examples/component/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ mod numeric_input {
127127
.horizontal_alignment(alignment::Horizontal::Center)
128128
.vertical_alignment(alignment::Vertical::Center),
129129
)
130-
.width(Length::Units(50))
130+
.width(50)
131131
.on_press(on_press)
132132
};
133133

examples/events/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Application for Events {
9393
.width(Length::Fill)
9494
.horizontal_alignment(alignment::Horizontal::Center),
9595
)
96-
.width(Length::Units(100))
96+
.width(100)
9797
.padding(10)
9898
.on_press(Message::Exit);
9999

examples/integration_opengl/src/controls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Program for Controls {
4242
let background_color = self.background_color;
4343

4444
let sliders = Row::new()
45-
.width(Length::Units(500))
45+
.width(500)
4646
.spacing(20)
4747
.push(
4848
Slider::new(0.0..=1.0, background_color.r, move |r| {

examples/integration_wgpu/src/controls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Program for Controls {
4848
let text = &self.text;
4949

5050
let sliders = Row::new()
51-
.width(Length::Units(500))
51+
.width(500)
5252
.spacing(20)
5353
.push(
5454
slider(0.0..=1.0, background_color.r, move |r| {

0 commit comments

Comments
 (0)