1 //===========================================
2 //  Lumina-DE source code
3 //  Copyright (c) 2017, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 #include "NativeWindow.h"
8 
9 #include <QDebug>
10 
11 // === PUBLIC ===
NativeWindow(WId id)12 NativeWindow::NativeWindow(WId id) : QObject(){
13   winid = id;
14   frameid = 0;
15   dmgID = 0;
16 }
17 
~NativeWindow()18 NativeWindow::~NativeWindow(){
19   hash.clear();
20 }
21 
addFrameWinID(WId fid)22 void NativeWindow::addFrameWinID(WId fid){
23   frameid = fid;
24 }
25 
addDamageID(unsigned int dmg)26 void NativeWindow::addDamageID(unsigned int dmg){
27   dmgID = dmg;
28 }
29 
isRelatedTo(WId tmp)30 bool NativeWindow::isRelatedTo(WId tmp){
31   return (relatedTo.contains(tmp) || winid == tmp || frameid == tmp);
32 }
33 
id()34 WId NativeWindow::id(){
35   return winid;
36 }
37 
frameId()38 WId NativeWindow::frameId(){
39   return frameid;
40 }
41 
damageId()42 unsigned int NativeWindow::damageId(){
43   return dmgID;
44 }
45 
property(NativeWindow::Property prop)46 QVariant NativeWindow::property(NativeWindow::Property prop){
47   if(hash.contains(prop)){ return hash.value(prop); }
48   else if(prop == NativeWindow::RelatedWindows){ return QVariant::fromValue(relatedTo); }
49   return QVariant(); //null variant
50 }
51 
setProperty(NativeWindow::Property prop,QVariant val,bool force)52 void NativeWindow::setProperty(NativeWindow::Property prop, QVariant val, bool force){
53   if(prop == NativeWindow::RelatedWindows){ relatedTo = val.value< QList<WId> >(); }
54   else if(prop == NativeWindow::None || (!force && hash.value(prop)==val)){ return; }
55   else{ hash.insert(prop, val); }
56   emit PropertiesChanged(QList<NativeWindow::Property>() << prop, QList<QVariant>() << val);
57 }
58 
setProperties(QList<NativeWindow::Property> props,QList<QVariant> vals,bool force)59 void NativeWindow::setProperties(QList<NativeWindow::Property> props, QList<QVariant> vals, bool force){
60   for(int i=0; i<props.length(); i++){
61     if(i>=vals.length()){ props.removeAt(i); i--; continue; } //no corresponding value for this property
62     if(props[i] == NativeWindow::None || (!force && (hash.value(props[i]) == vals[i])) ){ props.removeAt(i); vals.removeAt(i); i--; continue; } //Invalid property or identical value
63     hash.insert(props[i], vals[i]);
64   }
65   emit PropertiesChanged(props, vals);
66 }
67 
requestProperty(NativeWindow::Property prop,QVariant val,bool force)68 void NativeWindow::requestProperty(NativeWindow::Property prop, QVariant val, bool force){
69   if(prop == NativeWindow::None || prop == NativeWindow::RelatedWindows || (!force && hash.value(prop)==val) ){ return; }
70   emit RequestPropertiesChange(winid, QList<NativeWindow::Property>() << prop, QList<QVariant>() << val);
71 }
72 
requestProperties(QList<NativeWindow::Property> props,QList<QVariant> vals,bool force)73 void NativeWindow::requestProperties(QList<NativeWindow::Property> props, QList<QVariant> vals, bool force){
74   //Verify/adjust inputs as needed
75   for(int i=0; i<props.length(); i++){
76     if(i>=vals.length()){ props.removeAt(i); i--; continue; } //no corresponding value for this property
77     if(props[i] == NativeWindow::None || props[i] == NativeWindow::RelatedWindows || (!force && hash.value(props[i])==vals[i]) ){ props.removeAt(i); vals.removeAt(i); i--; continue; } //Invalid property or identical value
78     /*if( (props[i] == NativeWindow::Visible || props[i] == NativeWindow::Active) && frameid !=0){
79       //These particular properties needs to change the frame - not the window itself
80       emit RequestPropertiesChange(frameid, QList<NativeWindow::Property>() << props[i], QList<QVariant>() << vals[i]);
81       props.removeAt(i); vals.removeAt(i); i--;
82     }*/
83   }
84   emit RequestPropertiesChange(winid, props, vals);
85 }
86 
geometry()87 QRect NativeWindow::geometry(){
88   //Calculate the "full" geometry of the window + frame (if any)
89   //Check that the size is between the min/max limitations
90   QSize size = hash.value(NativeWindow::Size).toSize();
91   QSize min = hash.value(NativeWindow::MinSize).toSize();
92   QSize max = hash.value(NativeWindow::MaxSize).toSize();
93   if(min.isValid() && min.width() > size.width() ){ size.setWidth(min.width()); }
94   if(min.isValid() && min.height() > size.height()){ size.setHeight(min.height()); }
95   if(max.isValid() && max.width() < size.width()  && max.width()>min.width()){ size.setWidth(max.width()); }
96   if(max.isValid() && max.height() < size.height()  && max.height()>min.height()){ size.setHeight(max.height()); }
97   //Assemble the full geometry
98   QRect geom( hash.value(NativeWindow::GlobalPos).toPoint(), size );
99   //Now adjust the window geom by the frame margins
100   QList<int> frame = hash.value(NativeWindow::FrameExtents).value< QList<int> >(); //Left,Right,Top,Bottom
101   //qDebug() << "Calculate Geometry:" << geom << frame;
102   if(frame.length()==4){
103     geom = geom.adjusted( -frame[0], -frame[2], frame[1], frame[3] );
104   }
105   //qDebug() << " - Total:" << geom;
106   return geom;
107 }
108 // ==== PUBLIC SLOTS ===
toggleVisibility()109 void NativeWindow::toggleVisibility(){
110   setProperty(NativeWindow::Visible, !property(NativeWindow::Visible).toBool() );
111 }
112 
requestClose()113 void NativeWindow::requestClose(){
114   emit RequestClose(winid);
115 }
116 
requestKill()117 void NativeWindow::requestKill(){
118   emit RequestKill(winid);
119 }
120 
requestPing()121 void NativeWindow::requestPing(){
122   emit RequestPing(winid);
123 }
124