1/**************************************************************************** 2** 3** Copyright (C) 2016 The Qt Company Ltd. 4** Copyright (C) 2012 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Christoph Schleifenbaum <christoph.schleifenbaum@kdab.com> 5** Contact: https://www.qt.io/licensing/ 6** 7** This file is part of the plugins of the Qt Toolkit. 8** 9** $QT_BEGIN_LICENSE:LGPL$ 10** Commercial License Usage 11** Licensees holding valid commercial Qt licenses may use this file in 12** accordance with the commercial license agreement provided with the 13** Software or, alternatively, in accordance with the terms contained in 14** a written agreement between you and The Qt Company. For licensing terms 15** and conditions see https://www.qt.io/terms-conditions. For further 16** information use the contact form at https://www.qt.io/contact-us. 17** 18** GNU Lesser General Public License Usage 19** Alternatively, this file may be used under the terms of the GNU Lesser 20** General Public License version 3 as published by the Free Software 21** Foundation and appearing in the file LICENSE.LGPL3 included in the 22** packaging of this file. Please review the following information to 23** ensure the GNU Lesser General Public License version 3 requirements 24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 25** 26** GNU General Public License Usage 27** Alternatively, this file may be used under the terms of the GNU 28** General Public License version 2.0 or (at your option) the GNU General 29** Public license version 3 or any later version approved by the KDE Free 30** Qt Foundation. The licenses are as published by the Free Software 31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 32** included in the packaging of this file. Please review the following 33** information to ensure the GNU General Public License requirements will 34** be met: https://www.gnu.org/licenses/gpl-2.0.html and 35** https://www.gnu.org/licenses/gpl-3.0.html. 36** 37** $QT_END_LICENSE$ 38** 39****************************************************************************/ 40 41/**************************************************************************** 42** 43** Copyright (c) 2007-2008, Apple, Inc. 44** 45** All rights reserved. 46** 47** Redistribution and use in source and binary forms, with or without 48** modification, are permitted provided that the following conditions are met: 49** 50** * Redistributions of source code must retain the above copyright notice, 51** this list of conditions and the following disclaimer. 52** 53** * Redistributions in binary form must reproduce the above copyright notice, 54** this list of conditions and the following disclaimer in the documentation 55** and/or other materials provided with the distribution. 56** 57** * Neither the name of Apple, Inc. nor the names of its contributors 58** may be used to endorse or promote products derived from this software 59** without specific prior written permission. 60** 61** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 62** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 63** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 64** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 65** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 66** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 67** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 68** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 69** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 70** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 71** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 72** 73****************************************************************************/ 74 75#include "qcocoasystemtrayicon.h" 76 77#ifndef QT_NO_SYSTEMTRAYICON 78 79#include <qtemporaryfile.h> 80#include <qimagewriter.h> 81#include <qdebug.h> 82 83#include <QtCore/private/qcore_mac_p.h> 84 85#include "qcocoamenu.h" 86 87#include "qcocoahelpers.h" 88#include "qcocoaintegration.h" 89#include "qcocoascreen.h" 90#include <QtGui/private/qcoregraphics_p.h> 91 92#import <AppKit/AppKit.h> 93 94QT_BEGIN_NAMESPACE 95 96void QCocoaSystemTrayIcon::init() 97{ 98 m_statusItem = [[NSStatusBar.systemStatusBar statusItemWithLength:NSSquareStatusItemLength] retain]; 99 100 m_delegate = [[QStatusItemDelegate alloc] initWithSysTray:this]; 101 102 m_statusItem.button.target = m_delegate; 103 m_statusItem.button.action = @selector(statusItemClicked); 104 [m_statusItem.button sendActionOn:NSEventMaskLeftMouseUp | NSEventMaskRightMouseUp | NSEventMaskOtherMouseUp]; 105} 106 107void QCocoaSystemTrayIcon::cleanup() 108{ 109 NSUserNotificationCenter *center = NSUserNotificationCenter.defaultUserNotificationCenter; 110 if (center.delegate == m_delegate) 111 center.delegate = nil; 112 113 [NSStatusBar.systemStatusBar removeStatusItem:m_statusItem]; 114 [m_statusItem release]; 115 m_statusItem = nil; 116 117 [m_delegate release]; 118 m_delegate = nil; 119 120 m_menu = nullptr; 121} 122 123QRect QCocoaSystemTrayIcon::geometry() const 124{ 125 if (!m_statusItem) 126 return QRect(); 127 128 if (NSWindow *window = m_statusItem.button.window) { 129 if (QCocoaScreen *screen = QCocoaScreen::get(window.screen)) 130 return screen->mapFromNative(window.frame).toRect(); 131 } 132 133 return QRect(); 134} 135 136static bool heightCompareFunction (QSize a, QSize b) { return (a.height() < b.height()); } 137static QList<QSize> sortByHeight(const QList<QSize> &sizes) 138{ 139 QList<QSize> sorted = sizes; 140 std::sort(sorted.begin(), sorted.end(), heightCompareFunction); 141 return sorted; 142} 143 144void QCocoaSystemTrayIcon::updateIcon(const QIcon &icon) 145{ 146 if (!m_statusItem) 147 return; 148 149 // The recommended maximum title bar icon height is 18 points 150 // (device independent pixels). The menu height on past and 151 // current OS X versions is 22 points. Provide some future-proofing 152 // by deriving the icon height from the menu height. 153 const int padding = 4; 154 const int menuHeight = NSStatusBar.systemStatusBar.thickness; 155 const int maxImageHeight = menuHeight - padding; 156 157 // Select pixmap based on the device pixel height. Ideally we would use 158 // the devicePixelRatio of the target screen, but that value is not 159 // known until draw time. Use qApp->devicePixelRatio, which returns the 160 // devicePixelRatio for the "best" screen on the system. 161 qreal devicePixelRatio = qApp->devicePixelRatio(); 162 const int maxPixmapHeight = maxImageHeight * devicePixelRatio; 163 QSize selectedSize; 164 for (const QSize& size : sortByHeight(icon.availableSizes())) { 165 // Select a pixmap based on the height. We want the largest pixmap 166 // with a height smaller or equal to maxPixmapHeight. The pixmap 167 // may rectangular; assume it has a reasonable size. If there is 168 // not suitable pixmap use the smallest one the icon can provide. 169 if (size.height() <= maxPixmapHeight) { 170 selectedSize = size; 171 } else { 172 if (!selectedSize.isValid()) 173 selectedSize = size; 174 break; 175 } 176 } 177 178 // Handle SVG icons, which do not return anything for availableSizes(). 179 if (!selectedSize.isValid()) 180 selectedSize = icon.actualSize(QSize(maxPixmapHeight, maxPixmapHeight)); 181 182 QPixmap pixmap = icon.pixmap(selectedSize); 183 184 // Draw a low-resolution icon if there is not enough pixels for a retina 185 // icon. This prevents showing a small icon on retina displays. 186 if (devicePixelRatio > 1.0 && selectedSize.height() < maxPixmapHeight / 2) 187 devicePixelRatio = 1.0; 188 189 // Scale large pixmaps to fit the available menu bar area. 190 if (pixmap.height() > maxPixmapHeight) 191 pixmap = pixmap.scaledToHeight(maxPixmapHeight, Qt::SmoothTransformation); 192 193 // The icon will be stretched over the full height of the menu bar 194 // therefore we create a second pixmap which has the full height 195 QSize fullHeightSize(!pixmap.isNull() ? pixmap.width(): 196 menuHeight * devicePixelRatio, 197 menuHeight * devicePixelRatio); 198 QPixmap fullHeightPixmap(fullHeightSize); 199 fullHeightPixmap.fill(Qt::transparent); 200 if (!pixmap.isNull()) { 201 QPainter p(&fullHeightPixmap); 202 QRect r = pixmap.rect(); 203 r.moveCenter(fullHeightPixmap.rect().center()); 204 p.drawPixmap(r, pixmap); 205 } 206 fullHeightPixmap.setDevicePixelRatio(devicePixelRatio); 207 208 auto *nsimage = [NSImage imageFromQImage:fullHeightPixmap.toImage()]; 209 [nsimage setTemplate:icon.isMask()]; 210 m_statusItem.button.image = nsimage; 211 m_statusItem.button.imageScaling = NSImageScaleProportionallyDown; 212} 213 214void QCocoaSystemTrayIcon::updateMenu(QPlatformMenu *menu) 215{ 216 // We don't set the menu property of the NSStatusItem here, 217 // as that would prevent us from receiving the action for the 218 // click, and we wouldn't be able to emit the activated signal. 219 // Instead we show the menu manually when the status item is 220 // clicked. 221 m_menu = static_cast<QCocoaMenu *>(menu); 222} 223 224void QCocoaSystemTrayIcon::updateToolTip(const QString &toolTip) 225{ 226 if (!m_statusItem) 227 return; 228 229 m_statusItem.button.toolTip = toolTip.toNSString(); 230} 231 232bool QCocoaSystemTrayIcon::isSystemTrayAvailable() const 233{ 234 return true; 235} 236 237bool QCocoaSystemTrayIcon::supportsMessages() const 238{ 239 return true; 240} 241 242void QCocoaSystemTrayIcon::showMessage(const QString &title, const QString &message, 243 const QIcon& icon, MessageIcon, int msecs) 244{ 245 if (!m_statusItem) 246 return; 247 248 auto *notification = [[NSUserNotification alloc] init]; 249 notification.title = title.toNSString(); 250 notification.informativeText = message.toNSString(); 251 notification.contentImage = [NSImage imageFromQIcon:icon]; 252 253 NSUserNotificationCenter *center = NSUserNotificationCenter.defaultUserNotificationCenter; 254 center.delegate = m_delegate; 255 256 [center deliverNotification:[notification autorelease]]; 257 258 if (msecs) { 259 NSTimeInterval timeout = msecs / 1000.0; 260 [center performSelector:@selector(removeDeliveredNotification:) withObject:notification afterDelay:timeout]; 261 } 262} 263 264void QCocoaSystemTrayIcon::statusItemClicked() 265{ 266 auto *mouseEvent = NSApp.currentEvent; 267 268 auto activationReason = QPlatformSystemTrayIcon::Unknown; 269 270 if (mouseEvent.clickCount == 2) { 271 activationReason = QPlatformSystemTrayIcon::DoubleClick; 272 } else { 273 auto mouseButton = cocoaButton2QtButton(mouseEvent); 274 if (mouseButton == Qt::MiddleButton) 275 activationReason = QPlatformSystemTrayIcon::MiddleClick; 276 else if (mouseButton == Qt::RightButton) 277 activationReason = QPlatformSystemTrayIcon::Context; 278 else 279 activationReason = QPlatformSystemTrayIcon::Trigger; 280 } 281 282 emit activated(activationReason); 283 284 if (NSMenu *menu = m_menu ? m_menu->nsMenu() : nil) 285 [m_statusItem popUpStatusItemMenu:menu]; 286} 287 288QT_END_NAMESPACE 289 290@implementation QStatusItemDelegate 291 292- (instancetype)initWithSysTray:(QCocoaSystemTrayIcon *)platformSystemTray 293{ 294 if ((self = [super init])) 295 self.platformSystemTray = platformSystemTray; 296 297 return self; 298} 299 300- (void)dealloc 301{ 302 self.platformSystemTray = nullptr; 303 [super dealloc]; 304} 305 306- (void)statusItemClicked 307{ 308 self.platformSystemTray->statusItemClicked(); 309} 310 311- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification 312{ 313 Q_UNUSED(center); 314 Q_UNUSED(notification); 315 return YES; 316} 317 318- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification 319{ 320 [center removeDeliveredNotification:notification]; 321 emit self.platformSystemTray->messageClicked(); 322} 323 324@end 325 326#endif // QT_NO_SYSTEMTRAYICON 327