|
39 | 39 | import os
|
40 | 40 | import platform
|
41 | 41 | import pathlib
|
| 42 | +import threading |
42 | 43 |
|
43 | 44 | MOUSE_LEFT_BUTTON = 1
|
44 | 45 | MOUSE_WHEEL_BUTTON = 4
|
@@ -82,6 +83,9 @@ def __init__(self, buffer_id):
|
82 | 83 | self.focus_input_js = None
|
83 | 84 | self.simulated_wheel_event = False
|
84 | 85 |
|
| 86 | + self.last_mouse_word = None |
| 87 | + self.last_mouse_word_timer = None |
| 88 | + |
85 | 89 | (self.default_zoom, self.zoom_step,
|
86 | 90 | self.show_hover_link, self.marker_letters,
|
87 | 91 | self.marker_fontsize, self.scroll_step) = get_emacs_vars(
|
@@ -275,6 +279,20 @@ def eventFilter(self, obj, event):
|
275 | 279 | else:
|
276 | 280 | focus_emacs_buffer(self.buffer_id)
|
277 | 281 |
|
| 282 | + if event.type() == QEvent.Type.MouseMove: |
| 283 | + modifiers = QApplication.keyboardModifiers() |
| 284 | + if modifiers == Qt.KeyboardModifier.ControlModifier: |
| 285 | + word = self.get_cursor_word(event.pos().x() / self.zoomFactor(), event.pos().y() / self.zoomFactor()) |
| 286 | + if self.last_mouse_word != word: |
| 287 | + self.last_mouse_word = word |
| 288 | + |
| 289 | + if self.last_mouse_word is not None: |
| 290 | + if self.last_mouse_word_timer is not None and self.last_mouse_word_timer.is_alive(): |
| 291 | + self.last_mouse_word_timer.cancel() |
| 292 | + |
| 293 | + self.last_mouse_word_timer = threading.Timer(1, lambda : self.translate_cursor_word(word)) |
| 294 | + self.last_mouse_word_timer.start() |
| 295 | + |
278 | 296 | if event.type() == QEvent.Type.MouseButtonPress:
|
279 | 297 |
|
280 | 298 | if platform.system() == "Darwin":
|
@@ -322,6 +340,9 @@ def eventFilter(self, obj, event):
|
322 | 340 |
|
323 | 341 | return super(QWebEngineView, self).eventFilter(obj, event)
|
324 | 342 |
|
| 343 | + def translate_cursor_word(self, word): |
| 344 | + self.translate_selected_text.emit(word) |
| 345 | + |
325 | 346 | def link_hovered(self, url):
|
326 | 347 | self.url_hovered = url
|
327 | 348 |
|
@@ -715,6 +736,10 @@ def focus_input(self):
|
715 | 736 | self.eval_js(self.focus_input_js)
|
716 | 737 | eval_in_emacs('eaf-update-focus-state', [self.buffer_id, "'t"])
|
717 | 738 |
|
| 739 | + def get_cursor_word(self, x, y): |
| 740 | + get_cursor_word_js = self.read_js_content("get_cursor_word.js").replace("%{mouse_x}", str(x)).replace("%{mouse_y}", str(y)) |
| 741 | + return self.execute_js(get_cursor_word_js) |
| 742 | + |
718 | 743 | @interactive
|
719 | 744 | def clear_focus(self):
|
720 | 745 | ''' Clear the focus.'''
|
|
0 commit comments