-
Notifications
You must be signed in to change notification settings - Fork 18
/
manager.go
362 lines (290 loc) · 8.68 KB
/
manager.go
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package store
import (
"fmt"
"math"
"github.com/leukipp/cortile/v2/common"
log "github.com/sirupsen/logrus"
)
type Manager struct {
Name string // Manager name with window clients
Location *Location // Manager workspace and screen location
Proportions *Proportions // Manager proportions of window clients
Masters *Clients // List of master window clients
Slaves *Clients // List of slave window clients
}
type Location struct {
DeskNum uint // Location desktop number
ScreenNum uint // Location screen number
}
type Proportions struct {
MasterSlave map[int][]float64 // Master-slave proportions
MasterMaster map[int][]float64 // Master-master proportions
SlaveSlave map[int][]float64 // Slave-slave proportions
}
type Clients struct {
Maximum int // Currently maximum allowed clients
Stacked []*Client `json:"-"` // List of stored window clients
}
type Directions struct {
Top bool // Indicates proportion changes on the top
Right bool // Indicates proportion changes on the right
Bottom bool // Indicates proportion changes on the bottom
Left bool // Indicates proportion changes on the left
}
const (
Stacked uint8 = 1 // Flag for stacked (internal index order) clients
Ordered uint8 = 2 // Flag for ordered (bottom to top order) clients
Visible uint8 = 3 // Flag for visible (top only) clients
)
func CreateManager(loc Location) *Manager {
return &Manager{
Name: fmt.Sprintf("manager-%d-%d", loc.DeskNum, loc.ScreenNum),
Location: &loc,
Proportions: &Proportions{
MasterSlave: calcProportions(2),
MasterMaster: calcProportions(common.Config.WindowMastersMax),
SlaveSlave: calcProportions(common.Config.WindowSlavesMax),
},
Masters: &Clients{
Maximum: 1,
Stacked: make([]*Client, 0),
},
Slaves: &Clients{
Maximum: common.Config.WindowSlavesMax,
Stacked: make([]*Client, 0),
},
}
}
func (mg *Manager) AddClient(c *Client) {
if mg.IsMaster(c) || mg.IsSlave(c) {
return
}
log.Debug("Add client for manager [", c.Latest.Class, ", ", mg.Name, "]")
// Fill up master area then slave area
if len(mg.Masters.Stacked) < mg.Masters.Maximum {
mg.Masters.Stacked = addClient(mg.Masters.Stacked, c)
} else {
mg.Slaves.Stacked = addClient(mg.Slaves.Stacked, c)
}
}
func (mg *Manager) RemoveClient(c *Client) {
log.Debug("Remove client from manager [", c.Latest.Class, ", ", mg.Name, "]")
// Remove master window
mi := mg.Index(mg.Masters, c)
if mi >= 0 {
if len(mg.Slaves.Stacked) > 0 {
mg.SwapClient(mg.Masters.Stacked[mi], mg.Slaves.Stacked[0])
mg.Slaves.Stacked = mg.Slaves.Stacked[1:]
} else {
mg.Masters.Stacked = removeClient(mg.Masters.Stacked, mi)
}
}
// Remove slave window
si := mg.Index(mg.Slaves, c)
if si >= 0 {
mg.Slaves.Stacked = removeClient(mg.Slaves.Stacked, si)
}
}
func (mg *Manager) MakeMaster(c *Client) {
log.Info("Make window master [", c.Latest.Class, ", ", mg.Name, "]")
// Swap window with first master
if len(mg.Masters.Stacked) > 0 {
mg.SwapClient(c, mg.Masters.Stacked[0])
}
}
func (mg *Manager) SwapClient(c1 *Client, c2 *Client) {
log.Info("Swap clients [", c1.Latest.Class, "-", c2.Latest.Class, ", ", mg.Name, "]")
mIndex1 := mg.Index(mg.Masters, c1)
sIndex1 := mg.Index(mg.Slaves, c1)
mIndex2 := mg.Index(mg.Masters, c2)
sIndex2 := mg.Index(mg.Slaves, c2)
// Swap master with master
if mIndex1 >= 0 && mIndex2 >= 0 {
mg.Masters.Stacked[mIndex2], mg.Masters.Stacked[mIndex1] = mg.Masters.Stacked[mIndex1], mg.Masters.Stacked[mIndex2]
return
}
// Swap master with slave
if mIndex1 >= 0 && sIndex2 >= 0 {
mg.Slaves.Stacked[sIndex2], mg.Masters.Stacked[mIndex1] = mg.Masters.Stacked[mIndex1], mg.Slaves.Stacked[sIndex2]
return
}
// Swap slave with master
if sIndex1 >= 0 && mIndex2 >= 0 {
mg.Masters.Stacked[mIndex2], mg.Slaves.Stacked[sIndex1] = mg.Slaves.Stacked[sIndex1], mg.Masters.Stacked[mIndex2]
return
}
// Swap slave with slave
if sIndex1 >= 0 && sIndex2 >= 0 {
mg.Slaves.Stacked[sIndex2], mg.Slaves.Stacked[sIndex1] = mg.Slaves.Stacked[sIndex1], mg.Slaves.Stacked[sIndex2]
return
}
}
func (mg *Manager) NextClient() *Client {
clients := mg.Clients(Stacked)
last := len(clients) - 1
// Get next window
next := -1
for i, c := range clients {
if c.Window.Id == Windows.Active.Id {
next = i + 1
if next > last {
next = 0
}
break
}
}
// Invalid active window
if next == -1 {
return nil
}
return clients[next]
}
func (mg *Manager) PreviousClient() *Client {
clients := mg.Clients(Stacked)
last := len(clients) - 1
// Get previous window
prev := -1
for i, c := range clients {
if c.Window.Id == Windows.Active.Id {
prev = i - 1
if prev < 0 {
prev = last
}
break
}
}
// Invalid active window
if prev == -1 {
return nil
}
return clients[prev]
}
func (mg *Manager) IncreaseMaster() {
// Increase master area
if len(mg.Slaves.Stacked) > 1 && mg.Masters.Maximum < common.Config.WindowMastersMax {
mg.Masters.Maximum += 1
mg.Masters.Stacked = append(mg.Masters.Stacked, mg.Slaves.Stacked[0])
mg.Slaves.Stacked = mg.Slaves.Stacked[1:]
}
log.Info("Increase masters to ", mg.Masters.Maximum)
}
func (mg *Manager) DecreaseMaster() {
// Decrease master area
if len(mg.Masters.Stacked) > 0 {
mg.Masters.Maximum -= 1
mg.Slaves.Stacked = append([]*Client{mg.Masters.Stacked[len(mg.Masters.Stacked)-1]}, mg.Slaves.Stacked...)
mg.Masters.Stacked = mg.Masters.Stacked[:len(mg.Masters.Stacked)-1]
}
log.Info("Decrease masters to ", mg.Masters.Maximum)
}
func (mg *Manager) IncreaseSlave() {
// Increase slave area
if mg.Slaves.Maximum < common.Config.WindowSlavesMax {
mg.Slaves.Maximum += 1
}
log.Info("Increase slaves to ", mg.Slaves.Maximum)
}
func (mg *Manager) DecreaseSlave() {
// Decrease slave area
if mg.Slaves.Maximum > 1 {
mg.Slaves.Maximum -= 1
}
log.Info("Decrease slaves to ", mg.Slaves.Maximum)
}
func (mg *Manager) IncreaseProportion() {
precision := 1.0 / common.Config.ProportionStep
proportion := math.Round(mg.Proportions.MasterSlave[2][0]*precision)/precision + common.Config.ProportionStep
// Increase root proportion
mg.SetProportions(mg.Proportions.MasterSlave[2], proportion, 0, 1)
}
func (mg *Manager) DecreaseProportion() {
precision := 1.0 / common.Config.ProportionStep
proportion := math.Round(mg.Proportions.MasterSlave[2][0]*precision)/precision - common.Config.ProportionStep
// Decrease root proportion
mg.SetProportions(mg.Proportions.MasterSlave[2], proportion, 0, 1)
}
func (mg *Manager) SetProportions(ps []float64, pi float64, i int, j int) bool {
// Ignore changes on border sides
if i == j || i < 0 || i >= len(ps) || j < 0 || j >= len(ps) {
return false
}
// Clamp target proportion
pic := math.Min(math.Max(pi, common.Config.ProportionMin), 1.0-common.Config.ProportionMin)
if pi != pic {
return false
}
// Clamp neighbor proportion
pj := ps[j] + (ps[i] - pi)
pjc := math.Min(math.Max(pj, common.Config.ProportionMin), 1.0-common.Config.ProportionMin)
if pj != pjc {
return false
}
// Update proportions
ps[i] = pi
ps[j] = pj
return true
}
func (mg *Manager) IsMaster(c *Client) bool {
// Check if window is master
return mg.Index(mg.Masters, c) >= 0
}
func (mg *Manager) IsSlave(c *Client) bool {
// Check if window is slave
return mg.Index(mg.Slaves, c) >= 0
}
func (mg *Manager) Index(windows *Clients, c *Client) int {
// Traverse client list
for i, m := range windows.Stacked {
if m.Window.Id == c.Window.Id {
return i
}
}
return -1
}
func (mg *Manager) Ordered(windows *Clients) []*Client {
ordered := []*Client{}
// Create ordered client list
for _, w := range Windows.Stacked {
for _, c := range windows.Stacked {
if w.Id == c.Window.Id {
ordered = append(ordered, c)
break
}
}
}
return ordered
}
func (mg *Manager) Visible(windows *Clients) []*Client {
visible := make([]*Client, int(math.Min(float64(len(windows.Stacked)), float64(windows.Maximum))))
// Create visible client list
for _, c := range mg.Ordered(windows) {
visible[mg.Index(windows, c)%windows.Maximum] = c
}
return visible
}
func (mg *Manager) Clients(flag uint8) []*Client {
switch flag {
case Stacked:
return append(mg.Masters.Stacked, mg.Slaves.Stacked...)
case Ordered:
return append(mg.Ordered(mg.Masters), mg.Ordered(mg.Slaves)...)
case Visible:
return append(mg.Visible(mg.Masters), mg.Visible(mg.Slaves)...)
}
return make([]*Client, 0)
}
func addClient(cs []*Client, c *Client) []*Client {
return append([]*Client{c}, cs...)
}
func removeClient(cs []*Client, i int) []*Client {
return append(cs[:i], cs[i+1:]...)
}
func calcProportions(n int) map[int][]float64 {
p := map[int][]float64{}
for i := 1; i <= n; i++ {
for j := 1; j <= i; j++ {
p[i] = append(p[i], 1.0/float64(i))
}
}
return p
}