Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcocoasystemtrayicon.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2012 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Christoph Schleifenbaum <christoph.schleifenbaum@kdab.com>
3// Copyright (c) 2007-2008, Apple, Inc.
4// SPDX-License-Identifier: BSD-3-Clause
5// Qt-Security score:significant reason:default
6
7#include <AppKit/AppKit.h>
8
10
11#ifndef QT_NO_SYSTEMTRAYICON
12
13#include <qtemporaryfile.h>
14#include <qimagewriter.h>
15#include <qdebug.h>
16
17#include <QtCore/private/qcore_mac_p.h>
18
19#include "qcocoamenu.h"
20#include "qcocoansmenu.h"
21
22#include "qcocoahelpers.h"
24#include "qcocoascreen.h"
25#include <QtGui/private/qcoregraphics_p.h>
26
27// NSUserNotification was deprecated in macOS 11.
28// We should be using UserNotifications.framework instead.
29// See QTBUG-110998 for more information.
30#define NSUserNotificationCenter QT_IGNORE_DEPRECATIONS(NSUserNotificationCenter)
31#define NSUserNotification QT_IGNORE_DEPRECATIONS(NSUserNotification)
32
34
35void QCocoaSystemTrayIcon::init()
36{
37 m_statusItem = [[NSStatusBar.systemStatusBar statusItemWithLength:NSSquareStatusItemLength] retain];
38
39 m_delegate = [[QStatusItemDelegate alloc] initWithSysTray:this];
40
41 // In case the status item does not have a menu assigned to it
42 // we fall back to the item's button to detect activation.
43 m_statusItem.button.target = m_delegate;
44 m_statusItem.button.action = @selector(statusItemClicked);
45 [m_statusItem.button sendActionOn:NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskOtherMouseDown];
46}
47
48void QCocoaSystemTrayIcon::cleanup()
49{
50 NSUserNotificationCenter *center = NSUserNotificationCenter.defaultUserNotificationCenter;
51 if (center.delegate == m_delegate)
52 center.delegate = nil;
53
54 [NSStatusBar.systemStatusBar removeStatusItem:m_statusItem];
55 [m_statusItem release];
56 m_statusItem = nil;
57
58 [m_delegate release];
59 m_delegate = nil;
60}
61
62QRect QCocoaSystemTrayIcon::geometry() const
63{
64 if (!m_statusItem)
65 return QRect();
66
67 if (NSWindow *window = m_statusItem.button.window) {
68 if (QCocoaScreen *screen = QCocoaScreen::get(window.screen))
69 return screen->mapFromNative(window.frame).toRect();
70 }
71
72 return QRect();
73}
74
75static bool heightCompareFunction (QSize a, QSize b) { return (a.height() < b.height()); }
76static QList<QSize> sortByHeight(const QList<QSize> &sizes)
77{
78 QList<QSize> sorted = sizes;
79 std::sort(sorted.begin(), sorted.end(), heightCompareFunction);
80 return sorted;
81}
82
83void QCocoaSystemTrayIcon::updateIcon(const QIcon &icon)
84{
85 if (!m_statusItem)
86 return;
87
88 if (auto *image = [NSImage internalImageFromQIcon:icon]) {
89 // The icon is backed by QAppleIconEngine, in which case we
90 // want to pass on the underlying NSImage instead of flattening
91 // to a QImage. This preserves the isTemplate property of the
92 // image, and allows AppKit to size and configure the icon for
93 // the status bar. We also enable NSVariableStatusItemLength,
94 // to match the behavior of SwiftUI's MenuBarExtra.
95 m_statusItem.button.image = [[image copy] autorelease];
96 m_statusItem.button.imageScaling = NSImageScaleProportionallyDown;
97 m_statusItem.length = NSVariableStatusItemLength;
98 return;
99 }
100
101 // The recommended maximum title bar icon height is 18 points
102 // (device independent pixels). The menu height on past and
103 // current OS X versions is 22 points. Provide some future-proofing
104 // by deriving the icon height from the menu height.
105 const int padding = 4;
106 const int menuHeight = NSStatusBar.systemStatusBar.thickness;
107 const int maxImageHeight = menuHeight - padding;
108
109 // Select pixmap based on the device pixel height. Ideally we would use
110 // the devicePixelRatio of the target screen, but that value is not
111 // known until draw time. Use qApp->devicePixelRatio, which returns the
112 // devicePixelRatio for the "best" screen on the system.
113 qreal devicePixelRatio = qApp->devicePixelRatio();
114 const int maxPixmapHeight = maxImageHeight * devicePixelRatio;
115 QSize selectedSize;
116 for (const QSize& size : sortByHeight(icon.availableSizes())) {
117 // Select a pixmap based on the height. We want the largest pixmap
118 // with a height smaller or equal to maxPixmapHeight. The pixmap
119 // may rectangular; assume it has a reasonable size. If there is
120 // not suitable pixmap use the smallest one the icon can provide.
121 if (size.height() <= maxPixmapHeight) {
122 selectedSize = size;
123 } else {
124 if (!selectedSize.isValid())
125 selectedSize = size;
126 break;
127 }
128 }
129
130 // Handle SVG icons, which do not return anything for availableSizes().
131 if (!selectedSize.isValid())
132 selectedSize = icon.actualSize(QSize(maxPixmapHeight, maxPixmapHeight));
133
134 QPixmap pixmap = icon.pixmap(selectedSize);
135
136 // Draw a low-resolution icon if there is not enough pixels for a retina
137 // icon. This prevents showing a small icon on retina displays.
138 if (devicePixelRatio > 1.0 && selectedSize.height() < maxPixmapHeight / 2)
139 devicePixelRatio = 1.0;
140
141 // Scale large pixmaps to fit the available menu bar area.
142 if (pixmap.height() > maxPixmapHeight)
143 pixmap = pixmap.scaledToHeight(maxPixmapHeight, Qt::SmoothTransformation);
144
145 // The icon will be stretched over the full height of the menu bar
146 // therefore we create a second pixmap which has the full height
147 QSize fullHeightSize(!pixmap.isNull() ? pixmap.width():
148 menuHeight * devicePixelRatio,
149 menuHeight * devicePixelRatio);
150 QPixmap fullHeightPixmap(fullHeightSize);
151 fullHeightPixmap.fill(Qt::transparent);
152 if (!pixmap.isNull()) {
153 QPainter p(&fullHeightPixmap);
154 QRect r = pixmap.rect();
155 r.moveCenter(fullHeightPixmap.rect().center());
156 p.drawPixmap(r, pixmap);
157 }
158 fullHeightPixmap.setDevicePixelRatio(devicePixelRatio);
159
160 auto *nsimage = [NSImage imageFromQImage:fullHeightPixmap.toImage()];
161 [nsimage setTemplate:icon.isMask()];
162 m_statusItem.button.image = nsimage;
163 m_statusItem.button.imageScaling = NSImageScaleProportionallyDown;
164}
165
166void QCocoaSystemTrayIcon::updateMenu(QPlatformMenu *menu)
167{
168 auto *nsMenu = menu ? static_cast<QCocoaMenu *>(menu)->nsMenu() : nil;
169 if (m_statusItem.menu == nsMenu)
170 return;
171
172 if (m_statusItem.menu) {
173 [NSNotificationCenter.defaultCenter removeObserver:m_delegate
174 name:NSMenuDidBeginTrackingNotification
175 object:m_statusItem.menu
176 ];
177 }
178
179 m_statusItem.menu = nsMenu;
180
181 if (m_statusItem.menu) {
182 // When a menu is assigned, NSStatusBarButtonCell will intercept the mouse
183 // down to pop up the menu, and we never see the NSStatusBarButton action.
184 // To ensure we emit the 'activated' signal in both cases we detect when
185 // menu starts tracking, which happens before the menu delegate is sent
186 // the menuWillOpen callback we use to emit aboutToShow for the menu.
187 [NSNotificationCenter.defaultCenter addObserver:m_delegate
188 selector:@selector(statusItemMenuBeganTracking:)
189 name:NSMenuDidBeginTrackingNotification
190 object:m_statusItem.menu
191 ];
192 }
193}
194
195void QCocoaSystemTrayIcon::updateToolTip(const QString &toolTip)
196{
197 if (!m_statusItem)
198 return;
199
200 m_statusItem.button.toolTip = toolTip.toNSString();
201}
202
203bool QCocoaSystemTrayIcon::isSystemTrayAvailable() const
204{
205 return true;
206}
207
208bool QCocoaSystemTrayIcon::supportsMessages() const
209{
210 return true;
211}
212
213void QCocoaSystemTrayIcon::showMessage(const QString &title, const QString &message,
214 const QIcon& icon, MessageIcon, int msecs)
215{
216 if (!m_statusItem)
217 return;
218
219 auto *notification = [[NSUserNotification alloc] init];
220 notification.title = title.toNSString();
221 notification.informativeText = message.toNSString();
222
223 // Request a size that looks good on the highest resolution screen available
224 // for icon engines that don't have an intrinsic size (like SVG).
225 auto image = icon.pixmap(QSize(64, 64), qGuiApp->devicePixelRatio()).toImage();
226
227 // The assigned image is scaled by the system to fit into the tile,
228 // but without taking aspect ratio into account, so let's pad the
229 // image up front if it's not already square.
230 image = qt_mac_padToSquareImage(image);
231
232 notification.contentImage = [NSImage imageFromQImage:image];
233
234 NSUserNotificationCenter *center = NSUserNotificationCenter.defaultUserNotificationCenter;
235 center.delegate = m_delegate;
236
237 [center deliverNotification:[notification autorelease]];
238
239 if (msecs) {
240 NSTimeInterval timeout = msecs / 1000.0;
241 [center performSelector:@selector(removeDeliveredNotification:) withObject:notification afterDelay:timeout];
242 }
243}
244
245void QCocoaSystemTrayIcon::emitActivated()
246{
247 auto *mouseEvent = NSApp.currentEvent;
248
249 auto activationReason = QPlatformSystemTrayIcon::Unknown;
250
251 if (mouseEvent.clickCount == 2) {
252 activationReason = QPlatformSystemTrayIcon::DoubleClick;
253 } else {
254 auto mouseButton = cocoaButton2QtButton(mouseEvent);
255 if (mouseButton == Qt::MiddleButton)
256 activationReason = QPlatformSystemTrayIcon::MiddleClick;
257 else if (mouseButton == Qt::RightButton)
258 activationReason = QPlatformSystemTrayIcon::Context;
259 else
260 activationReason = QPlatformSystemTrayIcon::Trigger;
261 }
262
263 emit activated(activationReason);
264}
265
266QT_END_NAMESPACE
267
268@implementation QStatusItemDelegate
269
270- (instancetype)initWithSysTray:(QCocoaSystemTrayIcon *)platformSystemTray
271{
272 if ((self = [super init]))
273 self.platformSystemTray = platformSystemTray;
274
275 return self;
276}
277
278- (void)dealloc
279{
280 self.platformSystemTray = nullptr;
281 [super dealloc];
282}
283
284- (void)statusItemClicked
285{
286 self.platformSystemTray->emitActivated();
287}
288
289- (void)statusItemMenuBeganTracking:(NSNotification*)notification
290{
291 self.platformSystemTray->emitActivated();
292}
293
294- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
295{
296 Q_UNUSED(center);
297 Q_UNUSED(notification);
298 return YES;
299}
300
301- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
302{
303 [center removeDeliveredNotification:notification];
304 emit self.platformSystemTray->messageClicked();
305}
306
307@end
308
309#endif // QT_NO_SYSTEMTRAYICON
Combined button and popup list for selecting options.
static bool heightCompareFunction(QSize a, QSize b)
#define NSUserNotificationCenter
static QList< QSize > sortByHeight(const QList< QSize > &sizes)
#define NSUserNotification