euclid
euclid copied to clipboard
Rect usability improvements
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_xRect::set_yRect::set_widthRect::set_height
Source of inspiration: QRect.
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.
you can write let r = rect(x, y, w, h);
How it works?
Should I make a pull request?
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.
Oh, this a global function. Didn't thought about it.
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.
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()andy(),foo.set_width(w);vsfoo.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,widthandheight. 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.