iced
iced copied to clipboard
add helper fun for Padding
This add some new method to the Padding struct.
I have some other ideas, like also provide some more method like
/// Create a [`Padding`] with equally spaces of the right and left side
pub fn vertical(padding: f32) -> Self {
Self {
left: padding,
right: padding,
..Padding::ZERO
}
}
and the same for vertical, but that will involve breaking change.
And also provide a constructor API alternative:
pub fn vertical(self, padding: f32) -> Self {
self.left = padding;
self.right = padding;
self
}
to allow writting this:
let padding = Padding::horizontal(10f32)
.bottom(5f32);
But this will involve, reanaming the method i written like such
fn new_top()
fn new_bottom()
fn new_vertical()
which is fine imo.
I think it make more sense to provide this funcions rather than
pub fn vertical(self) -> f32 {
self.top + self.bottom
}
because i don't really see a lot of use case for this (user perspective).
I think that instead of these new function being constructors, they should be builder-like functions. It makes the api a lot more flexible, at the small cost of turning Padding::bottom(5.0)
into Padding::ZERO.bottom(5.0)
.
E.g.
/// Sets the top padding to `padding`.
pub const fn top(self, padding: f32) -> Self {
Self {
top: padding,
..self
}
}
I think that instead of these new function being constructors, they should be builder-like functions. It makes the api a lot more flexible, at the small cost of turning Padding::bottom(5.0) into Padding::ZERO.bottom(5.0).
Yes that what i was saying. But we can still use Padding::new_bottom(5.0)
for constructor and padding.bottom(5.0)
for builder.
What do you think @hecrj ?
I think that instead of these new function being constructors, they should be builder-like functions. It makes the api a lot more flexible, at the small cost of turning Padding::bottom(5.0) into Padding::ZERO.bottom(5.0).
Yes that what i was saying. But we can still use
Padding::new_bottom(5.0)
for constructor andpadding.bottom(5.0)
for builder.What do you think @hecrj ?
The naming convention for constructors of this type is with_<thing>
, so Padding::with_bottom(5.0)
in this example. However, I don't think having separate constructors for each is required here and just adds unnecessary complexity.
However, I don't think having separate constructors for each is required here and just adds unnecessary complexity.
I better than
Padding {
bottom: 5.0,
..Padding::ZERO
}
(one line instead of four)
However, I don't think having separate constructors for each is required here and just adds unnecessary complexity.
I better than
Padding { bottom: 5.0, ..Padding::ZERO }
(one line instead of four)
With builder pattern it would be
Padding::ZERO.bottom(5.0)
With builder pattern it would be
Padding::ZERO.bottom(5.0)
True, still prefer the with_bottom
though.
we could also add with_vertical
, with_horizontal
and change the behavior of vertical
and horizontal
.
I think we could also remove the From impl