aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/lumina-desktop-unified/src-desktop/src-cpp/NativeWindowObject.cpp
blob: 9bb78dd00cb73413fabdaec1b0af1a389ec78df3 (plain)
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
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2017-2018, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "NativeWindowObject.h"
#include <QQmlEngine>
#include <QDebug>
#include <QBuffer>

// == QML Type Registration ==
void NativeWindowObject::RegisterType(){
  static bool done = false;
  if(done){ return; }
  done=true;
  qmlRegisterType<NativeWindowObject>("Lumina.Backend.NativeWindowObject",2,0, "NativeWindowObject");
}

// === PUBLIC ===
NativeWindowObject::NativeWindowObject(WId id) : QObject(){
  winid = id;
  frameid = 0;
  dmgID = dmg = 0;
  geomTimer = new QTimer(this);
    geomTimer->setSingleShot(true);
    geomTimer->setInterval(50); //1/20 second
  connect(geomTimer, SIGNAL(timeout()), this, SLOT(sendNewGeom()) );
}

NativeWindowObject::~NativeWindowObject(){
  hash.clear();
}

void NativeWindowObject::addFrameWinID(WId fid){
  frameid = fid;
}

void NativeWindowObject::addDamageID(unsigned int dmg){
  dmgID = dmg;
}

bool NativeWindowObject::isRelatedTo(WId tmp){
  return (relatedTo.contains(tmp) || winid == tmp || frameid == tmp);
}

WId NativeWindowObject::id(){
  return winid;
}

WId NativeWindowObject::frameId(){
  return frameid;
}

unsigned int NativeWindowObject::damageId(){
  return dmgID;
}

QVariant NativeWindowObject::property(NativeWindowObject::Property prop){
  if(hash.contains(prop)){ return hash.value(prop); }
  else if(prop == NativeWindowObject::RelatedWindows){ return QVariant::fromValue(relatedTo); }
  return QVariant(); //null variant
}

void NativeWindowObject::setProperty(NativeWindowObject::Property prop, QVariant val, bool force){
  if(prop == NativeWindowObject::RelatedWindows){ relatedTo = val.value< QList<WId> >(); }
  else if(prop == NativeWindowObject::None || (!force && hash.value(prop)==val)){ return; }
  else if(prop == NativeWindowObject::WinImage){
      //special case - This should never be actually set in the property hash
      //  it is loaded dynamically by the QMLImageProvider instead (prevent flickering/caching image)
  } else{ hash.insert(prop, val); }
  emitSinglePropChanged(prop);
  emit PropertiesChanged(QList<NativeWindowObject::Property>() << prop, QList<QVariant>() << val);
}

void NativeWindowObject::setProperties(QList<NativeWindowObject::Property> props, QList<QVariant> vals, bool force){
  for(int i=0; i<props.length(); i++){
    if(i>=vals.length()){ props.removeAt(i); i--; continue; } //no corresponding value for this property
    if(props[i] == NativeWindowObject::None || (!force && (hash.value(props[i]) == vals[i])) ){
      props.removeAt(i); vals.removeAt(i); i--; continue; //Invalid property or identical value
    }else if(props[i] == NativeWindowObject::WinImage){
      //special case - This should never be actually set in the property hash
      //  it is loaded dynamically by the QMLImageProvider instead (prevent flickering/caching image)
    }else{
      hash.insert(props[i], vals[i]);
    }
    emitSinglePropChanged(props[i]);
  }
  emit PropertiesChanged(props, vals);
}

void NativeWindowObject::requestProperty(NativeWindowObject::Property prop, QVariant val, bool force){
  if(prop == NativeWindowObject::None || prop == NativeWindowObject::RelatedWindows || (!force && hash.value(prop)==val) ){ return; }
  emit RequestPropertiesChange(winid, QList<NativeWindowObject::Property>() << prop, QList<QVariant>() << val);
}

void NativeWindowObject::requestProperties(QList<NativeWindowObject::Property> props, QList<QVariant> vals, bool force){
  //Verify/adjust inputs as needed
  for(int i=0; i<props.length(); i++){
    if(i>=vals.length()){ props.removeAt(i); i--; continue; } //no corresponding value for this property
    if(props[i] == NativeWindowObject::None || props[i] == NativeWindowObject::RelatedWindows || (!force && hash.value(props[i])==vals[i]) ){ props.removeAt(i); vals.removeAt(i); i--; continue; } //Invalid property or identical value
    /*if( (props[i] == NativeWindowObject::Visible || props[i] == NativeWindowObject::Active) && frameid !=0){
      //These particular properties needs to change the frame - not the window itself
      emit RequestPropertiesChange(frameid, QList<NativeWindowObject::Property>() << props[i], QList<QVariant>() << vals[i]);
      props.removeAt(i); vals.removeAt(i); i--;
    }*/
  }
  emit RequestPropertiesChange(winid, props, vals);
}

QRect NativeWindowObject::geometry(){
  //Calculate the "full" geometry of the window + frame (if any)
  //Check that the size is between the min/max limitations
  QSize size = hash.value(NativeWindowObject::Size).toSize();
  QSize min = hash.value(NativeWindowObject::MinSize).toSize();
  QSize max = hash.value(NativeWindowObject::MaxSize).toSize();
  if(min.isValid() && min.width() > size.width() ){ size.setWidth(min.width()); }
  if(min.isValid() && min.height() > size.height()){ size.setHeight(min.height()); }
  if(max.isValid() && max.width() < size.width()  && max.width()>min.width()){ size.setWidth(max.width()); }
  if(max.isValid() && max.height() < size.height()  && max.height()>min.height()){ size.setHeight(max.height()); }
  //Assemble the full geometry
  QRect geom( hash.value(NativeWindowObject::GlobalPos).toPoint(), size );
  //Now adjust the window geom by the frame margins
  QList<int> frame = hash.value(NativeWindowObject::FrameExtents).value< QList<int> >(); //Left,Right,Top,Bottom
  //qDebug() << "Calculate Geometry:" << geom << frame;
  if(frame.length()==4){
    geom = geom.adjusted( -frame[0], -frame[2], frame[1], frame[3] );
  }
  //qDebug() << " - Total:" << geom;
  return geom;
}

void NativeWindowObject::setGeometryNow(QRect geom){
  updateGeometry(geom.x(), geom.y(), geom.width(), geom.height(), true);
}

// QML ACCESS FUNCTIONS (shortcuts for particular properties in a format QML can use)
QString NativeWindowObject::winImage(){
  //Need to alternate something on the end to ensure that QML knows to fetch the new image (non-cached only)
  if(dmg==0){ dmg = 1; }
  else{ dmg = 0; }
  return "image://native_window/image:"+QString::number(winid)+":"+QString::number(dmg);
}

QString NativeWindowObject::name(){
  return this->property(NativeWindowObject::Name).toString();
}

QString NativeWindowObject::title(){
  return this->property(NativeWindowObject::Title).toString();
}

QString NativeWindowObject::shortTitle(){
  QString tmp = this->property(NativeWindowObject::ShortTitle).toString();
  if(tmp.isEmpty()){ tmp = title(); }
  if(tmp.isEmpty()){ tmp = name(); }
  return tmp;
}

QString NativeWindowObject::icon(){
  if(icodmg==0){ icodmg=1; }
  else{ icodmg = 0; }
  qDebug() << "Window Icon:" << icodmg << this->property(NativeWindowObject::Icon).value<QIcon>().availableSizes();
  return "image://native_window/icon:"+QString::number(winid)+":"+QString::number(icodmg);
}

//QML Button states
bool NativeWindowObject::showCloseButton(){
  QList<NativeWindowObject::Type> types = this->property(NativeWindowObject::WinTypes).value<QList < NativeWindowObject::Type> >();
  QList<NativeWindowObject::Type> badtypes;
  badtypes << NativeWindowObject::T_DESKTOP << NativeWindowObject::T_TOOLBAR << NativeWindowObject::T_MENU \
	<< NativeWindowObject::T_SPLASH << NativeWindowObject::T_DROPDOWN_MENU << NativeWindowObject::T_POPUP_MENU \
	<< NativeWindowObject::T_NOTIFICATION << NativeWindowObject::T_COMBO << NativeWindowObject::T_DND;
  for(int i=0; i<types.length(); i++){
    if(badtypes.contains(types[i])){ return false; }
  }
  return true;
}

