-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy patheditor.py
More file actions
490 lines (411 loc) · 18.4 KB
/
editor.py
File metadata and controls
490 lines (411 loc) · 18.4 KB
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
# encoding: utf-8
import os
import re
import json
import objc
from io import open
from objc import super
from time import time
from ..lib.cocoa import *
from plotdevice.gui.preferences import get_default, editor_info
from plotdevice.gui import bundle_path, set_timeout
__all__ = ['EditorView', 'OutputTextView']
def args(*jsargs):
return ', '.join([json.dumps(v, ensure_ascii=False) for v in jsargs])
class DraggyWebView(WebView):
def initWithFrame_(self, rect):
return self.initWithFrame_frameName_groupName_(rect, None, None)
def draggingEntered_(self, sender):
pb = sender.draggingPasteboard()
options = { NSPasteboardURLReadingFileURLsOnlyKey:True,
NSPasteboardURLReadingContentsConformToTypesKey:NSImage.imageTypes() }
urls = pb.readObjectsForClasses_options_([NSURL], options)
strs = pb.readObjectsForClasses_options_([NSString], {})
rewrite = "\n".join(['"%s"'%u.path() for u in urls] + strs) + "\n"
pb.declareTypes_owner_([NSStringPboardType], self)
pb.setString_forType_(rewrite, NSStringPboardType)
return super(DraggyWebView, self).draggingEntered_(sender)
def performDragOperation_(self, sender):
pb = sender.draggingPasteboard()
txt = pb.readObjectsForClasses_options_([NSString], None)
if txt:
nc = NSNotificationCenter.defaultCenter()
nc.postNotificationName_object_userInfo_('DropOperation', self, txt[0])
sender.setAnimatesToDestination_(True)
return True
return False
def shouldCloseWithWindow(self):
return True
class EditorView(NSView):
document = IBOutlet()
jumpPanel = IBOutlet()
jumpLine = IBOutlet()
# WebKit mgmt
def awakeFromNib(self):
self.webview = DraggyWebView.alloc().initWithFrame_(self.bounds())
self.webview.setAllowsUndo_(False)
self.webview.setFrameLoadDelegate_(self)
self.webview.setUIDelegate_(self)
self.addSubview_(self.webview)
self.webview.setHidden_(True)
html = bundle_path(rsrc='ui/editor.html')
ui = open(html, encoding='utf-8').read()
baseurl = NSURL.fileURLWithPath_(os.path.dirname(html))
self.webview.mainFrame().loadHTMLString_baseURL_(ui, baseurl)
# set a theme-derived background for the webview's clipview
docview = self.webview.mainFrame().frameView().documentView()
clipview = docview.superview()
scrollview = clipview.superview()
if clipview is not None:
bgcolor = editor_info('colors')['background']
clipview.setDrawsBackground_(True)
clipview.setBackgroundColor_(bgcolor)
scrollview.setVerticalScrollElasticity_(1)
scrollview.setScrollerKnobStyle_(2)
nc = NSNotificationCenter.defaultCenter()
nc.addObserver_selector_name_object_(self, "themeChanged", "ThemeChanged", None)
nc.addObserver_selector_name_object_(self, "fontChanged", "FontChanged", None)
nc.addObserver_selector_name_object_(self, "bindingsChanged", "BindingsChanged", None)
nc.addObserver_selector_name_object_(self, "insertDroppedFiles:", "DropOperation", self.webview)
self._wakeup = set_timeout(self, '_jostle', .05, repeat=True)
self._queue = []
self._edits = 0
self.themeChanged()
self.fontChanged()
self.bindingsChanged()
mm=NSApp().mainMenu()
self._doers = mm.itemWithTitle_('Edit').submenu().itemArray()[1:3]
self._undo_mgr = None
def drawRect_(self, rect):
if self._wakeup:
# try to minimize the f.o.u.c. while the webview starts up
bgcolor = editor_info('colors')['background']
bgcolor.setFill()
NSRectFillUsingOperation(rect, NSCompositeCopy)
super(EditorView, self).drawRect_(rect)
def _jostle(self):
awoke = self.webview.stringByEvaluatingJavaScriptFromString_('window.editor && window.editor.ready')
if awoke:
for op in self._queue:
self.webview.stringByEvaluatingJavaScriptFromString_(op)
self._wakeup.invalidate()
self._wakeup = None
self._queue = None
self.webview.setHidden_(False)
def _cleanup(self):
nc = NSNotificationCenter.defaultCenter()
nc.removeObserver_(self)
self._doers = self._undo_mgr = self.jumpPanel = self.jumpLine = None
# def webView_didFinishLoadForFrame_(self, sender, frame):
def webView_didClearWindowObject_forFrame_(self, sender, win, frame):
self.webview.windowScriptObject().setValue_forKey_(self, 'app')
def webView_contextMenuItemsForElement_defaultMenuItems_(self, sender, elt, menu):
items = [
NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("Cut", "cut:", ""),
NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("Copy", "copy:", ""),
NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("Paste", "paste:", ""),
NSMenuItem.separatorItem(),
]
# once a doc viewer exists, add a lookup-ref menu item pointing to it:
# word = self.js('editor.selected')
# _ns = ['curveto', 'TEXT', 'BezierPath', ...]
# def ref_url(proc):
# if proc in _ns:
# return proc
# if ref_url(word):
# doc = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(u"Documentation for ‘%s()’"%word, "copy:", "")
# sep = NSMenuItem.separatorItem()
# items.insert(0, sep)
# items.insert(0, doc)
return items + [it for it in menu if it.title()=='Inspect Element']
def resizeSubviewsWithOldSize_(self, oldSize):
self.resizeWebview()
def resizeWebview(self):
self.webview.setFrame_(self.bounds())
def insertDroppedFiles_(self, note):
self.js('editor.insert', args(note.userInfo()))
def isSelectorExcludedFromWebScript_(self, sel):
return False
def windowDidResignKey_(self, note):
if note.object() is self.jumpPanel:
self.jumpPanel.orderOut_(self)
def validateMenuItem_(self, item):
# we're the delegate for the Edit menu
if item.title=='Undo':
return self._undo_mgr.canUndo()
elif item.title=='Redo':
return self._undo_mgr.canRedo()
return True
def updateTrackingAreas(self):
# keep a single tracking area (with our whole bounds) up to date
for area in self.trackingAreas():
self.removeTrackingArea_(area)
opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp);
area = NSTrackingArea.alloc().initWithRect_options_owner_userInfo_(self.bounds(), opts, self, None)
self.addTrackingArea_(area)
def mouseExited_(self, e):
# use the tracking area to restore the cursor when it leaves the bounds.
# works around a bug where either ace.js or the WebView was leaving the
# I-beam cursor active on-exit
NSCursor.arrowCursor().set()
# App-initiated actions
@objc.python_method
def _get_source(self):
return self.webview.stringByEvaluatingJavaScriptFromString_('editor.source();')
@objc.python_method
def _set_source(self, src):
self.js('editor.source', args(src))
source = property(_get_source, _set_source)
def fontChanged(self, note=None):
info = editor_info()
self.js('editor.font', args(info['family'], info['px']))
def themeChanged(self, note=None):
info = editor_info()
clipview = self.webview.mainFrame().frameView().documentView().superview()
clipview.setBackgroundColor_(info['colors']['background'])
self.js('editor.theme', args(info['module']))
def bindingsChanged(self, note=None):
self.js('editor.bindings', args(get_default('bindings')))
def focus(self):
self.js('editor.focus')
def blur(self):
self.js('editor.blur')
def clearErrors(self):
self.js('editor.mark', args(None))
@objc.python_method
def report(self, crashed, script):
if not crashed:
self.js('editor.mark', args(None))
else:
exc, traceback = crashed
err_lines = [line-1 for fn, line, env, src in reversed(traceback) if fn==script]
self.js('editor.mark', args("\n".join(exc), err_lines))
@objc.python_method
def js(self, cmd, args=''):
op = '%s(%s);'%(cmd,args)
if self._wakeup:
self._queue.append(op)
else:
return self.webview.stringByEvaluatingJavaScriptFromString_(op)
# Menubar actions
@IBAction
def editorAction_(self, sender):
# map the tags in the nib's menu items to ace.js commandnames
cmds = [None, 'selectline', 'centerselection', 'splitIntoLines', 'addCursorAbove', 'addCursorBelow',
'selectMoreAfter', 'selectMoreBefore', 'blockindent', 'blockoutdent', 'movelinesup',
'movelinesdown', 'togglecomment', 'modifyNumberUp','modifyNumberDown']
self.js('editor.exec', args(cmds[sender.tag()]))
@IBAction
def jumpToLine_(self, sender):
# place the panel in the middle of the editor's rect and display it
e_frame = self.frame()
p_frame = self.jumpPanel.frame()
w_frame = self.window().frame()
e_frame.origin.x = w_frame.origin.x + (w_frame.size.width - e_frame.size.width)
e_frame.origin.y = w_frame.origin.y + (w_frame.size.height - e_frame.size.height) - 22
p_frame.origin.x = int(e_frame.origin.x + (e_frame.size.width-p_frame.size.width)/2.0)
p_frame.origin.y = int(e_frame.origin.y + (e_frame.size.height-p_frame.size.height)/2.0)
self.jumpLine.setStringValue_('')
self.jumpPanel.setFrame_display_(p_frame, False)
self.jumpPanel.makeKeyAndOrderFront_(self)
@IBAction
def performJump_(self, sender):
# if triggered by the ok button, jump to the line. otherwise just hide the panel if sender.tag():
if sender.tag():
try:
line_str = re.sub(r'[^\d\.]', '', self.jumpLine.stringValue())
line = round(float(line_str))
self.js('editor.jump', args(line))
except ValueError:
pass # ignore non-integer input
self.jumpPanel.orderOut_(self)
@IBAction
def aceAutocomplete_(self, sender):
cmd = ['startAutocomplete', 'expandSnippet'][sender.tag()]
self.js('editor.exec', args(cmd))
@IBAction
def aceWrapLines_(self, sender):
newstate = NSOnState if sender.state()==NSOffState else NSOffState
sender.setState_(newstate)
self.js('editor.wrap', args(newstate==NSOnState))
@IBAction
def aceInvisibles_(self, sender):
newstate = NSOnState if sender.state()==NSOffState else NSOffState
sender.setState_(newstate)
self.js('editor.invisibles', args(newstate==NSOnState))
@IBAction
def performFindAction_(self, sender):
actions = {1:'find', 2:'findnext', 3:'findprevious', 4:'replace', 7:'setneedle'}
self.js('editor.exec', args(actions[sender.tag()]))
# JS-initiated actions
@IBAction
def undoAction_(self, sender):
undoredo = ['editor.undo', 'editor.redo']
self.js(undoredo[sender.tag()])
def loadPrefs(self):
NSApp().delegate().showPreferencesPanel_(self)
def cancelRun(self):
# catch command-period even when the editor is first responder
mm=NSApp().mainMenu()
menu = mm.itemWithTitle_("Python")
menu.submenu().performActionForItemAtIndex_(3)
def edits_(self, count):
# inform the undo manager of the changes
um = self._undo_mgr
c = int(count)
while self._edits < c:
um.prepareWithInvocationTarget_(self).syncUndoState_(self._edits)
self._edits+=1
while self._edits > c:
self._edits-=1
um.undo()
# update the undo/redo menus items
for item, can in zip(self._doers, (um.canUndo(), um.canRedo())):
item.setEnabled_(can)
def syncUndoState_(self, count):
pass # this would be useful if only it got called for redo as well as undo...
@objc.python_method
def setSearchPasteboard(self, query):
if not query: return
pb = NSPasteboard.pasteboardWithName_(NSFindPboard)
pb.declareTypes_owner_([NSStringPboardType],None)
pb.setString_forType_(query, NSStringPboardType)
self.flash_("Edit")
def flash_(self, menuname):
# when a menu item's key command was entered in the editor, flash the menu
# bar to give a hint of where the command lives
mm=NSApp().mainMenu()
menu = mm.itemWithTitle_(menuname)
menu.submenu().performActionForItemAtIndex_(0)
class OutputTextView(NSTextView):
# editor = IBOutlet()
endl = False
scroll_lock = True
def awakeFromNib(self):
self.ts = self.textStorage()
self.colorize()
self.setTextContainerInset_( (0,4) ) # a pinch of top-margin
# self.textContainer().setWidthTracksTextView_(NO) # disable word-wrap
# self.textContainer().setContainerSize_((10000000, 10000000))
# use a FindBar rather than FindPanel
self.setUsesFindBar_(True)
self._finder = NSTextFinder.alloc().init()
self._finder.setClient_(self)
self._finder.setFindBarContainer_(self.enclosingScrollView())
self._findTimer = None
self.setUsesFindBar_(True)
nc = NSNotificationCenter.defaultCenter()
nc.addObserver_selector_name_object_(self, "themeChanged", "ThemeChanged", None)
nc.addObserver_selector_name_object_(self, "fontChanged", "FontChanged", None)
def _cleanup(self):
nc = NSNotificationCenter.defaultCenter()
nc.removeObserver_(self)
def fontChanged(self, note=None):
self.setFont_(editor_info('font'))
self.colorize()
def themeChanged(self, note=None):
self.colorize()
def canBecomeKeyView(self):
return False
def colorize(self):
clr, font = editor_info('colors'), editor_info('font')
self.setBackgroundColor_(clr['background'])
self.setTypingAttributes_({"NSColor":clr['color'], "NSFont":font, "NSLigature":0})
self.setSelectedTextAttributes_({"NSBackgroundColor":clr['selection']})
scrollview = self.superview().superview()
scrollview.setScrollerKnobStyle_(2 if editor_info('dark') else 1)
# recolor previous contents
attrs = self._attrs()
self.ts.beginEditing()
last = self.ts.length()
cursor = 0
while cursor < last:
a, r = self.ts.attributesAtIndex_effectiveRange_(cursor, None)
self.ts.setAttributes_range_(attrs[a['stream']],r)
cursor = r.location+r.length
self.ts.endEditing()
def _attrs(self, stream=None):
clr, font = editor_info('colors'), editor_info('font')
basic_attrs = {"NSFont":font, "NSLigature":0}
attrs = {
'message':{"NSColor":clr['color']},
'info':{"NSColor":clr['comment']},
'err':{"NSColor":clr['error']}
}
for s,a in attrs.items():
a.update(basic_attrs)
a.update({"stream":s})
if stream:
return attrs.get(stream)
return attrs
def changeColor_(self, clr):
pass # ignore system color panel
@objc.python_method
def append(self, txt, stream='message'):
if not txt: return
defer_endl = txt.endswith('\n')
txt = ("\n" if self.endl else "") + (txt[:-1 if defer_endl else None])
atxt = NSAttributedString.alloc().initWithString_attributes_(txt, self._attrs(stream))
self.ts.beginEditing()
self.ts.appendAttributedString_(atxt)
self.ts.endEditing()
self.scrollRangeToVisible_(NSMakeRange(self.ts.length()-1, 0))
self.endl = defer_endl
self.setNeedsDisplay_(True)
def clear(self, timestamp=False):
self.endl = False
self.ts.replaceCharactersInRange_withString_((0,self.ts.length()), "")
self._begin = time()
if timestamp:
locale = NSUserDefaults.standardUserDefaults().dictionaryRepresentation()
timestamp = NSDate.date().descriptionWithCalendarFormat_timeZone_locale_("%Y-%m-%d %H:%M:%S", None, locale)
self.append(timestamp+"\n", 'info')
@objc.python_method
def report(self, crashed, frames):
if not hasattr(self, '_begin'):
return
val = time() - self._begin
# print "ran for", (time() - self._begin), "then", ("crashed" if crashed else "exited cleanly")
if crashed or (frames==None and val < 0.333):
del self._begin
return
hrs = val // 3600
val = val - (hrs * 3600)
mins = val // 60
secs = val - (mins * 60)
dur = ''
if hrs:
dur = '%ih%i\'%1.1f"' % (hrs, mins, secs)
else:
dur = '%i\'%1.1f"' % (mins, secs)
msg = "%i frame%s"%(frames, '' if frames==1 else 's') if frames else "rendered"
outcome = "%s in %s\n"%(msg, dur)
self.append(outcome, 'info')
del self._begin
@IBAction
def performFindAction_(self, sender):
# this is the renamed target of the Find... menu items (shared with EditorView)
# just pass the event along to the real implementation
self.performFindPanelAction_(sender)
def performFindPanelAction_(self, sender):
# frustrating bug:
# when the find bar is dismissed with esc, the *other* textview becomes
# first responder. the `solution' here is to monitor the find bar's field
# editor and notice when it is detached from the view hierarchy. it then
# re-sets itself as first responder
super(OutputTextView, self).performFindPanelAction_(sender)
if self._findTimer:
self._findTimer.invalidate()
self._findEditor = self.window().firstResponder().superview().superview()
self._findTimer = set_timeout(self, 'stillFinding:', 0.05, repeat=True)
def stillFinding_(self, note):
active = self._findEditor.superview().superview() is not None
if not active:
self.window().makeFirstResponder_(self)
self._findTimer.invalidate()
self._findTimer = None
def __del__(self):
nc = NSNotificationCenter.defaultCenter()
nc.removeObserver_(self)
if self._findTimer:
self._findTimer.invalidate()