iced icon indicating copy to clipboard operation
iced copied to clipboard

add helper fun for Padding

Open wiiznokes opened this issue 4 months ago • 9 comments

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).

wiiznokes avatar Feb 18 '24 15:02 wiiznokes

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).

Koranir avatar Feb 19 '24 10:02 Koranir

E.g.

    /// Sets the top padding to `padding`.
    pub const fn top(self, padding: f32) -> Self {
        Self {
            top: padding,
            ..self
        }
    }

Koranir avatar Feb 19 '24 10:02 Koranir

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 ?

wiiznokes avatar Feb 19 '24 16:02 wiiznokes

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 ?

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.

Koranir avatar Feb 19 '24 22:02 Koranir

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)

wiiznokes avatar Feb 19 '24 23:02 wiiznokes

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)

Koranir avatar Feb 20 '24 00:02 Koranir

With builder pattern it would be

Padding::ZERO.bottom(5.0)

True, still prefer the with_bottom though.

wiiznokes avatar Feb 23 '24 22:02 wiiznokes

we could also add with_vertical, with_horizontal and change the behavior of vertical and horizontal.

wiiznokes avatar Feb 23 '24 22:02 wiiznokes

I think we could also remove the From impl

wiiznokes avatar Feb 23 '24 22:02 wiiznokes