-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsync_scroll_manager.py
52 lines (40 loc) · 1.59 KB
/
sync_scroll_manager.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
import threading
import time
from ..core import log
class SyncScrollManager:
def __init__(self):
self.running = False
self.thread = None
self.lock = threading.Lock()
self.last_position = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.stop_sync_scroll()
def start_sync_scroll(self, target_type, active_view, target_view):
with self.lock:
if not self.running:
self.running = True
self.thread = threading.Thread(target=self.sync_scroll, args=(target_type, active_view, target_view))
self.thread.start()
def stop_sync_scroll(self):
with self.lock:
self.running = False
if self.thread and self.thread.is_alive():
self.thread.join(timeout=0.1)
self.thread = None
def sync_scroll(self, target_type, active_view, target_view):
try:
while self.running:
# log.debug('Sync scroll target: %s', target_type)
current_position = active_view.viewport_position()
# Only update if the position has changed
if current_position != self.last_position:
target_view.set_viewport_position(current_position, False)
self.last_position = current_position
time.sleep(0.05)
except Exception as e:
log.error('Error during sync_scroll: %s', e)
finally:
self.stop_sync_scroll()
sync_scroll_manager = SyncScrollManager()