-
Notifications
You must be signed in to change notification settings - Fork 9
/
max_pane.py
325 lines (253 loc) · 9.28 KB
/
max_pane.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
"""
DESCRIPTION ABOUT SUBLIME TEXT LAYOUTS:
window.get_layout() and window.set_layout() aren't really documented
in the API so I am making some notes here about how they work.
* rows are the positions (from 0.0 to 1.0) that make
up the horizontal lines around (sides of) views.
* columns are the positions (from 0.0 to 1.0) that make
up the vertical lines around (top/bottom of) views
* cells are the 4 positions (left,top,right,bottom)
that define the positions of each view
ROWS:
0.0 -----------------
| | |
| | |
0.66 -----------------
| | |
1.0 -----------------
COLUMNS: 0.0 0.3 1.0
CELLS:
---------
| 0 | 0 |
|0 1|1 2|
| 1 | 1 |
---------
| 1 | 1 |
|0 1|1 2|
| 2 | 2 |
---------
"""
import sublime
import sublime_plugin
def sublime_text_synced(fun):
# https://github.com/SublimeTextIssues/Core/issues/1785
def decorator(*args, **kwargs):
sublime.set_timeout(lambda: fun(*args, **kwargs), 10)
return decorator
# With ST3 layouts and maximized groups are stored persistent in the session.
PERSIST_LAYOUTS = hasattr(sublime.Window, "settings")
if PERSIST_LAYOUTS:
def is_editor_maximized(window):
return window.template_settings().has("max_editor")
def maximized_group(window):
return window.template_settings().get("max_pane", {}).get("group")
def active_layout(window):
return window.layout()
def has_stored_layout(window):
return window.template_settings().has("max_pane")
def pop_stored_layout(window):
settings = window.template_settings()
layout = settings.get("max_pane", {}).get("layout")
settings.erase("max_pane")
return layout
def store_layout(window, layout, group):
window.template_settings().set("max_pane", {"layout": layout, "group": group})
# ST2 doesn't provide a window settings API to store layouts persistent.
else:
_store = {}
def is_editor_maximized(window):
return False
def maximized_group(window):
return _store.get(window.id(), {}).get("group")
def active_layout(window):
return window.get_layout()
def has_stored_layout(window):
return window.id() in _store
def pop_stored_layout(window):
try:
return _store.pop(window.id()).get("layout")
except KeyError:
return None
def store_layout(window, layout, group):
_store[window.id()] = {"layout": layout, "group": group}
def is_group_maximized(window):
return has_stored_layout(window) or looks_maximized(window)
def looks_maximized(window):
if window.num_groups() < 2:
return False
layout = active_layout(window)
return set(layout["cols"] + layout["rows"]) == set([0.0, 1.0])
def maximize_active_group(window):
group = window.active_group()
layout = active_layout(window)
store_layout(window, layout, group)
cells = layout["cells"]
current_col = int(cells[group][2])
current_row = int(cells[group][3])
window.set_layout(
{
"rows": [
0.0 if index < current_row else 1.0
for index, row in enumerate(layout["rows"])
],
"cols": [
0.0 if index < current_col else 1.0
for index, col in enumerate(layout["cols"])
],
"cells": cells,
}
)
window.focus_group(group)
for view in window.views():
view.set_status("0_maxpane", "MAX")
ShareManager.add(window.id())
def unmaximize_group(window):
layout = pop_stored_layout(window)
if layout is None and looks_maximized(window):
# We don't have a previous layout for this window
# but it looks like it was maximized, so lets
# just evenly distribute the layout.
layout = active_layout(window)
layout["rows"] = distribute(layout["rows"])
layout["cols"] = distribute(layout["cols"])
if layout:
group = window.active_group()
window.set_layout(layout)
window.focus_group(group)
for view in window.views():
view.erase_status("0_maxpane")
ShareManager.remove(window.id())
def distribute(values):
num_values = len(values)
return [n / float(num_values - 1) for n in range(0, num_values)]
class ShareManager:
"""Exposes a list of window ids which currently contain maximized panes.
Shared via an in-memory .sublime-settings file."""
maxed_wnds = set()
previous = set()
@classmethod
def is_blocked(cls):
return sublime.load_settings("max_pane_share.sublime-settings").get(
"block_max_pane"
)
@classmethod
def check_and_submit(cls):
if cls.maxed_wnds != cls.previous:
cls.previous = cls.maxed_wnds
sublime.load_settings("max_pane_share.sublime-settings").set(
"maxed_wnds", list(cls.maxed_wnds)
)
@classmethod
def add(cls, id):
cls.maxed_wnds.add(id)
cls.check_and_submit()
@classmethod
def remove(cls, id):
cls.maxed_wnds.discard(id)
cls.check_and_submit()
class MaxEditorCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return PERSIST_LAYOUTS
def is_visible(self):
return PERSIST_LAYOUTS
def run(self, maximized=None):
w = self.window
s = w.template_settings()
max_editor = s.get("max_editor")
if maximized is True and max_editor is not None:
return
if maximized is False and max_editor is None:
return
if max_editor:
# restore normal state from session
w.set_menu_visible(max_editor.get("menu_visible", True))
w.set_minimap_visible(max_editor.get("minimap_visible", True))
w.set_sidebar_visible(max_editor.get("sidebar_visible", True))
w.set_status_bar_visible(max_editor.get("status_bar_visible", True))
w.set_tabs_visible(max_editor.get("tabs_visible", True))
if not max_editor.get("group_maximized", False):
unmaximize_group(w)
s.erase("max_editor")
else:
# store current state in session
group_maximized = is_group_maximized(w)
s.set(
"max_editor",
{
"menu_visible": w.is_menu_visible(),
"minimap_visible": w.is_minimap_visible(),
"sidebar_visible": w.is_sidebar_visible(),
"status_bar_visible": w.is_status_bar_visible(),
"tabs_visible": w.get_tabs_visible(),
"group_maximized": group_maximized,
},
)
w.set_menu_visible(False)
w.set_minimap_visible(False)
w.set_sidebar_visible(False)
w.set_status_bar_visible(False)
w.set_tabs_visible(False)
if not group_maximized:
maximize_active_group(w)
class MaxPaneCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return not is_editor_maximized(self.window)
def run(self):
w = self.window
if is_group_maximized(w):
unmaximize_group(w)
elif w.num_groups() > 1:
maximize_active_group(w)
class MaximizePaneCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return not is_editor_maximized(self.window)
def run(self):
maximize_active_group(self.window)
class UnmaximizePaneCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return not is_editor_maximized(self.window)
def run(self):
unmaximize_group(self.window)
class MaxPaneEvents(sublime_plugin.EventListener):
UNMAXIMIZE_BEFORE = frozenset(
(
"carry_file_to_pane",
"clone_file_to_pane",
"create_pane",
"create_pane_with_file",
"destroy_pane",
"move_to_group",
"move_to_neighbouring_group",
"new_pane",
"project_manager",
"set_layout",
"travel_to_pane",
)
)
def on_window_command(self, window, command_name, args):
if ShareManager.is_blocked():
return
if command_name in self.UNMAXIMIZE_BEFORE:
unmaximize_group(window)
return
if PERSIST_LAYOUTS is False and command_name == "exit":
for window in sublime.windows():
unmaximize_group(window)
@sublime_text_synced
def on_activated(self, view):
if ShareManager.is_blocked():
return
window = view.window() or sublime.active_window()
# Is the window currently maximized?
if window and is_group_maximized(window):
# Is the active group the group that is maximized?
if window.active_group() != maximized_group(window):
unmaximize_group(window)
maximize_active_group(window)
def plugin_loaded():
# restore status bar indicator after startup
if PERSIST_LAYOUTS:
for window in sublime.windows():
if is_group_maximized(window):
for view in window.views():
view.set_status("0_maxpane", "MAX")