-
Notifications
You must be signed in to change notification settings - Fork 5
/
mouse_event_listener.py
47 lines (39 loc) · 1.8 KB
/
mouse_event_listener.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
import sublime, sublime_plugin
class DragSelectCallbackCommand(sublime_plugin.TextCommand):
def run_(self, edit, args):
for c in sublime_plugin.all_callbacks.setdefault('on_pre_mouse_down',[]):
c.on_pre_mouse_down(args)
#We have to make a copy of the selection, otherwise we'll just have
#a *reference* to the selection which is useless if we're trying to
#roll back to a previous one. A RegionSet doesn't support slicing so
#we have a comprehension instead.
old_sel = [r for r in self.view.sel()]
#Only send the event so we don't do an extend or subtract or
#whatever. We want the only selection to be where they clicked.
self.view.run_command("drag_select", {'event': args['event']})
new_sel = self.view.sel()
click_point = new_sel[0].a
#Restore the old selection so when we call drag_select it will
#behave normally.
new_sel.clear()
for r in old_sel:
new_sel.add(r)
#This is the "real" drag_select that alters the selection for real.
self.view.run_command("drag_select", args)
for c in sublime_plugin.all_callbacks.setdefault('on_post_mouse_down',[]):
c.on_post_mouse_down(click_point)
class MouseEventListener(sublime_plugin.EventListener):
#If we add the callback names to the list of all callbacks, Sublime
#Text will automatically search for them in future imported classes.
#You don't actually *need* to inherit from MouseEventListener, but
#doing so forces you to import this file and therefore forces Sublime
#to add these to its callback list.
sublime_plugin.all_callbacks.setdefault('on_pre_mouse_down', [])
sublime_plugin.all_callbacks.setdefault('on_post_mouse_down', [])
"""
class MouseEventProcessor(MouseEventListener):
def on_pre_mouse_down(self, args):
print "on pre-click!", args
def on_post_mouse_down(self, point):
print "on post-click!", point
"""