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
|
//===========================================
// Lumina-desktop source code
// Copyright (c) 2018, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "NativeWindow.h"
#include <QWidget>
#include <QWindow>
// === PUBLIC ===
NativeWindow::NativeWindow( NativeWindowObject *obj ) : QFrame(0, Qt::Window | Qt::FramelessWindowHint){
WIN = obj;
createFrame();
this->setMouseTracking(true);
//Setup the timer object to syncronize info
moveTimer = new QTimer(this);
moveTimer->setSingleShot(true);
moveTimer->setInterval(100); //1/10 second
connect(moveTimer, SIGNAL(timeout()), this, SLOT(submitLocationChange()) ); //Let the window system know the window has moved
resizeTimer = new QTimer(this);
resizeTimer->setSingleShot(true);
resizeTimer->setInterval(10); //1/10 second
connect(resizeTimer, SIGNAL(timeout()), this, SLOT(submitSizeChange()) ); //Let the window system know the window has moved
WIN->addFrameWinID(this->winId());
}
NativeWindow::~NativeWindow(){
vlayout->deleteLater();
toolbarL->deleteLater();
}
QPoint NativeWindow::relativeOrigin(){
//Update all the margins for the frame
QList<int> frame = WIN->property(NativeWindowObject::FrameExtents).value<QList<int> >();
//QList<int> : [Left, Right, Top, Bottom] in pixels
QPoint containerCorner(frame[0], frame[2]);
//qDebug() << "Got Container Corner:" << containerCorner << "L,R,T,B:" << frame;
return containerCorner;
//return QPoint(0,0);
}
void NativeWindow::initProperties(){
//Setup all the property connections
connect(WIN, SIGNAL(winImageChanged()), this, SLOT(syncWinImage()) );
connect(WIN, SIGNAL(nameChanged()), this, SLOT(syncName()) );
connect(WIN, SIGNAL(titleChanged()), this, SLOT(syncTitle()) );
connect(WIN, SIGNAL(iconChanged()), this, SLOT(syncIcon()) );
connect(WIN, SIGNAL(stickyChanged()), this, SLOT(syncSticky()) );
connect(WIN, SIGNAL(visibilityChanged()), this, SLOT(syncVisibility()) );
connect(WIN, SIGNAL(winTypeChanged()), this, SLOT(syncWinType()) );
connect(WIN, SIGNAL(geomChanged()), this, SLOT(syncGeom()) );
connect(WIN, SIGNAL(WindowClosed(WId)), this, SLOT(deleteLater()) );
//Setup all the button connections
connect(minB, SIGNAL(clicked()), WIN, SLOT(toggleVisibility()) );
connect(maxB, SIGNAL(clicked()), WIN, SLOT(toggleMaximize()) );
connect(closeB, SIGNAL(clicked()), WIN, SLOT(requestClose()) );
//Now Perform the initial property loads
syncWinImage();
syncName();
syncTitle();
syncIcon();
syncSticky();
syncWinType();
syncGeom();
syncVisibility(true); //init visibility - force it visible to start with
container->activateWindow();
}
//Mouse Interactivity
void NativeWindow::startMoving(){
//If the cursor is not over this window, move it to the center of the titlebar
QPoint curpt = QCursor::pos(); //global coords
if(!this->geometry().contains(curpt)){
curpt = this->mapToGlobal(titleLabel->geometry().center());
QCursor::setPos(curpt);
}
//Calculate the offset
activeState = Move;
offset = this->mapFromGlobal(curpt);
setMouseCursor(activeState, true); //this one is an override cursor
//WinWidget->pause();
this->grabMouse();
}
void NativeWindow::startResizing(){
activeState = getStateAtPoint( this->mapFromGlobal(QCursor::pos()), true); //also have it set the offset variable
setMouseCursor(activeState, true); //this one is an override cursor
//WinWidget->pause();
this->grabMouse();
}
// === PRIVATE ===
NativeWindow::ModState NativeWindow::getStateAtPoint(QPoint pt, bool setoffset){
//Note: pt should be in widget-relative coordinates, not global
QList<int> frame = WIN->property(NativeWindowObject::FrameExtents).value<QList<int> >();
/*QList<int> : [Left, Right, Top, Bottom] in pixels */
bool onLeft = (pt.x()<=frame[0]);
bool onRight = (pt.x() >= (this->width()-frame[1])) && pt.x() <= this->width();
bool onTop = (pt.y() < titleLabel->y() ); //be careful about this one - the top frame gets split up and the title bar put in
bool onBottom = (pt.y() >= (this->height()-frame[3]) && (pt.y() <= this->height()) );
if(onLeft || onRight || onTop || onBottom){
//above the frame itself - need to figure out which quadrant it is in (8-directions)
if(onTop){
//One of the top options
if(onLeft){
if(setoffset){ offset.setX(pt.x()); offset.setY(pt.y()); } //difference from top-left corner
return ResizeTopLeft;
}else if(onRight){
if(setoffset){ offset.setX(pt.x()-this->width()); offset.setY(pt.y()); } //difference from top-right corner
return ResizeTopRight;
}else{
if(setoffset){ offset.setX(0); offset.setY(pt.y()); } //difference from top edge (X does not matter)
return ResizeTop;
}
}else if(onBottom){
//One of the bottom options
if(onLeft){
if(setoffset){ offset.setX(pt.x()); offset.setY(pt.y()-this->height()); } //difference from bottom-left corner
return ResizeBottomLeft;
}else if(onRight){
if(setoffset){ offset.setX(pt.x()-this->width()); offset.setY(pt.y()-this->height()); } //difference from bottom-right corner
return ResizeBottomRight;
}else{
if(setoffset){ offset.setX(0); offset.setY(pt.y()-this->height()); } //difference from bottom edge (X does not matter)
return ResizeBottom;
}
}else if(onLeft){
if(setoffset){ offset.setX(pt.x()); offset.setY(0); } //difference from left edge (Y does not matter)
return ResizeLeft;
}else if(onRight){
if(setoffset){ offset.setX(pt.x()-this->width()); offset.setY(0); } //difference from right edge (Y does not matter)
return ResizeRight;
}else{
return Normal;
}
}
//if it gets this far just return normal
return Normal;
}
void NativeWindow::setMouseCursor(ModState state, bool override){
if(currentCursor==state && !override){ return; } //nothing to change
Qt::CursorShape shape;
switch(state){
case Normal:
shape = Qt::ArrowCursor;
break;
case Move:
shape = Qt::SizeAllCursor;
break;
case ResizeTop:
shape = Qt::SizeVerCursor;
break;
case ResizeTopRight:
shape = Qt::SizeBDiagCursor;
break;
case ResizeRight:
shape = Qt::SizeHorCursor;
break;
case ResizeBottomRight:
shape = Qt::SizeFDiagCursor;
break;
case ResizeBottom:
shape = Qt::SizeVerCursor;
break;
case ResizeBottomLeft:
shape = Qt::SizeBDiagCursor;
break;
case ResizeLeft:
shape = Qt::SizeHorCursor;
break;
case ResizeTopLeft:
shape = Qt::SizeFDiagCursor;
break;
}
if(override){
QApplication::setOverrideCursor(QCursor(shape));
}else{
currentCursor = state;
this->setCursor(shape);
}
}
void NativeWindow::createFrame(){
//Initialize the widgets
closeB = new QToolButton(this);
closeB->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
closeB->setAutoRaise(true);
closeB->setCursor(Qt::ArrowCursor);
minB = new QToolButton(this);
minB->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
minB->setAutoRaise(true);
minB->setCursor(Qt::ArrowCursor);
maxB = new QToolButton(this);
maxB->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
maxB->setAutoRaise(true);
maxB->setCursor(Qt::ArrowCursor);
otherB = new QToolButton(this);
otherB->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
otherB->setAutoRaise(true);
otherB->setCursor(Qt::ArrowCursor);
vlayout = new QVBoxLayout();
vlayout->setSpacing(0);
toolbarL = new QHBoxLayout();
toolbarL->setSpacing(0);
qDebug() << "Create Native Embed Widget";
container = new NativeEmbedWidget(this, WIN);
container->widget()->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
//vlayout.align
titleLabel = new QLabel(this);
titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
titleLabel->setCursor(Qt::ArrowCursor);
//Now put the widgets in the right places
toolbarL->addWidget(otherB);
toolbarL->addWidget(titleLabel);
toolbarL->addWidget(minB);
toolbarL->addWidget(maxB);
toolbarL->addWidget(closeB);
vlayout->addLayout(toolbarL);
//vlayout->addStretch();
vlayout->addWidget(container->widget());
this->setLayout(vlayout);
// Load the icons for the buttons
loadIcons();
}
void NativeWindow::loadIcons(){
closeB->setIcon( QIcon::fromTheme("window-close") );
minB->setIcon( QIcon::fromTheme("window-minimize") );
maxB->setIcon( QIcon::fromTheme("window-maximize") );
}
// === PRIVATE SLOTS ===
//Property Change slots
void NativeWindow::syncWinImage(){
//Do nothing at the moment (internal compositing disabled)
}
void NativeWindow::syncName(){
//qDebug() << "Got Name Change:" << WIN->name();
}
void NativeWindow::syncTitle(){
titleLabel->setText(WIN->title());
titleLabel->setToolTip(WIN->title());
}
void NativeWindow::syncIcon(){
//Do not use the "WIN->icon()" function, that is the URL format for QML
otherB->setIcon( QIcon(WIN->property(NativeWindowObject::Icon).value<QIcon>()) );
}
void NativeWindow::syncSticky(){
//qDebug() << "Got Sticky Change:" << WIN->isSticky();
}
void NativeWindow::syncVisibility(bool init){
if(init){
WIN->setProperty(NativeWindowObject::Visible, true, true); //force it
}else if(this->isVisible() != WIN->isVisible()){
//qDebug() << "Sync Visibility:" << WIN->isVisible();
this->setVisible(WIN->isVisible());
}
}
void NativeWindow::syncWinType(){
//qDebug() << "Sync Win Type";
closeB->setVisible(WIN->showCloseButton());
maxB->setVisible(WIN->showMaxButton());
minB->setVisible(WIN->showMinButton());
titleLabel->setVisible(WIN->showTitlebar());
otherB->setVisible(WIN->showGenericButton());
//toolbarL->setVisible(WIN->showTitlebar());
//Update all the margins for the frame
QList<int> frame = WIN->property(NativeWindowObject::FrameExtents).value<QList<int> >();
/*QList<int> : [Left, Right, Top, Bottom] in pixels */
//qDebug() << "Setup Frame Margins:" << frame;
vlayout->setContentsMargins( frame[0], 0,frame[1], frame[3]);
int topM = frame[2] - titleLabel->fontMetrics().height(); //figure out how much extra we have to work with
if(topM<0){ topM = 0; }
int botM = topM/2.0;
toolbarL->setContentsMargins( 0, topM-botM, 0, botM);
//QPoint containerCorner(frame[0], topM-botM);
//WIN->emit RequestReparent(WIN->id(), this->winId(), containerCorner);
}
void NativeWindow::syncGeom(){
qDebug() << "Sync Geometry:" << WIN->name();
qDebug() << " Frame:" << WIN->frameGeometry() << "Win:" << WIN->imageGeometry();
QRect geom = WIN->frameGeometry();
if(geom!=this->geometry()){
this->setGeometry( geom );
}
}
void NativeWindow::submitLocationChange(){
emit windowMoved(WIN);
}
void NativeWindow::submitSizeChange(){
if(!pending_geom.isNull()){
this->setGeometry(pending_geom);
pending_geom = QRect();
}
}
// === PROTECTED ===
void NativeWindow::mousePressEvent(QMouseEvent *ev){
container->activateWindow();
this->raise();
QFrame::mousePressEvent(ev);
//qDebug() << "Frame Mouse Press Event";
if(activeState != Normal){ return; } // do nothing - already in a state of grabbed mouse
offset.setX(0); offset.setY(0);
if(ev->button()==Qt::LeftButton){
if(this->childAt(ev->pos())!=0){
//Clicked on the titlebar
startMoving();
}else{
//Clicked on the frame somewhere
startResizing();
}
}
}
void NativeWindow::mouseMoveEvent(QMouseEvent *ev){
//qDebug() << "Got Mouse Move Event:" << ev->pos();
QFrame::mouseMoveEvent(ev);
if(activeState == Normal){
setMouseCursor( getStateAtPoint(ev->pos()) ); //just update the mouse cursor
}else{
//Currently in a modification state
QRect geom = this->geometry();
QList<int> frame = WIN->property(NativeWindowObject::FrameExtents).value<QList<int> >();
/*QList<int> : [Left, Right, Top, Bottom] in pixels */
QSize minsize(container->widget()->minimumSize().width() + frame[0]+frame[1], container->widget()->minimumSize().height()+frame[2]+frame[3] );
switch(activeState){
case Move:
geom.moveTopLeft(ev->globalPos()-offset); //will not change size
break;
case ResizeTop:
geom.setTop(ev->globalPos().y()-offset.y());
if(geom.size().height() < minsize.height()){
geom.setTop(geom.y() - (minsize.height()-geom.size().height())); //reset back to min height
}
break;
case ResizeTopRight:
geom.setTopRight(ev->globalPos()-offset);
if(geom.size().height() < minsize.height()){
geom.setTop(geom.y() - (minsize.height()-geom.size().height())); //reset back to min height
}
if(geom.size().width() < minsize.width()){
geom.setRight(geom.x() + minsize.width()); //reset back to min width
}
break;
case ResizeRight:
geom.setRight(ev->globalPos().x()-offset.x());
if(geom.size().width() < minsize.width()){
geom.setRight(geom.x() + minsize.width()); //reset back to min width
}
break;
case ResizeBottomRight:
geom.setBottomRight(ev->globalPos()-offset);
if(geom.size().height() < minsize.height()){
geom.setBottom(geom.y() + minsize.height()); //reset back to min height
}
if(geom.size().width() < minsize.width()){
geom.setRight(geom.x() + minsize.width()); //reset back to min width
}
break;
case ResizeBottom:
geom.setBottom(ev->globalPos().y()-offset.y());
if(geom.size().height() < minsize.height()){
geom.setBottom(geom.y() + minsize.height()); //reset back to min height
}
break;
case ResizeBottomLeft:
geom.setBottomLeft(ev->globalPos()-offset);
if(geom.size().height() < minsize.height()){
geom.setBottom(geom.y() + minsize.height()); //reset back to min height
}
if(geom.size().width() < minsize.width()){
geom.setLeft(geom.x() - (minsize.width()-geom.size().width())); //reset back to min width
}
break;
case ResizeLeft:
geom.setLeft(ev->globalPos().x()-offset.x());
if(geom.size().width() < minsize.width()){
geom.setLeft(geom.x() - (minsize.width()-geom.size().width())); //reset back to min width
}
break;
case ResizeTopLeft:
geom.setTopLeft(ev->globalPos()-offset);
if(geom.size().height() < minsize.height()){
geom.setTop(geom.y() - (minsize.height()-geom.size().height())); //reset back to min height
}
if(geom.size().width() < minsize.width()){
geom.setLeft(geom.x() - (minsize.width()-geom.size().width())); //reset back to min width
}
break;
default:
break;
}
//if( (geom.width()%2==0 && geom.height()%2==0) || activeState==Move){
//qDebug() << " Change Window:" << this->geometry() << geom;
if(activeState==Move){ this->setGeometry(geom); }
else if(this->geometry()!=geom){
pending_geom = geom;
if(!resizeTimer->isActive()){ QTimer::singleShot(0,resizeTimer, SLOT(start()) ); }
/*qDebug() << " Change Window Dimensions:" << this->geometry() << geom;
qDebug() << " - Mouse Pos:" << ev->globalPos() << ev->pos() << "Offset" << offset;
this->setGeometry(geom);
qDebug() << " - Done setting geom";*/
}
//}
}
}
void NativeWindow::mouseReleaseEvent(QMouseEvent *ev){
//Check for a right-click event
//qDebug() << "Frame Mouse Release Event";
QFrame::mouseReleaseEvent(ev);
if( (activeState==Normal) && (titleLabel->geometry().contains(ev->pos())) && (ev->button()==Qt::RightButton) ){
//container->raiseWindow();//need to ensure the native window is always on top of this frame but under the menu
otherB->emit clicked();
return;
}
if(activeState!=Normal){
//if(container->isPaused()){ container->resume(); }
activeState = Normal;
QApplication::restoreOverrideCursor();
setMouseCursor( getStateAtPoint(ev->pos()) );
}
if(QFrame::mouseGrabber() == this){ this->releaseMouse(); }
container->activateWindow();
//QTimer::singleShot(0, container, SLOT(raiseWindow()) );
}
/*void NativeWindow::enterEvent(QEvent *ev){
QFrame::enterEvent(ev);
container->raiseWindow();
}*/
void NativeWindow::leaveEvent(QEvent *ev){
QFrame::leaveEvent(ev);
if(activeState == Normal){
setMouseCursor(Normal);
}
//if(!QRect(QPoint(0,0),this->size()).contains( this->mapFromGlobal(QCursor::pos())) ){ container->lowerWindow(); }
}
void NativeWindow::moveEvent(QMoveEvent *ev){
//qDebug() << "Got Move Event:" << ev->pos() << container->geometry();
QFrame::moveEvent(ev);
//if(!closing && !container->isPaused()){
QTimer::singleShot(0,moveTimer, SLOT(start())); //this needs to be thread-safe
//}
}
|