Lomiri
Loading...
Searching...
No Matches
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
17import QtQuick 2.12
18import Lomiri.Components 1.3
19import Utils 0.1
20
21QtObject {
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.normalWidth = target.windowedWidth;
47 target.normalHeight = target.windowedHeight;
48 target.normalX = target.windowedX;
49 target.normalY = target.windowedY;
50
51 // initialize the x/y to restore to
52 target.restoredX = target.normalX;
53 target.restoredY = target.normalY;
54
55 loadedState = WindowStateStorage.getState(target.appId, WindowStateStorage.WindowStateNormal);
56 }
57
58 function save() {
59 var state = target.windowState;
60 if (state === WindowStateStorage.WindowStateRestored) {
61 state = WindowStateStorage.WindowStateNormal;
62 }
63
64 WindowStateStorage.saveState(target.appId, state & ~WindowStateStorage.WindowStateMinimized); // clear the minimized bit when saving
65 WindowStateStorage.saveGeometry(target.appId, Qt.rect(target.normalX, target.normalY, target.normalWidth, target.normalHeight));
66 }
67}