euclid icon indicating copy to clipboard operation
euclid copied to clipboard

Rect usability improvements

Open RazrFalcon opened this issue 7 years ago • 5 comments

Currently, the API is pretty unusable comparing to similar libraries.

Examples:

Now Proposal
Rect::new(Point2D::new(0, 0), Size2D::new(10, 10)) Rect::from_xywh(0, 0, 10, 10)
Rect::new(Point2D::new(0, 0), Size2D::new(10, 10)) Rect::from((0, 0, 10, 10)) // From trait
rect.origin.x rect.x()
rect.origin.y rect.y()
rect.size.width rect.width()
rect.size.height rect.height()
rect.min_x() rect.left()
rect.max_x() rect.right()
rect.min_y() rect.top()
rect.max_y() rect.bottom()

New:

  • Rect::set_x
  • Rect::set_y
  • Rect::set_width
  • Rect::set_height

Source of inspiration: QRect.

RazrFalcon avatar Mar 24 '18 15:03 RazrFalcon

Note that you can write let r = rect(x, y, w, h); as a shorthand to create rects instead of Rect::new which is indeed cumbersome. I think that it is already nicer than Rect::from_xywh and implementing the From trait for a tuple of floats.

I'd be happy to add left, right, top, bottom, width and height. I am less enthused by x() and y() since left and top would provide the same thing in a less ambiguous ways (some libraries defines rects as a pair of points or a point at the center with width and height, etc. we even were close to change the representation in euclid at some point). I don't think that setters are providing much (foo.set_width(w); vs foo.origin.width = w;). Although set_left/right/top/bottom would provide something useful.

nical avatar Mar 25 '18 18:03 nical

you can write let r = rect(x, y, w, h);

How it works?

Should I make a pull request?

RazrFalcon avatar Mar 25 '18 18:03 RazrFalcon

See: https://docs.rs/euclid/0.17.2/euclid/fn.rect.html it's a simple function that just calls TypedRect::new(...). There are equivalent functions point2, point3, size2, vec2, etc.

nical avatar Mar 25 '18 18:03 nical

Oh, this a global function. Didn't thought about it.

RazrFalcon avatar Mar 25 '18 18:03 RazrFalcon

I'm not particularly happy about "Rect::from((0, 0, 10, 10)) // From trait" variant since it's not clear what the parameters are. Same applies to the existing rect function to an extent.

kvark avatar Mar 26 '18 15:03 kvark

Perhaps this issue should be closed as well as https://github.com/servo/euclid/issues/321

  • For the first two points of the table, the author's question has been fully resolved.
  • Regarding x() and y(), foo.set_width(w); vs foo.size.width = w; I find your explanation satisfactory and no clarifications have followed from the author and others for 5 years :)
  • The only unresolved issue is with synonyms left, right, top, bottom, width and height. My suggestion - guided by the principle of "less code is better" and given the long period of time that has passed, leave it as it is. Until new considerations.

ArtHome12 avatar Mar 28 '23 09:03 ArtHome12