Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve geometry APIs, to be closer to euclid #1621

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ids1024
Copy link
Member

@ids1024 ids1024 commented Dec 27, 2024

I want to update #1136, make any needed improvements upstream to euclid, and get that merged. But it requires a lot of changes both in Smithay and compositors using it. Perhaps adding some APIs and uses of the #[deprecated] attribute first could make that a little less painful to deal with.

Even if Smithay didn't move to euclid, it would be worth borrowing some ideas to make geometry handling more strongly typed, as well as a couple API conveniences.

@ids1024 ids1024 force-pushed the geometry branch 3 times, most recently from ef1a272 to 771ba0b Compare December 27, 2024 00:46
@ids1024 ids1024 marked this pull request as ready for review December 27, 2024 01:16
ids1024 added a commit to pop-os/cosmic-comp that referenced this pull request Dec 27, 2024
ids1024 added a commit to pop-os/cosmic-comp that referenced this pull request Dec 27, 2024
ids1024 added a commit to pop-os/cosmic-comp that referenced this pull request Dec 27, 2024
@Drakulix
Copy link
Member

This matches euclid::Rect's API, but it also seems a little better to
have strong typing without implicit conversion like this.

I am not sure I agree with this, when we are just adding into-calls everywhere. It just makes everything more busy. What is the future goal here to actually make use of that type-safety?

src/utils/geometry.rs Outdated Show resolved Hide resolved
Matches definitions in `euclid`. This seems useful and is a pattern
widely used in Smithay.
A couple patterns are used for this already. This method makes it
consistent and a bit more concise.

This seems to cover a lot of the uses of `Rectangle::from_loc_and_size`.
This new function is the same as `from_loc_and_size`, except it takes
concrete types as arguments instead of `impl Into`.

This matches `euclid::Rect`'s API, but it also seems a little better to
have strong typing without implicit conversion like this. In a few cases
it's not obvious that the argument is variable of tuple type, so there
is an implied assertion that the coordinate space is right.

The use of `#[deprecated]` makes this not entirely urgent for
compositors, but uses of this should be updated to `::new`, or other
functions like `from_size`.
@codecov-commenter
Copy link

codecov-commenter commented Dec 28, 2024

Codecov Report

Attention: Patch coverage is 40.50633% with 47 lines in your changes missing coverage. Please review.

Project coverage is 19.34%. Comparing base (2eddf12) to head (5af799e).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/utils/geometry.rs 53.33% 14 Missing ⚠️
src/backend/renderer/element/texture.rs 0.00% 5 Missing ⚠️
src/wayland/compositor/handlers.rs 16.66% 5 Missing ⚠️
src/backend/renderer/element/memory.rs 0.00% 4 Missing ⚠️
src/backend/renderer/element/solid.rs 0.00% 3 Missing ⚠️
src/backend/renderer/mod.rs 0.00% 2 Missing ⚠️
src/backend/renderer/utils/wayland.rs 60.00% 2 Missing ⚠️
src/wayland/viewporter/mod.rs 0.00% 2 Missing ⚠️
anvil/src/render.rs 0.00% 1 Missing ⚠️
anvil/src/shell/mod.rs 66.66% 1 Missing ⚠️
... and 8 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1621      +/-   ##
==========================================
+ Coverage   19.33%   19.34%   +0.01%     
==========================================
  Files         171      171              
  Lines       28365    28363       -2     
==========================================
+ Hits         5485     5488       +3     
+ Misses      22880    22875       -5     
Flag Coverage Δ
wlcs-buffer 16.84% <39.24%> (+0.01%) ⬆️
wlcs-core 16.58% <37.97%> (+0.01%) ⬆️
wlcs-output 6.83% <15.18%> (+<0.01%) ⬆️
wlcs-pointer-input 18.31% <37.97%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

ids1024 added a commit to pop-os/cosmic-comp that referenced this pull request Dec 31, 2024
@ids1024
Copy link
Member Author

ids1024 commented Dec 31, 2024

I've added a Mul impl for Rectangle (which euclid has). This is useful in at least a couple places in cosmic-comp.

Where possible it seems best to use high-level operations like that on these strongly-typed geometry types instead of calls to things like Rectange::new, though I don't know how many of the use cases can be better handled with something like that.

I am not sure I agree with this, when we are just adding into-calls everywhere. It just makes everything more busy. What is the future goal here to actually make use of that type-safety?

Where we really want to get type safety is by using more coordinate spaces, distinguishing between points and vectors (it shouldn't be possible to add two "points" in the same space to get an element of that space.) And things like euclid::Translation2D. #1620 happens to be a good example of something we'd want to make statically impossible.

If we want to move to euclid, this change to match euclid::Rectangle::new will be necessary. That and renaming loc/w/h to origin/width/height account for a lot of the changes needed to compile with the euclid types (plus extension traits for to_physical, etc).

If we don't move to euclid, avoiding implicit conversion generally seems like a best practice. It's not a big deal, but there are a couple places where it's not obvious that the argument is a tuple, so we're asserting what coordinate space it's part of. In general it doesn't make things much worse. Something like euclid::rect could be handy when constructing a rectangle from four scalars (particularly in tests).

Though if that change is too controversial I can drop 5471a59 from this PR. The other small additions here seem useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants