Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/CTrayIcon.m
38829 views
1
/*
2
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#import <AppKit/AppKit.h>
27
#import <JavaNativeFoundation/JavaNativeFoundation.h>
28
29
#import "CTrayIcon.h"
30
#import "ThreadUtilities.h"
31
#include "GeomUtilities.h"
32
#import "LWCToolkit.h"
33
34
#define kImageInset 4.0
35
36
/**
37
* If the image of the specified size won't fit into the status bar,
38
* then scale it down proprtionally. Otherwise, leave it as is.
39
*/
40
static NSSize ScaledImageSizeForStatusBar(NSSize imageSize) {
41
NSRect imageRect = NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height);
42
43
// There is a black line at the bottom of the status bar
44
// that we don't want to cover with image pixels.
45
CGFloat desiredHeight = [[NSStatusBar systemStatusBar] thickness] - 1.0;
46
CGFloat scaleFactor = MIN(1.0, desiredHeight/imageSize.height);
47
48
imageRect.size.width *= scaleFactor;
49
imageRect.size.height *= scaleFactor;
50
imageRect = NSIntegralRect(imageRect);
51
52
return imageRect.size;
53
}
54
55
@implementation AWTTrayIcon
56
57
- (id) initWithPeer:(jobject)thePeer {
58
if (!(self = [super init])) return nil;
59
60
peer = thePeer;
61
62
theItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
63
[theItem retain];
64
65
view = [[AWTTrayIconView alloc] initWithTrayIcon:self];
66
[theItem setView:view];
67
68
return self;
69
}
70
71
-(void) dealloc {
72
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
73
JNFDeleteGlobalRef(env, peer);
74
75
[[NSStatusBar systemStatusBar] removeStatusItem: theItem];
76
77
// Its a bad idea to force the item to release our view by setting
78
// the item's view to nil: it can lead to a crash in some scenarios.
79
// The item will release the view later on, so just set the view's image
80
// and tray icon to nil since we are done with it.
81
[view setImage: nil];
82
[view setTrayIcon: nil];
83
[view release];
84
85
[theItem release];
86
87
[super dealloc];
88
}
89
90
- (void) setTooltip:(NSString *) tooltip{
91
[view setToolTip:tooltip];
92
}
93
94
-(NSStatusItem *) theItem{
95
return theItem;
96
}
97
98
- (jobject) peer{
99
return peer;
100
}
101
102
- (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize{
103
NSSize imageSize = [imagePtr size];
104
NSSize scaledSize = ScaledImageSizeForStatusBar(imageSize);
105
if (imageSize.width != scaledSize.width ||
106
imageSize.height != scaledSize.height) {
107
[imagePtr setSize: scaledSize];
108
}
109
110
CGFloat itemLength = scaledSize.width + 2.0*kImageInset;
111
[theItem setLength:itemLength];
112
113
[view setImage:imagePtr];
114
}
115
116
- (NSPoint) getLocationOnScreen {
117
return [[view window] convertBaseToScreen: NSZeroPoint];
118
}
119
120
-(void) deliverJavaMouseEvent: (NSEvent *) event {
121
[AWTToolkit eventCountPlusPlus];
122
123
JNIEnv *env = [ThreadUtilities getJNIEnv];
124
125
NSPoint eventLocation = [event locationInWindow];
126
NSPoint localPoint = [view convertPoint: eventLocation fromView: nil];
127
localPoint.y = [view bounds].size.height - localPoint.y;
128
129
NSPoint absP = [NSEvent mouseLocation];
130
NSEventType type = [event type];
131
132
NSRect screenRect = [[NSScreen mainScreen] frame];
133
absP.y = screenRect.size.height - absP.y;
134
jint clickCount;
135
136
clickCount = [event clickCount];
137
138
jdouble deltaX = [event deltaX];
139
jdouble deltaY = [event deltaY];
140
if ([AWTToolkit hasPreciseScrollingDeltas: event]) {
141
deltaX = [event scrollingDeltaX] * 0.1;
142
deltaY = [event scrollingDeltaY] * 0.1;
143
}
144
145
static JNF_CLASS_CACHE(jc_NSEvent, "sun/lwawt/macosx/NSEvent");
146
static JNF_CTOR_CACHE(jctor_NSEvent, jc_NSEvent, "(IIIIIIIIDDI)V");
147
jobject jEvent = JNFNewObject(env, jctor_NSEvent,
148
[event type],
149
[event modifierFlags],
150
clickCount,
151
[event buttonNumber],
152
(jint)localPoint.x, (jint)localPoint.y,
153
(jint)absP.x, (jint)absP.y,
154
deltaY,
155
deltaX,
156
[AWTToolkit scrollStateWithEvent: event]);
157
if (jEvent == nil) {
158
// Unable to create event by some reason.
159
return;
160
}
161
162
static JNF_CLASS_CACHE(jc_TrayIcon, "sun/lwawt/macosx/CTrayIcon");
163
static JNF_MEMBER_CACHE(jm_handleMouseEvent, jc_TrayIcon, "handleMouseEvent", "(Lsun/lwawt/macosx/NSEvent;)V");
164
JNFCallVoidMethod(env, peer, jm_handleMouseEvent, jEvent);
165
}
166
167
@end //AWTTrayIcon
168
//================================================
169
170
@implementation AWTTrayIconView
171
172
-(id)initWithTrayIcon:(AWTTrayIcon *)theTrayIcon {
173
self = [super initWithFrame:NSMakeRect(0, 0, 1, 1)];
174
175
[self setTrayIcon: theTrayIcon];
176
isHighlighted = NO;
177
image = nil;
178
179
return self;
180
}
181
182
-(void) dealloc {
183
[image release];
184
[super dealloc];
185
}
186
187
- (void)setHighlighted:(BOOL)aFlag
188
{
189
if (isHighlighted != aFlag) {
190
isHighlighted = aFlag;
191
[self setNeedsDisplay:YES];
192
}
193
}
194
195
- (void)setImage:(NSImage*)anImage {
196
[anImage retain];
197
[image release];
198
image = anImage;
199
200
if (image != nil) {
201
[self setNeedsDisplay:YES];
202
}
203
}
204
205
-(void)setTrayIcon:(AWTTrayIcon*)theTrayIcon {
206
trayIcon = theTrayIcon;
207
}
208
209
- (void)menuWillOpen:(NSMenu *)menu
210
{
211
[self setHighlighted:YES];
212
}
213
214
- (void)menuDidClose:(NSMenu *)menu
215
{
216
[menu setDelegate:nil];
217
[self setHighlighted:NO];
218
}
219
220
- (void)drawRect:(NSRect)dirtyRect
221
{
222
if (image == nil) {
223
return;
224
}
225
226
NSRect bounds = [self bounds];
227
NSSize imageSize = [image size];
228
229
NSRect drawRect = {{ (bounds.size.width - imageSize.width) / 2.0,
230
(bounds.size.height - imageSize.height) / 2.0 }, imageSize};
231
232
// don't cover bottom pixels of the status bar with the image
233
if (drawRect.origin.y < 1.0) {
234
drawRect.origin.y = 1.0;
235
}
236
drawRect = NSIntegralRect(drawRect);
237
238
[trayIcon.theItem drawStatusBarBackgroundInRect:bounds
239
withHighlight:isHighlighted];
240
[image drawInRect:drawRect
241
fromRect:NSZeroRect
242
operation:NSCompositeSourceOver
243
fraction:1.0
244
];
245
}
246
247
- (void)mouseDown:(NSEvent *)event {
248
[trayIcon deliverJavaMouseEvent: event];
249
250
// don't show the menu on ctrl+click: it triggers ACTION event, like right click
251
if (([event modifierFlags] & NSControlKeyMask) == 0) {
252
//find CTrayIcon.getPopupMenuModel method and call it to get popup menu ptr.
253
JNIEnv *env = [ThreadUtilities getJNIEnv];
254
static JNF_CLASS_CACHE(jc_CTrayIcon, "sun/lwawt/macosx/CTrayIcon");
255
static JNF_MEMBER_CACHE(jm_getPopupMenuModel, jc_CTrayIcon, "getPopupMenuModel", "()J");
256
jlong res = JNFCallLongMethod(env, trayIcon.peer, jm_getPopupMenuModel);
257
258
if (res != 0) {
259
CPopupMenu *cmenu = jlong_to_ptr(res);
260
NSMenu* menu = [cmenu menu];
261
[menu setDelegate:self];
262
[trayIcon.theItem popUpStatusItemMenu:menu];
263
[self setNeedsDisplay:YES];
264
}
265
}
266
}
267
268
- (void) mouseUp:(NSEvent *)event {
269
[trayIcon deliverJavaMouseEvent: event];
270
}
271
272
- (void) mouseDragged:(NSEvent *)event {
273
[trayIcon deliverJavaMouseEvent: event];
274
}
275
276
- (void) rightMouseDown:(NSEvent *)event {
277
[trayIcon deliverJavaMouseEvent: event];
278
}
279
280
- (void) rightMouseUp:(NSEvent *)event {
281
[trayIcon deliverJavaMouseEvent: event];
282
}
283
284
- (void) rightMouseDragged:(NSEvent *)event {
285
[trayIcon deliverJavaMouseEvent: event];
286
}
287
288
- (void) otherMouseDown:(NSEvent *)event {
289
[trayIcon deliverJavaMouseEvent: event];
290
}
291
292
- (void) otherMouseUp:(NSEvent *)event {
293
[trayIcon deliverJavaMouseEvent: event];
294
}
295
296
- (void) otherMouseDragged:(NSEvent *)event {
297
[trayIcon deliverJavaMouseEvent: event];
298
}
299
300
301
@end //AWTTrayIconView
302
//================================================
303
304
/*
305
* Class: sun_lwawt_macosx_CTrayIcon
306
* Method: nativeCreate
307
* Signature: ()J
308
*/
309
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CTrayIcon_nativeCreate
310
(JNIEnv *env, jobject peer) {
311
__block AWTTrayIcon *trayIcon = nil;
312
313
JNF_COCOA_ENTER(env);
314
315
jobject thePeer = JNFNewGlobalRef(env, peer);
316
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
317
trayIcon = [[AWTTrayIcon alloc] initWithPeer:thePeer];
318
}];
319
320
JNF_COCOA_EXIT(env);
321
322
return ptr_to_jlong(trayIcon);
323
}
324
325
326
/*
327
* Class: java_awt_TrayIcon
328
* Method: initIDs
329
* Signature: ()V
330
*/
331
JNIEXPORT void JNICALL Java_java_awt_TrayIcon_initIDs
332
(JNIEnv *env, jclass cls) {
333
//Do nothing.
334
}
335
336
/*
337
* Class: sun_lwawt_macosx_CTrayIcon
338
* Method: nativeSetToolTip
339
* Signature: (JLjava/lang/String;)V
340
*/
341
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTrayIcon_nativeSetToolTip
342
(JNIEnv *env, jobject self, jlong model, jstring jtooltip) {
343
JNF_COCOA_ENTER(env);
344
345
AWTTrayIcon *icon = jlong_to_ptr(model);
346
NSString *tooltip = JNFJavaToNSString(env, jtooltip);
347
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
348
[icon setTooltip:tooltip];
349
}];
350
351
JNF_COCOA_EXIT(env);
352
}
353
354
/*
355
* Class: sun_lwawt_macosx_CTrayIcon
356
* Method: setNativeImage
357
* Signature: (JJZ)V
358
*/
359
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTrayIcon_setNativeImage
360
(JNIEnv *env, jobject self, jlong model, jlong imagePtr, jboolean autosize) {
361
JNF_COCOA_ENTER(env);
362
363
AWTTrayIcon *icon = jlong_to_ptr(model);
364
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
365
[icon setImage:jlong_to_ptr(imagePtr) sizing:autosize];
366
}];
367
368
JNF_COCOA_EXIT(env);
369
}
370
371
JNIEXPORT jobject JNICALL
372
Java_sun_lwawt_macosx_CTrayIcon_nativeGetIconLocation
373
(JNIEnv *env, jobject self, jlong model) {
374
jobject jpt = NULL;
375
376
JNF_COCOA_ENTER(env);
377
378
__block NSPoint pt = NSZeroPoint;
379
AWTTrayIcon *icon = jlong_to_ptr(model);
380
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
381
NSPoint loc = [icon getLocationOnScreen];
382
pt = ConvertNSScreenPoint(env, loc);
383
}];
384
385
jpt = NSToJavaPoint(env, pt);
386
387
JNF_COCOA_EXIT(env);
388
389
return jpt;
390
}
391
392