-
Notifications
You must be signed in to change notification settings - Fork 162
Rework new Image constructors accepting a ImageGcDrawer #1985
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
Draft
HannesWell
wants to merge
2
commits into
eclipse-platform:master
Choose a base branch
from
HannesWell:rework-image-drawer-constructors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDrawer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 Yatta and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Yatta - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.swt.graphics; | ||
|
||
import java.util.function.*; | ||
|
||
import org.eclipse.swt.*; | ||
import org.eclipse.swt.internal.image.*; | ||
|
||
/** | ||
* Interface to provide a callback mechanism to draw on different GC instances | ||
* depending on the zoom the image will be used for. A common use case is when | ||
* the application is moved from a low DPI monitor to a high DPI monitor. This | ||
* provides API which will be called by SWT during the image rendering. | ||
* | ||
* This interface needs to be implemented by client code to provide logic that | ||
* draws on the empty GC on demand. | ||
* | ||
* @since 3.130 | ||
*/ | ||
@FunctionalInterface | ||
public interface ImageDrawer { | ||
|
||
/** | ||
* Draws an image on a GC for a requested zoom level. | ||
* | ||
* @param gc The GC will draw on the underlying Image and is configured | ||
* for the targeted zoom | ||
* @param imageWidth The width of the image in points to draw on | ||
* @param imageHeight The height of the image in points to draw on | ||
*/ | ||
void drawOn(GC gc, int imageWidth, int imageHeight); | ||
|
||
/** | ||
* @since 3.130 | ||
*/ | ||
public static ImageDrawer withGCStyle(int style, ImageDrawer drawer) { | ||
if (drawer instanceof ExtendedImageDrawer extendedDrawer) { | ||
return new ExtendedImageDrawer(extendedDrawer.original(), extendedDrawer.postProcessor(), style); | ||
} | ||
return new ExtendedImageDrawer(drawer, null, style); | ||
} | ||
|
||
/** | ||
* @since 3.130 | ||
*/ | ||
public static ImageDrawer create(ImageDrawer drawer, Consumer<ImageData> postProcessor) { | ||
if (drawer instanceof ExtendedImageDrawer extendedDrawer) { | ||
return new ExtendedImageDrawer(extendedDrawer.original(), postProcessor, extendedDrawer.style()); | ||
} | ||
return new ExtendedImageDrawer(drawer, postProcessor, SWT.NONE); | ||
} | ||
|
||
// TODO: or use some kind of builder pattern? | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...rg.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/ExtendedImageDrawer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 Hannes Wellmann and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Hannes Wellmann - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.swt.internal.image; | ||
|
||
import java.util.*; | ||
import java.util.function.*; | ||
|
||
import org.eclipse.swt.*; | ||
import org.eclipse.swt.graphics.*; | ||
|
||
public record ExtendedImageDrawer(ImageDrawer original, Consumer<ImageData> postProcessor, int style) | ||
implements ImageDrawer { | ||
|
||
public ExtendedImageDrawer { | ||
Objects.requireNonNull(original); | ||
} | ||
|
||
@Override | ||
public void drawOn(GC gc, int imageWidth, int imageHeight) { | ||
original.drawOn(gc, imageWidth, imageHeight); | ||
} | ||
|
||
public static int getGCStyle(ImageDrawer drawer) { | ||
if (drawer instanceof ExtendedImageDrawer extendedDrawer) { | ||
return extendedDrawer.style(); | ||
} | ||
return SWT.NONE; | ||
} | ||
|
||
public static void postProcess(ImageDrawer drawer, ImageData data) { | ||
if (drawer instanceof @SuppressWarnings("removal") ImageGcDrawer gcDrawer) { | ||
gcDrawer.postProcess(data); | ||
} else if (drawer instanceof ExtendedImageDrawer extendedDrawer) { | ||
Consumer<ImageData> postProcessor2 = extendedDrawer.postProcessor(); | ||
if (postProcessor2 != null) { | ||
postProcessor2.accept(data); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'd rather not have to use static methods in here since one needs to know about the existence of
ExtendedImageDrawer
(just like Heiko pointed out). A more natural approach to me would be:Which means that you need to move
getGcStyle()
back toImageDrawer
.