1 //===========================================
2 //  Lumina-desktop source code
3 //  Copyright (c) 2018, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 #include "SystemTrayMenu.h"
8 
9 #include <global-objects.h>
10 
SystemTrayMenu()11 SystemTrayMenu::SystemTrayMenu() : QMenu() {
12   this->setWindowFlags(Qt::Popup | Qt::WindowTransparentForInput);
13   widget = new QWidget();
14   layout = new QGridLayout(widget);
15   WA = new QWidgetAction(this);
16     WA->setDefaultWidget(widget);
17   this->addAction(WA);
18   connect(RootDesktopObject::instance(), SIGNAL(trayWindowsChanged()), this, SLOT(trayWindowsChanged()) );
19   QTimer::singleShot(50, this, SLOT(trayWindowsChanged()) ); //first-time load of tray windows
20 }
21 
~SystemTrayMenu()22 SystemTrayMenu::~SystemTrayMenu(){
23   widget->deleteLater();
24 }
25 
numTrayIcons()26 int SystemTrayMenu::numTrayIcons(){
27   return TIcons.count();
28 }
29 
trayWindowsChanged()30 void SystemTrayMenu::trayWindowsChanged(){
31   //Clear all the tray icons
32   for(int i=0; i<TIcons.length(); i++){ layout->removeWidget(TIcons[i]); TIcons[i]->deleteLater(); }
33   TIcons.clear();
34   //Now generate all the tray icons
35   QList<NativeWindowObject*> wins = RootDesktopObject::instance()->trayWindowObjects();
36   for(int i=0; i<wins.length(); i++){
37     TrayIcon *tmp = new TrayIcon(this, wins[i]);
38 	connect(this, SIGNAL(aboutToShow()), tmp, SLOT(aboutToShow()) );
39     TIcons << tmp;
40     layout->addWidget(tmp, i/3, i%3); //3 columns of icons
41   }
42   emit hasTrayIcons( numTrayIcons()>0 );
43 }
44 
45 // ================
46 //    TrayIcon
47 // ================
TrayIcon(QWidget * parent,NativeWindowObject * win)48 TrayIcon::TrayIcon(QWidget *parent, NativeWindowObject *win) : QLabel(parent){
49   WIN = win;
50   connect(win, SIGNAL(iconChanged()), this, SLOT(updateIcon()) );
51 }
52 
~TrayIcon()53 TrayIcon::~TrayIcon(){
54 
55 }
56 
updateIcon()57 void TrayIcon::updateIcon(){
58   //Just set a hard 48x48 pixel size for now
59   // It is small enough that most apps support it, but large enough to be visible on high-DPI screens
60   this->setPixmap( WIN->property(NativeWindowObject::Icon).value<QIcon>().pixmap(QSize(48,48)) );
61 }
62 
aboutToShow()63 void TrayIcon::aboutToShow(){
64   WIN->setGeometryNow( QRect(this->mapToGlobal(this->geometry().topLeft()), QSize(48,48)) );
65 }
66