/
tray_icon_cocoa.mm
537 lines (449 loc) · 16.4 KB
/
tray_icon_cocoa.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/ui/tray_icon_cocoa.h"
#include "atom/browser/mac/atom_application.h"
#include "atom/browser/ui/cocoa/NSString+ANSI.h"
#include "atom/browser/ui/cocoa/atom_menu_controller.h"
#include "base/mac/sdk_forward_declarations.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task/post_task.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "ui/display/screen.h"
#include "ui/events/cocoa/cocoa_event_utils.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/mac/coordinate_conversion.h"
namespace {
// By default, macOS sets 4px to tray image as left and right padding margin.
const CGFloat kHorizontalMargin = 4;
// macOS tends to make the title 2px lower.
const CGFloat kVerticalTitleMargin = 2;
} // namespace
@interface StatusItemView : NSView {
atom::TrayIconCocoa* trayIcon_; // weak
AtomMenuController* menuController_; // weak
atom::TrayIcon::HighlightMode highlight_mode_;
BOOL ignoreDoubleClickEvents_;
BOOL forceHighlight_;
BOOL inMouseEventSequence_;
BOOL ANSI_;
base::scoped_nsobject<NSImage> image_;
base::scoped_nsobject<NSImage> alternateImage_;
base::scoped_nsobject<NSString> title_;
base::scoped_nsobject<NSMutableAttributedString> attributedTitle_;
base::scoped_nsobject<NSStatusItem> statusItem_;
base::scoped_nsobject<NSTrackingArea> trackingArea_;
}
@end // @interface StatusItemView
@implementation StatusItemView
- (void)dealloc {
trayIcon_ = nil;
menuController_ = nil;
[super dealloc];
}
- (id)initWithIcon:(atom::TrayIconCocoa*)icon {
trayIcon_ = icon;
menuController_ = nil;
highlight_mode_ = atom::TrayIcon::HighlightMode::SELECTION;
ignoreDoubleClickEvents_ = NO;
forceHighlight_ = NO;
inMouseEventSequence_ = NO;
if ((self = [super initWithFrame:CGRectZero])) {
[self registerForDraggedTypes:@[
NSFilenamesPboardType,
NSStringPboardType,
]];
// Create the status item.
NSStatusItem* item = [[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength];
statusItem_.reset([item retain]);
[statusItem_ setView:self];
// Finalize setup by sizing our views
[self updateDimensions];
// Add NSTrackingArea for listening to mouseEnter, mouseExit, and mouseMove
// events
trackingArea_.reset([[NSTrackingArea alloc]
initWithRect:[self bounds]
options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved |
NSTrackingActiveAlways
owner:self
userInfo:nil]);
[self addTrackingArea:trackingArea_];
}
return self;
}
- (void)updateDimensions {
NSStatusBar* bar = [NSStatusBar systemStatusBar];
[self setFrame:NSMakeRect(0, 0, [self fullWidth], [bar thickness])];
[self setNeedsDisplay:YES];
}
- (void)removeItem {
// Turn off tracking events to prevent crash
if (trackingArea_) {
[self removeTrackingArea:trackingArea_];
trackingArea_.reset();
}
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem_];
[statusItem_ setView:nil];
statusItem_.reset();
}
- (void)drawRect:(NSRect)dirtyRect {
// Draw the tray icon and title that align with NSStatusItem, layout:
// ----------------
// | icon | title |
/// ----------------
CGFloat thickness = [[statusItem_ statusBar] thickness];
// Draw the system bar background.
[statusItem_ drawStatusBarBackgroundInRect:self.bounds
withHighlight:[self shouldHighlight]];
// Determine which image to use.
NSImage* image = image_.get();
if (inMouseEventSequence_ && alternateImage_) {
image = alternateImage_.get();
}
// Apply the higlight color if the image is a template image. When this moves
// to using the new [NSStatusItem button] API, this should work automagically.
if ([image isTemplate] == YES) {
NSImage* imageWithColor = [[image copy] autorelease];
[imageWithColor lockFocus];
[[self colorWithHighlight:[self isHighlighted]] set];
CGRect imageBounds = CGRectMake(0, 0, image.size.width, image.size.height);
NSRectFillUsingOperation(imageBounds, NSCompositeSourceAtop);
[imageWithColor unlockFocus];
image = imageWithColor;
}
// Draw the image
[image
drawInRect:CGRectMake(roundf(([self iconWidth] - image.size.width) / 2),
roundf((thickness - image.size.height) / 2),
image.size.width, image.size.height)];
if (title_) {
// Draw title.
NSRect titleDrawRect = NSMakeRect([self iconWidth], -kVerticalTitleMargin,
[self titleWidth], thickness);
[attributedTitle_ drawInRect:titleDrawRect];
}
}
- (BOOL)isDarkMode {
if (@available(macOS 10.14, *)) {
return [[NSApplication sharedApplication].effectiveAppearance.name
isEqualToString:NSAppearanceNameDarkAqua];
}
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* mode = [defaults stringForKey:@"AppleInterfaceStyle"];
return mode && [mode isEqualToString:@"Dark"];
}
- (BOOL)isHighlighted {
BOOL highlight = [self shouldHighlight];
return highlight | [self isDarkMode];
}
// The width of the full status item.
- (CGFloat)fullWidth {
if (title_)
return [self iconWidth] + [self titleWidth] + kHorizontalMargin;
else
return [self iconWidth];
}
// The width of the icon.
- (CGFloat)iconWidth {
if (!image_ && title_)
return kHorizontalMargin;
CGFloat thickness = [[NSStatusBar systemStatusBar] thickness];
CGFloat imageHeight = [image_ size].height;
CGFloat imageWidth = [image_ size].width;
CGFloat iconWidth = imageWidth;
if (imageWidth < thickness) {
// Image's width must be larger than menu bar's height.
iconWidth = thickness;
} else {
CGFloat verticalMargin = thickness - imageHeight;
// Image must have same horizontal vertical margin.
if (verticalMargin > 0 && imageWidth != imageHeight)
iconWidth = imageWidth + verticalMargin;
CGFloat horizontalMargin = thickness - imageWidth;
// Image must have at least kHorizontalMargin horizontal margin on each
// side.
if (horizontalMargin < 2 * kHorizontalMargin)
iconWidth = imageWidth + 2 * kHorizontalMargin;
}
return iconWidth;
}
// The width of the title.
- (CGFloat)titleWidth {
if (!title_)
return 0;
return [attributedTitle_ size].width;
}
- (NSColor*)colorWithHighlight:(BOOL)highlight {
return highlight ? [NSColor whiteColor]
: [NSColor colorWithRed:0.265625
green:0.25390625
blue:0.234375
alpha:1.0];
}
- (void)setImage:(NSImage*)image {
image_.reset([image copy]);
[self updateDimensions];
}
- (void)setAlternateImage:(NSImage*)image {
alternateImage_.reset([image copy]);
}
- (void)setHighlight:(atom::TrayIcon::HighlightMode)mode {
highlight_mode_ = mode;
[self setNeedsDisplay:YES];
}
- (void)setIgnoreDoubleClickEvents:(BOOL)ignore {
ignoreDoubleClickEvents_ = ignore;
}
- (BOOL)getIgnoreDoubleClickEvents {
return ignoreDoubleClickEvents_;
}
- (void)setTitle:(NSString*)title {
if (title.length > 0) {
title_.reset([title copy]);
ANSI_ = [title containsANSICodes];
} else {
title_.reset();
ANSI_ = NO;
}
[self updateAttributedTitle];
[self updateDimensions];
}
- (void)updateAttributedTitle {
NSDictionary* attributes =
@{NSFontAttributeName : [NSFont menuBarFontOfSize:0]};
if (ANSI_) {
NSCharacterSet* whites = [NSCharacterSet whitespaceCharacterSet];
NSString* title = [title_ stringByTrimmingCharactersInSet:whites];
attributedTitle_.reset([title attributedStringParsingANSICodes]);
[attributedTitle_ addAttributes:attributes
range:NSMakeRange(0, [attributedTitle_ length])];
return;
}
// check title_ being nil
NSString* title = @"";
if (title_)
title = title_;
attributedTitle_.reset([[NSMutableAttributedString alloc]
initWithString:title
attributes:attributes]);
// NSFontAttributeName:[NSFont menuBarFontOfSize:0],
// NSForegroundColorAttributeName:[self colorWithHighlight: highlight]
[attributedTitle_ addAttributes:attributes
range:NSMakeRange(0, [attributedTitle_ length])];
[attributedTitle_ addAttribute:NSForegroundColorAttributeName
value:[self colorWithHighlight:[self isHighlighted]]
range:NSMakeRange(0, [attributedTitle_ length])];
}
- (void)setMenuController:(AtomMenuController*)menu {
menuController_ = menu;
}
- (void)mouseDown:(NSEvent*)event {
inMouseEventSequence_ = YES;
[self setNeedsDisplay:YES];
}
- (void)mouseUp:(NSEvent*)event {
if (!inMouseEventSequence_) {
// If the menu is showing, when user clicked the tray icon, the `mouseDown`
// event will be dissmissed, we need to close the menu at this time.
[self setNeedsDisplay:YES];
return;
}
inMouseEventSequence_ = NO;
// Show menu when there is a context menu.
// NB(hokein): Make tray's behavior more like official one's.
// When the tray icon gets clicked quickly multiple times, the
// event.clickCount doesn't always return 1. Instead, it returns a value that
// counts the clicked times.
// So we don't check the clickCount here, just pop up the menu for each click
// event.
if (menuController_)
[statusItem_ popUpStatusItemMenu:[menuController_ menu]];
// Don't emit click events when menu is showing.
if (menuController_)
return;
// If we are ignoring double click events, we should ignore the `clickCount`
// value and immediately emit a click event.
BOOL shouldBeHandledAsASingleClick =
(event.clickCount == 1) || ignoreDoubleClickEvents_;
if (shouldBeHandledAsASingleClick)
trayIcon_->NotifyClicked(
gfx::ScreenRectFromNSRect(event.window.frame),
gfx::ScreenPointFromNSPoint([event locationInWindow]),
ui::EventFlagsFromModifiers([event modifierFlags]));
// Double click event.
BOOL shouldBeHandledAsADoubleClick =
(event.clickCount == 2) && !ignoreDoubleClickEvents_;
if (shouldBeHandledAsADoubleClick)
trayIcon_->NotifyDoubleClicked(
gfx::ScreenRectFromNSRect(event.window.frame),
ui::EventFlagsFromModifiers([event modifierFlags]));
[self setNeedsDisplay:YES];
}
- (void)popUpContextMenu:(atom::AtomMenuModel*)menu_model {
// Make sure events can be pumped while the menu is up.
base::MessageLoopCurrent::ScopedNestableTaskAllower allow;
// Show a custom menu.
if (menu_model) {
base::scoped_nsobject<AtomMenuController> menuController(
[[AtomMenuController alloc] initWithModel:menu_model
useDefaultAccelerator:NO]);
forceHighlight_ = YES; // Should highlight when showing menu.
[self setNeedsDisplay:YES];
[statusItem_ popUpStatusItemMenu:[menuController menu]];
forceHighlight_ = NO;
[self setNeedsDisplay:YES];
return;
}
if (menuController_ && ![menuController_ isMenuOpen]) {
// Ensure the UI can update while the menu is fading out.
base::ScopedPumpMessagesInPrivateModes pump_private;
// Redraw the tray icon to show highlight if it is enabled.
[self setNeedsDisplay:YES];
[statusItem_ popUpStatusItemMenu:[menuController_ menu]];
// The popUpStatusItemMenu returns only after the showing menu is closed.
// When it returns, we need to redraw the tray icon to not show highlight.
[self setNeedsDisplay:YES];
}
}
- (void)rightMouseUp:(NSEvent*)event {
trayIcon_->NotifyRightClicked(
gfx::ScreenRectFromNSRect(event.window.frame),
ui::EventFlagsFromModifiers([event modifierFlags]));
}
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
trayIcon_->NotifyDragEntered();
return NSDragOperationCopy;
}
- (void)mouseExited:(NSEvent*)event {
trayIcon_->NotifyMouseExited(
gfx::ScreenPointFromNSPoint([event locationInWindow]),
ui::EventFlagsFromModifiers([event modifierFlags]));
}
- (void)mouseEntered:(NSEvent*)event {
trayIcon_->NotifyMouseEntered(
gfx::ScreenPointFromNSPoint([event locationInWindow]),
ui::EventFlagsFromModifiers([event modifierFlags]));
}
- (void)mouseMoved:(NSEvent*)event {
trayIcon_->NotifyMouseMoved(
gfx::ScreenPointFromNSPoint([event locationInWindow]),
ui::EventFlagsFromModifiers([event modifierFlags]));
}
- (void)draggingExited:(id<NSDraggingInfo>)sender {
trayIcon_->NotifyDragExited();
}
- (void)draggingEnded:(id<NSDraggingInfo>)sender {
trayIcon_->NotifyDragEnded();
if (NSPointInRect([sender draggingLocation], self.frame)) {
trayIcon_->NotifyDrop();
}
}
- (BOOL)handleDrop:(id<NSDraggingInfo>)sender {
NSPasteboard* pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType]) {
std::vector<std::string> dropFiles;
NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
for (NSString* file in files)
dropFiles.push_back(base::SysNSStringToUTF8(file));
trayIcon_->NotifyDropFiles(dropFiles);
return YES;
} else if ([[pboard types] containsObject:NSStringPboardType]) {
NSString* dropText = [pboard stringForType:NSStringPboardType];
trayIcon_->NotifyDropText(base::SysNSStringToUTF8(dropText));
return YES;
}
return NO;
}
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
return YES;
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
[self handleDrop:sender];
return YES;
}
- (void)setNeedsDisplay:(BOOL)display {
[self updateAttributedTitle];
[super setNeedsDisplay:display];
}
- (BOOL)shouldHighlight {
switch (highlight_mode_) {
case atom::TrayIcon::HighlightMode::ALWAYS:
return true;
case atom::TrayIcon::HighlightMode::NEVER:
return false;
case atom::TrayIcon::HighlightMode::SELECTION:
BOOL isMenuOpen = menuController_ && [menuController_ isMenuOpen];
return forceHighlight_ || inMouseEventSequence_ || isMenuOpen;
}
}
@end
namespace atom {
TrayIconCocoa::TrayIconCocoa() : weak_factory_(this) {
status_item_view_.reset([[StatusItemView alloc] initWithIcon:this]);
}
TrayIconCocoa::~TrayIconCocoa() {
[status_item_view_ removeItem];
if (menu_model_)
menu_model_->RemoveObserver(this);
}
void TrayIconCocoa::SetImage(const gfx::Image& image) {
[status_item_view_ setImage:image.IsEmpty() ? nil : image.AsNSImage()];
}
void TrayIconCocoa::SetPressedImage(const gfx::Image& image) {
[status_item_view_ setAlternateImage:image.AsNSImage()];
}
void TrayIconCocoa::SetToolTip(const std::string& tool_tip) {
[status_item_view_ setToolTip:base::SysUTF8ToNSString(tool_tip)];
}
void TrayIconCocoa::SetTitle(const std::string& title) {
[status_item_view_ setTitle:base::SysUTF8ToNSString(title)];
}
void TrayIconCocoa::SetHighlightMode(TrayIcon::HighlightMode mode) {
[status_item_view_ setHighlight:mode];
}
void TrayIconCocoa::SetIgnoreDoubleClickEvents(bool ignore) {
[status_item_view_ setIgnoreDoubleClickEvents:ignore];
}
bool TrayIconCocoa::GetIgnoreDoubleClickEvents() {
return [status_item_view_ getIgnoreDoubleClickEvents];
}
void TrayIconCocoa::PopUpOnUI(AtomMenuModel* menu_model) {
[status_item_view_ popUpContextMenu:menu_model];
}
void TrayIconCocoa::PopUpContextMenu(const gfx::Point& pos,
AtomMenuModel* menu_model) {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&TrayIconCocoa::PopUpOnUI, weak_factory_.GetWeakPtr(),
base::Unretained(menu_model)));
}
void TrayIconCocoa::SetContextMenu(AtomMenuModel* menu_model) {
// Substribe to MenuClosed event.
if (menu_model_)
menu_model_->RemoveObserver(this);
menu_model_ = menu_model;
if (menu_model) {
menu_model->AddObserver(this);
// Create native menu.
menu_.reset([[AtomMenuController alloc] initWithModel:menu_model
useDefaultAccelerator:NO]);
} else {
menu_.reset();
}
[status_item_view_ setMenuController:menu_.get()];
}
gfx::Rect TrayIconCocoa::GetBounds() {
auto bounds = gfx::ScreenRectFromNSRect([status_item_view_ window].frame);
return bounds;
}
void TrayIconCocoa::OnMenuWillClose() {
[status_item_view_ setNeedsDisplay:YES];
}
// static
TrayIcon* TrayIcon::Create() {
return new TrayIconCocoa;
}
} // namespace atom