Skip to content

Commit 400dc76

Browse files
Peter Zhelezniakovtresf
authored and
Alexander Scherbatiy
committed
8252015: [macos11] java.awt.TrayIcon requires updates for template images
Co-authored-by: Tres Finocchiaro <tres.finocchiaro@gmail.com> Co-authored-by: Peter Zhelezniakov <peterz@openjdk.org> Reviewed-by: serb
1 parent ac2dee5 commit 400dc76

File tree

4 files changed

+28
-39
lines changed

4 files changed

+28
-39
lines changed

src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import java.awt.image.BufferedImage;
4646
import java.awt.image.ImageObserver;
4747
import java.awt.peer.TrayIconPeer;
48+
import java.security.AccessController;
49+
import java.security.PrivilegedAction;
4850

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

74+
private final static boolean useTemplateImages = AccessController.doPrivileged((PrivilegedAction<Boolean>)
75+
() -> Boolean.getBoolean("apple.awt.enableTemplateImages")
76+
);
77+
7278
CTrayIcon(TrayIcon target) {
7379
super(0, true);
7480

@@ -211,13 +217,13 @@ void updateNativeImage(Image image) {
211217
if (cimage != null) {
212218
cimage.execute(imagePtr -> {
213219
execute(ptr -> {
214-
setNativeImage(ptr, imagePtr, imageAutoSize);
220+
setNativeImage(ptr, imagePtr, imageAutoSize, useTemplateImages);
215221
});
216222
});
217223
}
218224
}
219225

220-
private native void setNativeImage(final long model, final long nsimage, final boolean autosize);
226+
private native void setNativeImage(final long model, final long nsimage, final boolean autosize, final boolean template);
221227

222228
private void postEvent(final AWTEvent event) {
223229
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {

src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extern "C" {
5252
- (void) setTooltip:(NSString *)tooltip;
5353
- (NSStatusItem *)theItem;
5454
- (jobject) peer;
55-
- (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize;
55+
- (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize template:(BOOL)isTemplate;
5656
- (NSPoint) getLocationOnScreen;
5757
- (void) deliverJavaMouseEvent:(NSEvent*) event;
5858

@@ -61,16 +61,14 @@ extern "C" {
6161
//==================================================================================
6262
/*
6363
* AWTTrayIconView */
64-
@interface AWTTrayIconView : NSView <NSMenuDelegate> {
64+
@interface AWTTrayIconView : NSStatusBarButton <NSMenuDelegate> {
6565
@public
6666
AWTTrayIcon *trayIcon;
67-
NSImage* image;
6867
NSTrackingArea *trackingArea;
6968
BOOL isHighlighted;
7069
}
7170
-(id)initWithTrayIcon:(AWTTrayIcon *)theTrayIcon;
7271
-(void)setHighlighted:(BOOL)aFlag;
73-
-(void)setImage:(NSImage*)anImage;
7472
-(void)setTrayIcon:(AWTTrayIcon*)theTrayIcon;
7573
-(void)addTrackingArea;
7674

src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.m

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ - (jobject) peer{
104104
return peer;
105105
}
106106

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

118+
[imagePtr setTemplate: isTemplate];
118119
[view setImage:imagePtr];
119120
}
120121

@@ -176,8 +177,8 @@ -(id)initWithTrayIcon:(AWTTrayIcon *)theTrayIcon {
176177
self = [super initWithFrame:NSMakeRect(0, 0, 1, 1)];
177178

178179
[self setTrayIcon: theTrayIcon];
180+
[self setImage: nil];
179181
isHighlighted = NO;
180-
image = nil;
181182
trackingArea = nil;
182183

183184
[self addTrackingArea];
@@ -197,7 +198,6 @@ - (void)addTrackingArea {
197198
}
198199

199200
-(void) dealloc {
200-
[image release];
201201
[trackingArea release];
202202
[super dealloc];
203203
}
@@ -210,16 +210,6 @@ - (void)setHighlighted:(BOOL)aFlag
210210
}
211211
}
212212

213-
- (void)setImage:(NSImage*)anImage {
214-
[anImage retain];
215-
[image release];
216-
image = anImage;
217-
218-
if (image != nil) {
219-
[self setNeedsDisplay:YES];
220-
}
221-
}
222-
223213
-(void)setTrayIcon:(AWTTrayIcon*)theTrayIcon {
224214
trayIcon = theTrayIcon;
225215
}
@@ -237,29 +227,15 @@ - (void)menuDidClose:(NSMenu *)menu
237227

238228
- (void)drawRect:(NSRect)dirtyRect
239229
{
240-
if (image == nil) {
230+
if (self.image == nil) {
241231
return;
242232
}
243233

244234
NSRect bounds = [self bounds];
245-
NSSize imageSize = [image size];
246-
247-
NSRect drawRect = {{ (bounds.size.width - imageSize.width) / 2.0,
248-
(bounds.size.height - imageSize.height) / 2.0 }, imageSize};
249-
250-
// don't cover bottom pixels of the status bar with the image
251-
if (drawRect.origin.y < 1.0) {
252-
drawRect.origin.y = 1.0;
253-
}
254-
drawRect = NSIntegralRect(drawRect);
255-
256235
[trayIcon.theItem drawStatusBarBackgroundInRect:bounds
257236
withHighlight:isHighlighted];
258-
[image drawInRect:drawRect
259-
fromRect:NSZeroRect
260-
operation:NSCompositeSourceOver
261-
fraction:1.0
262-
];
237+
238+
[super drawRect: dirtyRect];
263239
}
264240

265241
- (void)mouseDown:(NSEvent *)event {
@@ -377,15 +353,15 @@ - (void) otherMouseDragged:(NSEvent *)event {
377353
/*
378354
* Class: sun_lwawt_macosx_CTrayIcon
379355
* Method: setNativeImage
380-
* Signature: (JJZ)V
356+
* Signature: (JJZZ)V
381357
*/
382358
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTrayIcon_setNativeImage
383-
(JNIEnv *env, jobject self, jlong model, jlong imagePtr, jboolean autosize) {
359+
(JNIEnv *env, jobject self, jlong model, jlong imagePtr, jboolean autosize, jboolean isTemplate) {
384360
JNF_COCOA_ENTER(env);
385361

386362
AWTTrayIcon *icon = jlong_to_ptr(model);
387363
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
388-
[icon setImage:jlong_to_ptr(imagePtr) sizing:autosize];
364+
[icon setImage:jlong_to_ptr(imagePtr) sizing:autosize template:isTemplate];
389365
}];
390366

391367
JNF_COCOA_EXIT(env);

src/java.desktop/share/classes/java/awt/TrayIcon.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@
7171
* a {@code TrayIcon}. Otherwise the constructor will throw a
7272
* SecurityException.
7373
*
74+
* <p>
75+
* @implnote
76+
* When the {@systemProperty apple.awt.enableTemplateImages} property is
77+
* set, all images associated with instances of this class are treated
78+
* as template images by the native desktop system. This means all color
79+
* information is discarded, and the image is adapted automatically to
80+
* be visible when desktop theme and/or colors change. This property
81+
* only affects MacOSX.
82+
*
7483
* <p> See the {@link SystemTray} class overview for an example on how
7584
* to use the {@code TrayIcon} API.
7685
*

0 commit comments

Comments
 (0)