1 //===========================================
2 // Lumina-DE source code
3 // Copyright (c) 2016, Ken Moore
4 // Available under the 3-clause BSD license
5 // See the LICENSE file for full details
6 //===========================================
7 #include "NativeWindowSystem.h"
screenUnderMouse()8 inline QScreen* screenUnderMouse(){
9 QPoint pos = QCursor::pos();
10 QList<QScreen*> scrns = QGuiApplication::screens();
11 for(int i=0; i<scrns.length(); i++){
12 if(scrns[i]->geometry().contains(pos)){ return scrns[i]; }
13 }
14 return 0;
15 }
16
17
18 //Primary/private function
arrangeWindows(NativeWindowObject * primary,QString type,bool primaryonly)19 void NativeWindowSystem::arrangeWindows(NativeWindowObject *primary, QString type, bool primaryonly){
20 if(type.isEmpty()){ type = "center"; }
21 if(primary==0){
22 //Get the currently active window and treat that as the primary
23 for(int i=0; i<NWindows.length(); i++){
24 if(NWindows[i]->property(NativeWindowObject::Active).toBool()){ primary = NWindows[i]; }
25 }
26 if(primary==0 && !NWindows.isEmpty()){ primary = NWindows[0]; } //just use the first one in the list
27 }
28 //Now get the current screen that the mouse cursor is over (and valid area)
29 QScreen *screen = screenUnderMouse();
30 if(screen==0){ return; } //should never happen (theoretically)
31 QRect desktopArea = screen->availableGeometry();
32 //qDebug() << "Arrange Windows:" << primary->geometry() << type << primaryonly << desktopArea;
33 //Now start filtering out all the windows that need to be ignored
34 int wkspace = primary->property(NativeWindowObject::Workspace).toInt();
35 QList<NativeWindowObject*> winlist = NWindows;
36 for(int i=0; i<winlist.length(); i++){
37 if(winlist[i]->property(NativeWindowObject::Workspace).toInt()!=wkspace
38 || !winlist[i]->property(NativeWindowObject::Visible).toBool()
39 || desktopArea.intersected(winlist[i]->geometry()).isNull() ){
40 //window is outside of the desired area or invisible - ignore it
41 winlist.removeAt(i);
42 i--;
43 }
44 }
45 if(!winlist.contains(primary)){ winlist << primary; } //could be doing this on a window right before it is shown
46 else if(primaryonly){ winlist.removeAll(primary); winlist << primary; } //move primary window to last
47 //QRegion used;
48 for(int i=0; i<winlist.length(); i++){
49 if(primaryonly && winlist[i]!=primary){ continue; } //skip this window
50 //Now loop over the windows and arrange them as needed
51 QRect geom = winlist[i]->geometry();
52 //verify that the window is contained by the desktop area
53 if(geom.width()>desktopArea.width()){ geom.setWidth(desktopArea.width()); }
54 if(geom.height()>desktopArea.height()){ geom.setHeight(desktopArea.height()); }
55 //Now apply the proper placement routine
56 if(type=="center"){
57 QPoint ct = desktopArea.center();
58 winlist[i]->setGeometryNow( QRect( ct.x()-(geom.width()/2), ct.y()-(geom.height()/2), geom.width(), geom.height()) );
59 }else if(type=="snap"){
60
61 }else if(type=="single_max"){
62 winlist[i]->setGeometryNow( QRect( desktopArea.x(), desktopArea.y(), desktopArea.width(), desktopArea.height()) );
63 }else if(type=="under-mouse"){
64 QPoint ct = QCursor::pos();
65 geom = QRect(ct.x()-(geom.width()/2), ct.y()-(geom.height()/2), geom.width(), geom.height() );
66 //Now verify that the top of the window is still contained within the desktop area
67 if(geom.y() < desktopArea.y() ){ geom.moveTop(desktopArea.y()); }
68 winlist[i]->setGeometryNow(geom);
69
70 }
71 //qDebug() << " - New Geometry:" << winlist[i]->geometry();
72 } //end loop over winlist
73 }
74
75 // ================
76 // Public slots for starting the arrangement routine(s) above
77 // ================
ArrangeWindows(WId primary,QString type)78 void NativeWindowSystem::ArrangeWindows(WId primary, QString type){
79 NativeWindowObject* win = findWindow(primary);
80 if(type.isEmpty()){ type = "center"; } //grab the default arrangement format
81 arrangeWindows(win, type);
82 }
83
TileWindows(WId primary,QString type)84 void NativeWindowSystem::TileWindows(WId primary, QString type){
85 NativeWindowObject* win = findWindow(primary);
86 if(type.isEmpty()){ type = "single_max"; } //grab the default arrangement format for tiling
87 arrangeWindows(win, type);
88 }
89
CheckWindowPosition(WId id,bool newwindow)90 void NativeWindowSystem::CheckWindowPosition(WId id, bool newwindow){
91 //used after a "drop" to validate/snap/re-arrange window(s) as needed
92 // if "newwindow" is true, then this is the first-placement routine for a window before it initially appears
93 NativeWindowObject* win = findWindow(id);
94 CheckWindowPosition(win, newwindow);
95 }
96
CheckWindowPosition(NativeWindowObject * win,bool newwindow)97 void NativeWindowSystem::CheckWindowPosition(NativeWindowObject *win, bool newwindow){
98 if(win==0){ return; } //invalid window
99 QRect geom = win->geometry();
100 qDebug() << "Got Window Geometry:" << geom;
101 bool changed = false;
102 //Make sure it is on the screen (quick check)
103 if(geom.x() < 0){ changed = true; geom.moveLeft(0); }
104 if(geom.y() < 0){ changed = true; geom.moveTop(0); }
105 if(geom.width() < 20){ changed = true; geom.setWidth(100); }
106 if(geom.height() < 20){ changed = true; geom.setHeight(100); }
107 if(changed){ win->setGeometryNow(geom); }
108 //Now run it through the window arrangement routine
109 qDebug() << "ArrangeWindows";
110 arrangeWindows(win, newwindow ?"center" : "snap", true);
111 }
112