forked from FunkMonkey/SublimeResizeActiveGroup
/
ResizeActiveGroup.py
78 lines (63 loc) · 3.21 KB
/
ResizeActiveGroup.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
import sublime_plugin
import time
from copy import deepcopy
class ResizeActiveGroup(sublime_plugin.EventListener):
def __init__(self):
self.col_times = []
# based on the active group we calculate the cols/rows array
def get_big(self, arr, activeGroup):
return [arr[0], arr[1] if (activeGroup == 1) ^ (arr[1] > 0.5) else 1 - arr[1], arr[2]]
# called when a different view is activated
def on_activated(self, view):
window = view.window()
if window:
activeGroup = window.active_group()
oldLayout = window.get_layout()
newLayout = deepcopy(oldLayout)
# 2 cells
if len(oldLayout["cells"]) == 2:
# Columns: 2
if len(oldLayout["rows"]) == 2:
newLayout["cols"] = self.get_big(oldLayout["cols"], activeGroup)
# Rows: 2
else:
newLayout["rows"] = self.get_big(oldLayout["rows"], activeGroup)
# 4 cells
elif len(oldLayout["cells"]) == 4:
# Grid: 4
if len(oldLayout["rows"]) == 3 and len(oldLayout["cols"]) == 3:
if activeGroup == 0:
newLayout["cols"] = self.get_big(oldLayout["cols"], activeGroup)
newLayout["rows"] = self.get_big(oldLayout["rows"], activeGroup)
elif activeGroup == 1:
newLayout["cols"] = self.get_big(oldLayout["cols"], activeGroup)
newLayout["rows"] = self.get_big(oldLayout["rows"], activeGroup - 1)
elif activeGroup == 2:
newLayout["cols"] = self.get_big(oldLayout["cols"], activeGroup - 2)
newLayout["rows"] = self.get_big(oldLayout["rows"], activeGroup - 1)
elif activeGroup == 3:
newLayout["cols"] = self.get_big(oldLayout["cols"], activeGroup - 2)
newLayout["rows"] = self.get_big(oldLayout["rows"], activeGroup - 2)
# 3 cols/cells? not sure how it works
elif len(oldLayout["cells"]) == 3 and len(oldLayout["cols"]) == 4:
import sys
if sys.version_info[0] >= 3:
xrange = range
widths = []
for i in xrange(len(oldLayout["cols"])-1):
widths += [oldLayout["cols"][i+1]-oldLayout["cols"][i]]
widths = sorted(widths)
w2 = [0 for i in xrange(len(widths))]
for i in xrange(len(oldLayout["cols"])-len(self.col_times)-1):
self.col_times += [0];
self.col_times[activeGroup] = time.time()
order = sorted(zip(xrange(len(self.col_times)),self.col_times), key=lambda value:value[1])
order = [o[0] for o in order]
for i in xrange(len(widths)):
w2[order[i]] = widths[i]
left = 0
for i in xrange(len(w2)):
left += w2[i]
newLayout["cols"][i+1] = left;
if oldLayout != newLayout:
window.set_layout(newLayout)