Unity 8
WindowStateSaver.qml
1 /*
2  * Copyright (C) 2016 Canonical, Ltd.
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; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 import Ubuntu.Components 1.3
19 import Utils 0.1
20 
21 QtObject {
22  id: root
23 
24  // set from outside
25  property var target
26  property int screenWidth: 0
27  property int screenHeight: 0
28  property int leftMargin: 0
29  property int minimumY: 0
30 
31  function load() {
32  var defaultWidth = units.gu(60);
33  var defaultHeight = units.gu(50);
34  var windowGeometry = WindowStateStorage.getGeometry(target.appId,
35  Qt.rect(target.windowedX, target.windowedY, defaultWidth, defaultHeight));
36 
37  target.windowedWidth = Qt.binding(function() { return Math.min(Math.max(windowGeometry.width, target.minimumWidth), screenWidth - root.leftMargin); });
38  target.windowedHeight = Qt.binding(function() { return Math.min(Math.max(windowGeometry.height, target.minimumHeight),
39  screenHeight - (target.fullscreen ? 0 : minimumY)); });
40  target.windowedX = Qt.binding(function() { return Math.max(Math.min(windowGeometry.x, screenWidth - root.leftMargin - target.windowedWidth),
41  (target.fullscreen ? 0 : root.leftMargin)); });
42  target.windowedY = Qt.binding(function() { return Math.max(Math.min(windowGeometry.y, screenHeight - target.windowedHeight), minimumY); });
43 
44  var windowState = WindowStateStorage.getState(target.appId, WindowStateStorage.WindowStateNormal)
45  target.restore(false /* animated */, windowState);
46 
47  target.updateNormalGeometry();
48 
49  // initialize the x/y to restore to
50  target.restoredX = target.normalX;
51  target.restoredY = target.normalY;
52  }
53 
54  function save() {
55  var state = target.windowState;
56  if (state === WindowStateStorage.WindowStateRestored) {
57  state = WindowStateStorage.WindowStateNormal;
58  }
59 
60  WindowStateStorage.saveState(target.appId, state & ~WindowStateStorage.WindowStateMinimized); // clear the minimized bit when saving
61  WindowStateStorage.saveGeometry(target.appId, Qt.rect(target.normalX, target.normalY, target.normalWidth, target.normalHeight));
62  }
63 }