-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_manager.nim
494 lines (425 loc) · 15.7 KB
/
window_manager.nim
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# MIT License
#
# Copyright (c) 2019 - 2021 Can Joshua Lehmann
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import strutils, tables, unicode, sequtils, sugar
import hashes, times, algorithm, asyncdispatch
import utils, ui_utils, termdiff, buffer, log
type
Window* = ref object of RootObj
Command* = object
name*: string
shortcut*: seq[Key]
cmd*: proc () {.closure.}
Launcher* = ref object of Window
app: App
list: List
WindowConstructor* = object
name: string
make: proc (app: App): Window
AppMode* = enum
AppModeNone, AppModePane,
AppModeNewPane, AppModeResizePane
Panes*[T] = object
axis: Axis
selected*: int
sizes*: seq[float64]
children*: seq[T]
App* = ref object
columns*: Panes[Panes[Window]]
copy_buffer*: CopyBuffer
languages*: seq[Language]
window_constructors*: seq[WindowConstructor]
mode*: AppMode
buffers*: Table[string, Buffer]
autocompleters*: Table[int, Autocompleter]
log*: Log
method process_key*(window: Window, key: Key) {.base.} = quit "Not implemented: process_key"
method process_mouse*(window: Window, mouse: Mouse): bool {.base.} = discard
method render*(window: Window, box: Box, ren: var TermRenderer) {.base.} = quit "Not implemented: render"
method close*(window: Window) {.base.} = discard
method list_commands*(window: Window): seq[Command] {.base.} = discard
# Launcher
proc new_launcher(app: App): Window =
return Launcher(app: app, list: make_list(app.window_constructors.map(c => c.name)))
proc open_window*[T](panes: var Panes[T], window: Window)
proc open_selected(launcher: Launcher) =
let
selected = launcher.list.selected
window = launcher.app.window_constructors[selected].make(launcher.app)
launcher.app.columns.open_window(window)
method process_key(launcher: Launcher, key: Key) =
case key.kind:
of LIST_KEYS:
launcher.list.process_key(key)
of KeyReturn:
launcher.open_selected()
else: discard
method process_mouse(launcher: Launcher, mouse: Mouse): bool =
if mouse.x == 0 and mouse.y == 0:
return true
var mouse_rel = mouse
mouse_rel.x -= 2
mouse_rel.y -= 1
case mouse.kind:
of MouseUp:
if launcher.list.process_mouse(mouse_rel):
launcher.open_selected()
else:
discard launcher.list.process_mouse(mouse_rel)
method render(launcher: Launcher, box: Box, ren: var TermRenderer) =
render_border("Launcher", 1, box, ren)
launcher.list.render(Box(min: box.min + Index2d(x: 2, y: 1), max: box.max), ren)
proc make_window*(app: App): Window =
return app.window_constructors[0].make(app)
proc init_window_constructor*(name: string, make: proc (app: App): Window): WindowConstructor =
WindowConstructor(name: name, make: make)
# Command Search
type
CommandSearch = ref object of Window
app: App
prev_window: Window
list: List
entry: Entry
commands: seq[Command]
shown_commands: seq[Command]
proc display(cmd: Command): string =
if cmd.shortcut.len == 0:
return cmd.name
return cmd.name & " (" & $cmd.shortcut & ")"
proc update_list(cmd_search: CommandSearch) =
cmd_search.shown_commands = cmd_search.commands
.filter(cmd => to_lower($cmd_search.entry.text) in cmd.name.to_lower())
cmd_search.list.items = cmd_search.shown_commands
.map(cmd => to_runes(cmd.display()))
if cmd_search.list.selected >= cmd_search.list.items.len:
cmd_search.list.selected = cmd_search.list.items.len - 1
if cmd_search.list.selected < 0:
cmd_search.list.selected = 0
proc new_command_search(app: App, prev_window: Window): Window =
let cmd_search = CommandSearch(
app: app,
prev_window: prev_window,
list: make_list(@[""]),
entry: make_entry(app.copy_buffer),
commands: prev_window.list_commands()
)
cmd_search.commands.sort(proc (a, b: Command): int =
cmp(a.name.to_lower(), b.name.to_lower()))
cmd_search.update_list()
return cmd_search
proc run_command(cmd_search: CommandSearch) =
let selected = cmd_search.list.selected
if selected < 0 or selected >= cmd_search.shown_commands.len:
return
cmd_search.app.columns.open_window(cmd_search.prev_window)
cmd_search.shown_commands[selected].cmd()
method process_mouse(cmd_search: CommandSearch, mouse: Mouse): bool =
if mouse.x < len("Search:") and mouse.y == 0:
return true
var mouse_rel = mouse
mouse_rel.x -= 2
mouse_rel.y -= 2
case mouse.kind:
of MouseUp:
if cmd_search.list.process_mouse(mouse_rel):
cmd_search.run_command()
else:
discard cmd_search.list.process_mouse(mouse_rel)
method process_key(cmd_search: CommandSearch, key: Key) =
if key.kind == KeyEscape or
(key.kind == KeyChar and key.chr == 'e' and key.ctrl):
cmd_search.app.columns.open_window(cmd_search.prev_window)
return
case key.kind:
of KeyReturn:
cmd_search.run_command()
of LIST_KEYS:
cmd_search.list.process_key(key)
else:
cmd_search.entry.process_key(key)
cmd_search.update_list()
method render(cmd_search: CommandSearch, box: Box, ren: var TermRenderer) =
let sidebar_width = len("Search:")
render_border("Command Search", sidebar_width, box, ren)
ren.move_to(box.min + Index2d(y: 1))
ren.put("Search:", fg=Color(base: ColorBlack), bg=Color(base: ColorWhite))
ren.move_to(box.min + Index2d(y: 1, x: sidebar_width + 1))
cmd_search.entry.render(ren)
cmd_search.list.render(Box(min: box.min + Index2d(x: sidebar_width + 1, y: 2), max: box.max), ren)
# Panes
proc init_panes*[T](axis: Axis, children: seq[T]): Panes[T] =
result = Panes[T](
axis: axis,
children: children,
sizes: new_seq[float64](children.len)
)
for it in 0..<children.len:
result.sizes[it] = 1.0
proc open_window*(panes: var Panes[Window], window: Window) =
panes.children[panes.selected] = window
proc open_window*[T](panes: var Panes[T], window: Window) =
panes.children[panes.selected].open_window(window)
proc active_window*(window: Window): Window = window
proc active_window*[T](panes: Panes[T]): Window =
result = panes.children[panes.selected].active_window()
proc adjust_sizes*[T](panes: var Panes[T]) =
var total: float64 = 0.0
for size in panes.sizes:
total += size
for size in panes.sizes.mitems:
size /= total
proc constrain_selected*[T](panes: var Panes[T]) =
panes.selected = panes.selected.min(panes.children.len - 1).max(0)
proc close_active_window*(panes: var Panes[Panes[Window]]) =
template active_column: var Panes[Window] = panes.children[panes.selected]
if panes.children.len == 1 and
active_column.children.len == 1:
return
active_column.children.delete(active_column.selected)
active_column.sizes.delete(active_column.selected)
active_column.adjust_sizes()
active_column.constrain_selected()
if active_column.children.len == 0:
panes.children.delete(panes.selected)
panes.sizes.delete(panes.selected)
panes.adjust_sizes()
panes.constrain_selected()
proc process_mouse*(window: Window, mouse: Mouse, box: Box, app: App) =
var mouse_rel = mouse
mouse_rel.x -= box.min.x
mouse_rel.y -= box.min.y
if window.process_mouse(mouse_rel) and mouse.kind == MouseDown:
app.mode = AppModeResizePane
iterator iter_layout[T](panes: Panes[T], box: Box): (int, Box, T) =
var offset = 0
for it, child in panes.children:
let space = int(panes.sizes[it] * float64(box.size[panes.axis]))
var child_box = Box()
child_box.min[not panes.axis] = box.min[not panes.axis]
child_box.max[not panes.axis] = box.max[not panes.axis]
child_box.min[panes.axis] = offset + box.min[panes.axis]
child_box.max[panes.axis] = offset + space + box.min[panes.axis]
if it == panes.children.len - 1:
child_box.max[panes.axis] = box.max[panes.axis]
yield (it, child_box, child)
offset += space
proc process_mouse*[T](panes: var Panes[T], mouse: Mouse, box: Box, app: App) =
case mouse.kind:
of MouseDown, MouseScroll:
for it, child_box, child in panes.iter_layout(box):
if child_box.is_inside(mouse.pos):
if mouse.kind == MouseDown:
panes.selected = it
panes.children[it].process_mouse(mouse, child_box, app)
break
else:
for it, child_box, child in panes.iter_layout(box):
if it == panes.selected:
panes.children[it].process_mouse(mouse, child_box, app)
break
proc process_key*[T](panes: Panes[T], key: Key) =
panes.children[panes.selected].process_key(key)
proc render*[T](panes: Panes[T], box: Box, ren: var TermRenderer) =
for it, child_box, child in panes.iter_layout(box):
ren.clip(box)
child.render(child_box, ren)
proc add*[T](panes: var Panes[T], child: T, size: float64 = 1) =
panes.children.add(child)
panes.sizes.add(size)
proc select*[T](panes: var Panes[T], dir: Direction) =
if panes.axis == dir.to_axis():
panes.selected += dir.on_axis()
panes.constrain_selected()
else:
when T isnot Window:
panes.children[panes.selected].select(dir)
proc split*[T](panes: var Panes[T], child: T, index: int) =
panes.children.insert(child, index)
panes.sizes.insert(0.0, index)
for size in panes.sizes.mitems:
size = 1 / panes.sizes.len
proc split*(panes: var Panes[Window], dir: Direction, app: App) =
let offset = (dir.on_axis() + 1) div 2
panes.split(app.make_window(), panes.selected + offset)
panes.selected += offset
proc split*(panes: var Panes[Panes[Window]], dir: Direction, app: App) =
if panes.axis == dir.to_axis():
let offset = (dir.on_axis() + 1) div 2
let child = init_panes(not panes.axis, @[app.make_window()])
panes.split(child, panes.selected + offset)
panes.selected += offset
else:
panes.children[panes.selected].split(dir, app)
proc resize*(panes: Window, pos: Index2d, box: Box) = discard
proc resize*[T](panes: var Panes[T], pos: Index2d, box: Box) =
for it, child_box, child in panes.iter_layout(box):
if it == panes.selected:
panes.children[panes.selected].resize(pos, child_box)
if panes.selected > 0:
var base: float64 = 0.0
for it in 0..<(panes.selected - 1):
base += panes.sizes[it]
let
frac = pos[panes.axis] / box.size[panes.axis]
total = panes.sizes[panes.selected] + panes.sizes[panes.selected - 1]
var new_size = min(frac - base, total).max(0)
panes.sizes[panes.selected] = total - new_size
panes.sizes[panes.selected - 1] = new_size
# App
proc open_launcher(app: App) =
app.columns.open_window(app.new_launcher())
proc open_command_search(app: App) =
app.columns.open_window(app.new_command_search(app.columns.active_window()))
proc new_app*(languages: seq[Language], window_constructors: seq[WindowConstructor]): owned App =
for it, lang in languages.pairs:
lang.id = it
result = App(
copy_buffer: new_copy_buffer(),
columns: Panes[Panes[Window]](axis: AxisX),
languages: languages,
window_constructors: window_constructors,
buffers: init_table[string, Buffer](),
log: new_log()
)
proc get_autocompleter*(app: App, language: Language): Autocompleter =
if language.is_nil:
return nil
if language.make_autocompleter.is_nil:
return nil
if language.id notin app.autocompleters:
let autocompleter = language.make_autocompleter(app.log)
if autocompleter.is_nil:
return nil
app.autocompleters[language.id] = autocompleter
return app.autocompleters[language.id]
proc new_buffer*(app: App, path: string): Buffer =
if app.buffers.has_key(path):
return app.buffers[path]
result = new_buffer(path, app.languages)
app.buffers[path] = result
let comp = app.get_autocompleter(result.language)
if not comp.is_nil:
comp.track(result)
proc is_changed*(app: App, path: string): bool =
if not app.buffers.has_key(path):
return false
return app.buffers[path].changed
proc list_changed*(app: App): seq[string] =
for path in app.buffers.keys:
if app.buffers[path].changed:
result.add(path)
proc process_mouse*(app: App, mouse: Mouse, size: Index2d) =
case app.mode:
of AppModeResizePane:
app.columns.resize(mouse.pos, Box(max: size))
if mouse.kind == MouseUp:
app.mode = AppModeNone
else:
app.columns.process_mouse(mouse, Box(max: size), app)
proc close*(app: App) =
for comp in app.autocompleters.values:
comp.close()
proc process_key*(app: App, key: Key): bool =
if has_pending_operations():
try:
let start = get_time()
drain(1)
let diff = get_time() - start
app.log.add_info("window_manager", "Call to drain took " & $diff)
except OSError as err:
app.log.add_error("window_manager", err.msg)
defer:
if result:
app.close()
case app.mode:
of AppModeNewPane:
case key.kind:
of KeyArrowDown:
app.columns.split(DirDown, app)
of KeyArrowUp:
app.columns.split(DirUp, app)
of KeyArrowLeft:
app.columns.split(DirLeft, app)
of KeyArrowRight:
app.columns.split(DirRight, app)
of KeyNone:
return false
else: discard
app.mode = AppModeNone
return
of AppModePane:
case key.kind:
of KeyChar:
if key.chr == Rune('n'):
app.mode = AppModeNewPane
return
elif key.chr == Rune('a'):
app.open_launcher()
of KeyArrowUp: app.columns.select(DirUp)
of KeyArrowDown: app.columns.select(DirDown)
of KeyArrowLeft: app.columns.select(DirLeft)
of KeyArrowRight: app.columns.select(DirRight)
of KeyNone:
return false
else: discard
app.mode = AppModeNone
return
of AppModeNone, AppModeResizePane:
case key.kind:
of KeyQuit:
return true
of KeyChar:
if key.ctrl:
case key.chr:
of Rune('q'): return true
of Rune('w'):
app.columns.close_active_window()
return
of Rune('p'):
app.mode = AppModePane
return
else: discard
of KeyArrowDown:
if key.alt and not key.shift and not key.ctrl:
app.columns.select(DirDown)
return
of KeyArrowUp:
if key.alt and not key.shift and not key.ctrl:
app.columns.select(DirUp)
return
of KeyArrowLeft:
if key.alt and not key.shift and not key.ctrl:
app.columns.select(DirLeft)
return
of KeyArrowRight:
if key.alt and not key.shift and not key.ctrl:
app.columns.select(DirRight)
return
of KeyFn:
if key.fn == 1:
app.open_command_search()
return
else: discard
app.columns.process_key(key)
return false
proc render*(app: App, ren: var TermRenderer) =
ren.clear()
app.columns.render(Box(max: ren.screen.size), ren)