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.15
18import QtMir.Application 0.1
19import Lomiri.Components 1.3
20import Utils 0.1
21
22QtObject {
23 id: root
24
25 // set from outside
26 property var target
27 property int screenWidth: 0
28 property int screenHeight: 0
29 property int leftMargin: 0
30 property int minimumY: 0
31
32 property int loadedState
33
34 function load(constrain) {
35 var defaultWidth = units.gu(60);
36 var defaultHeight = units.gu(50);
37 var windowGeometry = WindowStateStorage.getGeometry(target.appId,
38 Qt.rect(target.windowedX, target.windowedY, defaultWidth, defaultHeight));
39
40 if (constrain) {
41 target.windowedWidth = Qt.binding(function() { return Math.min(Math.max(windowGeometry.width, target.minimumWidth), screenWidth - root.leftMargin); });
42 target.windowedHeight = Qt.binding(function() { return Math.min(Math.max(windowGeometry.height, target.minimumHeight),
43 screenHeight - (target.fullscreen ? 0 : minimumY)); });
44 target.windowedX = Qt.binding(function() { return Math.max(Math.min(windowGeometry.x, screenWidth - root.leftMargin - target.windowedWidth),
45 (target.fullscreen ? 0 : root.leftMargin)); });
46 target.windowedY = Qt.binding(function() { return Math.max(Math.min(windowGeometry.y, screenHeight - target.windowedHeight), minimumY); });
47 } else {
48 target.windowedWidth = windowGeometry.width;
49 target.windowedHeight = windowGeometry.height;
50 target.windowedX = windowGeometry.x;
51 target.windowedY = windowGeometry.y;
52 }
53
54 target.normalWidth = target.windowedWidth;
55 target.normalHeight = target.windowedHeight;
56 target.normalX = target.windowedX;
57 target.normalY = target.windowedY;
58
59 // initialize the x/y to restore to
60 target.restoredX = target.normalX;
61 target.restoredY = target.normalY;
62
63 loadedState = WindowStateStorage.getState(target.appId, Mir.RestoredState);
64 }
65
66 function save() {
67 var state = target.windowState;
68 if (state === Mir.MinimizedState)
69 state = target.prevWindowState;
70
71 WindowStateStorage.saveState(target.appId, state);
72 WindowStateStorage.saveGeometry(target.appId, Qt.rect(target.normalX, target.normalY, target.normalWidth, target.normalHeight));
73 }
74}