forked from HaujetZhao/CapsWriter-Offline
-
Notifications
You must be signed in to change notification settings - Fork 3
/
start_client_gui.py
572 lines (496 loc) · 26.5 KB
/
start_client_gui.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import os
import sys
import subprocess
from queue import Queue
import threading
from PySide6.QtWidgets import (QApplication,
QMainWindow,
QTextEdit,
QSystemTrayIcon,
QMenu,
QPushButton,
QCheckBox, QVBoxLayout,
QHBoxLayout,
QWidget,
QLabel,
QSpacerItem,
QSizePolicy,
QLabel)
from PySide6.QtGui import (QIcon,
QAction,
QPalette,
QColor,
QFont,
QWheelEvent)
from PySide6.QtCore import (Qt,
QPoint,
QTimer)
from qt_material import apply_stylesheet
from config import ClientConfig as Config
from util.check_process import check_process
from util.cloud_clipboard_show_qrcode import CloudClipboardShowQRCode
import win32api
import win32con
import win32gui
import win32print
import keyboard
class Hint_While_Recording_At_Cursor_Position(QLabel):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.ToolTip | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setVisible(False) # 初始时隐藏标签
# 创建一个定时器来定期更新鼠标位置
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_tooltip_position)
self.timer.start(100) # 每100毫秒更新一次
def update_tooltip_position(self):
# 使用pywin32获取全局鼠标位置
x, y = win32api.GetCursorPos()
global scale_x, scale_y
x, y = x / scale_x, y / scale_y
# 更新标签的位置和文本
self.move(x+(20/scale_x),y+(20/scale_y))
if keyboard.is_pressed(Config.speech_recognition_shortcut):
self.setText(chr(0xF8B1))
self.setVisible(True)
else:
self.setVisible(False)
class GUI(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
self.output_queue_client = Queue()
self.start_script()
self.edgeMargin = 5 # 侧边停靠残余像素值
self.isBerthLeft = False
self.isBerthRight = False
def init_ui(self):
self.resize(425, 425)
self.setWindowTitle('CapsWriter-Offline-Client')
self.setWindowIcon(QIcon("assets/client-icon.ico"))
self.setWindowOpacity(0.9)
self.setWindowFlags(
self.windowFlags()
| Qt.FramelessWindowHint # 隐藏标题栏
| Qt.Tool # 隐藏Windows任务栏上的图标
| Qt.WindowStaysOnTopHint # 置顶
)
self.create_stay_on_top_button()
self.create_cloudypaste_button() # Create cloudy paste button
self.create_clear_button() # Create clear button
self.create_close_button()
self.create_custom_title_bar()
self.create_text_box()
self.create_monitor_checkbox() # Create monitor checkbox
# self.create_stay_on_top_checkbox()
self.create_wordcount_label()
self.create_systray_icon()
# Create a vertical layout
self.layout = QVBoxLayout()
self.layout.setSpacing(0) # 设置控件间距为0像素
self.layout.setContentsMargins(3, 3, 3, 3) # 设置左、上、右、下的边距
self.layout2 = QHBoxLayout()
self.layout2.setSpacing(0) # 设置控件间距为0像素
self.layout2.setContentsMargins(0, 0, 0, 0) # 设置左、上、右、下的边距为0像素
# Add text box and button to the layout
self.layout.addLayout(self.title_bar)
self.layout.addWidget(self.text_box_client)
self.layout2.addWidget(self.monitor_checkbox, alignment=Qt.AlignLeft)
# self.layout2.addWidget(self.stay_on_top_checkbox, alignment=Qt.AlignLeft)
self.layout2.addSpacerItem(QSpacerItem(40, 0, QSizePolicy.Expanding, QSizePolicy.Minimum))
self.layout2.addWidget(self.text_box_wordCountLabel, alignment=Qt.AlignRight)
self.layout.addLayout(self.layout2)
# Create a central widget
central_widget = QWidget()
central_widget.setLayout(self.layout)
# Set the central widget
self.setCentralWidget(central_widget)
def create_custom_title_bar(self):
# 创建自定义标题栏
self.title_bar = QHBoxLayout()
self.title_bar.addWidget(self.stay_on_top_button)
self.title = QLabel("CapsWriter-Offline-Client")
font = QFont()
font.setBold(True)
self.title.setFont(font)
self.title_bar.addWidget(self.title)
self.title_bar.addSpacerItem(QSpacerItem(80, 0, QSizePolicy.Expanding, QSizePolicy.Minimum))
self.title_bar.addWidget(self.cloudypaste_button, alignment=Qt.AlignRight)
self.title_bar.addWidget(self.clear_button, alignment=Qt.AlignRight)
self.title_bar.addWidget(self.close_button)
def create_stay_on_top_button(self):
self.stay_on_top_button = QPushButton()
pin_char = chr(0xE840)
self.stay_on_top_button.setText(pin_char)
self.stay_on_top_button.setToolTip("置顶窗口,将它显示在其他窗口之上 / 不置顶")
self.stay_on_top_button.setMaximumSize(50, 50)
self.stay_on_top_button.clicked.connect(self.window_stay_on_top_toggled)
def create_close_button(self):
self.close_button = QPushButton(chr(0xE8BB))
self.close_button.setMaximumSize(50, 50)
self.close_button.clicked.connect(self.hide)
def create_text_box(self):
self.text_box_client = QTextEdit()
self.text_box_client.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.text_box_client.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
def create_monitor_checkbox(self):
# 创建一个QCheckBox控件
self.monitor_checkbox = QCheckBox("监听")
self.monitor_checkbox.setToolTip("监听客户端输出 / 不监听,仅用作笔记本")
self.monitor_checkbox.setMaximumSize(65, 30)
# 当状态改变时,调用self.on_monitor_toggled函数
self.monitor_checkbox.stateChanged.connect(self.on_monitor_toggled)
# 设置默认状态
self.monitor_checkbox.setChecked(True)
# def create_stay_on_top_checkbox(self):
# self.stay_on_top_checkbox = QCheckBox('置顶')
# self.stay_on_top_checkbox.setToolTip("置顶窗口,将它显示在其他窗口之上 / 不置顶")
# self.stay_on_top_checkbox.setMaximumSize(65, 30)
# self.stay_on_top_checkbox.stateChanged.connect(self.window_stay_on_top_toggled)
# self.stay_on_top_checkbox.setChecked(True)
def create_wordcount_label(self):
self.text_box_wordCountLabel = QLabel("字符数字节数", self)
self.text_box_wordCountLabel.setToolTip("光标已选中字符数 / 总字符数 | 字节数")
self.text_box_wordCountLabel.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
self.text_box_client.textChanged.connect(self.update_word_count_toggled)
self.text_box_client.selectionChanged.connect(self.update_word_count_toggled)
def create_cloudypaste_button(self):
self.cloudypaste_button = QPushButton(chr(0xE753), self)
self.cloudypaste_button.setToolTip("将文本上传至云剪切板,方便向ios设备分享。基于 cv.j20.cc ,一个无依赖即用即走的剪切板。实测5~1024字节,不足字节补.超出字节无效。")
self.cloudypaste_button.setMaximumSize(80, 30)
self.cloudypaste_button.clicked.connect(self.cloudy_paste)
def create_clear_button(self):
# Create a button
self.clear_button = QPushButton(chr(0xE75C), self)
self.clear_button.setToolTip("清空文本框中的全部内容")
self.clear_button.setMaximumSize(80, 30)
# Connect click event
self.clear_button.clicked.connect(lambda: self.clear_text_box())
def create_systray_icon(self):
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon("assets/client-icon.ico"))
edit_hot_en_action = QAction("Edit hot-en.txt", self)
edit_hot_rule_action = QAction("Edit hot-rule.txt", self)
edit_hot_zh_action = QAction("Edit hot-zh.txt", self)
edit_keyword_action = QAction("Edit keywords.txt", self)
explore_home_folder_action = QAction("📁 Open Home Folder With Explorer", self)
vscode_home_folder_action = QAction("🤓 Open Home Folder With VSCode", self)
chatglm_website_action = QAction("🤖 ChatGLM Website", self)
github_website_action = QAction("🌐 GitHub Website", self)
show_action = QAction("🪟 Show", self)
quit_action = QAction("❌ Quit", self)
edit_hot_en_action.triggered.connect(self.edit_hot_en)
edit_hot_rule_action.triggered.connect(self.edit_hot_rule)
edit_hot_zh_action.triggered.connect(self.edit_hot_zh)
edit_keyword_action.triggered.connect(self.edit_keyword)
explore_home_folder_action.triggered.connect(self.explore_home_folder)
vscode_home_folder_action.triggered.connect(self.vscode_home_folder)
chatglm_website_action.triggered.connect(self.open_chatglm_website)
github_website_action.triggered.connect(self.open_github_website)
show_action.triggered.connect(self.showNormal)
quit_action.triggered.connect(self.quit_app)
self.tray_icon.activated.connect(self.on_tray_icon_activated)
tray_menu = QMenu()
edit_menu = QMenu("📝 Edit Hot Rules", tray_menu)
view_menu = QMenu("👁️ View", tray_menu)
edit_menu.addAction(edit_hot_en_action)
edit_menu.addAction(edit_hot_rule_action)
edit_menu.addAction(edit_hot_zh_action)
edit_menu.addAction(edit_keyword_action)
view_menu.addAction(explore_home_folder_action)
view_menu.addAction(vscode_home_folder_action)
view_menu.addAction(chatglm_website_action)
tray_menu.addMenu(edit_menu)
tray_menu.addMenu(view_menu)
tray_menu.addAction(github_website_action)
tray_menu.addSeparator()
tray_menu.addAction(show_action)
tray_menu.addAction(quit_action)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
def cloudy_paste(self):
text = self.text_box_client.toPlainText()
CloudClipboardShowQRCode(text)
def clear_text_box(self):
# Clear the content of the client text box
self.text_box_client.clear()
# Resize Window
self.resize(425, 425)
def on_monitor_toggled(self, state):
# 检查复选框的选中状态
if state == 2: # 2 表示选中状态
self.update_timer.start(100)
else:
self.update_timer.stop()
# def window_stay_on_top_toggled(self):
# # 切换窗口置顶状态
# if self.windowFlags() & Qt.WindowStaysOnTopHint:
# self.setWindowFlags(self.windowFlags() ^ Qt.WindowStaysOnTopHint)
# else:
# self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
# self.show() # 重新显示窗口以应用更改
def window_stay_on_top_toggled(self):
# 切换窗口置顶状态
if self.windowFlags() & Qt.WindowStaysOnTopHint:
self.setWindowFlags(self.windowFlags() ^ Qt.WindowStaysOnTopHint)
else:
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
global gui
window_is_on_top = bool(gui.windowFlags() & Qt.WindowStaysOnTopHint)
if window_is_on_top:
pin_char = chr(0xE840)
self.stay_on_top_button.setText(pin_char)
else:
unpin_char = ' '
self.stay_on_top_button.setText(unpin_char)
self.show() # 重新显示窗口以应用更改
def update_word_count_toggled(self):
select_text_count = len(self.text_box_client.textCursor().selectedText())
select_text_bytes = len(self.text_box_client.textCursor().selectedText().encode('utf-8'))
total_text_count = len(self.text_box_client.toPlainText())
total_text_bytes = len(self.text_box_client.toPlainText().encode('utf-8'))
unselect_text_count = total_text_count - select_text_count
unselect_text_bytes = total_text_bytes - select_text_bytes
self.text_box_wordCountLabel.setText(f"{select_text_count} + {unselect_text_count} = {total_text_count} Words | {select_text_bytes} + {unselect_text_bytes} = {total_text_bytes} Bytes")
if total_text_count > 10000: # 字符数过多时自动清空
self.text_box_client.clear()
def edit_hot_en(self):
os.startfile('hot-en.txt')
def edit_hot_rule(self):
os.startfile('hot-rule.txt')
def edit_hot_zh(self):
os.startfile('hot-zh.txt')
def edit_keyword(self):
os.startfile('keywords.txt')
def explore_home_folder(self):
current_directory = os.getcwd()
os.startfile(current_directory)
def vscode_home_folder(self):
current_directory = os.getcwd()
vscode_exe_path = Config.vscode_exe_path
subprocess.Popen([vscode_exe_path, current_directory])
def open_chatglm_website(self):
os.system(f'start https://chatglm.cn/main/alltoolsdetail')
def open_github_website(self):
os.system(f'start https://github.com/H1DDENADM1N/CapsWriter-Offline')
def closeEvent(self, event):
# Minimize to system tray instead of closing the window when the user clicks the close button
self.hide() # Hide the window
event.ignore() # Ignore the close event
def quit_app(self):
# Terminate core_client.py process
if hasattr(self, 'core_client_process') and self.core_client_process:
self.core_client_process.terminate()
self.core_client_process.kill()
# Hide the system tray icon
self.tray_icon.setVisible(False)
# Quit the application
QApplication.quit()
# TODO: Quit models The above method can not completely exit the model, rename pythonw.exe to pythonw_CapsWriter.exe and taskkill. It's working but not the best way.
proc = subprocess.Popen('taskkill /IM start_client_gui_admin.exe /IM start_client_gui.exe /IM pythonw_CapsWriter_Client.exe /IM hint_while_recording.exe /F', creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True)
def on_tray_icon_activated(self, reason):
# Called when the system tray icon is activated
if reason == QSystemTrayIcon.DoubleClick:
self.showNormal() # Show the main window
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.hide() # Press ESC to hide main window
def start_script(self):
# Start core_client.py and redirect output to the client queue
# While Debug error for line in iter(out.readline, ''):
# Use this line to replace the original code
# self.core_client_process = subprocess.Popen(['.\\runtime\\pythonw_CapsWriter_Client.exe', 'core_client.py'], creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding='utf-8')
self.translate_and_replace_selected_text_offline_process = subprocess.Popen(['.\\runtime\\pythonw_CapsWriter_Client.exe', '.\\util\\client_translate_and_replace_selected_text_offline.py'], creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
threading.Thread(target=self.enqueue_output, args=(self.translate_and_replace_selected_text_offline_process.stdout, self.output_queue_client), daemon=True).start()
self.translate_and_replace_selected_text_online_process = subprocess.Popen(['.\\runtime\\pythonw_CapsWriter_Client.exe', '.\\util\\client_translate_and_replace_selected_text_online.py'], creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
threading.Thread(target=self.enqueue_output, args=(self.translate_and_replace_selected_text_online_process.stdout, self.output_queue_client), daemon=True).start()
if Config.use_search_selected_text_with_everything_function:
self.search_selected_text_with_everything = subprocess.Popen(['.\\runtime\\pythonw_CapsWriter_Client.exe', '.\\util\\client_search_selected_text_with_everything.py'], creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
threading.Thread(target=self.enqueue_output, args=(self.search_selected_text_with_everything.stdout, self.output_queue_client), daemon=True).start()
self.core_client_process = subprocess.Popen(['.\\runtime\\pythonw_CapsWriter_Client.exe', 'core_client.py'], creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
threading.Thread(target=self.enqueue_output, args=(self.core_client_process.stdout, self.output_queue_client), daemon=True).start()
# Update text box
self.update_timer = QTimer()
self.update_timer.timeout.connect(self.update_text_box)
self.update_timer.start(100)
def enqueue_output(self, out, queue):
for line in iter(out.readline, ''):
line = line.strip()
queue.put(line)
def update_text_box(self):
# Update client text box
while not self.output_queue_client.empty():
try:
line = self.output_queue_client.get()
self.text_box_client.append(line)
except Exception as e:
self.text_box_client.append(e)
break
def checkWindowActive(self):
# 检查窗口是否处于活跃状态
if self.isActiveWindow():
pass
else:
x, y, width, height, screenWidth, screenHeight = self.checkWindowInfo()
if x == 0: # 窗口非活跃状态,从左边弹出的,恢复继续停靠在左边
self.berthToLeft(x, y, width, height, screenWidth, screenHeight)
elif x == screenWidth - width: # 窗口非活跃状态,从右边弹出的,恢复继续停靠在右边
self.berthToRight(x, y, width, height, screenWidth, screenHeight)
else:
print("窗口无需恢复停靠")
pass
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.old_pos = event.globalPosition().toPoint()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
delta = QPoint(event.globalPosition().toPoint() - self.old_pos)
self.move(self.x() + delta.x(), self.y() + delta.y())
self.old_pos = event.globalPosition().toPoint()
def enterEvent(self, event):
super().enterEvent(event)
for i in range(self.title_bar.count()): # 鼠标进入时显示标题栏
widget = self.title_bar.itemAt(i).widget()
if widget is not None:
widget.setVisible(True)
for i in range(self.layout2.count()): # 鼠标进入时显示操作栏
widget = self.layout2.itemAt(i).widget()
if widget is not None:
widget.setVisible(True)
x, y, width, height, screenWidth, screenHeight = self.checkWindowInfo()
if self.isBerthLeft: # 已停靠在左边
self.move(0, y) # 从左边弹出,31是标题栏高度
self.isBerthLeft = False
elif self.isBerthRight: # 已停靠在右边
self.move(screenWidth - width, y) # 从右边弹出,31是标题栏高度
self.isBerthRight = False
else:
# print("窗口未停靠")
pass
def leaveEvent(self, event):
super().leaveEvent(event)
for i in range(self.title_bar.count()): # 鼠标离开时隐藏标题栏
widget = self.title_bar.itemAt(i).widget()
if widget is not None:
widget.setVisible(False)
for i in range(self.layout2.count()): # 鼠标离开时隐藏操作栏
widget = self.layout2.itemAt(i).widget()
if widget is not None:
widget.setVisible(False)
x, y, width, height, screenWidth, screenHeight = self.checkWindowInfo()
# print(f"左右,高低,宽,高,屏宽,屏高: {(x, y, width, height, screenWidth, screenHeight)}")
if self.isActiveWindow(): # 窗口活跃状态,用户点击了窗口,则不恢复继续停靠
# print("窗口活跃状态")
if x < 0 - width/2 :
# print("活跃状态,但是窗口的一半已超出屏幕左边界,将窗口停靠在左边")
self.berthToLeft(x, y, width, height, screenWidth, screenHeight)
elif x > screenWidth - width/2:
# print("窗口活跃状态,但是窗口的一半已超出屏幕右边界,将窗口停靠在右边")
self.berthToRight(x, y, width, height, screenWidth, screenHeight)
else:
# print("窗口活跃状态,无需停靠")
pass
else: # 窗口非活跃状态,用户可能只是鼠标划过看一眼,失去焦点时恢复继续停靠
# print("窗口不活跃状态")
if x < 0 - width/2 :
# print("窗口的一半已超出屏幕左边界")
self.berthToLeft(x, y, width, height, screenWidth, screenHeight)
elif x > screenWidth - width/2:
# print("窗口的一半已超出屏幕右边界")
self.berthToRight(x, y, width, height, screenWidth, screenHeight)
elif x == 0: # 窗口非活跃状态,从左边弹出的,恢复继续停靠在左边
self.berthToLeft(x, y, width, height, screenWidth, screenHeight)
elif x == screenWidth - width: # 窗口非活跃状态,从右边弹出的,恢复继续停靠在右边
self.berthToRight(x, y, width, height, screenWidth, screenHeight)
else:
# print("窗口未超出屏幕边界")
pass
def berthToLeft(self, x, y, width, height, screenWidth, screenHeight):
self.move(0-width+self.edgeMargin, y) # 停靠到左边,31是标题栏高度
self.isBerthLeft = True
def berthToRight(self, x, y, width, height, screenWidth, screenHeight):
self.move(screenWidth-self.edgeMargin, y) # 停靠到右边,31是标题栏高度
self.isBerthRight = True
def checkWindowInfo(self):
geometry = self.geometry()
x = geometry.x()
y = geometry.y()
width = geometry.width()
height = geometry.height()
primaryScreen = QApplication.instance().primaryScreen()
screenRect = primaryScreen.geometry()
screenWidth = screenRect.width()
screenHeight = screenRect.height()
return x, y, width, height, screenWidth, screenHeight
def wheelEvent(self, event: QWheelEvent):
# 设置初始缩放因子
self.scale_factor = 1.0
# 设置缩放因子的最小和最大值
self.min_scale = 0.5
self.max_scale = 2.0
# 检测Ctrl键是否被按下
if event.modifiers() == Qt.ControlModifier:
# 计算缩放因子
# print(event.angleDelta().y())
if event.angleDelta().y() > 0:
self.scale_factor *= 1.1 # 放大
elif event.angleDelta().y() < 0:
self.scale_factor *= 0.9 # 缩小
# 限制缩放因子的范围
self.scale_factor = max(self.min_scale, min(self.max_scale, self.scale_factor))
# 应用缩放因子到所有控件
self.apply_scale_factor()
else:
super().wheelEvent(event)
def apply_scale_factor(self):
# 应用缩放因子
for widget in [self.text_box_client]:
# 检查字体大小是否已设置,如果没有设置,则使用一个默认值
current_font = widget.font()
if current_font.pointSizeF() < 9:
current_font.setPointSizeF(9) # 设置一个默认字体大小
current_font.setPointSizeF(current_font.pointSizeF() * self.scale_factor)
widget.setFont(current_font)
def start_client_gui():
Print_Screen_Scale()
if Config.only_run_once and check_process('pythonw_CapsWriter_Client.exe'):
raise Exception("已经有一个客户端在运行了!(用户配置了 只允许运行一次,禁止多开;而且检测到 pythonw_CapsWriter_Client.exe 进程已在运行。如果你确定需要启动多个客户端同时运行,请先修改 config.py class ClientConfig: Only_run_once = False 。)")
if not check_process('hint_while_recording.exe'):
subprocess.Popen(['hint_while_recording.exe'], creationflags=subprocess.CREATE_NO_WINDOW)
app = QApplication(sys.argv)
if Config.hint_while_recording_at_cursor_position:
tooltip = Hint_While_Recording_At_Cursor_Position()
tooltip.show()
apply_stylesheet(app, theme='dark_teal.xml', css_file='util\\client_gui_theme_custom.css')
global gui
gui = GUI()
if not Config.shrink_automatically_to_tray:
gui.show()
sys.exit(app.exec())
def Print_Screen_Scale():
# 获取屏幕的宽度和高度
hDC = win32gui.GetDC(0)
screen_width = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
screen_height = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES)
print(f"屏幕尺寸: {screen_width}x{screen_height}")
# 获取逻辑的宽度和高度
logical_width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
logical_height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
print(f"逻辑尺寸: {logical_width}x{logical_height}")
# 计算缩放比例
global scale_x, scale_y
scale_x = screen_width / logical_width
scale_y = screen_height / logical_height
print(f"屏幕缩放比例: {scale_x}, {scale_y}")
if __name__ == '__main__':
if sys.argv[1:]:
# 如果参数传入文件,那就转录文件
CapsWriter_path = os.path.dirname(os.path.abspath(__file__))
script_path = os.path.join(CapsWriter_path, 'core_client.py')
python_exe_path = os.path.join(CapsWriter_path, 'runtime\\python.exe')
args = [arg for arg in sys.argv[1:]]
command = [python_exe_path, script_path] + args
subprocess.Popen(["cmd.exe", "/c"] + command, cwd=CapsWriter_path)
else:
# GUI
start_client_gui()