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

8252015: [macos11] java.awt.TrayIcon requires updates for template images #481

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java
Expand Up @@ -45,6 +45,8 @@
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.peer.TrayIconPeer;
import java.security.AccessController;
import java.security.PrivilegedAction;

import javax.swing.Icon;
import javax.swing.UIManager;
Expand All @@ -69,6 +71,10 @@ public class CTrayIcon extends CFRetainedResource implements TrayIconPeer {
// events between MOUSE_PRESSED and MOUSE_RELEASED for particular button
private static int mouseClickButtons = 0;

private final static boolean useTemplateImages = AccessController.doPrivileged((PrivilegedAction<Boolean>)
() -> Boolean.getBoolean("apple.awt.enableTemplateImages")
);

CTrayIcon(TrayIcon target) {
super(0, true);

Expand Down Expand Up @@ -211,13 +217,13 @@ void updateNativeImage(Image image) {
if (cimage != null) {
cimage.execute(imagePtr -> {
execute(ptr -> {
setNativeImage(ptr, imagePtr, imageAutoSize);
setNativeImage(ptr, imagePtr, imageAutoSize, useTemplateImages);
});
});
}
}

private native void setNativeImage(final long model, final long nsimage, final boolean autosize);
private native void setNativeImage(final long model, final long nsimage, final boolean autosize, final boolean template);

private void postEvent(final AWTEvent event) {
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
Expand Down
6 changes: 2 additions & 4 deletions src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.h
Expand Up @@ -52,7 +52,7 @@ extern "C" {
- (void) setTooltip:(NSString *)tooltip;
- (NSStatusItem *)theItem;
- (jobject) peer;
- (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize;
- (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize template:(BOOL)isTemplate;
- (NSPoint) getLocationOnScreen;
- (void) deliverJavaMouseEvent:(NSEvent*) event;

Expand All @@ -61,16 +61,14 @@ extern "C" {
//==================================================================================
/*
* AWTTrayIconView */
@interface AWTTrayIconView : NSView <NSMenuDelegate> {
@interface AWTTrayIconView : NSStatusBarButton <NSMenuDelegate> {
@public
AWTTrayIcon *trayIcon;
NSImage* image;
NSTrackingArea *trackingArea;
BOOL isHighlighted;
}
-(id)initWithTrayIcon:(AWTTrayIcon *)theTrayIcon;
-(void)setHighlighted:(BOOL)aFlag;
-(void)setImage:(NSImage*)anImage;
-(void)setTrayIcon:(AWTTrayIcon*)theTrayIcon;
-(void)addTrackingArea;

Expand Down
42 changes: 9 additions & 33 deletions src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.m
Expand Up @@ -104,7 +104,7 @@ - (jobject) peer{
return peer;
}

- (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize {
- (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize template:(BOOL)isTemplate {
NSSize imageSize = [imagePtr size];
NSSize scaledSize = ScaledImageSizeForStatusBar(imageSize, autosize);
if (imageSize.width != scaledSize.width ||
Expand All @@ -115,6 +115,7 @@ - (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize {
CGFloat itemLength = scaledSize.width + 2.0*kImageInset;
[theItem setLength:itemLength];

[imagePtr setTemplate: isTemplate];
[view setImage:imagePtr];
}

Expand Down Expand Up @@ -175,8 +176,8 @@ -(id)initWithTrayIcon:(AWTTrayIcon *)theTrayIcon {
self = [super initWithFrame:NSMakeRect(0, 0, 1, 1)];

[self setTrayIcon: theTrayIcon];
[self setImage: nil];
isHighlighted = NO;
image = nil;
trackingArea = nil;

[self addTrackingArea];
Expand All @@ -196,7 +197,6 @@ - (void)addTrackingArea {
}

-(void) dealloc {
[image release];
[trackingArea release];
[super dealloc];
}
Expand All @@ -209,16 +209,6 @@ - (void)setHighlighted:(BOOL)aFlag
}
}

- (void)setImage:(NSImage*)anImage {
[anImage retain];
[image release];
image = anImage;

if (image != nil) {
[self setNeedsDisplay:YES];
}
}

-(void)setTrayIcon:(AWTTrayIcon*)theTrayIcon {
trayIcon = theTrayIcon;
}
Expand All @@ -236,29 +226,15 @@ - (void)menuDidClose:(NSMenu *)menu

- (void)drawRect:(NSRect)dirtyRect
{
if (image == nil) {
if (self.image == nil) {
return;
}

NSRect bounds = [self bounds];
NSSize imageSize = [image size];

NSRect drawRect = {{ (bounds.size.width - imageSize.width) / 2.0,
(bounds.size.height - imageSize.height) / 2.0 }, imageSize};

// don't cover bottom pixels of the status bar with the image
if (drawRect.origin.y < 1.0) {
drawRect.origin.y = 1.0;
}
drawRect = NSIntegralRect(drawRect);

[trayIcon.theItem drawStatusBarBackgroundInRect:bounds
withHighlight:isHighlighted];
[image drawInRect:drawRect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0
];

[super drawRect: dirtyRect];
}

- (void)mouseDown:(NSEvent *)event {
Expand Down Expand Up @@ -375,15 +351,15 @@ - (void) otherMouseDragged:(NSEvent *)event {
/*
* Class: sun_lwawt_macosx_CTrayIcon
* Method: setNativeImage
* Signature: (JJZ)V
* Signature: (JJZZ)V
*/
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTrayIcon_setNativeImage
(JNIEnv *env, jobject self, jlong model, jlong imagePtr, jboolean autosize) {
(JNIEnv *env, jobject self, jlong model, jlong imagePtr, jboolean autosize, jboolean isTemplate) {
JNF_COCOA_ENTER(env);

AWTTrayIcon *icon = jlong_to_ptr(model);
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
[icon setImage:jlong_to_ptr(imagePtr) sizing:autosize];
[icon setImage:jlong_to_ptr(imagePtr) sizing:autosize template:isTemplate];
}];

JNF_COCOA_EXIT(env);
Expand Down
9 changes: 9 additions & 0 deletions src/java.desktop/share/classes/java/awt/TrayIcon.java
Expand Up @@ -71,6 +71,15 @@
* a {@code TrayIcon}. Otherwise the constructor will throw a
* SecurityException.
*
* <p>
* @implnote
* When the {@systemProperty apple.awt.enableTemplateImages} property is
* set, all images associated with instances of this class are treated
* as template images by the native desktop system. This means all color
* information is discarded, and the image is adapted automatically to
* be visible when desktop theme and/or colors change. This property
* only affects MacOSX.
*
* <p> See the {@link SystemTray} class overview for an example on how
* to use the {@code TrayIcon} API.
*
Expand Down