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.12
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  property int loadedState
32 
33  function load() {
34  var defaultWidth = units.gu(60);
35  var defaultHeight = units.gu(50);
36  var windowGeometry = WindowStateStorage.getGeometry(target.appId,
37  Qt.rect(target.windowedX, target.windowedY, defaultWidth, defaultHeight));
38 
39  target.windowedWidth = Qt.binding(function() { return Math.min(Math.max(windowGeometry.width, target.minimumWidth), screenWidth - root.leftMargin); });
40  target.windowedHeight = Qt.binding(function() { return Math.min(Math.max(windowGeometry.height, target.minimumHeight),
41  screenHeight - (target.fullscreen ? 0 : minimumY)); });
42  target.windowedX = Qt.binding(function() { return Math.max(Math.min(windowGeometry.x, screenWidth - root.leftMargin - target.windowedWidth),
43  (target.fullscreen ? 0 : root.leftMargin)); });
44  target.windowedY = Qt.binding(function() { return Math.max(Math.min(windowGeometry.y, screenHeight - target.windowedHeight), minimumY); });
45 
46  target.updateNormalGeometry();
47 
48  // initialize the x/y to restore to
49  target.restoredX = target.normalX;
50  target.restoredY = target.normalY;
51 
52  loadedState = WindowStateStorage.getState(target.appId, WindowStateStorage.WindowStateNormal);
53  }
54 
55  function save() {
56  var state = target.windowState;
57  if (state === WindowStateStorage.WindowStateRestored) {
58  state = WindowStateStorage.WindowStateNormal;
59  }
60 
61  WindowStateStorage.saveState(target.appId, state & ~WindowStateStorage.WindowStateMinimized); // clear the minimized bit when saving
62  WindowStateStorage.saveGeometry(target.appId, Qt.rect(target.normalX, target.normalY, target.normalWidth, target.normalHeight));
63  }
64 }