theme.py
20.8 KB
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
"""设计系统:Apple 高级浅色主题 + 浅深双模式。
单一真相源 — 所有颜色 / 间距 / 圆角 / 字号在本模块定义,
通过 build_qss(mode) 拼成完整 QSS 字符串应用到 QApplication。
跟随系统切换:监听 QGuiApplication.styleHints().colorSchemeChanged,
macOS 按时间自动切深色 / Windows 11 系统级深浅都会自动响应。
业务代码不要写 inline setStyleSheet — 通过 setObjectName / setProperty
让全局 QSS 命中。状态色(success/warning/danger)用 setProperty('status', ...)。
"""
from __future__ import annotations
from PySide6.QtCore import QObject, Qt, Signal
from PySide6.QtGui import QGuiApplication
from PySide6.QtWidgets import QApplication
# =============================================================================
# 设计令牌
# =============================================================================
FONT_STACK = (
'"-apple-system", "SF Pro Text", "PingFang SC", '
'"Microsoft YaHei UI", "Segoe UI Variable", "Segoe UI", sans-serif'
)
TOKENS_LIGHT: dict[str, str] = {
# 表面层级
"bg_canvas": "#fbfbfd",
"bg_surface": "#ffffff",
"bg_elevated": "#ffffff",
"bg_subtle": "#f4f4f7",
"bg_hover": "#f0f0f3",
# 文字层级
"text_primary": "#1d1d1f",
"text_secondary": "#6e6e73",
"text_tertiary": "#86868b",
"text_on_accent": "#ffffff",
"text_on_danger": "#ffffff",
# 边框 / 分隔
"border_default": "rgba(0, 0, 0, 0.10)",
"border_strong": "rgba(0, 0, 0, 0.18)",
"border_focus": "#0071e3",
"divider": "rgba(0, 0, 0, 0.06)",
# 主色(accent)
"accent": "#0071e3",
"accent_hover": "#0077ed",
"accent_pressed": "#0062c4",
"accent_subtle": "rgba(0, 113, 227, 0.08)",
"accent_subtle_hover": "rgba(0, 113, 227, 0.14)",
# 状态色
"success": "#34c759",
"warning": "#ff9500",
"danger": "#ff3b30",
"danger_hover": "#ff5247",
"danger_subtle": "rgba(255, 59, 48, 0.10)",
}
TOKENS_DARK: dict[str, str] = {
"bg_canvas": "#1a1a1c",
"bg_surface": "#2c2c2e",
"bg_elevated": "#3a3a3c",
"bg_subtle": "#242426",
"bg_hover": "#34343a",
"text_primary": "#ffffff",
"text_secondary": "#98989d",
"text_tertiary": "#6e6e73",
"text_on_accent": "#ffffff",
"text_on_danger": "#ffffff",
"border_default": "rgba(255, 255, 255, 0.10)",
"border_strong": "rgba(255, 255, 255, 0.20)",
"border_focus": "#0a84ff",
"divider": "rgba(255, 255, 255, 0.06)",
"accent": "#0a84ff",
"accent_hover": "#1d8cff",
"accent_pressed": "#006edc",
"accent_subtle": "rgba(10, 132, 255, 0.16)",
"accent_subtle_hover": "rgba(10, 132, 255, 0.24)",
"success": "#30d158",
"warning": "#ff9f0a",
"danger": "#ff453a",
"danger_hover": "#ff5b50",
"danger_subtle": "rgba(255, 69, 58, 0.16)",
}
# 尺寸 / 圆角 / 间距 ─ 浅深一致
SIZES: dict[str, str] = {
"radius_sm": "4px",
"radius_md": "8px",
"radius_lg": "12px",
"radius_pill": "980px",
"space_1": "4px",
"space_2": "8px",
"space_3": "12px",
"space_4": "16px",
"space_5": "20px",
"space_6": "24px",
"control_h_sm": "26px",
"control_h_md": "32px",
"control_h_lg": "40px",
"font_xs": "10pt",
"font_sm": "11pt",
"font_base": "12pt",
"font_lg": "14pt",
"font_xl": "17pt",
}
# =============================================================================
# QSS 构建
# =============================================================================
def build_qss(mode: str) -> str:
"""根据 mode('light'/'dark')拼出完整 QSS。"""
c = TOKENS_DARK if mode == "dark" else TOKENS_LIGHT
s = SIZES
f = FONT_STACK
return f"""
/* ========== 全局 ========== */
* {{
font-family: {f};
color: {c['text_primary']};
}}
QWidget {{
background-color: {c['bg_canvas']};
color: {c['text_primary']};
font-size: {s['font_base']};
}}
QMainWindow, QDialog {{
background-color: {c['bg_canvas']};
}}
/* 二级弹窗(QMessageBox 等)走 surface 颜色,凸出于主窗口 */
QMessageBox {{
background-color: {c['bg_surface']};
}}
QMessageBox QLabel {{
background: transparent;
color: {c['text_primary']};
font-size: {s['font_base']};
}}
/* ========== 文字 ========== */
QLabel {{
background: transparent;
color: {c['text_primary']};
}}
QLabel[role="secondary"] {{
color: {c['text_secondary']};
}}
QLabel[role="muted"] {{
color: {c['text_tertiary']};
font-size: {s['font_sm']};
}}
QLabel[role="caption"] {{
color: {c['text_secondary']};
font-size: {s['font_xs']};
text-transform: uppercase;
letter-spacing: 1px;
}}
QLabel[role="title"] {{
color: {c['text_primary']};
font-size: {s['font_xl']};
font-weight: 600;
}}
/* 状态色 — 业务用 setProperty('status', '...') 命中 */
QLabel[status="success"] {{ color: {c['success']}; }}
QLabel[status="warning"] {{ color: {c['warning']}; }}
QLabel[status="danger"] {{ color: {c['danger']}; }}
QLabel[status="muted"] {{ color: {c['text_tertiary']}; }}
QLabel[status="info"] {{ color: {c['accent']}; }}
/* ========== 卡片 / GroupBox ========== */
QGroupBox {{
background-color: {c['bg_surface']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
margin-top: 14px;
padding: {s['space_4']} {s['space_3']} {s['space_3']} {s['space_3']};
font-weight: 600;
font-size: {s['font_sm']};
color: {c['text_secondary']};
}}
QGroupBox::title {{
subcontrol-origin: margin;
subcontrol-position: top left;
left: {s['space_3']};
padding: 0 {s['space_2']};
color: {c['text_secondary']};
background: transparent;
text-transform: uppercase;
letter-spacing: 1px;
font-size: {s['font_xs']};
}}
/* ========== 按钮 ========== */
QPushButton {{
background-color: {c['bg_surface']};
color: {c['text_primary']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
padding: 6px 14px;
min-height: {s['control_h_md']};
font-size: {s['font_sm']};
font-weight: 500;
}}
QPushButton:hover {{
background-color: {c['bg_hover']};
border-color: {c['border_strong']};
}}
QPushButton:pressed {{
background-color: {c['bg_subtle']};
}}
QPushButton:disabled {{
color: {c['text_tertiary']};
background-color: {c['bg_subtle']};
border-color: {c['border_default']};
}}
QPushButton:focus {{
outline: none;
border-color: {c['accent']};
}}
/* 主按钮("生成图片"等)— pill 圆角 + 实色背景 */
QPushButton[variant="primary"] {{
background-color: {c['accent']};
color: {c['text_on_accent']};
border: 1px solid {c['accent']};
border-radius: {s['radius_pill']};
padding: 8px 22px;
min-height: {s['control_h_lg']};
font-weight: 600;
}}
QPushButton[variant="primary"]:hover {{
background-color: {c['accent_hover']};
border-color: {c['accent_hover']};
}}
QPushButton[variant="primary"]:pressed {{
background-color: {c['accent_pressed']};
border-color: {c['accent_pressed']};
}}
QPushButton[variant="primary"]:disabled {{
background-color: {c['accent_subtle']};
color: {c['text_tertiary']};
border-color: transparent;
}}
/* 危险按钮 */
QPushButton[variant="danger"] {{
background-color: {c['bg_surface']};
color: {c['danger']};
border: 1px solid {c['danger']};
}}
QPushButton[variant="danger"]:hover {{
background-color: {c['danger_subtle']};
}}
QPushButton[variant="danger"]:pressed {{
background-color: {c['danger']};
color: {c['text_on_danger']};
}}
/* 幽灵按钮(图标按钮等) */
QPushButton[variant="ghost"] {{
background-color: transparent;
border: 1px solid transparent;
color: {c['text_secondary']};
}}
QPushButton[variant="ghost"]:hover {{
background-color: {c['accent_subtle']};
color: {c['accent']};
}}
/* 短暂成功反馈("已复制"等,2秒后回退)*/
QPushButton[variant="success-flash"] {{
background-color: {c['success']};
color: white;
border: 1px solid {c['success']};
}}
/* 链接按钮 — 像超链接 */
QPushButton[variant="link"] {{
background-color: transparent;
border: none;
color: {c['accent']};
padding: 2px 6px;
min-height: 0;
}}
QPushButton[variant="link"]:hover {{
color: {c['accent_hover']};
text-decoration: underline;
}}
/* ========== 输入框 ========== */
QLineEdit, QTextEdit, QPlainTextEdit {{
background-color: {c['bg_surface']};
color: {c['text_primary']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
padding: 6px 10px;
selection-background-color: {c['accent']};
selection-color: {c['text_on_accent']};
font-size: {s['font_base']};
}}
QLineEdit:focus, QTextEdit:focus, QPlainTextEdit:focus {{
border: 2px solid {c['accent']};
padding: 5px 9px;
}}
QLineEdit:disabled, QTextEdit:disabled, QPlainTextEdit:disabled {{
background-color: {c['bg_subtle']};
color: {c['text_tertiary']};
}}
QLineEdit[placeholder="true"], QTextEdit[placeholder="true"] {{
color: {c['text_tertiary']};
}}
/* ========== 下拉框 ========== */
QComboBox {{
background-color: {c['bg_surface']};
color: {c['text_primary']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
padding: 5px 10px;
min-height: {s['control_h_md']};
min-width: 100px;
}}
QComboBox:hover {{
border-color: {c['border_strong']};
}}
QComboBox:focus {{
border: 2px solid {c['accent']};
padding: 4px 9px;
}}
QComboBox::drop-down {{
width: 20px;
border: none;
background: transparent;
}}
QComboBox QAbstractItemView {{
background-color: {c['bg_elevated']};
color: {c['text_primary']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
selection-background-color: {c['accent_subtle']};
selection-color: {c['text_primary']};
padding: 4px;
outline: none;
}}
QComboBox QAbstractItemView::item {{
padding: 6px 10px;
border-radius: {s['radius_sm']};
}}
QComboBox QAbstractItemView::item:hover {{
background-color: {c['accent_subtle']};
}}
/* ========== Tab ========== */
QTabWidget::pane {{
border: none;
background-color: {c['bg_canvas']};
top: -1px;
}}
QTabWidget::tab-bar {{
left: 4px;
}}
QTabBar {{
background: transparent;
qproperty-drawBase: 0;
}}
QTabBar::tab {{
background: transparent;
color: {c['text_secondary']};
padding: 8px 16px;
margin: 0 2px;
border: none;
border-bottom: 2px solid transparent;
font-size: {s['font_base']};
min-width: 80px;
}}
QTabBar::tab:hover {{
color: {c['text_primary']};
}}
QTabBar::tab:selected {{
color: {c['accent']};
border-bottom: 2px solid {c['accent']};
font-weight: 600;
}}
QTabBar::tab:disabled {{
color: {c['text_tertiary']};
}}
/* ========== 列表 ========== */
QListWidget, QListView, QTreeWidget, QTreeView {{
background-color: {c['bg_surface']};
color: {c['text_primary']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
outline: none;
padding: 4px;
}}
QListWidget::item, QListView::item {{
padding: 8px;
border-radius: {s['radius_sm']};
color: {c['text_primary']};
}}
QListWidget::item:hover, QListView::item:hover {{
background-color: {c['bg_hover']};
}}
QListWidget::item:selected, QListView::item:selected {{
background-color: {c['accent_subtle']};
color: {c['text_primary']};
}}
/* ========== 滚动区 ========== */
QScrollArea {{
background-color: {c['bg_canvas']};
border: none;
}}
QScrollArea > QWidget > QWidget {{
background-color: transparent;
}}
/* ========== 滚动条(Apple 隐式风格)========== */
QScrollBar:vertical {{
background: transparent;
width: 10px;
margin: 2px;
border: none;
}}
QScrollBar::handle:vertical {{
background: {c['border_strong']};
border-radius: 3px;
min-height: 30px;
}}
QScrollBar::handle:vertical:hover {{
background: {c['text_tertiary']};
}}
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{
height: 0;
background: transparent;
border: none;
}}
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {{
background: transparent;
}}
QScrollBar:horizontal {{
background: transparent;
height: 10px;
margin: 2px;
border: none;
}}
QScrollBar::handle:horizontal {{
background: {c['border_strong']};
border-radius: 3px;
min-width: 30px;
}}
QScrollBar::handle:horizontal:hover {{
background: {c['text_tertiary']};
}}
QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal {{
width: 0;
background: transparent;
border: none;
}}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {{
background: transparent;
}}
/* ========== 进度条 ========== */
QProgressBar {{
background-color: {c['bg_subtle']};
border: none;
border-radius: 4px;
height: 6px;
text-align: center;
color: {c['text_secondary']};
font-size: {s['font_xs']};
}}
QProgressBar::chunk {{
background-color: {c['accent']};
border-radius: 4px;
}}
/* ========== 复选 / 单选 ========== */
QCheckBox, QRadioButton {{
color: {c['text_primary']};
spacing: 6px;
background: transparent;
}}
QCheckBox::indicator, QRadioButton::indicator {{
width: 16px;
height: 16px;
}}
QCheckBox::indicator:unchecked {{
background: {c['bg_surface']};
border: 1px solid {c['border_strong']};
border-radius: 3px;
}}
QCheckBox::indicator:checked {{
background: {c['accent']};
border: 1px solid {c['accent']};
border-radius: 3px;
}}
QRadioButton::indicator:unchecked {{
background: {c['bg_surface']};
border: 1px solid {c['border_strong']};
border-radius: 8px;
}}
QRadioButton::indicator:checked {{
background: {c['accent']};
border: 1px solid {c['accent']};
border-radius: 8px;
}}
/* ========== 工具提示 ========== */
QToolTip {{
background-color: {c['bg_elevated']};
color: {c['text_primary']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_sm']};
padding: 4px 8px;
font-size: {s['font_sm']};
}}
/* ========== 菜单 ========== */
QMenu {{
background-color: {c['bg_elevated']};
color: {c['text_primary']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
padding: 4px;
}}
QMenu::item {{
padding: 6px 16px;
border-radius: {s['radius_sm']};
}}
QMenu::item:selected {{
background-color: {c['accent_subtle']};
color: {c['text_primary']};
}}
QMenu::separator {{
height: 1px;
background: {c['divider']};
margin: 4px 6px;
}}
/* ========== 分割器 ========== */
QSplitter::handle {{
background-color: {c['divider']};
}}
QSplitter::handle:horizontal {{
width: 1px;
}}
QSplitter::handle:vertical {{
height: 1px;
}}
/* ========== 任务队列 sidebar ========== */
#taskQueueSidebar {{
background-color: {c['bg_surface']};
border-left: 1px solid {c['divider']};
}}
#sidebarHeader {{
background-color: {c['bg_surface']};
border-bottom: 1px solid {c['divider']};
min-height: 40px;
max-height: 40px;
}}
#sidebarHeader QLabel {{
color: {c['text_primary']};
font-size: {s['font_base']};
font-weight: 600;
padding: 0 12px;
}}
/* ========== 登录对话框 ========== */
#loginDialog {{
background-color: {c['bg_canvas']};
}}
#loginDialog QLabel#loginTitle {{
font-size: {s['font_xl']};
font-weight: 600;
color: {c['text_primary']};
}}
#loginDialog QLabel#loginSubtitle {{
font-size: {s['font_sm']};
color: {c['text_secondary']};
}}
/* ========== 预览区占位 ========== */
#previewPlaceholder {{
color: {c['text_tertiary']};
background: {c['bg_subtle']};
border-radius: {s['radius_md']};
}}
/* ========== 参考图拖拽区(drag_state property)========== */
#referenceImageDrop[drag_state="idle"] {{
border: 2px dashed {c['border_default']};
border-radius: {s['radius_md']};
background-color: {c['bg_subtle']};
}}
#referenceImageDrop[drag_state="idle"]:hover {{
border: 2px dashed {c['accent']};
background-color: {c['accent_subtle']};
}}
#referenceImageDrop[drag_state="active"] {{
border: 2px dashed {c['accent']};
border-radius: {s['radius_md']};
background-color: {c['accent_subtle']};
}}
/* ========== 已生成图片缩略图卡 ========== */
QLabel[role="thumb"] {{
border: 1px solid {c['border_default']};
border-radius: {s['radius_sm']};
background-color: {c['bg_surface']};
}}
QLabel[role="thumb_index"] {{
color: {c['text_secondary']};
font-size: {s['font_base']};
font-weight: 600;
}}
/* ========== 预览区生成图(result_label / generated_image_label)========== */
QLabel#previewImage {{
background-color: {c['bg_subtle']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
color: {c['text_tertiary']};
}}
QLabel#previewImage[has_image="true"] {{
background-color: {c['bg_canvas']};
border-color: {c['border_default']};
}}
/* ========== 历史详情提示词显示框 ========== */
QLabel#promptDisplay {{
background-color: {c['bg_subtle']};
border: 1px solid {c['border_default']};
border-radius: {s['radius_md']};
padding: 8px 10px;
color: {c['text_primary']};
}}
/* ========== 缩略图删除按钮(20x20 紧凑)========== */
QPushButton#thumbDeleteBtn {{
background-color: {c['danger']};
color: white;
border: none;
border-radius: 3px;
padding: 0;
min-height: 0;
font-size: {s['font_xs']};
font-weight: 700;
}}
QPushButton#thumbDeleteBtn:hover {{
background-color: {c['danger_hover']};
}}
"""
# =============================================================================
# 主题管理器
# =============================================================================
def _detect_system_mode() -> str:
"""检测当前系统色彩偏好。Qt 6.5+ 支持,回退到 light。"""
try:
scheme = QGuiApplication.styleHints().colorScheme()
if scheme == Qt.ColorScheme.Dark:
return "dark"
return "light"
except Exception:
return "light"
class ThemeManager(QObject):
"""全局主题管理器。监听系统色彩偏好变化,自动切换 QSS。
使用:在 main() 创建 QApplication 后调用 apply_theme(app) 即可。
业务代码不需要直接接触 ThemeManager。
通过 force_mode('light'|'dark'|None) 可以强制锁定,传 None 恢复跟随系统。
"""
theme_changed = Signal(str) # 'light' | 'dark'
def __init__(self, app: QApplication):
super().__init__(app)
self._app = app
self._forced_mode: str | None = None
self._current_mode: str = _detect_system_mode()
self._apply_qss()
# 监听系统色彩切换
try:
QGuiApplication.styleHints().colorSchemeChanged.connect(
self._on_system_color_scheme_changed
)
except Exception:
# Qt 版本不支持 / 平台不发射,无所谓
pass
def current_mode(self) -> str:
return self._current_mode
def force_mode(self, mode: str | None) -> None:
"""锁定主题。None = 跟随系统。"""
self._forced_mode = mode
new_mode = mode if mode in ("light", "dark") else _detect_system_mode()
if new_mode != self._current_mode:
self._current_mode = new_mode
self._apply_qss()
self.theme_changed.emit(new_mode)
def _on_system_color_scheme_changed(self, scheme) -> None:
if self._forced_mode in ("light", "dark"):
return # 用户锁定模式时不响应系统切换
new_mode = "dark" if scheme == Qt.ColorScheme.Dark else "light"
if new_mode != self._current_mode:
self._current_mode = new_mode
self._apply_qss()
self.theme_changed.emit(new_mode)
def _apply_qss(self) -> None:
self._app.setStyleSheet(build_qss(self._current_mode))
def apply_theme(app: QApplication) -> ThemeManager:
"""主题应用入口。在 main() 创建 QApplication 后立即调用。
返回 ThemeManager 实例,调用方应持有引用以保持信号连接(否则会被
Python GC 当作未引用的 QObject 回收,导致系统切换失效)。
"""
mgr = ThemeManager(app)
# 全局可访问点,方便 get_color() 等无 widget 引用的场景
app.theme_manager = mgr
return mgr
def get_color(token: str, fallback: str = "#000000") -> str:
"""读取当前主题下的颜色 token。
用于 widget 内嵌颜色调用(如 QListWidgetItem.setForeground 等),
QSS 命中不到的渲染层 API。注意:返回的是当前快照,主题切换后需要
在下一次重绘时重新读取。
"""
try:
app = QApplication.instance()
mgr = getattr(app, "theme_manager", None)
mode = mgr.current_mode() if mgr else "light"
except Exception:
mode = "light"
table = TOKENS_DARK if mode == "dark" else TOKENS_LIGHT
return table.get(token, fallback)