Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/CTrayIcon.m
41152 views
1
/*
2
* Copyright (c) 2011, 2021, 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 "jni_util.h"
27
28
#import <AppKit/AppKit.h>
29
30
#import "CTrayIcon.h"
31
#import "ThreadUtilities.h"
32
#include "GeomUtilities.h"
33
#import "LWCToolkit.h"
34
35
#define kImageInset 4.0
36
37
/**
38
* If the image of the specified size won't fit into the status bar,
39
* then scale it down proprtionally. Otherwise, leave it as is.
40
*/
41
static NSSize ScaledImageSizeForStatusBar(NSSize imageSize, BOOL autosize) {
42
NSRect imageRect = NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height);
43
44
// There is a black line at the bottom of the status bar
45
// that we don't want to cover with image pixels.
46
CGFloat desiredSize = [[NSStatusBar systemStatusBar] thickness] - 1.0;
47
if (autosize) {
48
imageRect.size.width = desiredSize;
49
imageRect.size.height = desiredSize;
50
} else {
51
CGFloat scaleFactor = MIN(1.0, desiredSize/imageSize.height);
52
imageRect.size.width *= scaleFactor;
53
imageRect.size.height *= scaleFactor;
54
}
55
imageRect = NSIntegralRect(imageRect);
56
return imageRect.size;
57
}
58
59
@implementation AWTTrayIcon
60
61
- (id) initWithPeer:(jobject)thePeer {
62
if (!(self = [super init])) return nil;
63
64
peer = thePeer;
65
66
theItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
67
[theItem retain];
68
69
view = [[AWTTrayIconView alloc] initWithTrayIcon:self];
70
[theItem setView:view];
71
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
72
73
return self;
74
}
75
76
-(void) dealloc {
77
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:nil];
78
79
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
80
(*env)->DeleteGlobalRef(env, peer);
81
82
[[NSStatusBar systemStatusBar] removeStatusItem: theItem];
83
84
// Its a bad idea to force the item to release our view by setting
85
// the item's view to nil: it can lead to a crash in some scenarios.
86
// The item will release the view later on, so just set the view's image
87
// and tray icon to nil since we are done with it.
88
[view setImage: nil];
89
[view setTrayIcon: nil];
90
[view release];
91
92
[theItem release];
93
94
[super dealloc];
95
}
96
97
- (void) setTooltip:(NSString *) tooltip{
98
[view setToolTip:tooltip];
99
}
100
101
-(NSStatusItem *) theItem{
102
return theItem;
103
}
104
105
- (jobject) peer{
106
return peer;
107
}
108
109
- (void) setImage:(NSImage *) imagePtr sizing:(BOOL)autosize template:(BOOL)isTemplate {
110
NSSize imageSize = [imagePtr size];
111
NSSize scaledSize = ScaledImageSizeForStatusBar(imageSize, autosize);
112
if (imageSize.width != scaledSize.width ||
113
imageSize.height != scaledSize.height) {
114
[imagePtr setSize: scaledSize];
115
}
116
117
CGFloat itemLength = scaledSize.width + 2.0*kImageInset;
118
[theItem setLength:itemLength];
119
120
[imagePtr setTemplate: isTemplate];
121
[view setImage:imagePtr];
122
}
123
124
- (NSPoint) getLocationOnScreen {
125
return [[view window] convertBaseToScreen: NSZeroPoint];
126
}
127
128
-(void) deliverJavaMouseEvent: (NSEvent *) event {
129
[AWTToolkit eventCountPlusPlus];
130
131
JNIEnv *env = [ThreadUtilities getJNIEnv];
132
133
NSPoint eventLocation = [event locationInWindow];
134
NSPoint localPoint = [view convertPoint: eventLocation fromView: nil];
135
localPoint.y = [view bounds].size.height - localPoint.y;
136
137
NSPoint absP = [NSEvent mouseLocation];
138
NSEventType type = [event type];
139
140
absP = ConvertNSScreenPoint(NULL, absP);
141
jint clickCount;
142
143
clickCount = [event clickCount];
144
145
jdouble deltaX = [event deltaX];
146
jdouble deltaY = [event deltaY];
147
if ([AWTToolkit hasPreciseScrollingDeltas: event]) {
148
deltaX = [event scrollingDeltaX] * 0.1;
149
deltaY = [event scrollingDeltaY] * 0.1;
150
}
151
152
DECLARE_CLASS(jc_NSEvent, "sun/lwawt/macosx/NSEvent");
153
DECLARE_METHOD(jctor_NSEvent, jc_NSEvent, "<init>", "(IIIIIIIIDDI)V");
154
jobject jEvent = (*env)->NewObject(env, jc_NSEvent, jctor_NSEvent,
155
[event type],
156
[event modifierFlags],
157
clickCount,
158
[event buttonNumber],
159
(jint)localPoint.x, (jint)localPoint.y,
160
(jint)absP.x, (jint)absP.y,
161
deltaY,
162
deltaX,
163
[AWTToolkit scrollStateWithEvent: event]);
164
CHECK_NULL(jEvent);
165
166
DECLARE_CLASS(jc_TrayIcon, "sun/lwawt/macosx/CTrayIcon");
167
DECLARE_METHOD(jm_handleMouseEvent, jc_TrayIcon, "handleMouseEvent", "(Lsun/lwawt/macosx/NSEvent;)V");
168
(*env)->CallVoidMethod(env, peer, jm_handleMouseEvent, jEvent);
169
CHECK_EXCEPTION();
170
(*env)->DeleteLocalRef(env, jEvent);
171
}
172
173
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
174
shouldPresentNotification:(NSUserNotification *)notification
175
{
176
return YES; // We always show notifications to the user
177
}
178
179
@end //AWTTrayIcon
180
//================================================
181
182
@implementation AWTTrayIconView
183
184
-(id)initWithTrayIcon:(AWTTrayIcon *)theTrayIcon {
185
self = [super initWithFrame:NSMakeRect(0, 0, 1, 1)];
186
187
[self setTrayIcon: theTrayIcon];
188
[self setImage: nil];
189
isHighlighted = NO;
190
trackingArea = nil;
191
192
[self addTrackingArea];
193
194
return self;
195
}
196
197
- (void)addTrackingArea {
198
NSTrackingAreaOptions options = NSTrackingMouseMoved |
199
NSTrackingInVisibleRect |
200
NSTrackingActiveAlways;
201
trackingArea = [[NSTrackingArea alloc] initWithRect: CGRectZero
202
options: options
203
owner: self
204
userInfo: nil];
205
[self addTrackingArea:trackingArea];
206
}
207
208
-(void) dealloc {
209
[trackingArea release];
210
[super dealloc];
211
}
212
213
- (void)setHighlighted:(BOOL)aFlag
214
{
215
if (isHighlighted != aFlag) {
216
isHighlighted = aFlag;
217
[self setNeedsDisplay:YES];
218
}
219
}
220
221
-(void)setTrayIcon:(AWTTrayIcon*)theTrayIcon {
222
trayIcon = theTrayIcon;
223
}
224
225
- (void)menuWillOpen:(NSMenu *)menu
226
{
227
[self setHighlighted:YES];
228
}
229
230
- (void)menuDidClose:(NSMenu *)menu
231
{
232
[menu setDelegate:nil];
233
[self setHighlighted:NO];
234
}
235
236
- (void)drawRect:(NSRect)dirtyRect
237
{
238
if (self.image == nil) {
239
return;
240
}
241
242
NSRect bounds = [self bounds];
243
[trayIcon.theItem drawStatusBarBackgroundInRect:bounds
244
withHighlight:isHighlighted];
245
246
[super drawRect: dirtyRect];
247
}
248
249
- (void)mouseDown:(NSEvent *)event {
250
[trayIcon deliverJavaMouseEvent: event];
251
252
// don't show the menu on ctrl+click: it triggers ACTION event, like right click
253
if (([event modifierFlags] & NSControlKeyMask) == 0) {
254
//find CTrayIcon.getPopupMenuModel method and call it to get popup menu ptr.
255
JNIEnv *env = [ThreadUtilities getJNIEnv];
256
DECLARE_CLASS(jc_CTrayIcon, "sun/lwawt/macosx/CTrayIcon");
257
DECLARE_METHOD(jm_getPopupMenuModel, jc_CTrayIcon, "getPopupMenuModel", "()J");
258
jlong res = (*env)->CallLongMethod(env, trayIcon.peer, jm_getPopupMenuModel);
259
CHECK_EXCEPTION();
260
261
if (res != 0) {
262
CPopupMenu *cmenu = jlong_to_ptr(res);
263
NSMenu* menu = [cmenu menu];
264
[menu setDelegate:self];
265
[trayIcon.theItem popUpStatusItemMenu:menu];
266
[self setNeedsDisplay:YES];
267
}
268
}
269
}
270
271
- (void) mouseUp:(NSEvent *)event {
272
[trayIcon deliverJavaMouseEvent: event];
273
}
274
275
- (void) mouseDragged:(NSEvent *)event {
276
[trayIcon deliverJavaMouseEvent: event];
277
}
278
279
- (void) mouseMoved: (NSEvent *)event {
280
[trayIcon deliverJavaMouseEvent: event];
281
}
282
283
- (void) rightMouseDown:(NSEvent *)event {
284
[trayIcon deliverJavaMouseEvent: event];
285
}
286
287
- (void) rightMouseUp:(NSEvent *)event {
288
[trayIcon deliverJavaMouseEvent: event];
289
}
290
291
- (void) rightMouseDragged:(NSEvent *)event {
292
[trayIcon deliverJavaMouseEvent: event];
293
}
294
295
- (void) otherMouseDown:(NSEvent *)event {
296
[trayIcon deliverJavaMouseEvent: event];
297
}
298
299
- (void) otherMouseUp:(NSEvent *)event {
300
[trayIcon deliverJavaMouseEvent: event];
301
}
302
303
- (void) otherMouseDragged:(NSEvent *)event {
304
[trayIcon deliverJavaMouseEvent: event];
305
}
306
307
308
@end //AWTTrayIconView
309
//================================================
310
311
/*
312
* Class: sun_lwawt_macosx_CTrayIcon
313
* Method: nativeCreate
314
* Signature: ()J
315
*/
316
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CTrayIcon_nativeCreate
317
(JNIEnv *env, jobject peer) {
318
__block AWTTrayIcon *trayIcon = nil;
319
320
JNI_COCOA_ENTER(env);
321
322
jobject thePeer = (*env)->NewGlobalRef(env, peer);
323
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
324
trayIcon = [[AWTTrayIcon alloc] initWithPeer:thePeer];
325
}];
326
327
JNI_COCOA_EXIT(env);
328
329
return ptr_to_jlong(trayIcon);
330
}
331
332
333
/*
334
* Class: java_awt_TrayIcon
335
* Method: initIDs
336
* Signature: ()V
337
*/
338
JNIEXPORT void JNICALL Java_java_awt_TrayIcon_initIDs
339
(JNIEnv *env, jclass cls) {
340
//Do nothing.
341
}
342
343
/*
344
* Class: sun_lwawt_macosx_CTrayIcon
345
* Method: nativeSetToolTip
346
* Signature: (JLjava/lang/String;)V
347
*/
348
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTrayIcon_nativeSetToolTip
349
(JNIEnv *env, jobject self, jlong model, jstring jtooltip) {
350
JNI_COCOA_ENTER(env);
351
352
AWTTrayIcon *icon = jlong_to_ptr(model);
353
NSString *tooltip = JavaStringToNSString(env, jtooltip);
354
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
355
[icon setTooltip:tooltip];
356
}];
357
358
JNI_COCOA_EXIT(env);
359
}
360
361
/*
362
* Class: sun_lwawt_macosx_CTrayIcon
363
* Method: setNativeImage
364
* Signature: (JJZZ)V
365
*/
366
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTrayIcon_setNativeImage
367
(JNIEnv *env, jobject self, jlong model, jlong imagePtr, jboolean autosize, jboolean isTemplate) {
368
JNI_COCOA_ENTER(env);
369
370
AWTTrayIcon *icon = jlong_to_ptr(model);
371
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
372
[icon setImage:jlong_to_ptr(imagePtr) sizing:autosize template:isTemplate];
373
}];
374
375
JNI_COCOA_EXIT(env);
376
}
377
378
JNIEXPORT jobject JNICALL
379
Java_sun_lwawt_macosx_CTrayIcon_nativeGetIconLocation
380
(JNIEnv *env, jobject self, jlong model) {
381
jobject jpt = NULL;
382
383
JNI_COCOA_ENTER(env);
384
385
__block NSPoint pt = NSZeroPoint;
386
AWTTrayIcon *icon = jlong_to_ptr(model);
387
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
388
NSPoint loc = [icon getLocationOnScreen];
389
pt = ConvertNSScreenPoint(env, loc);
390
}];
391
392
jpt = NSToJavaPoint(env, pt);
393
394
JNI_COCOA_EXIT(env);
395
396
return jpt;
397
}
398
399
JNIEXPORT void JNICALL
400
Java_sun_lwawt_macosx_CTrayIcon_nativeShowNotification
401
(JNIEnv *env, jobject self, jlong model, jobject jcaption, jobject jtext,
402
long nsimage) {
403
JNI_COCOA_ENTER(env);
404
405
AWTTrayIcon *icon = jlong_to_ptr(model);
406
NSString *caption = JavaStringToNSString(env, jcaption);
407
NSString *text = JavaStringToNSString(env, jtext);
408
NSImage * contentImage = jlong_to_ptr(nsimage);
409
410
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
411
NSUserNotification *notification = [[NSUserNotification alloc] init];
412
notification.title = caption;
413
notification.informativeText = text;
414
notification.contentImage = contentImage;
415
notification.soundName = NSUserNotificationDefaultSoundName;
416
417
[[NSUserNotificationCenter defaultUserNotificationCenter]
418
deliverNotification:notification];
419
}];
420
421
JNI_COCOA_EXIT(env);
422
}
423
424