-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathholo_layer.py
executable file
·438 lines (353 loc) · 16.5 KB
/
holo_layer.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2022 Andy Stewart
#
# Author: Andy Stewart <[email protected]>
# Maintainer: Andy Stewart <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import platform
import signal
import sys
import threading
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QColor, QPainter
from PyQt6.QtCore import QRectF
from epc.server import ThreadingEPCServer
from plugin.cursor_animation import CursorAnimation
from plugin.place_info import PlaceInfo
from plugin.sort_tab import SortTab
from plugin.window_border import WindowBorder
from plugin.window_number import WindowNumber
from plugin.window_screenshot import WindowScreenshot
from plugin.indent_line import IndentLine
from plugin.type_animation import TypeAnimation
from pynput.keyboard import Listener as kbListener
from PyQt6.QtGui import QGuiApplication, QPainterPath
from utils import *
class HoloLayer:
def __init__(self, args):
# Init EPC client port.
init_epc_client(int(args[0]))
# Init vars.
self.window_info_args = None
self.window_info = []
self.cursor_info_args = None
self.menu_info_args = None
self.is_insert_command = False
self.cursor_info = []
self.menu_info = []
self.sort_tab_info = {}
self.emacs_frame_info = None
self.holo_window = HoloWindow()
self.holo_window_is_show = True
self.emacs_xid = None
self.emacs_name = None
# Build EPC server.
self.server = ThreadingEPCServer(('127.0.0.1', 0), log_traceback=True)
self.server.logger.setLevel(logging.DEBUG)
self.server.allow_reuse_address = True
# ch = logging.FileHandler(filename=os.path.expanduser("~/holo-layer.log"), mode='w')
# formatter = logging.Formatter('%(asctime)s | %(levelname)-8s | %(lineno)04d | %(message)s')
# ch.setFormatter(formatter)
# ch.setLevel(logging.DEBUG)
# self.server.logger.addHandler(ch)
# self.server.logger = logger
self.server.register_instance(self) # register instance functions let elisp side call
# Start EPC server with sub-thread, avoid block Qt main loop.
self.server_thread = threading.Thread(target=self.server.serve_forever)
self.server_thread.start()
# Start key event listener thread.
self.key_event_listener = threading.Thread(target=self.listen_key_event)
self.key_event_listener.start()
# Pass epc port and webengine codec information to Emacs when first start holo-layer.
eval_in_emacs('holo-layer--first-start', self.server.server_address[1])
def update_window_info(self, emacs_frame_info, window_info_args, cursor_info_args, menu_info_args, is_insert_command):
self.is_insert_command = is_insert_command
cursor_info_args = cursor_info_args if len(cursor_info_args) else ""
window_info_args = window_info_args if len(window_info_args) else ""
menu_info_args = menu_info_args if len(menu_info_args) else ""
if emacs_frame_info != self.emacs_frame_info or window_info_args != self.window_info_args or menu_info_args != self.menu_info_args:
self.window_info_args = window_info_args
self.cursor_info_args = cursor_info_args
self.emacs_frame_info = emacs_frame_info
self.menu_info_args = menu_info_args
if self.window_info_args == "":
self.window_info = []
self.cursor_info = []
else:
self.window_info = list(map(lambda info: info.split(":"), self.window_info_args.split(",")))
self.cursor_info = self.cursor_info_args.split(':')
self.menu_info = list(map(lambda info: info.split(":"), self.menu_info_args.split(",")))
self.update()
elif cursor_info_args != self.cursor_info_args:
self.cursor_info_args = cursor_info_args
self.cursor_info = self.cursor_info_args.split(':')
self.update()
@PostGui()
def show_holo_window(self):
self.holo_window_is_show = True
self.holo_window.show_up()
@PostGui()
def hide_holo_window(self):
self.holo_window_is_show = False
self.holo_window.hide()
@PostGui()
def update(self):
if platform.system() == "Darwin":
from AppKit import NSApplication
NSApp = NSApplication.sharedApplication()
windows = NSApp.windows()
if windows and len(windows) > 0:
NSApp.windows()[0].makeKeyAndOrderFront_(None)
self.holo_window.update_info(self.emacs_frame_info, self.window_info, self.cursor_info, self.menu_info, self.sort_tab_info, self.is_insert_command)
def update_place_info(self, word):
self.holo_window.update_place_info(word)
def update_indent_info(self, emacs_indent_infos):
self.holo_window.update_indent_info(emacs_indent_infos)
@PostGui()
def show_window_number(self):
self.holo_window.show_window_number()
@PostGui()
def hide_window_number(self):
self.holo_window.hide_window_number()
def take_window_screenshot(self, screenshot_window_info):
self.screenshot_window_info = screenshot_window_info
self.take_screenshot()
@PostGui()
def take_screenshot(self):
self.holo_window.window_screenshot.take_screenshot(self.screenshot_window_info, self.emacs_frame_info)
@PostGui()
def render_sort_tab(self, tab_names, tab_modes, current_tab_index, current_tab_name,
tab_height, tab_name_max_length, emacs_frame_info,
emacs_theme_mode, emacs_theme_foreground_color, emacs_theme_background_color):
self.emacs_frame_info = emacs_frame_info
self.sort_tab_info = {
"tab_names": tab_names,
"tab_modes": tab_modes,
"current_tab_index": current_tab_index,
"current_tab_name": current_tab_name,
"tab_height": tab_height,
"tab_name_max_length": tab_name_max_length,
"emacs_theme_mode": emacs_theme_mode,
"emacs_theme_foreground_color": emacs_theme_foreground_color,
"emacs_theme_background_color": emacs_theme_background_color
}
self.update()
def listen_key_event(self):
while True:
with kbListener(
on_press=self.key_press,
on_release=self.key_release) as listener:
listener.join()
def key_press(self, key):
pass
def key_release(self, key):
if self.get_active_window_id() == self.get_emacs_id():
if not self.holo_window_is_show:
self.show_holo_window()
else:
if self.holo_window_is_show:
self.hide_holo_window()
def get_emacs_id(self):
if platform.system() == "Windows":
import pygetwindow as gw
if self.emacs_name is None:
self.emacs_name = get_emacs_func_result("get-emacs-name")
windows = gw.getWindowsWithTitle(self.emacs_name)
return windows[0]._hWnd if len(windows) > 0 else None
else:
if self.emacs_xid is None:
self.emacs_xid = get_emacs_func_result("get-emacs-id")
return self.emacs_xid
def get_active_window_id(self):
if platform.system() == "Darwin":
from AppKit import NSWorkspace
return NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationProcessIdentifier']
elif platform.system() == "Windows":
import pygetwindow as gw
return gw.getActiveWindow()._hWnd
else:
from Xlib import X
from Xlib.display import Display
if not hasattr(self, "NET_ACTIVE_WINDOW"):
self.disp = Display()
self.root = self.disp.screen().root
self.NET_ACTIVE_WINDOW = self.disp.intern_atom('_NET_ACTIVE_WINDOW')
response = self.root.get_full_property(self.NET_ACTIVE_WINDOW, X.AnyPropertyType)
win_id = response.value[0]
return win_id
def cleanup(self):
"""Do some cleanup before exit python process."""
close_epc_client()
class HoloWindow(QWidget):
def __init__(self) -> None:
super().__init__()
self.active_window_border_color = None
self.inactive_window_border_color = None
self.emacs_frame_info = None
self.emacs_indent_infos = None
self.menu_info = None
self.window_info = []
self.sort_tab_info = {}
self.place_word = ""
self.window_border = WindowBorder()
self.window_number = WindowNumber()
self.window_screenshot = WindowScreenshot()
self.cursor_animation = CursorAnimation(self)
self.place_info = PlaceInfo()
self.sort_tab = SortTab()
self.indent_line = IndentLine()
self.show_window_number_flag = False
self.setStyleSheet("border: none;")
self.setContentsMargins(0, 0, 0, 0)
self.setStyleSheet("background-color:transparent;")
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
self.screen_index = 0
self.screen = QGuiApplication.primaryScreen()
self.screen_geometry = self.screen.availableGeometry()
self.setGeometry(self.screen_geometry)
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
self.type_animation = TypeAnimation(self)
layout.addWidget(self.type_animation)
self.setLayout(layout)
self.show_up()
def show_up(self):
if not self.isVisible():
window_flags = Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint | Qt.WindowType.WindowTransparentForInput | Qt.WindowType.WindowDoesNotAcceptFocus
if platform.system() == "Darwin":
window_flags |= Qt.WindowType.NoDropShadowWindowHint
self.setWindowFlags(window_flags)
self.window_bias_x, self.window_bias_y = self.screen_geometry.x(), self.screen_geometry.y()
self.show()
else:
# Why use `Tool and X11BypassWindowManagerHint` flag for holo-layer fullsreen window?
#
# Because below flag can't work. ;)
# 1. Qt.WindowType.SplashScreen, SplashScreen will change behavior with different Qt version
# 2. Qt.WindowType.Popup, popup window is modal window, and it will grab keyboard to cause system can't response keyboard event
# 3. Qt.WindowType.Tooltip or Qt.Window.X11BypassWindowManagerHint, window height can't fullscreen, window border at bottom can't render
# 4. Qt.WindowType.SubWindow, window can't skip taskbar or alt-tab window list.
if platform.system() == "Linux":
window_flags |= Qt.WindowType.Tool
window_flags |= Qt.WindowType.X11BypassWindowManagerHint
self.setWindowFlags(window_flags)
self.window_bias_x, self.window_bias_y = 0, 0
self.showFullScreen()
def paintEvent(self, event):
painter = QPainter(self)
background_color = QColor(0, 0, 0, 0)
painter.setBrush(background_color)
painter.setPen(background_color)
if self.emacs_frame_info:
[x, y, w, h] = self.emacs_frame_info
painter.eraseRect(x, y, w, h)
else:
painter.eraseRect(self.rect())
self.sort_tab.draw(painter, self.emacs_frame_info, self.sort_tab_info)
self.cursor_animation.draw(painter)
painter.setBrush(background_color)
painter.setPen(background_color)
self.update_menu_clip_area(painter)
self.indent_line.draw(painter, self.emacs_indent_infos, self.emacs_frame_info)
self.window_border.draw(painter, self.window_info, self.emacs_frame_info)
self.place_info.draw(painter, self.window_info, self.emacs_frame_info, self.place_word)
if self.show_window_number_flag:
self.window_number.draw(painter, self.window_info, self.emacs_frame_info)
def update_menu_clip_area(self, painter):
if self.emacs_frame_info:
[emacs_x, emacs_y, emacs_width, emacs_height] = self.emacs_frame_info
emacs_area = QPainterPath()
emacs_area.addRect(QRectF(emacs_x, emacs_y, emacs_width, emacs_height))
if self.menu_info:
total_mask = None
for info in self.menu_info:
try:
(x, y, w, h) = info
mask = QPainterPath()
mask.addRect(int(x), int(y), int(w), int(h))
if total_mask is None:
total_mask = mask
else:
total_mask += mask
except:
pass
if total_mask is not None:
painter.setClipPath(emacs_area - total_mask, Qt.ClipOperation.IntersectClip)
def update_place_info(self, word):
word = word.lower()
if self.place_word != word:
self.place_word = word
self.update()
def update_indent_info(self, emacs_indent_infos):
self.emacs_indent_infos = emacs_indent_infos
self.update()
def update_screen_geometry_info(self, screen_index):
if platform.system() != "Darwin":
return
if screen_index != self.screen_index:
self.screen_index = screen_index
self.screen = super().screen().virtualSiblings()[screen_index]
self.screen_geometry = self.screen.availableGeometry()
self.window_bias_x, self.window_bias_y = self.screen_geometry.x(), self.screen_geometry.y()
self.setGeometry(self.screen_geometry)
self.move(self.window_bias_x, self.window_bias_y)
def update_info(self, emacs_frame_info, window_info, cursor_info, menu_info, sort_tab_info, is_insert_command):
if emacs_frame_info:
self.emacs_frame_info = emacs_frame_info[:4].copy()
self.update_screen_geometry_info(emacs_frame_info[4])
self.emacs_frame_info[0] -= self.window_bias_x
self.emacs_frame_info[1] -= self.window_bias_y
self.menu_info = menu_info
self.sort_tab_info = sort_tab_info
window_info = window_info.copy()
for i in range(len(window_info)):
[x, y, w, h, is_active_window] = window_info[i]
window_info[i] = [int(x), int(y), int(w), int(h), is_active_window]
self.window_info = window_info
if is_insert_command and self.type_animation.enable_type_animation:
if len(cursor_info) > 1:
type_animation_x = int(cursor_info[0])
type_animation_y = int(cursor_info[1])
if len(self.emacs_frame_info) > 1:
type_animation_x += self.emacs_frame_info[0]
type_animation_y += self.emacs_frame_info[1]
self.type_animation.show(type_animation_x, type_animation_y)
if not self.cursor_animation.update_info(cursor_info, self.emacs_frame_info):
# skip update if cursor position is changed.
self.update()
def show_window_number(self):
if len(self.window_info) > 1:
self.show_window_number_flag = True
self.update()
def hide_window_number(self):
self.show_window_number_flag = False
self.update()
if __name__ == "__main__":
if platform.system() == "Darwin":
import AppKit
info = AppKit.NSBundle.mainBundle().infoDictionary()
info["LSBackgroundOnly"] = "1"
app = QApplication(sys.argv)
app.setStyleSheet("""
QScrollBar:vertical, QScrollBar:horizontal {
width: 0px;
height: 0px;
}
""")
HoloLayer(sys.argv[1:])
signal.signal(signal.SIGINT, signal.SIG_DFL)
sys.exit(app.exec())