-
Notifications
You must be signed in to change notification settings - Fork 189
Introduce a new Image(Display,Point) constructor
#2865
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
base: master
Are you sure you want to change the base?
Conversation
The deprecation of the `Image(Display,Rectangle)` constructor in 1d59663 introduces a lot of verbosity. Where one could previously create an image via: > new Image(display, widget.getBounds()) One now has to store the bounds in a local variable and then call: > new Image(display, r.width, r.height) This approach is also error prone, as one might easily mix up the order of parameters. With this new constructor, this problem can be avoided as one can simply call: > new Image(display, widget.getSize())
cf6e76c to
bbdf959
Compare
Test Results 173 files - 3 173 suites - 3 25m 13s ⏱️ - 1m 50s Results for commit bbdf959. ± Comparison against base commit 9412c36. This pull request removes 1 and adds 1 tests. Note that renamed tests count towards both.This pull request skips 1 test. |
HeikoKlare
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the wish for creating an image from a given size without the need to decompose an according size object. Still I am hesitant to add such a constructor for at least the following reasons:
- The existing amount of constructors is a burden already. Nowadays, one would rather create factories or builders for types of a library/framework. In particular, convenience constructors (like the proposed one and the existing one taking a
Rectangle) are better placed in factories / factory methods. - Constructing an empty image is something that should be avoided as far as possible anyway. Such images cannot be scaled "properly"/sharply, so it should only be used for, e.g., screenshot functionalities. In all other cases, the "dynamic" image types based on
ImageDataProvider,ImageFileNameProvider,ImageGcDraweretc. should be used.
Do you have references where the pattern ...
new Image(display, widget.getBounds())
is necessary and cannot/should not be replaced with, e.g., an ImageGcDrawer?
In case we agree that such a construction pattern is still needed, I propose to rather do one of the following:
- Undeprecate the
Rectangleconstructor: in case that one is sufficient, we probably better preserve an existing constructor instead of introducing an additional, very similar one, as we would then also avoid the potential complications when finally removing that constructor - Provide the convenience construction functionality as a factory / factory method: we have already discussed the idea of constructing resource with factories (that are OS-independent common code) instead of constructors at some other place as well. It's probably nothing we can enforce full migration to, but at least for new code (or for new ways of construction), we could use it. So we could either have an
Image#create(Device, Point)factory method or a new commonImageFactoryclass or the like which only needs to implement that method once instead of multiple times for every OS.
There are probably several reasons, but quite often, it is simply not needed. I'm also not sure whether using an ImageGcDrawer (which is even more verbose) is the correct solution, when the goal is to reduce the amount of bloat. At least for my use case, it generally revolves around the idea of taking a screenshot of a widget. For example: Control c = ...
Image img = new Image(null, c.getBounds());
GC gc = new GC(img);
c.print(gc);
gc.dispose();This generally doesn't make sense with an More broadly speaking, I would never use an
The constructor is not only deprecated but also marked as "for removal". Which I assume was done for a very good reason? At least my understanding is that it was done because it doesn't make much sense to use a I don't have a strong opinion on how this instance is created, as long as it's something that can be easily written down. Be it via a new constructor, a factory method or whatever else is fine by me, as long as it can be written down without any additional overhead. |
We already have those static factory methods in the |
|
Thank you for the detailed information. The use case obviously makes sense and I agree that in such a case of just taking a screenshot, a "dynamic" image construction (e.g., via Actually, when taking a look into the PR introducing the deprecation (for removal), we had a short discussion about that exact use case: #2088 (comment)
That's correct, the So yes, the
@HannesWell since you were involved in the discussion on deprecation back then and since you have already drafted the screenshot API functionality (https://github.com/eclipse-platform/eclipse.platform.swt/pull/2104/files), do you have an opinion on this? |
The deprecation of the
Image(Display,Rectangle)constructor in 1d59663 introduces a lot of verbosity.Where one could previously create an image via:
One now has to store the bounds in a local variable and then call:
This approach is also error prone, as one might easily mix up the order of parameters. With this new constructor, this problem can be avoided as one can simply call: