| 1 |
/* |
| 2 |
* Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras) |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
public class DesktopFolder.ResolutionSettings : Object { |
| 19 |
public int resx { get; set; } |
| 20 |
public int resy { get; set; } |
| 21 |
public int x { get; set; } |
| 22 |
public int y { get; set; } |
| 23 |
public int w { get; set; } |
| 24 |
public int h { get; set; } |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* @name PositionSettings |
| 30 |
* @description abstract class to store a position x,y and a dimension width, height for a set of resolutions |
| 31 |
*/ |
| 32 |
public abstract class DesktopFolder.PositionSettings : Object, Json.Serializable { |
| 33 |
public bool flagChanged = false; |
| 34 |
|
| 35 |
/** the internal list of resolutions stored for this object */ |
| 36 |
public SList <ResolutionSettings> _resolutions = new SList <ResolutionSettings>(); |
| 37 |
// temporal WEIRD hack to avoid the problem of set an SLIST to the json @value deserializer |
| 38 |
private bool flag_deserialized = false; |
| 39 |
|
| 40 |
private enum ResolutionStrategy { |
| 41 |
NONE, |
| 42 |
SCALE, |
| 43 |
STORE |
| 44 |
} |
| 45 |
|
| 46 |
// positions and dimensions for certain resolutions |
| 47 |
public SList <ResolutionSettings> resolutions { |
| 48 |
get { |
| 49 |
return this._resolutions; |
| 50 |
} |
| 51 |
set { |
| 52 |
if (flag_deserialized) { |
| 53 |
// temporal WEIRD hack to avoid the problem of set an SLIST to the json @value deserializer |
| 54 |
flag_deserialized = false; |
| 55 |
} else { |
| 56 |
this._resolutions = value.copy_deep ((CopyFunc) Object.ref); |
| 57 |
} |
| 58 |
flagChanged = false; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// current working position and dimensions |
| 63 |
private int _resx; |
| 64 |
public int resx { |
| 65 |
get { |
| 66 |
return _resx; |
| 67 |
} |
| 68 |
set { |
| 69 |
if (_resx != value) { |
| 70 |
_resx = value; flagChanged = true; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
private int _resy; |
| 75 |
public int resy { |
| 76 |
get { |
| 77 |
return _resy; |
| 78 |
} |
| 79 |
set { |
| 80 |
if (_resy != value) { |
| 81 |
_resy = value; flagChanged = true; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
private int _x = 0; |
| 86 |
public int x { |
| 87 |
get { |
| 88 |
return _x; |
| 89 |
} |
| 90 |
set { |
| 91 |
if (_x != value) { |
| 92 |
_x = value; flagChanged = true; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
private int _y = 0; |
| 97 |
public int y { |
| 98 |
get { |
| 99 |
return _y; |
| 100 |
} |
| 101 |
set { |
| 102 |
if (_y != value) { |
| 103 |
_y = value; flagChanged = true; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
private int _w = 0; |
| 108 |
public int w { |
| 109 |
get { |
| 110 |
return _w; |
| 111 |
} |
| 112 |
set { |
| 113 |
if (_w != value) { |
| 114 |
_w = value; flagChanged = true; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
private int _h = 0; |
| 119 |
public int h { |
| 120 |
get { |
| 121 |
return _h; |
| 122 |
} |
| 123 |
set { |
| 124 |
if (_h != value) { |
| 125 |
_h = value; flagChanged = true; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* @name store_resolution_position |
| 133 |
* @description store the current position with the corresponding resolution |
| 134 |
*/ |
| 135 |
protected void store_resolution_position () { |
| 136 |
if (this.resx <= 0 || this.resy <= 0) { |
| 137 |
ResolutionSettings rs = calculate_screen_resolutions (); |
| 138 |
int swidth = rs.resx; |
| 139 |
int sheight = rs.resy; |
| 140 |
this.resx = swidth; |
| 141 |
this.resy = sheight; |
| 142 |
} |
| 143 |
|
| 144 |
// getting the current screen resolution |
| 145 |
ResolutionSettings resolution = this.create_current_resolution (); |
| 146 |
resolution.x = this.x; |
| 147 |
resolution.y = this.y; |
| 148 |
resolution.w = this.w; |
| 149 |
resolution.h = this.h; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @name get_resolution_strategy_setting |
| 154 |
* @description return the global setting resolution-strategy |
| 155 |
*/ |
| 156 |
private ResolutionStrategy get_resolution_strategy_setting () { |
| 157 |
GLib.Settings settings = new GLib.Settings ("com.github.spheras.desktopfolder"); |
| 158 |
string[] keys = settings.list_keys (); |
| 159 |
bool found = false; |
| 160 |
for (int i = 0; i < keys.length; i++) { |
| 161 |
string key = keys[i]; |
| 162 |
if (key == "resolution-strategy") { |
| 163 |
found = true; |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
ResolutionStrategy resolution_strategy = ResolutionStrategy.STORE; |
| 168 |
if (found) { |
| 169 |
resolution_strategy = (ResolutionStrategy) settings.get_enum ("resolution-strategy"); |
| 170 |
} |
| 171 |
return resolution_strategy; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @name calculate_current_position |
| 176 |
* @description set the current position and size based on the strategy setting |
| 177 |
*/ |
| 178 |
public void calculate_current_position () { |
| 179 |
// debug ("calculate_current_position"); |
| 180 |
if (this._resolutions == null) { |
| 181 |
// debug ("null resolutions!"); |
| 182 |
this._resolutions = new SList <ResolutionSettings>(); |
| 183 |
} |
| 184 |
ResolutionStrategy strategy = this.get_resolution_strategy_setting (); |
| 185 |
switch (strategy) { |
| 186 |
case ResolutionStrategy.STORE: |
| 187 |
// debug ("strategy store"); |
| 188 |
this.strategy_store (); |
| 189 |
// debug("check_off_screen"); |
| 190 |
check_off_screen (); |
| 191 |
break; |
| 192 |
case ResolutionStrategy.SCALE: |
| 193 |
// debug ("strategy scale"); |
| 194 |
this.strategy_scale (this.resx, this.resy); |
| 195 |
// debug("check_off_screen"); |
| 196 |
check_off_screen (); |
| 197 |
break; |
| 198 |
default: |
| 199 |
case ResolutionStrategy.NONE: |
| 200 |
// nothing todo |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @name check_off_screen |
| 207 |
* @description check whether the new position is inside the monitor rectangle or not, and fix the position |
| 208 |
*/ |
| 209 |
protected void check_off_screen () { |
| 210 |
Gdk.Screen screen = Gdk.Screen.get_default (); |
| 211 |
Gdk.Monitor monitor = screen.get_display ().get_monitor_at_point (this.x, this.y); |
| 212 |
Gdk.Rectangle monitor_rect = monitor.get_geometry (); |
| 213 |
|
| 214 |
Gdk.Rectangle widget_rect = Gdk.Rectangle (); |
| 215 |
widget_rect.x = this.x; |
| 216 |
widget_rect.y = this.y; |
| 217 |
widget_rect.width = this.w; |
| 218 |
widget_rect.height = this.h; |
| 219 |
|
| 220 |
Gdk.Rectangle intersect_rect; |
| 221 |
|
| 222 |
bool intersect = monitor_rect.intersect (widget_rect, out intersect_rect); |
| 223 |
// debug(" intersection? %s, (%d, %d) - (%d,%d)",(intersect?"true":"false"),intersect_rect.width,intersect_rect.height,this.w,this.h); |
| 224 |
if (!intersect || intersect_rect.width < this.w || intersect_rect.height < this.h) { |
| 225 |
// debug("¡¡CAUTION - NO INTERSECTION (%d,%d,%d,%d)",intersect_rect.x,intersect_rect.y,intersect_rect.width,intersect_rect.height); |
| 226 |
if (this.y < monitor_rect.y) { |
| 227 |
var wingpanel_height_security = 50; |
| 228 |
this.y = monitor_rect.y + wingpanel_height_security; |
| 229 |
} else if (this.y > monitor_rect.y + monitor_rect.height) { |
| 230 |
this.y = monitor_rect.y + monitor_rect.height - this.h; |
| 231 |
} |
| 232 |
|
| 233 |
if (this.x < monitor_rect.x) { |
| 234 |
this.x = monitor_rect.x; |
| 235 |
} else if (this.x > monitor_rect.x + monitor_rect.width) { |
| 236 |
this.x = monitor_rect.x + monitor_rect.width - this.w; |
| 237 |
} |
| 238 |
|
| 239 |
// debug("MOVED TO-> (x:%d,y:%d,w:%d,h:%d)",this.x,this.y,this.w,this.h); |
| 240 |
} else { |
| 241 |
// debug("OK - INTERSCIONAN (%d,%d,%d,%d)",intersect_rect.x,intersect_rect.y,intersect_rect.width,intersect_rect.height); |
| 242 |
// debug("widget(%d,%d,%d,%d) - monitor(%d,%d,%d,%d)",widget_rect.x,widget_rect.y,this.w,this.h,monitor_rect.x,monitor_rect.y,monitor_rect.width,monitor_rect.height); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* @name strategy_store |
| 248 |
* @description the positions are all stored for the concrete resolution, if it is stored, it is restaured |
| 249 |
*/ |
| 250 |
private void strategy_store () { |
| 251 |
ResolutionSettings rs = find_current_resolution (); |
| 252 |
if (rs != null) { |
| 253 |
// debug("setting a store resolution"); |
| 254 |
this.x = rs.x; |
| 255 |
this.y = rs.y; |
| 256 |
this.w = rs.w; |
| 257 |
this.h = rs.h; |
| 258 |
this.resx = rs.resx; |
| 259 |
this.resy = rs.resy; |
| 260 |
// debug ("strategy_store:(%d,%d,%d,%d)", this.x, this.y, this.w, this.h); |
| 261 |
} else { |
| 262 |
// debug ("strategy store: no resolution, lets scale"); |
| 263 |
// should we resize?? not sure, normally I just add a monitor, don't need to resize, just to position it correctly |
| 264 |
// this.strategy_scale (oldresx, oldresy); |
| 265 |
this.create_current_resolution (); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* @name strategy_scale |
| 271 |
* @description the positions are scaled to the new screen resolution |
| 272 |
*/ |
| 273 |
private void strategy_scale (int oldresx, int oldresy) { |
| 274 |
ResolutionSettings rs = calculate_screen_resolutions (); |
| 275 |
int swidth = rs.resx; |
| 276 |
int sheight = rs.resy; |
| 277 |
|
| 278 |
if (this.resx > 0 && this.resy > 0) { |
| 279 |
// debug("1-strategy_scale:(%d,%d,%d,%d)",this.x,this.y,this.w,this.h); |
| 280 |
this.x = (this.x * swidth) / oldresx; |
| 281 |
this.w = (this.w * swidth) / oldresx; |
| 282 |
this.y = (this.y * sheight) / oldresy; |
| 283 |
this.h = (this.h * sheight) / oldresy; |
| 284 |
this.resx = swidth; |
| 285 |
this.resy = sheight; |
| 286 |
// debug("2-strategy_scale:(%d,%d,%d,%d)",this.x,this.y,this.w,this.h); |
| 287 |
// debug ("strategy scale: %d,%d - %d,%d", this.x, this.y, this.w, this.h); |
| 288 |
} else { |
| 289 |
// debug ("strategy scale: we don't know the resolution, default position"); |
| 290 |
this.resx = swidth; |
| 291 |
this.resy = sheight; |
| 292 |
// nothing to do |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* @name calcualte_screen_resolutions |
| 298 |
* @description calculate the screen resolution taking into account all the monitors |
| 299 |
* @return {ResolutionSettings} the resolution calculated (resx and resy) |
| 300 |
*/ |
| 301 |
private ResolutionSettings calculate_screen_resolutions () { |
| 302 |
// this was the old -deprecated- way gtk (since Gtk+2.2) to obtain that. |
| 303 |
// Gdk.Screen screen = Gdk.Screen.get_default (); |
| 304 |
// int swidth = screen.get_width (); |
| 305 |
// int sheight = screen.get_height (); |
| 306 |
|
| 307 |
var result = new ResolutionSettings (); |
| 308 |
Gdk.Rectangle boundingbox = DesktopFolder.Util.get_desktop_bounding_box (); |
| 309 |
result.resx = boundingbox.width; |
| 310 |
result.resy = boundingbox.height; |
| 311 |
// debug("calculating: %d,%d",result.resx,result.resy); |
| 312 |
return result; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* @name create_current_resolution |
| 317 |
* @description find and create if necessary the current resolution |
| 318 |
* @return {ResolutionSettings} the current screen resolution |
| 319 |
*/ |
| 320 |
public ResolutionSettings create_current_resolution () { |
| 321 |
ResolutionSettings current = this.find_current_resolution (); |
| 322 |
if (current == null) { |
| 323 |
// debug ("create_current_resolution.. not current"); |
| 324 |
ResolutionSettings rs = calculate_screen_resolutions (); |
| 325 |
int swidth = rs.resx; |
| 326 |
int sheight = rs.resy; |
| 327 |
|
| 328 |
current = new ResolutionSettings (); |
| 329 |
current.resx = swidth; |
| 330 |
current.resy = sheight; |
| 331 |
current.x = 0; |
| 332 |
current.y = 0; |
| 333 |
current.w = 0; |
| 334 |
current.h = 0; |
| 335 |
this._resolutions.append (current); |
| 336 |
} |
| 337 |
return current; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* @name find_current_resolution |
| 342 |
* @description find the current screen resolution, null if nothing found |
| 343 |
* @return {ResolutionSettings} the resolution found, null if none |
| 344 |
*/ |
| 345 |
public ResolutionSettings find_current_resolution () { |
| 346 |
ResolutionSettings rs = calculate_screen_resolutions (); |
| 347 |
int swidth = rs.resx; |
| 348 |
int sheight = rs.resy; |
| 349 |
this.resx = swidth; |
| 350 |
this.resy = sheight; |
| 351 |
|
| 352 |
// we got here the full resolution (big bounding box for all the monitors) |
| 353 |
// debug("find_current_resolution: %d,%d",swidth,sheight); |
| 354 |
|
| 355 |
return this.find_resolution (swidth, sheight); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* @name find_resolution |
| 360 |
* @description find a certain resolution, null if nothing found |
| 361 |
* @return {ResolutionSettings} the resolution found, null if none |
| 362 |
*/ |
| 363 |
public ResolutionSettings find_resolution (int width, int height) { |
| 364 |
// debug ("find resolution(%d,%d)", width, height); |
| 365 |
for (int i = 0; i < this._resolutions.length (); i++) { |
| 366 |
ResolutionSettings found = this._resolutions.nth_data (i); |
| 367 |
// debug ("found i=%d (%d,%d)", i, found.resx, found.resy); |
| 368 |
if (found.resx == width && found.resy == height) { |
| 369 |
// debug ("found!"); |
| 370 |
return found; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
// debug ("not found!"); |
| 375 |
// not found |
| 376 |
return null as ResolutionSettings; |
| 377 |
} |
| 378 |
|
| 379 |
// Serializable interface |
| 380 |
|
| 381 |
public unowned ParamSpec ? find_property (string name) |
| 382 |
{ |
| 383 |
GLib.Type type = this.get_type (); |
| 384 |
GLib.ObjectClass ocl = (GLib.ObjectClass)type.class_ref (); |
| 385 |
unowned GLib.ParamSpec ? spec = ocl.find_property (name); |
| 386 |
return spec; |
| 387 |
} |
| 388 |
public GLib.Value Json.Serializable.get_property (GLib.ParamSpec pspec) { |
| 389 |
GLib.Value result = GLib.Value (pspec.value_type); |
| 390 |
base.get_property (pspec.name, ref result); |
| 391 |
return result; |
| 392 |
} |
| 393 |
|
| 394 |
public void Json.Serializable.set_property (GLib.ParamSpec pspec, GLib.Value value) { |
| 395 |
base.set_property (pspec.name, value); |
| 396 |
} |
| 397 |
|
| 398 |
public Json.Node serialize_property (string property_name, Value @value, ParamSpec pspec) { |
| 399 |
// debug("SERIALIZE -- property_name:%s",property_name); |
| 400 |
if (property_name == "resolutions") { |
| 401 |
var array = new Json.Array.sized (this._resolutions.length ()); |
| 402 |
for (int i = 0; i < this._resolutions.length (); i++) { |
| 403 |
ResolutionSettings resolution = this._resolutions.nth_data (i); |
| 404 |
array.add_element (Json.gobject_serialize (resolution)); |
| 405 |
} |
| 406 |
var node = new Json.Node (Json.NodeType.ARRAY); |
| 407 |
node.set_array (array); |
| 408 |
return node; |
| 409 |
} |
| 410 |
|
| 411 |
Json.Node result = default_serialize_property (property_name, @value, pspec); |
| 412 |
return result; |
| 413 |
} |
| 414 |
|
| 415 |
public bool deserialize_property (string property_name, out Value @value, ParamSpec pspec, Json.Node property_node) { |
| 416 |
switch (property_name) { |
| 417 |
case "resolutions": |
| 418 |
this._resolutions = new SList <ResolutionSettings>(); |
| 419 |
var array = property_node.get_array (); |
| 420 |
array.foreach_element ((a, i, n) => { |
| 421 |
this._resolutions.append (Json.gobject_deserialize (typeof (ResolutionSettings), n) as ResolutionSettings); |
| 422 |
}); |
| 423 |
GLib.Type type = typeof (SList); |
| 424 |
@value = Value (type); |
| 425 |
this.flag_deserialized = true; |
| 426 |
// TODO - Help, how the hell we set an SLIST!!! |
| 427 |
// @value.set_boxed(this._resolutions); |
| 428 |
return true; |
| 429 |
} |
| 430 |
|
| 431 |
if (pspec.value_type == typeof (bool)) { |
| 432 |
@value = Value (typeof (bool)); |
| 433 |
@value.set_boolean ((bool) property_node.get_boolean ()); |
| 434 |
return true; |
| 435 |
} |
| 436 |
|
| 437 |
if (pspec.value_type == typeof (int)) { |
| 438 |
@value = Value (typeof (int)); |
| 439 |
@value.set_int ((int) property_node.get_int ()); |
| 440 |
return true; |
| 441 |
} |
| 442 |
|
| 443 |
if (pspec.value_type == typeof (string)) { |
| 444 |
@value = Value (typeof (string)); |
| 445 |
@value.set_string (property_node.get_string ()); |
| 446 |
return true; |
| 447 |
} |
| 448 |
|
| 449 |
if (pspec.value_type == typeof (string[])) { |
| 450 |
@value = Value (typeof (string[])); |
| 451 |
string[] result = {}; |
| 452 |
var array = property_node.get_array (); |
| 453 |
array.foreach_element ((a, i, n) => { |
| 454 |
result += n.get_string (); |
| 455 |
}); |
| 456 |
|
| 457 |
@value = Value (typeof (string[])); |
| 458 |
@value.set_boxed (result); |
| 459 |
return true; |
| 460 |
} |
| 461 |
|
| 462 |
debug ("WARNING! NOT DESERIALIZED!! %s:%s", pspec.get_name (), pspec.value_type.name ()); |
| 463 |
return default_deserialize_property (property_name, out @value, pspec, property_node); |
| 464 |
} |
| 465 |
|
| 466 |
} |