-
Notifications
You must be signed in to change notification settings - Fork 66
/
sbp_layout.py
340 lines (270 loc) · 10.3 KB
/
sbp_layout.py
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
import unittest
import functools
def cmp_cells(ea,eb):
a = ea[1]
b = eb[1]
if a[1] < b[1]:
return -1
elif a[1] == b[1]:
if a[0] < b[0]:
return -1
else:
return 1
else:
return 1
class LayoutManager:
""" Manages the layout of a sublime window."""
MAX_COLS = 20
MAX_ROWS = 20
def _buildCoordCells(self):
self.coord_cells = [ [self._col_val(x[0]), self._row_val(x[1]), self._col_val(x[2]), self._row_val(x[3])] for x in self.grid["cells"]]
def _col_val(self, index):
return self.cols()[index]
def _row_val(self, index):
return self.rows()[index]
def _cell(self, index):
return self.coord_cells[index]
def _replace(self, index, result):
""" Takes an input index and removes the cell at this index and injects
the range given by result into the coord_cells
"""
left, right = self.coord_cells[0:index], self.coord_cells[index+1:]
self.coord_cells = left + [result[0]] + right + [result[1]]
def __init__(self, grid):
if grid:
self.grid = grid
self._buildCoordCells()
self._col_count = len(self.cols())
self._row_count = len(self.rows())
def cols(self):
return self.grid["cols"]
def rows(self):
return self.grid["rows"]
def split(self, index, mode):
"""Splits a cell identified by index into two.
The split itself is described by the mode parameter and can be either
horizontal or vertical
mode: v | h
"""
if self._col_count + 1 > LayoutManager.MAX_COLS and mode == "v":
return False
if self._row_count +1 > LayoutManager.MAX_ROWS and mode == "h":
return False
current = self._cell(index)
# Depending on the split calculate the new offsets
if mode == "v":
self._col_count += 1
delta = (current[2] - current[0]) / 2.0
result = [ [current[0], current[1], current[0] + delta, current[3]], [current[0]+delta, current[1], current[2], current[3]] ]
else:
self._row_count += 1
delta = (current[3] - current[1]) / 2.0
result = [ [current[0], current[1], current[2], current[1] + delta], [current[0], current[1] + delta, current[2], current[3]] ]
self._replace(index, result)
return True
def killSelf(self, index):
"""
Takes the cell identified by index and removes it from the list of cells.
While in original emacs the order of created cells are tracked, we need to
improvise here a bit. We first try to merge clockwise.
"""
if self._col_count == 2 and self._row_count == 2:
return
cell = self.coord_cells[index]
del(self.coord_cells[index])
# First check if there are neighbouring cells atop or below
expand = []
# Shift top
expand += [[i, 1] for i, x in enumerate(self.coord_cells) if x[1] == cell[3] and x[0] >= cell[0] and x[2] <= cell[2]]
# Shift down
expand += [[i, 3] for i, x in enumerate(self.coord_cells) if x[3] == cell[1] and x[0] >= cell[0] and x[2] <= cell[2]]
# Shift right
expand += [[i, 2] for i, x in enumerate(self.coord_cells) if x[2] == cell[0] and x[1] >= cell[1] and x[3] <= cell[3]]
# Shift left
expand += [[i, 0] for i, x in enumerate(self.coord_cells) if x[0] == cell[2] and x[1] >= cell[1] and x[3] <= cell[3]]
# Replace the values with their default
for match in expand:
self.coord_cells[match[0]][match[1]] = cell[match[1]]
def killOther(self, index):
"""
"""
self.coord_cells = [[0.0, 0.0, 1.0, 1.0],]
def build(self):
"""
Based on the cells with absolute coordinates build the structure required
for sublime text
"""
col_list = sorted(list(set(sum([[x[0], x[2]] for x in self.coord_cells], []))))
cols = dict([ [v,k] for k,v in enumerate(col_list)])
row_list = sorted(list(set(sum([[x[1], x[3]] for x in self.coord_cells], []))))
rows = dict([ [v,k] for k,v in enumerate(row_list)])
result = {
"cols" : sorted(list(cols.keys())),
"rows" : sorted(list(rows.keys())),
"cells" : [ [ cols[cell[0]], rows[cell[1]], cols[cell[2]], rows[cell[3]] ] for cell in self.coord_cells ]
}
return result
def next(self, index, direction=1):
"""
Find the visually next cell for the given index. This must not necessarily
be the adjacent cell in the list
"""
cell = self.grid["cells"][index]
# Sort the cells
new_grid = sorted(enumerate(self.grid["cells"]), key=functools.cmp_to_key(cmp_cells), reverse=False)
new_pos = [i for i, x in enumerate(new_grid) if x[1] == cell].pop()
# find the position of the old cell and get the new index
return new_grid[(new_pos + direction) % len(self.grid["cells"])][0]
def extend(self, index, direction, unit, count):
"""
Grows / shrinks the cell give the direction. It copies the emacs algorithm
to do that. Which does the following.
* growing horizontally goes right first, if not possible go left
* growing vertically goes down first, if not top
"""
# figure out which row or col entry we're going to adjust and make sure not to allow any
# adjascent rows/cols to get too close to each other
cell = self.grid["cells"][index]
rows = self.rows()
cols = self.cols()
amount = unit * count
if "s" in direction:
amount = -amount
x1,y1,x2,y2 = cell
if direction in ('g', 's') and (y1 > 0 or y2 < self._row_count - 1):
# adjust heights
if y2 == self._row_count - 1:
# Never adjust top or bottom: they should be 0 and 1.0 always, so when at the top or the
# bottom move inward.
y2 = abs(y2 - 1)
amount = -amount
# check for too small height
new_pos = rows[y2] + amount
min_height = 5 * unit
if new_pos - rows[y2 - 1] >= min_height and rows[y2 + 1] - new_pos >= min_height:
rows[y2] = new_pos
elif direction in ('gh', 'sh') and (x1 > 0 or x2 < self._col_count - 1):
# adjust widths
if x2 == self._col_count - 1:
# Never adjust left or right: they should be 0 and 1.0 always, so when at the left or the
# right move inward.
x2 = abs(x2 - 1)
amount = -amount
# check for too small width
min_width = 20 * unit
new_pos = cols[x2] + amount
if new_pos - cols[x2 - 1] >= min_width and cols[x2 + 1] - new_pos >= min_width:
cols[x2] = new_pos
return self.grid
# Test Code below
class TestLayoutManager(unittest.TestCase):
def setUp(self):
self.base = {'cols': [0.0, 1.0], 'rows': [0.0, 1.0], 'cells': [[0, 0, 1, 1]]}
self.hbase = {'cols': [0.0, 1.0], 'rows': [0.0, 0.5, 1.0], 'cells': [[0, 0, 1, 1], [0, 1, 1, 2]]}
self.vbase = {'cols': [0.0, 0.5, 1.0], 'rows': [0.0, 1.0], 'cells': [[0, 0, 1, 1], [1,0,2,1]]}
self.vhbase = {'cols': [0.0, 0.5, 1.0], 'rows': [0.0, 0.5, 1.0], 'cells': [[0, 0, 1, 1], [1,0,2,1],[0, 1, 1, 2], [1,1,2,2]]}
def testKillSelfComplicated(self):
lm = LayoutManager(self.base)
lm.split(0, 'v')
lm.split(0, 'h')
lm.killSelf(2)
self.assertEqual(self.hbase, lm.build())
lm.killOther(0)
lm.split(0, 'v')
lm.split(1, 'h')
lm.killSelf(0)
self.assertEqual(self.hbase, lm.build())
def testKillSelf(self):
lm = LayoutManager(self.base)
lm.killSelf(0)
self.assertEqual(self.base, lm.build())
lm.split(0, 'h')
self.assertEqual(self.hbase, lm.build())
lm.killSelf(1)
self.assertEqual(self.base, lm.build())
lm.split(0, 'h')
self.assertEqual(self.hbase, lm.build())
lm.killSelf(0)
self.assertEqual(self.base, lm.build())
lm.split(0, 'v')
self.assertEqual(self.vbase, lm.build())
lm.killSelf(0)
self.assertEqual(self.base, lm.build())
lm.split(0, 'v')
self.assertEqual(self.vbase, lm.build())
lm.killSelf(1)
self.assertEqual(self.base, lm.build())
def testBasicValues(self):
lm = LayoutManager(self.base)
self.assertEqual(2, len(lm.cols()))
self.assertEqual(2, len(lm.rows()))
def testCreateMapping(self):
lm = LayoutManager(self.hbase)
self.assertEqual([[0.0, 0.0, 1.0, 0.5],[0.0, 0.5, 1.0, 1.0]], lm.coord_cells)
def testReplaceCells(self):
lm = LayoutManager(self.hbase)
new_split = [[0.0, 0.0, 0.5, 0.5], [0.5, 0.0, 1.0, 0.5]]
lm._replace(0, new_split)
self.assertEqual(new_split[0], lm.coord_cells[0])
self.assertEqual(new_split[1], lm.coord_cells[1])
def testSplitVertical(self):
lm = LayoutManager(self.hbase)
lm.split(0, 'v')
new_split = [[0.0, 0.0, 0.5, 0.5], [0.5, 0.0, 1.0, 0.5], [0.0, 0.5, 1.0, 1.0]]
self.assertEqual(new_split, lm.coord_cells)
lm = LayoutManager(self.vbase)
lm.split(1, 'v')
new_split = [[0.0, 0.0, 0.5, 1.0], [0.5, 0.0, 0.75, 1], [0.75, 0.0, 1.0, 1.0]]
self.assertEqual(new_split, lm.coord_cells)
def testSplitHorizontal(self):
lm = LayoutManager(self.hbase)
lm.split(0, 'h')
new_split = [[0.0, 0.0, 1.0, 0.25], [0.0, 0.25, 1.0, 0.5], [0.0, 0.5, 1.0, 1.0]]
self.assertEqual(new_split, lm.coord_cells)
lm = LayoutManager(self.vbase)
lm.split(1, 'h')
new_split = [[0.0, 0.0, 0.5, 1.0], [0.5, 0.0, 1.0, 0.5], [0.5, 0.5, 1.0, 1.0]]
self.assertEqual(new_split, lm.coord_cells)
def testKillOther(self):
pass
def testBuild(self):
lm = LayoutManager(self.base)
lm.split(0, 'v')
result = lm.build()
#print(result)
def testShouldNotCreateMoreColsThanMax(self):
lm = LayoutManager(self.base)
self.assertEqual(2, lm._col_count)
lm.split(0, 'v')
self.assertEqual(3, lm._col_count)
lm.split(0, 'v')
self.assertEqual(4, lm._col_count)
lm.split(0, 'v')
self.assertEqual(5, lm._col_count)
lm.split(0, 'v')
self.assertEqual(6, lm._col_count)
def testMixMaxCount(self):
lm = LayoutManager(self.base)
self.assertEqual(2, lm._col_count)
self.assertEqual(2, lm._row_count)
lm.split(0, 'v')
lm.split(0, 'h')
lm.split(0, 'v')
lm.split(0, 'h')
lm.split(0, 'v')
lm.split(0, 'h')
self.assertEqual(5, lm._col_count)
self.assertEqual(5, lm._row_count)
def testShouldNotCreateMoreRowsThanMax(self):
lm = LayoutManager(self.base)
self.assertEqual(2, lm._row_count)
lm.split(0, 'h')
self.assertEqual(3, lm._row_count)
lm.split(0, 'h')
self.assertEqual(4, lm._row_count)
lm.split(0, 'h')
self.assertEqual(5, lm._row_count)
lm.split(0, 'h')
self.assertEqual(6, lm._row_count)
if __name__ == '__main__':
unittest.main()