forked from ChWick/gnomesome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.js
277 lines (264 loc) · 10.3 KB
/
layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const Lang = imports.lang;
const GObject = imports.gi.GObject;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const SplitLayout = Me.imports.splitlayout;
const FloatLayout = Me.imports.floatlayout;
const MaximizeLayout = Me.imports.maximizelayout;
const Utils = Me.imports.utils;
const logging = Me.imports.logging;
const logger = logging.getLogger('Gnomesome.Layout');
const Gettext = imports.gettext.domain('gnomesome');
const _ = Gettext.gettext;
var Modes = {
FLOATING: 0,
VBOXLAYOUT: 1,
HBOXLAYOUT: 2,
MAXIMIZED: 3,
properties: {
0: {
value: 0, name: "floating", display: _("Floating"),
enterLayout: FloatLayout.enterFloatingLayout,
exitLayout: FloatLayout.exitFloatingLayout,
layout: FloatLayout.updateFloatingLayout,
icon: "icons/status/gnomesome-window-tile-floating-symbolic.svg",
},
1: {
value: 1, name: "vertical", display: _("Vertical"),
enterLayout: SplitLayout.enterVBoxLayout,
exitLayout: SplitLayout.exitVBoxLayout,
layout: SplitLayout.applyVBoxLayout,
icon: "icons/status/gnomesome-window-tile-vertical-symbolic.svg",
},
2: {
value: 2, name: "horizontal", display: _("Horizontal"),
enterLayout: SplitLayout.enterHBoxLayout,
exitLayout: SplitLayout.exitHBoxLayout,
layout: SplitLayout.applyHBoxLayout,
icon: "icons/status/gnomesome-window-tile-horizontal-symbolic.svg",
},
3: {
value: 3, name: "maximized", display: _("Maximized"),
enterLayout: MaximizeLayout.enterMaximizeLayout,
exitLayout: MaximizeLayout.exitMaximizeLayout,
layout: MaximizeLayout.updateMaximizeLayout,
icon: "icons/status/gnomesome-window-tile-full-symbolic.svg",
},
},
byName: function(name) {
for (var key in Modes.properties) {
if (Modes.properties.hasOwnProperty(key)) {
if (name == Modes.properties[key].name) {
return key;
}
}
}
logger.warn("Error layout with label " + name + " not found");
return -1;
},
};
const NumModes = Object.keys(Modes.properties).length;
var Layout = new GObject.Class({
Name: 'Gnomesome.Layout',
GTypeName: 'GnomesomeLayout',
Properties: {
'mode': GObject.ParamSpec.int('mode', 'ModeProperty', 'Mode property', GObject.ParamFlags.READWRITE, 0, NumModes - 1, Modes.FLOATING),
'split-pos': GObject.ParamSpec.float('split-pos', "Split position", "Position of the split", GObject.ParamFlags.READWRITE, 0.2, 0.8, 0.5),
'n-master': GObject.ParamSpec.int('n-master', "Number of master", "Number of master windows", GObject.ParamFlags.READWRITE, 0, 5, 1),
},
Signals: {
},
_init: function(prefs) {
this.parent();
this.gswindows = [];
this._mode = Modes.FLOATING;
this._split_pos = 0.5;
this._n_master = 1;
// this.connect('notify::mode', Lang.bind(this, function () {this.relayout();})); // handled in layout_changed(from, to)
this.connect('notify::split-pos', Lang.bind(this, function () {this.relayout();}));
this.connect('notify::n-master', Lang.bind(this, function () {this.relayout();}));
// monitor prefs
let update = Lang.bind(this, function() {
let name = prefs.DEFAULT_LAYOUT.get();
logger.info("Updating default layout to " + name);
let new_layout = Modes.byName(name);
if (this._initial) {
this.mode = new_layout;
// keep this as initial!
this._initial = true;
}
});
Utils.connect_and_track(this, prefs.DEFAULT_LAYOUT.gsettings, "changed::" + prefs.DEFAULT_LAYOUT.key, update);
// setup to initial layout
this._initial = true;
update();
},
destroy: function() {
logger.info("Cleaning up layout.");
for (let gswindow of Object.values(this.gswindows)) {
gswindow.destroy();
}
this.gswindows = [];
Utils.disconnect_tracked_signals(this);
},
get mode() {return this._mode;},
set mode(mode) { if (this._mode != mode) { this.layout_changed(this._mode, mode); this._mode = mode; this.notify("mode"); } this._initial = false;},
//set mode(mode) { if (this._mode != mode) { this._mode = mode; this.notify("mode"); } },
get split_pos() {return this._split_pos;},
set split_pos(pos) {pos = Math.max(Math.min(pos, 0.8), 0.2); if (this._split_pos != pos) {this._split_pos = pos; this.notify("split-pos");}},
resize_master_area: function(scale) {this.split_pos += scale;},
get n_master() {return this._n_master;},
set n_master(n) {n = Math.max(Math.min(n, 5), 0); if (this._n_master != n) {this._n_master = n; this.notify("n-master"); } },
increment_n_master: function(n) {this.n_master += n;},
set_master: function(window) {
const gswindow = this.getGSWindowFromWindow(window);
if (!gswindow) {return;}
// remove window but dont relayout
this.removeGSWindow(gswindow, false);
// add window at front
this.gswindows.unshift(gswindow);
// set n_master to 1 and force relayout if there is no change
if (this.n_master != 1) {this.n_master = 1;}
else {this.relayout();}
},
layout_name: function() {
return Modes.properties[this.mode].name;
},
layout: function() {
return Modes.properties[this.mode].layout;
},
properties: function() {
return Modes.properties[this.mode];
},
relayout: function() {
logger.debug("Current layout " + this.layout_name());
const gswindows = this.allLayoutGSWindows();
this.layout()(gswindows, this.split_pos, this.n_master);
},
layout_changed: function(old_mode, new_mode) {
logger.debug("Layout changed from " + Modes.properties[old_mode].name
+ " to " + Modes.properties[new_mode].name);
const gs_wnds = this.allLayoutGSWindows();
Modes.properties[old_mode].exitLayout(gs_wnds, this.split_pos, this.n_master);
Modes.properties[new_mode].enterLayout(gs_wnds, this.split_pos, this.n_master);
Modes.properties[new_mode].layout(gs_wnds, this.split_pos, this.n_master);
},
num_layouts: function() {
return NumModes;
},
roll_layout: function(offset) {
const next_layout_id = (this.mode + offset + this.num_layouts()) % this.num_layouts();
logger.debug("Rolling layout " + Modes.properties[next_layout_id].name);
this.mode = next_layout_id;
},
addGSWindow: function(gswindow, relayout=true) {
if (!gswindow) {return;}
gswindow.gslayout = this;
const index = this.gswindows.indexOf(gswindow);
if (index < 0) {
// only if not in list
this.gswindows.push(gswindow);
if (relayout) {
this.relayout();
}
}
},
removeGSWindow: function(gswindow, relayout=true) {
if (!gswindow) {return;}
gswindow.gslayout = null;
const index = this.gswindows.indexOf(gswindow);
if (index >= 0) {
// only if in list
this.gswindows.splice(index, 1);
if (relayout) {
this.relayout();
}
}
},
allWindows: function() {
const windows = [];
for (var idx = 0; idx < this.gswindows.length; ++idx) {
windows.push(this.gswindows[idx].window);
}
return windows;
},
allLayoutGSWindows: function() {
const gswindows = [];
for (var idx = 0; idx < this.gswindows.length; ++idx) {
if (this.gswindows[idx].layoutAllowed()) {
gswindows.push(this.gswindows[idx]);
}
}
return gswindows;
},
allLayoutWindows: function() {
var windows = [];
for (var idx = 0; idx < this.gswindows.length; ++idx) {
if (this.gswindows[idx].layoutAllowed()) {
windows.push(this.gswindows[idx].window);
}
}
return windows;
},
sortedWindowsByStacking: function() {
var windows = this.allWindows();
return global.display.sort_windows_by_stacking(windows);
},
topmostWindow: function() {
var sortedWindows = this.sortedWindowsByStacking();
return this.getGSWindowFromWindow(sortedWindows[sortedWindows.length - 1]);
},
getGSWindowFromWindow: function(window) {
if (window && window.gswindow && window.gswindow.gslayout == this) {
return window.gswindow;
}
for (var idx = 0; idx < this.gswindows.length; ++idx) {
if (this.gswindows[idx].window === window) {
return this.gswindows[idx];
}
}
return null;
},
numberOfWindows: function() {
return this.gswindows.length;
},
indexOfWindow: function(window) {
for (var idx = 0; idx < this.gswindows.length; ++idx) {
if (this.gswindows[idx].window === window) {
return idx;
}
}
return -1;
},
gsWindowByIndex: function(index) {
return this.gswindows[index];
},
swap_with_window: function(window, offset) {
const gswindow = this.getGSWindowFromWindow(window);
// do nothing if the current window is not in a true layout
if (!gswindow || !gswindow.layoutAllowed()) {return;}
let idx = 0;
for (; idx < this.gswindows.length; ++idx) {
if (this.gswindows[idx] === gswindow) {
break;
}
}
let oidx;
for (oidx = (idx + offset + this.gswindows.length) % this.gswindows.length;
oidx !== idx;
oidx = (oidx + offset + this.gswindows.length) % this.gswindows.length) {
// find next window in layout
if (this.gswindows[oidx].layoutAllowed()) {
break;
}
}
if (idx === oidx) {
// same window
return;
}
this.gswindows[idx] = this.gswindows[oidx];
this.gswindows[oidx] = gswindow;
this.relayout();
gswindow.center_pointer();
}
});