diff --git a/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java b/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java index faa7571246117..b92c62f4620fe 100644 --- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java +++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java @@ -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.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() { diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.h b/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.h index 49e0e7d8645bd..458594157be97 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.h +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.h @@ -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 { +@interface AWTTrayIconView : NSStatusBarButton { @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; diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.m index 23e17fb7b1494..233888591694f 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.m @@ -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]; } @@ -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]; @@ -196,7 +197,6 @@ - (void)addTrackingArea { } -(void) dealloc { - [image release]; [trackingArea release]; [super dealloc]; } @@ -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; } @@ -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 { @@ -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); diff --git a/src/java.desktop/share/classes/java/awt/TrayIcon.java b/src/java.desktop/share/classes/java/awt/TrayIcon.java index ccbf0ae0599b2..12482912da01a 100644 --- a/src/java.desktop/share/classes/java/awt/TrayIcon.java +++ b/src/java.desktop/share/classes/java/awt/TrayIcon.java @@ -71,6 +71,15 @@ * a {@code TrayIcon}. Otherwise the constructor will throw a * SecurityException. * + *

+ * @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. + * *

See the {@link SystemTray} class overview for an example on how * to use the {@code TrayIcon} API. *