bool NativeWindowObject::showMaxButton(){
  QList<NativeWindowObject::Type> types = this->property(NativeWindowObject::WinTypes).value<QList < NativeWindowObject::Type> >();
  QList<NativeWindowObject::Type> badtypes;
  badtypes << NativeWindowObject::T_DESKTOP << NativeWindowObject::T_TOOLBAR << NativeWindowObject::T_MENU \
	<< NativeWindowObject::T_SPLASH << NativeWindowObject::T_DROPDOWN_MENU << NativeWindowObject::T_POPUP_MENU \
	<< NativeWindowObject::T_NOTIFICATION << NativeWindowObject::T_COMBO << NativeWindowObject::T_DND;
  for(int i=0; i<types.length(); i++){
    if(badtypes.contains(types[i])){ return false; }
  }
  return true;
}

bool NativeWindowObject::showMinButton(){
  QList<NativeWindowObject::Type> types = this->property(NativeWindowObject::WinTypes).value<QList < NativeWindowObject::Type> >();
  QList<NativeWindowObject::Type> badtypes;
  badtypes << NativeWindowObject::T_DESKTOP << NativeWindowObject::T_TOOLBAR << NativeWindowObject::T_MENU \
	<< NativeWindowObject::T_SPLASH << NativeWindowObject::T_DROPDOWN_MENU << NativeWindowObject::T_POPUP_MENU \
	<< NativeWindowObject::T_NOTIFICATION << NativeWindowObject::T_COMBO << NativeWindowObject::T_DND << NativeWindowObject::T_DIALOG;
  for(int i=0; i<types.length(); i++){
    if(badtypes.contains(types[i])){ return false; }
  }
  return true;
}

bool NativeWindowObject::showTitlebar(){
  QList<NativeWindowObject::Type> types = this->property(NativeWindowObject::WinTypes).value<QList < NativeWindowObject::Type> >();
  QList<NativeWindowObject::Type> badtypes;
  badtypes << NativeWindowObject::T_DESKTOP << NativeWindowObject::T_TOOLBAR << NativeWindowObject::T_MENU \
	<< NativeWindowObject::T_SPLASH << NativeWindowObject::T_DROPDOWN_MENU << NativeWindowObject::T_POPUP_MENU \
	<< NativeWindowObject::T_NOTIFICATION << NativeWindowObject::T_COMBO << NativeWindowObject::T_DND;
  for(int i=0; i<types.length(); i++){
    if(badtypes.contains(types[i])){ return false; }
  }
  return true;
}

bool NativeWindowObject::showGenericButton(){
  QList<NativeWindowObject::Type> types = this->property(NativeWindowObject::WinTypes).value<QList < NativeWindowObject::Type> >();
  QList<NativeWindowObject::Type> badtypes;
  badtypes << NativeWindowObject::T_DESKTOP << NativeWindowObject::T_TOOLBAR << NativeWindowObject::T_MENU \
	<< NativeWindowObject::T_SPLASH << NativeWindowObject::T_DROPDOWN_MENU << NativeWindowObject::T_POPUP_MENU \
	<< NativeWindowObject::T_NOTIFICATION << NativeWindowObject::T_COMBO << NativeWindowObject::T_DND;
  for(int i=0; i<types.length(); i++){
    if(badtypes.contains(types[i])){ return false; }
  }
  return true;
}

bool NativeWindowObject::showWindowFrame(){
  QList<NativeWindowObject::Type> types = this->property(NativeWindowObject::WinTypes).value<QList < NativeWindowObject::Type> >();
  QList<NativeWindowObject::Type> badtypes;
  badtypes << NativeWindowObject::T_DESKTOP << NativeWindowObject::T_TOOLBAR << NativeWindowObject::T_MENU \
	<< NativeWindowObject::T_SPLASH << NativeWindowObject::T_DROPDOWN_MENU << NativeWindowObject::T_POPUP_MENU \
	<< NativeWindowObject::T_NOTIFICATION << NativeWindowObject::T_COMBO << NativeWindowObject::T_DND;
  for(int i=0; i<types.length(); i++){
    if(badtypes.contains(types[i])){ return false; }
  }
  return true;
}

