Skip to content
Permalink
Browse files
8252015: [macos11] java.awt.TrayIcon requires updates for template im…
…ages

Co-authored-by: Tres Finocchiaro <tres.finocchiaro@gmail.com>
Co-authored-by: Peter Zhelezniakov <peterz@openjdk.org>
Reviewed-by: serb
  • Loading branch information
2 people authored and Alexander Scherbatiy committed Jan 12, 2021
1 parent ac2dee5 commit 400dc76f686206d17c6ff87799c10021edc146d0
@@ -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;
@@ -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);

@@ -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() {
@@ -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;

@@ -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;

@@ -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 ||
@@ -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];
}

@@ -176,8 +177,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];
@@ -197,7 +198,6 @@ - (void)addTrackingArea {
}

-(void) dealloc {
[image release];
[trackingArea release];
[super dealloc];
}
@@ -210,16 +210,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;
}
@@ -237,29 +227,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 {
@@ -377,15 +353,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);
@@ -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.
*

1 comment on commit 400dc76

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 400dc76 Jan 12, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.