Skip to content

Improve API for cloning/creating rectangles #2186

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

Conversation

amartya4256
Copy link
Contributor

This PR contributes to providing better interfaces and APIs for handling MonitorAware Rectangles and Points with Abstraction using prototype pattern.

All the Monitor Aware classes implement MonitorAware. Also th coordinate classes liek Rectangle and Point implement an interface copy. This should be introduced as a convention to use copy or copyWith, when a consumer wants to create a new object from the existing one. If there's any offsetting needed, the method copyWith can be used, making sure we copy all the abstract fields of the underlying Child class (if applicable) to the new object.

Also this PR introduces a new method Rectangle.of(Point, int, int) to make it easy to create rectangles using points instead of calling the Rectangle Constructor.

contributes to
#62 and #128

Copy link
Contributor

github-actions bot commented May 27, 2025

Test Results

   545 files  ±0     545 suites  ±0   28m 17s ⏱️ +14s
 4 399 tests ±0   4 381 ✅ ±0   18 💤 ±0  0 ❌ ±0 
16 723 runs  ±0  16 583 ✅ ±0  140 💤 ±0  0 ❌ ±0 

Results for commit 128175b. ± Comparison against base commit e39c288.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@fedejeanne fedejeanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking at this PR together with its first usage (eclipse-platform/eclipse.platform.ui#3005) and I have some questions (it might be that there are more consumers that haven't been adapted yet so I might be missing "the big picture", feel free to comment on that :-) ):

  • Do we really need the interface MonitorAware? How is it helpful?
  • I would remove the new method copyWith(...) if it's only necessary to copy and modify the receiver since that is something that the consumer can do on its own without "special handling" (no monitor-awareness, zooms, etc seem to play a role here)
  • I see that Copyable is merely a "better named version of Cloneable" and it was probably added so that the methods copy() and copyWith(...) are associated with each other (?) . If that's the case, I think getting rid of copyWith(...) and replacing Copyable with Clonable is really a valid option.

@amartya4256 amartya4256 force-pushed the amartya4256/montior_aware_coordinates branch from c4be5d7 to dcb6310 Compare May 30, 2025 11:08
@amartya4256
Copy link
Contributor Author

  • I see that Copyable is merely a "better named version of Cloneable" and it was probably added so that the methods copy() and copyWith(...) are associated with each other (?) . If that's the case, I think getting rid of copyWith(...) and replacing Copyable with Clonable is really a valid option.

Cloneable has clone() method as protected. If it is fine to override it to public without breaking the API conventions of SWT, I can use that for sure.

@amartya4256 amartya4256 force-pushed the amartya4256/montior_aware_coordinates branch 2 times, most recently from e113616 to c5f86dd Compare May 30, 2025 11:36
Copy link
Contributor

@HeikoKlare HeikoKlare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, the change looks reasonable to me. Having proper clone functionality for those data classes is fine. I am not sure whether we can have a more expressive name for Rectangle.of.

Cloneable has clone() method as protected. If it is fine to override it to public without breaking the API conventions of SWT, I can use that for sure.

Note that Cloneable has not clone() method at all. clone() is defined by Object in Java and needs to be overwritten and made public in classes that implement the Clonable interface. I don't see that this can conflict with any conventions or contracts in SWT. Rectangle cannot be extended, so there is not chance that there is a conflicting overwrite of the method.

@laeubi
Copy link
Contributor

laeubi commented Jun 2, 2025

Just an idea:

Do we really need an own class here? Can't we add a (package protected?) field to the regular Rectangle and mark it as not for public use like we do with the handles in Control?

@amartya4256 amartya4256 force-pushed the amartya4256/montior_aware_coordinates branch 3 times, most recently from 60dbf84 to 1c2b80a Compare June 2, 2025 11:55
@amartya4256 amartya4256 marked this pull request as ready for review June 2, 2025 13:03
@HeikoKlare
Copy link
Contributor

Just an idea:

Do we really need an own class here? Can't we add a (package protected?) field to the regular Rectangle and mark it as not for public use like we do with the handles in Control?

The class already exists. This change just adds further functionality.

Back then, we also discussed adding a field to the existing Rectangle class, but there are at least two reasons against it:

  • The Rectangle is a quite general type: it is used all over the place and adding another (usually useless) field unnecessary bloats up instances
  • A pure Rectangle has nothing to with a monitor (as it is not semantically tied to a monitor), so adding such a field would be strange also regarding the class' semantics.

@laeubi
Copy link
Contributor

laeubi commented Jun 6, 2025

If that holds true then one should actually not extend Rectangle but e.g. use a record that combines a rectangle with a monitor.

@HeikoKlare
Copy link
Contributor

If that holds true then one should actually not extend Rectangle but e.g. use a record that combines a rectangle with a monitor.

If it's not a subclass, you cannot pass it via existing APIs that accept or return instances of Rectangle.

@laeubi
Copy link
Contributor

laeubi commented Jun 6, 2025

If that holds true then one should actually not extend Rectangle but e.g. use a record that combines a rectangle with a monitor.

If it's not a subclass, you cannot pass it via existing APIs that accept or return instances of Rectangle.

The obviously the monitor is an important property in some cases and there are only others where it is irrelevant. This is similar to how we do it with Events where some fields are filled and others are not depending on the context / usage. I also don't see that we create millions of Point/Rectangles that the object size actually matters.

So either we should have new API that accepts a Point/Rectangle + Monitor or we should account for the fact that the monitor is a property of a Rectangles or point. Having only internal used classes with marker interface and trying to restrict what we made public seems not very useful and complicates the matter more than it solves it.

@HeikoKlare
Copy link
Contributor

Having only internal used classes with marker interface and trying to restrict what we made public seems not very useful and complicates the matter more than it solves it.

Why? And what's the marker interface here?
I have to admit that I don't see the problem that we need to address. Unless it's necessary and well designed to expose a propery, why should we do that?
The proposed API for cloning rectangles in this PR is even reasonable independent of whether there are monitor-aware specialization or not.

This commit contributes to providing better interfaces and APIs for
cloning/creating rectangles, including the proper consideration of
subclasses like the monitor-aware implementation of Rectangle.

Contributes to
eclipse-platform#62 and
eclipse-platform#128
@HeikoKlare HeikoKlare force-pushed the amartya4256/montior_aware_coordinates branch from 1c2b80a to 34884b2 Compare June 16, 2025 09:08
@HeikoKlare HeikoKlare force-pushed the amartya4256/montior_aware_coordinates branch from 34884b2 to 128175b Compare June 16, 2025 09:16
Copy link
Contributor

@HeikoKlare HeikoKlare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've corrected wrong code formatting and added the necessary version bump commit, as a minor bump for SWT is required due to added API.

@HeikoKlare HeikoKlare changed the title Better API for handling MonitorAware Coordinates Improve API for cloning/creating rectangles Jun 16, 2025
@HeikoKlare HeikoKlare merged commit fe64f69 into eclipse-platform:master Jun 16, 2025
17 checks passed
@HeikoKlare HeikoKlare deleted the amartya4256/montior_aware_coordinates branch June 16, 2025 11:35
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.

Drag and Drop Region moves with a different scale as compared to the cursor between 2 monitors
5 participants