Skip to content

Commit 8cadd3b

Browse files
authored
Merge pull request #2504 from iced-rs/view-ergonomics
Improved `view` ergonomics
2 parents be06060 + 3f480d3 commit 8cadd3b

64 files changed

Lines changed: 805 additions & 658 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/alignment.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ pub enum Horizontal {
4646
Right,
4747
}
4848

49+
impl From<Alignment> for Horizontal {
50+
fn from(alignment: Alignment) -> Self {
51+
match alignment {
52+
Alignment::Start => Self::Left,
53+
Alignment::Center => Self::Center,
54+
Alignment::End => Self::Right,
55+
}
56+
}
57+
}
58+
4959
/// The vertical [`Alignment`] of some resource.
5060
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5161
pub enum Vertical {
@@ -58,3 +68,13 @@ pub enum Vertical {
5868
/// Align bottom
5969
Bottom,
6070
}
71+
72+
impl From<Alignment> for Vertical {
73+
fn from(alignment: Alignment) -> Self {
74+
match alignment {
75+
Alignment::Start => Self::Top,
76+
Alignment::Center => Self::Center,
77+
Alignment::End => Self::Bottom,
78+
}
79+
}
80+
}

core/src/border.rs

Lines changed: 200 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,64 @@ pub struct Border {
1010
/// The width of the border.
1111
pub width: f32,
1212

13-
/// The radius of the border.
13+
/// The [`Radius`] of the border.
1414
pub radius: Radius,
1515
}
1616

17+
/// Creates a new [`Border`] with the given [`Radius`].
18+
///
19+
/// ```
20+
/// # use iced_core::border::{self, Border};
21+
/// #
22+
/// assert_eq!(border::rounded(10), Border::default().rounded(10));
23+
/// ```
24+
pub fn rounded(radius: impl Into<Radius>) -> Border {
25+
Border::default().rounded(radius)
26+
}
27+
28+
/// Creates a new [`Border`] with the given [`Color`].
29+
///
30+
/// ```
31+
/// # use iced_core::border::{self, Border};
32+
/// # use iced_core::Color;
33+
/// #
34+
/// assert_eq!(border::color(Color::BLACK), Border::default().color(Color::BLACK));
35+
/// ```
36+
pub fn color(color: impl Into<Color>) -> Border {
37+
Border::default().color(color)
38+
}
39+
40+
/// Creates a new [`Border`] with the given `width`.
41+
///
42+
/// ```
43+
/// # use iced_core::border::{self, Border};
44+
/// # use iced_core::Color;
45+
/// #
46+
/// assert_eq!(border::width(10), Border::default().width(10));
47+
/// ```
48+
pub fn width(width: impl Into<Pixels>) -> Border {
49+
Border::default().width(width)
50+
}
51+
1752
impl Border {
18-
/// Creates a new default rounded [`Border`] with the given [`Radius`].
19-
///
20-
/// ```
21-
/// # use iced_core::Border;
22-
/// #
23-
/// assert_eq!(Border::rounded(10), Border::default().with_radius(10));
24-
/// ```
25-
pub fn rounded(radius: impl Into<Radius>) -> Self {
26-
Self::default().with_radius(radius)
27-
}
28-
29-
/// Updates the [`Color`] of the [`Border`].
30-
pub fn with_color(self, color: impl Into<Color>) -> Self {
53+
/// Sets the [`Color`] of the [`Border`].
54+
pub fn color(self, color: impl Into<Color>) -> Self {
3155
Self {
3256
color: color.into(),
3357
..self
3458
}
3559
}
3660

37-
/// Updates the [`Radius`] of the [`Border`].
38-
pub fn with_radius(self, radius: impl Into<Radius>) -> Self {
61+
/// Sets the [`Radius`] of the [`Border`].
62+
pub fn rounded(self, radius: impl Into<Radius>) -> Self {
3963
Self {
4064
radius: radius.into(),
4165
..self
4266
}
4367
}
4468

45-
/// Updates the width of the [`Border`].
46-
pub fn with_width(self, width: impl Into<Pixels>) -> Self {
69+
/// Sets the width of the [`Border`].
70+
pub fn width(self, width: impl Into<Pixels>) -> Self {
4771
Self {
4872
width: width.into().0,
4973
..self
@@ -54,11 +78,160 @@ impl Border {
5478
/// The border radii for the corners of a graphics primitive in the order:
5579
/// top-left, top-right, bottom-right, bottom-left.
5680
#[derive(Debug, Clone, Copy, PartialEq, Default)]
57-
pub struct Radius([f32; 4]);
81+
pub struct Radius {
82+
/// Top left radius
83+
pub top_left: f32,
84+
/// Top right radius
85+
pub top_right: f32,
86+
/// Bottom right radius
87+
pub bottom_right: f32,
88+
/// Bottom left radius
89+
pub bottom_left: f32,
90+
}
91+
92+
/// Creates a new [`Radius`] with the same value for each corner.
93+
pub fn radius(value: impl Into<Pixels>) -> Radius {
94+
Radius::new(value)
95+
}
96+
97+
/// Creates a new [`Radius`] with the given top left value.
98+
pub fn top_left(value: impl Into<Pixels>) -> Radius {
99+
Radius::default().top_left(value)
100+
}
101+
102+
/// Creates a new [`Radius`] with the given top right value.
103+
pub fn top_right(value: impl Into<Pixels>) -> Radius {
104+
Radius::default().top_right(value)
105+
}
106+
107+
/// Creates a new [`Radius`] with the given bottom right value.
108+
pub fn bottom_right(value: impl Into<Pixels>) -> Radius {
109+
Radius::default().bottom_right(value)
110+
}
111+
112+
/// Creates a new [`Radius`] with the given bottom left value.
113+
pub fn bottom_left(value: impl Into<Pixels>) -> Radius {
114+
Radius::default().bottom_left(value)
115+
}
116+
117+
/// Creates a new [`Radius`] with the given value as top left and top right.
118+
pub fn top(value: impl Into<Pixels>) -> Radius {
119+
Radius::default().top(value)
120+
}
121+
122+
/// Creates a new [`Radius`] with the given value as bottom left and bottom right.
123+
pub fn bottom(value: impl Into<Pixels>) -> Radius {
124+
Radius::default().bottom(value)
125+
}
126+
127+
/// Creates a new [`Radius`] with the given value as top left and bottom left.
128+
pub fn left(value: impl Into<Pixels>) -> Radius {
129+
Radius::default().left(value)
130+
}
131+
132+
/// Creates a new [`Radius`] with the given value as top right and bottom right.
133+
pub fn right(value: impl Into<Pixels>) -> Radius {
134+
Radius::default().right(value)
135+
}
136+
137+
impl Radius {
138+
/// Creates a new [`Radius`] with the same value for each corner.
139+
pub fn new(value: impl Into<Pixels>) -> Self {
140+
let value = value.into().0;
141+
142+
Self {
143+
top_left: value,
144+
top_right: value,
145+
bottom_right: value,
146+
bottom_left: value,
147+
}
148+
}
149+
150+
/// Sets the top left value of the [`Radius`].
151+
pub fn top_left(self, value: impl Into<Pixels>) -> Self {
152+
Self {
153+
top_left: value.into().0,
154+
..self
155+
}
156+
}
157+
158+
/// Sets the top right value of the [`Radius`].
159+
pub fn top_right(self, value: impl Into<Pixels>) -> Self {
160+
Self {
161+
top_right: value.into().0,
162+
..self
163+
}
164+
}
165+
166+
/// Sets the bottom right value of the [`Radius`].
167+
pub fn bottom_right(self, value: impl Into<Pixels>) -> Self {
168+
Self {
169+
bottom_right: value.into().0,
170+
..self
171+
}
172+
}
173+
174+
/// Sets the bottom left value of the [`Radius`].
175+
pub fn bottom_left(self, value: impl Into<Pixels>) -> Self {
176+
Self {
177+
bottom_left: value.into().0,
178+
..self
179+
}
180+
}
181+
182+
/// Sets the top left and top right values of the [`Radius`].
183+
pub fn top(self, value: impl Into<Pixels>) -> Self {
184+
let value = value.into().0;
185+
186+
Self {
187+
top_left: value,
188+
top_right: value,
189+
..self
190+
}
191+
}
192+
193+
/// Sets the bottom left and bottom right values of the [`Radius`].
194+
pub fn bottom(self, value: impl Into<Pixels>) -> Self {
195+
let value = value.into().0;
196+
197+
Self {
198+
bottom_left: value,
199+
bottom_right: value,
200+
..self
201+
}
202+
}
203+
204+
/// Sets the top left and bottom left values of the [`Radius`].
205+
pub fn left(self, value: impl Into<Pixels>) -> Self {
206+
let value = value.into().0;
207+
208+
Self {
209+
top_left: value,
210+
bottom_left: value,
211+
..self
212+
}
213+
}
214+
215+
/// Sets the top right and bottom right values of the [`Radius`].
216+
pub fn right(self, value: impl Into<Pixels>) -> Self {
217+
let value = value.into().0;
218+
219+
Self {
220+
top_right: value,
221+
bottom_right: value,
222+
..self
223+
}
224+
}
225+
}
58226

59227
impl From<f32> for Radius {
60-
fn from(w: f32) -> Self {
61-
Self([w; 4])
228+
fn from(radius: f32) -> Self {
229+
Self {
230+
top_left: radius,
231+
top_right: radius,
232+
bottom_right: radius,
233+
bottom_left: radius,
234+
}
62235
}
63236
}
64237

@@ -80,14 +253,13 @@ impl From<i32> for Radius {
80253
}
81254
}
82255

83-
impl From<[f32; 4]> for Radius {
84-
fn from(radi: [f32; 4]) -> Self {
85-
Self(radi)
86-
}
87-
}
88-
89256
impl From<Radius> for [f32; 4] {
90257
fn from(radi: Radius) -> Self {
91-
radi.0
258+
[
259+
radi.top_left,
260+
radi.top_right,
261+
radi.bottom_right,
262+
radi.bottom_left,
263+
]
92264
}
93265
}

core/src/border_radius.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod keyboard;
2020
pub mod layout;
2121
pub mod mouse;
2222
pub mod overlay;
23+
pub mod padding;
2324
pub mod renderer;
2425
pub mod svg;
2526
pub mod text;
@@ -35,7 +36,6 @@ mod color;
3536
mod content_fit;
3637
mod element;
3738
mod length;
38-
mod padding;
3939
mod pixels;
4040
mod point;
4141
mod rectangle;

0 commit comments

Comments
 (0)