-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrayapplet.cpp
191 lines (163 loc) · 4.92 KB
/
trayapplet.cpp
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
#include "trayapplet.h"
#include <QtGui/QPainter>
#include <QtGui/QGraphicsSceneMouseEvent>
#include "panelapplication.h"
#include "panelwindow.h"
#include "x11support.h"
#include "dpisupport.h"
TrayItem::TrayItem(TrayApplet* trayApplet, unsigned long window)
: m_trayApplet(trayApplet), m_window(window)
{
setParentItem(m_trayApplet);
// This is needed for non-composited tray icons, otherwise we'll get a BadMatch on reparent attempt.
// Doesn't affect composited icons.
X11Support::setWindowBackgroundBlack(m_window);
X11Support::registerForTrayIconUpdates(m_window);
X11Support::reparentWindow(m_window, m_trayApplet->panelWindow()->winId());
X11Support::resizeWindow(m_window, m_trayApplet->iconSize(), m_trayApplet->iconSize());
X11Support::redirectWindow(m_window);
X11Support::mapWindow(m_window);
m_trayApplet->registerTrayItem(this);
}
TrayItem::~TrayItem()
{
X11Support::reparentWindow(m_window, X11Support::rootWindow());
m_trayApplet->unregisterTrayItem(this);
}
void TrayItem::setPosition(const QPoint& position)
{
setPos(position.x(), position.y());
X11Support::moveWindow(m_window,
static_cast<int>(m_trayApplet->pos().x()) + position.x() + m_size.width()/2 - m_trayApplet->iconSize()/2,
static_cast<int>(m_trayApplet->pos().y()) + position.y() + m_size.height()/2 - m_trayApplet->iconSize()/2
);
}
void TrayItem::setSize(const QSize& size)
{
m_size = size;
update();
}
QRectF TrayItem::boundingRect() const
{
return QRectF(0.0, 0.0, m_size.width() - 1, m_size.height() - 1);
}
void TrayItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
// Background.
painter->setPen(Qt::NoPen);
QPointF center(m_size.width()/2.0, m_size.height()/2.0);
QRadialGradient gradient(center, m_size.width()/2.0, center);
gradient.setColorAt(0.0, QColor(255, 255, 255, 80));
gradient.setColorAt(1.0, QColor(255, 255, 255, 0));
painter->setBrush(QBrush(gradient));
painter->drawRect(boundingRect());
// Icon itself.
painter->drawPixmap(m_size.width()/2 - m_trayApplet->iconSize()/2, m_size.height()/2 - m_trayApplet->iconSize()/2, X11Support::getWindowPixmap(m_window));
}
TrayApplet::TrayApplet(PanelWindow* panelWindow)
: Applet(panelWindow), m_initialized(false), m_iconSize(adjustHardcodedPixelSize(24)), m_spacing(adjustHardcodedPixelSize(4))
{
}
TrayApplet::~TrayApplet()
{
if(m_initialized)
X11Support::freeSystemTray();
while(!m_trayItems.isEmpty())
{
delete m_trayItems[m_trayItems.size() - 1];
}
}
bool TrayApplet::init()
{
m_initialized = X11Support::makeSystemTray(m_panelWindow->winId());
if(!m_initialized)
{
// Another tray is active.
return false;
}
connect(X11Support::instance(), SIGNAL(windowClosed(ulong)), this, SLOT(windowClosed(ulong)));
connect(X11Support::instance(), SIGNAL(windowReconfigured(ulong,int,int,int,int)), this, SLOT(windowReconfigured(ulong,int,int,int,int)));
connect(X11Support::instance(), SIGNAL(windowDamaged(ulong)), this, SLOT(windowDamaged(ulong)));
connect(X11Support::instance(), SIGNAL(clientMessageReceived(ulong,ulong,void*)), this, SLOT(clientMessageReceived(ulong,ulong,void*)));
return true;
}
QSize TrayApplet::desiredSize()
{
int desiredWidth = (m_iconSize + m_spacing)*m_trayItems.size() - m_spacing;
if(desiredWidth < 0)
desiredWidth = 0;
return QSize(desiredWidth, -1);
}
void TrayApplet::registerTrayItem(TrayItem* trayItem)
{
m_trayItems.append(trayItem);
m_panelWindow->updateLayout();
}
void TrayApplet::unregisterTrayItem(TrayItem* trayItem)
{
m_trayItems.remove(m_trayItems.indexOf(trayItem));
m_panelWindow->updateLayout();
}
void TrayApplet::layoutChanged()
{
updateLayout();
}
void TrayApplet::clientMessageReceived(unsigned long window, unsigned long atom, void* data)
{
if(atom == X11Support::atom("_NET_SYSTEM_TRAY_OPCODE"))
{
unsigned long* l = reinterpret_cast<unsigned long*>(data);
if(l[1] == 0) // TRAY_REQUEST_DOCK
{
for(int i = 0; i < m_trayItems.size(); i++)
{
if(m_trayItems[i]->window() == l[2])
return; // Already added.
}
new TrayItem(this, l[2]);
}
}
}
void TrayApplet::windowClosed(unsigned long window)
{
for(int i = 0; i < m_trayItems.size(); i++)
{
if(m_trayItems[i]->window() == window)
{
delete m_trayItems[i];
break;
}
}
}
void TrayApplet::windowReconfigured(unsigned long window, int x, int y, int width, int height)
{
for(int i = 0; i < m_trayItems.size(); i++)
{
if(m_trayItems[i]->window() == window)
{
X11Support::resizeWindow(window, m_iconSize, m_iconSize);
break;
}
}
}
void TrayApplet::windowDamaged(unsigned long window)
{
for(int i = 0; i < m_trayItems.size(); i++)
{
if(m_trayItems[i]->window() == window)
{
m_trayItems[i]->update();
break;
}
}
}
void TrayApplet::updateLayout()
{
int currentPosition = 0;
for(int i = 0; i < m_trayItems.size(); i++)
{
m_trayItems[i]->setSize(QSize(m_iconSize, m_size.height()));
m_trayItems[i]->setPosition(QPoint(currentPosition, 0));
currentPosition += m_iconSize + m_spacing;
}
}