forked from chikatoike/IMESupport
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathimesupportplugin.py
96 lines (70 loc) · 2.82 KB
/
imesupportplugin.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
import os
import time
import sublime
import sublime_plugin
try:
from imesupport import globalhook
except ImportError:
from .imesupport import globalhook
class SlackCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
class WindowLayout(object):
def __init__(self, window):
self.window = window
self.last_extents = None
self.settings = sublime.load_settings('IMESupport.sublime-settings')
def calc_cursor_position(self, view, cursor):
if "text_to_window" in dir(view):
pos = view.text_to_window(cursor)
font_size = self.get_font_size(view)
caret_width = view.settings().get('caret_extra_width')
scaling = self.settings.get('imesupport_screen_scaling')
return (int(pos[0]) + caret_width, int(pos[1]), font_size, scaling)
@staticmethod
def get_font_face(view):
return view.settings().get('font_face', '')
@staticmethod
def get_font_size(view):
return int(view.settings().get('font_size', ''))
def setup(hwnd):
if int(sublime.version()) < 3000:
pass
else:
# load dll
globalhook.setup(hwnd, sublime.arch() == 'x64')
class ImeSupportEventListener(sublime_plugin.EventListener):
def __init__(self):
self.layouts = {}
self.initialized = False
def on_new(self, view):
sublime.set_timeout(lambda: self.update(view), 400)
def on_activated(self, view):
sublime.set_timeout(lambda: self.update(view), 200)
def on_deactivated(self, view):
sublime.set_timeout(lambda: self.update(view), 200)
def on_modified(self, view):
sublime.set_timeout(lambda: self.update(view), 200)
def on_selection_modified(self, view):
sublime.set_timeout(lambda: self.update(view), 200)
def on_post_window_command(self, window, command_name, args):
sublime.set_timeout(lambda: self.update(window.active_view()), 400)
def update(self, view):
if view is None:
return
window = view.window()
if window is None:
return
if not self.initialized:
setup(window.hwnd())
fontFile = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'font_name.txt')
with open(fontFile, 'wb') as f:
f.write(view.settings().get('font_face', '').encode('utf-8'))
self.initialized = True
id = window.id()
if id not in self.layouts:
self.layouts[id] = WindowLayout(window)
pos = self.layouts[id].calc_cursor_position(view, view.sel()[0].b)
globalhook.set_inline_position(window.hwnd(), *pos)
def plugin_unload():
globalhook.cleanup()