-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowManager.py
563 lines (433 loc) · 18.2 KB
/
windowManager.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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import os.path
import funobj as fo
import tn, buffer, CodeEditor
import iop, screen, window
import reader, eval, leditor_exceptions as ex
import cmdList
import refs
from cmdBar import CmdBar
from iop import Key
import repl, simpleFileEditor as sfe
def syncWindowsToEditorList(windowList, newEditorList):
return windowList.mapRoot(lambda node: node.syncWithEditorList(newEditorList))
def syncEditorsToImage(editorList, newImage):
return editorList.mapRoot(lambda node: node.syncWithImage(newImage))
def syncEditorsToStore(editorList, newStore):
return editorList.mapRoot(lambda node: node.syncToStore(newStore))
class WindowManager(fo.FuncObject):
def __init__(self, app=None, imageFileName=None):
if imageFileName == 'testIDImage':
pyLoad = reader.loadFile(imageFileName)
pyImage = [0]
pyImage.append(pyLoad)
imageRoot = tn.createTNodeExpFromPyNumberedExp(pyImage)
self.ImageRoot = imageRoot
self.hist = imageRoot
startEditor = self.createListEdFromEditorSettings(imageRoot, "EditorSettings")
self.editorList = buffer.SimpleBuffer.fromPyExp(startEditor, [0, 0])
startWindow = window.StdWindow(self.editorList, 0, 0, app.screenCols, app.screenHeight)
self.windowList = buffer.SimpleBuffer.fromPyExp(startWindow, [0, 0])
self.imageFileName = imageFileName
else:
self.ImageRoot = None
self.hist = None
self.editorList = None
self.windowList = None
self.imageFileName = None
self.app = app
self.winCmd = False
self.cmdBar = None
self.message = None
self.persist = ['editorList', 'windowList']
self.track = None
# Shared References
self.store = refs.Store()
self.trackref = None
self.wincl = cmdList.CmdList([
(Key.c('j'), cmdWinDown),
(Key.c('k'), cmdWinUp),
(Key.c('d'), cmdWinDel),
#(Key.c('u'), cmdWinUndo),
(Key.c('w'), cmdWinNext),
(Key.c('>'), cmdOpenWinInspectProc),
(Key.vk(iop.KEY_SPACE), cmdRunProg),
(Key.vk(iop.KEY_ENTER), cmdOpenWinOnCursor),
(Key.vk(iop.KEY_F10), cmdToggleFullscreen),
(Key.vk(iop.KEY_ESCAPE), cmdExitWinMode)
])
self.mainCl = cmdList.CmdList([
(Key.c('w', ctrl=True), cmdStartWinMode),
(Key.c('s', ctrl=True), 'cmdSave'),
(Key.c(':'), cmdStartCmdBar),
(Key.vk(iop.KEY_F5), cmdPlayMedia),
(Key.vk(iop.KEY_F9, alt=True), cmdScreenEditor),
(Key.vk(iop.KEY_F11, alt=True), cmdTextPager),
])
def initStore(self):
store, trackref = refs.Store().newValue(None)
return self.newStore(store).update('trackref', trackref)
def getCmdBarEnv(self):
return eval.Env.fromList([
('screenEditor', lambda:cmdScreenEditor(self)),
('textEditor', lambda:cmdTextEditor(self)),
('sfe', lambda:cmdSFE(self)),
('repl', lambda:cmdReplEditor(self)),
('save', lambda:cmdSave(self)),
('winNext', lambda:cmdWinNext(self)),
('split', lambda:cmdWinSplit(self)),
('music', lambda:cmdMusic(self)),
('page', lambda:cmdTextPager(self)),
('nvs', self.cmdPrintNVS),
('print', self.cmdPrintCursor),
('we', self.cmdWriteEditorSettingsTS),
('le', self.cmdLoadEditorSettings),
('wi', self.cmdWriteImageTS),
('li', self.cmdLoadLatestImage),
('wa', self.cmdWriteAll),
('la', self.cmdLoadLatestAll)
], eval.global_env)
def test(self):
print 'test'
return 'tested'
def quitWithoutSave(self):
return False
def updateSound(self):
try:
track = self.store.get(self.trackref)
if track and track != self.track:
self.track = track
self.app.playMedia(track)
except AttributeError:
return self
def getWinCount(self):
return self.windowList.length()
def getWMSettings(self):
editorList = [e.getEditorSettings() for e in self.editorList.toPyExp()]
return [reader.Symbol('WindowManager'),
[reader.Symbol('EditorList'),
[reader.Symbol('cursor'), self.editorList.cursorAdd],
[reader.Symbol('root'), editorList]]]
def getEditSexp(self):
curWin = self.windowList.current
curEd = curWin.editor
return curEd.getEditorSettings()
@property
def activeWindow(self):
return self.windowList.current
def writeEditor(self):
print self.getWMSettings()
pyObj = self.getEditSexp()
text = reader.to_string(pyObj)
f = open('EditorSettings', 'w')
f.write(text)
f.close()
def writeImage(self):
pyObj = self.ImageRoot.child.toPyNumberedExp()
text = reader.to_string(pyObj)
f = open(self.imageFileName, 'w')
f.write(text)
f.close()
def createListEdFromEditorSettings(self, root, filename):
"returns an editor with settings as per the EditorSettings file"
if os.path.isfile(filename):
pyEditorLoad = reader.loadFile(filename)
window = dict(pyEditorLoad[2][1:])
edZipNode = dict(zip(window['zippedNodes'], [True]*len(window['zippedNodes'])))
newBuffer = buffer.BufferSexp(root, window['address'], window['cursor'])
listEd = CodeEditor.CodeEditor(newBuffer, edZipNode)
listEd.printingMode = window['printingMode']
listEd.id = window['id']
else:
newBuffer = buffer.BufferSexp(root)
listEd = CodeEditor.CodeEditor(newBuffer)
return listEd
# calculates and sets posx, posy, maxx, maxy for all windows trying to allocate available screen real estate
# as equally as possible
def calculateWindowPositions(self):
maxX = self.app.screenCols
curY = 0
wins = self.windowList.length()
screenForWins = self.app.screenRows - 1 #- numberOfBorders
minYStep = screenForWins / wins
leftover = screenForWins % wins
editorTNodeList = self.editorList.root
windowTNodeList = self.windowList.root
for winPos, winNode in enumerate(self.windowList.rootChild()):
if leftover > 0:
curYStep = minYStep + 1
leftover -= 1
else:
curYStep = minYStep
newWindow = winNode.child.setPosition(0, curY, maxX, curYStep)
windowTNodeList = tn.replaceAdd(windowTNodeList, [0, winPos], newWindow)
curY += curYStep
editBuffer = winNode.child.editorList
editor = editBuffer.current
editorAdd = editBuffer.cursorAdd
newEditor = editor.updateSize(maxX, curYStep)
editorTNodeList = tn.replaceAdd(editorTNodeList, editorAdd, newEditor)
newwindowList = self.windowList.syncToNewRoot(windowTNodeList)
self.editorList = buffer.SimpleBuffer(editorTNodeList)
self.windowList = syncWindowsToEditorList(newwindowList, self.editorList)
def calculateMsg(self):
if self.message:
return self.message
elif self.activeWindow.message:
return self.activeWindow.message
else:
return ''
def printToScreen(self, image, posx, posy):
for y, col in enumerate(image):
for x, cell in enumerate(col):
self.app.screenPrint(posx + x, posy + y, cell)
def draw(self):
self.calculateWindowPositions()
maxX = self.app.screenCols
curY = 0
wins = self.windowList.length()
screenForWins = self.app.screenRows - 1 # leave space for cmd bar
minYStep = screenForWins / wins
leftover = screenForWins % wins
for winNode in self.windowList.rootChild():
window = winNode.child
if leftover > 0:
curYStep = minYStep + 1
leftover -= 1
else:
curYStep = minYStep
if winNode == self.windowList.cursor:
winImage = window.draw(0, curY, maxX, curYStep, True)
else:
winImage = window.draw(0, curY, maxX, curYStep, False)
self.printToScreen(winImage, 0, curY)
curY += curYStep
cmdPosy = screenForWins
if self.cmdBar:
cmdImage = self.cmdBar.draw(maxX, 1, isActive=True)
self.printToScreen(cmdImage, 0, cmdPosy)
else:
msg = screen.stringToImage(self.calculateMsg(), maxX, 1)
self.printToScreen(msg, 0, cmdPosy)
def matchWindowToClick(self, x, y):
def matchWin(win):
return True if win.posx <= x < win.posx + win.maxx and win.posy <= y < win.posy + win.maxy -1 \
else False
return self.windowList.findFirst(matchWin)
# not sure if this is conceptually the right way to go about doing this, but once we've collect all the
# updates in a newWindowList then this function unpicks it all to get at the various level of changes:
# newWindow, new editor, new image
# and then syncs the various parts
def integrateUpdatedWindowList(self, newWinList):
newWin = newWinList.current
newEditorList = newWin.editorList
newEditor = newEditorList.current
if newEditor.isRootImageEditor() and self.ImageRoot != newEditor.buffer.root:
newImage = newEditor.buffer.root
self.ImageRoot = newImage
if newEditor.updateUndo:
self.hist = tn.cons(self.ImageRoot.child, self.hist)
syncedEditorList = syncEditorsToImage(newEditorList, newImage)
else:
syncedEditorList = newEditorList
self.store = newEditor.store
storeSyncedEditorList = syncEditorsToStore(syncedEditorList, newEditor.store)
return self.updateList(
('editorList', storeSyncedEditorList),
('windowList', syncWindowsToEditorList(newWinList, syncedEditorList)))
def loadNewImage(self, newImage):
syncedEditorList = syncEditorsToImage(self.editorList, newImage)
syncedWindowList = syncWindowsToEditorList(self.windowList, syncedEditorList)
return self.updateList(
('ImageRoot', newImage),
('hist', newImage),
('editorList', syncedEditorList),
('windowList', syncedWindowList))
def newStore(self, newStore):
syncedEditorList = syncEditorsToStore(self.editorList, newStore)
syncedWindowList = syncWindowsToEditorList(self.windowList, syncedEditorList)
return self.updateList(
('store', newStore),
('editorList', syncedEditorList),
('windowList', syncedWindowList))
def addWindow(self, newWindow):
newWinList = self.windowList.appendAtCursor(newWindow).curNext()
return self.integrateUpdatedWindowList(newWinList)
def replaceWindow(self, newWindow):
newWinList = self.windowList.replaceAtCursor(newWindow)
return self.integrateUpdatedWindowList(newWinList)
def evalCmdBarResult(self, cmdBuffer):
# Maybe should get done in the actual cmdbar
cmd = cmdBuffer.toPyExp()
print cmd
if cmd and cmd[0] in ('q', 'quit'):
return 'QUIT-WM'
result = eval.eval(buffer.BufferSexp(cmdBuffer.root), self.getCmdBarEnv())
print result
if isinstance(result, WindowManager):
return result.update('cmdBar', None)
self.message = reader.to_string(result)
return self.updateList(
('cmdBar', None))
def handleCmdBarInput(self, key):
cmdResult = self.cmdBar.handleKeys(key)
if cmdResult.returnState == 'ESCAPE':
return self.update('cmdBar', None)
elif cmdResult.returnState == 'PRINT':
return self.evalCmdBarResult(cmdResult.buffer)
else:
return self.update('cmdBar', cmdResult)
def handleMouse(self, mouse):
try:
newwindowList = self.matchWindowToClick(mouse.x, mouse.y)
except ValueError:
return self
else:
resultWin = newwindowList.current.handleMouse(mouse)
newwindowList2 = newwindowList.replaceAtCursor(resultWin)
return self.update('windowList', newwindowList2)
def handleKeys(self, key):
if self.cmdBar:
return self.handleCmdBarInput(key)
return self.update('message', None).handleKeysMain(key)
def handleKeysMain(self, key):
if self.winCmd:
result = self.wincl.process(key, self)
ret = result if result else self
return ret.update('winCmd', False)
mainResult = self.mainCl.process(key, self)
if mainResult:
return mainResult
else:
resultWin = self.activeWindow.handleKeys(key)
resultEd = resultWin.editor
if resultEd == 'UNDO':
return cmdUndo(self)
else:
return self.replaceWindow(resultWin)
def loadEditorSettingsFromPyExp(self, pyExp):
root = tn.TNode(tn.createTNodeExpFromPyExp(pyExp))
newBuff = buffer.BufferSexp(root)
return eval.eval(newBuff)
def cmdPrintNVS(self):
cur, NVS = self.activeWindow.editor.buffer.getNVSListAtCursor()
printableNVS = reader.to_string(NVS)
return self.update('message', printableNVS)
def cmdPrintCursor(self):
cursor = self.activeWindow.editor.buffer.current
printableCursor = reader.to_string(cursor)
print cursor
print type(cursor)
return self.update('message', printableCursor)
def cmdWriteAll(self):
self.cmdWriteImageTS()
self.cmdWriteEditorSettingsTS()
return self.update('message', "Image and editor settings saved")
def cmdWriteEditorSettingsTS(self):
# bit of a hack, but basically change the persistence definition at save time as currently difficult to
# retain with the way objects are set up.
self.editorList.persist.append('root')
self.windowList.persist.append('root')
pyObj = self.serialise()
text = reader.to_string(pyObj)
reader.writeLatestFile('editor-settings-fs/', 'EditorSettings', text)
return self.update('message', "Saving Editor settings")
def cmdWriteImageTS(self):
pyObj = self.ImageRoot.child.toPyNumberedExp()
text = reader.to_string(pyObj)
reader.writeLatestFile('imagefs/', 'image', text)
return self.update('message', "Saving Latest Image")
def cmdLoadLatestAll(self):
pyEditorLoad = reader.readLatestFile('editor-settings-fs/')
newWM = self.loadEditorSettingsFromPyExp(pyEditorLoad)
newWM.app = self.app
syncedWM = newWM.initStore()
# syncedWM = newWM.newStore(newWM.store)
return syncedWM.cmdLoadLatestImage()
def cmdLoadLatestImage(self):
pyImageLoad = reader.readLatestFile('imagefs/')
pyImage = [0]
pyImage.append(pyImageLoad)
imageRoot = tn.createTNodeExpFromPyNumberedExp(pyImage)
return self.loadNewImage(imageRoot)
def cmdLoadEditorSettings(self):
pyEditorLoad = reader.readLatestFile('editor-settings-fs/')
newWM = self.loadEditorSettingsFromPyExp(pyEditorLoad)
newWM2 = newWM.loadNewImage(self.ImageRoot)
return newWM2
def cmdWinDown(wm):
try:
return wm.update('windowList', wm.windowList.curNext())
except ValueError:
return wm
def cmdWinUp(wm):
try:
return wm.update('windowList', wm.windowList.curPrev())
except ValueError:
return wm
def cmdWinDel(wm):
if wm.windowList.length() > 1:
return wm.update('windowList', wm.windowList.deleteAtCursor())
else:
return wm
def cmdWinSplit(wm):
curWin = wm.activeWindow
newEd = curWin.editor.clone()
newWin = curWin.addEditor(newEd)
return wm.addWindow(newWin)
def cmdUndo(wm):
if wm.hist.next:
wm.hist = wm.hist.next
wm.ImageRoot = tn.TNode(wm.hist.child)
syncedEditorList = syncEditorsToImage(wm.editorList, wm.ImageRoot)
return wm.updateList(
('editorList', syncedEditorList),
('windowList', syncWindowsToEditorList(wm.windowList, syncedEditorList)))
def cmdWinNext(wm):
return wm.updateList(
('windowList', wm.windowList.curCycle()),
('winCmd', False))
def cmdRunProg(wm):
return wm.addWindow(window.cmdEditorRunProg(wm.activeWindow))
def cmdOpenWinOnCursor(wm):
return wm.addWindow(window.cmdNewEditorOnCursor(wm.activeWindow))
def cmdOpenWinInspectProc(wm):
try:
return wm.addWindow(window.cmdInspectProcedureCall2(wm.activeWindow))
except ex.UnappliedProcedureException:
return wm
def cmdExitWinMode(wm):
return wm.update('winCmd', False)
def cmdToggleFullscreen(wm):
print 'fullscreen'
iop.toggleFullScreen()
return wm.update('winCmd', False)
def cmdPlayMedia(wm):
wm.app.playMedia()
def cmdSave(wm):
wm.writeImage()
wm.writeEditor()
print "saving"
return wm.update('message', "Saving Image")
def cmdStartWinMode(wm):
return wm.updateList(
('message', 'Window Mode'),
('winCmd', True))
def cmdStartCmdBar(wm):
return wm.update('cmdBar', CmdBar())
def cmdScreenEditor(wm):
print "changing to screen mode"
return wm.replaceWindow(window.cmdNewScreenEditor(wm.activeWindow))
def cmdTextEditor(wm):
return wm.replaceWindow(window.cmdNewTextEditor(wm.activeWindow))
def cmdTextPager(wm):
print "changing to text paging mode"
return wm.replaceWindow(window.cmdNewPager(wm.activeWindow))
def cmdReplEditor(wm):
winCmd = window.createAddEditorCommand(repl.Repl)
return wm.replaceWindow(winCmd(wm.activeWindow))
def cmdSFE(wm):
winCmd = window.createAddEditorCommand(sfe.SimpleFileEditor.fromPath, './')
return wm.replaceWindow(winCmd(wm.activeWindow))
def cmdMusic(wm):
winCmd = window.createAddEditorCommand(sfe.SimpleFileEditor.fromPath, 'D:/Music', wm.store, wm.trackref)
return wm.replaceWindow(winCmd(wm.activeWindow))