//QML Window States
bool NativeWindowObject::isSticky(){
  return (this->property(NativeWindowObject::Workspace).toInt()<0 || this->property(NativeWindowObject::States).value<QList<NativeWindowObject::State> >().contains(NativeWindowObject::S_STICKY) );
}

int NativeWindowObject::workspace(){
  return this->property(NativeWindowObject::Workspace).toInt();
}

//QML Geometry reporting
QRect NativeWindowObject::frameGeometry(){
  return geometry();
}

QRect NativeWindowObject::imageGeometry(){
  QRect geom( this->property(NativeWindowObject::GlobalPos).toPoint(), this->property(NativeWindowObject::Size).toSize() );
  return geom;
}

void NativeWindowObject::updateGeometry(int x, int y, int width, int height, bool now){
  // Full frame+window geometry - go ahead and pull it apart and only update the interior window geom
  QList<int> fgeom = this->property(NativeWindowObject::FrameExtents).value<QList<int> >();
  if(fgeom.isEmpty()){ fgeom << 0<<0<<0<<0; } //just in case (left/right/top/bottom)
  QPoint pos(x+fgeom[0], y+fgeom[2]);
  QSize sz(width-fgeom[0]-fgeom[1], height-fgeom[2]-fgeom[3]);
  if(!now){
    newgeom = QRect(pos, sz);
    //qDebug() << "Update Geometry:" << fgeom << QRect(x,y,width,height) << pos << sz;
    //requestProperties(QList<NativeWindowObject::Property>() << NativeWindowObject::GlobalPos << NativeWindowObject::Size, QList<QVariant>() << pos << sz);
    if(!geomTimer->isActive()){ geomTimer->start(); }
  }else{
   requestProperties(QList<NativeWindowObject::Property>() << NativeWindowObject::GlobalPos << NativeWindowObject::Size , QList<QVariant>() << pos << sz );
   setProperties(QList<NativeWindowObject::Property>() << NativeWindowObject::GlobalPos << NativeWindowObject::Size , QList<QVariant>() << pos << sz );
  }
}

// ==== PUBLIC SLOTS ===
void NativeWindowObject::toggleVisibility(){
  setProperty(NativeWindowObject::Visible, !property(NativeWindowObject::Visible).toBool() );
}

void NativeWindowObject::requestClose(){
  emit RequestClose(winid);
}

void NativeWindowObject::requestKill(){
  emit RequestKill(winid);
}

void NativeWindowObject::requestPing(){
  emit RequestPing(winid);
}

// ==== PRIVATE ====
void NativeWindowObject::emitSinglePropChanged(NativeWindowObject::Property prop){
  //Simple switch to emit the QML-usable signals as properties are changed
  switch(prop){
	case NativeWindowObject::Name:
		emit nameChanged(); break;
	case NativeWindowObject::Title:
		emit titleChanged();
		if(this->property(NativeWindowObject::ShortTitle).toString().isEmpty()){ emit shortTitleChanged(); }
		break;
	case NativeWindowObject::ShortTitle:
		emit shortTitleChanged(); break;
	case NativeWindowObject::Icon:
		emit iconChanged(); break;
	case NativeWindowObject::Workspace:
	case NativeWindowObject::States:
		emit stickyChanged(); break;
	case NativeWindowObject::WinImage:
		emit winImageChanged(); break;
	case NativeWindowObject::WinTypes:
		emit winTypeChanged(); break;
	default:
		break; //do nothing otherwise
  }
}

void NativeWindowObject::sendNewGeom(){
  QList<NativeWindowObject::Property> props; props << NativeWindowObject::GlobalPos << NativeWindowObject::Size;
  QList<QVariant> vals; vals << newgeom.topLeft() << newgeom.size();
  requestProperties(props, vals);
  setProperties(props,vals);
}
bgstack15