Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
theme_manager.h
Go to the documentation of this file.
1
10#ifndef THEME_MANAGER_H
11#define THEME_MANAGER_H
12
13#include <QColor>
14#include <QFont>
15#include <QHash>
16#include <QMutex>
17#include <QObject>
18#include <QPalette>
19#include <QString>
20
22
26struct ThemeInfo {
27 QString name;
28 QString internalName;
29 int version = 0;
30 QString description;
31 QString author;
32};
33
35 bool required;
36 QStringList tokens;
37};
38
40 QColor light;
41 QColor dark;
42};
43
74class ThemeManager : public QObject {
75 Q_OBJECT
76public:
77
87 enum class ThemeMode {
88 System,
89 Dark,
90 Light
91 };
92 Q_ENUM(ThemeMode)
93
94 enum ThemeToken {
95 // Brand
96 BrandPrimary,
97 BrandDeep,
98
99 // Accent
100 AccentSuccess,
101 AccentWarning,
102 AccentError,
103 AccentInfo,
104
105 // Expert
106 ExpertComment,
107 ExpertChat,
108 ExpertNote,
109 ExpertWarn,
110 ExpertError,
111 ExpertForeground,
112
113 // Packets — for each state, <Name> is the row background tint and
114 // <Name>Text is the foreground. Hidden is text-only by design (it
115 // dims an item that otherwise renders with the normal row bg).
116 // When a theme omits a Text token, ThemeTokenHandler derives it
117 // from contrastingText(<Name>) or PaletteText as appropriate.
118 PacketsSelection,
119 PacketsSelectionText,
120 PacketsInactive,
121 PacketsInactiveText,
122 PacketsMarked,
123 PacketsMarkedText,
124 PacketsIgnored,
125 PacketsIgnoredText,
126 PacketsHidden,
127
128 // Conversation — same fg/bg convention as Packets.
129 ConversationClient,
130 ConversationClientText,
131 ConversationServer,
132 ConversationServerText,
133
134 // Filter state (single color per state — use as bg tint; pair
135 // with QPalette::Text for foreground). Derived from accent.
136 FilterValid,
137 FilterInvalid,
138 FilterDeprecated,
139
140 // Syntax highlighting — foreground text colours for code- and
141 // data-viewers (JSON, hex, Lua debugger, etc.). Derived so
142 // the variant is darker in light mode and lighter in dark
143 // mode, keeping enough contrast against PaletteBase.
144 SyntaxKey,
145 SyntaxString,
146 SyntaxNumber,
147
148 // Palette Overrides
149 PaletteWindow,
150 PaletteBase,
151 PaletteText,
152 PaletteWindowText,
153 PaletteAlternateBase,
154 PaletteMid,
155 PaletteMidLight,
156#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
157 PaletteAccent,
158#endif
159
160 // Header (derived from brand)
161 HeaderGradientStart,
162 HeaderGradientEnd,
163
164 // Section headers (derived from brand + QPalette)
165 SectionHeader,
166 SectionHeaderHover,
167
168 // Text on dark surfaces (derived from brand)
169 TextOnDark,
170 TextOnDarkMuted,
171
172 // Update bar (derived from accent.success)
173 UpdateGradientStart,
174 UpdateGradientEnd,
175 UpdateBorder,
176 UpdateText,
177 UpdateTextHighlight,
178 UpdateLink,
179 UpdateLinkHover,
180 UpdateLinkPressed,
181 UpdateButtonBg,
182 UpdateButtonHover,
183 UpdateButtonPressed,
184 UpdateButtonDisabledBg,
185 UpdateButtonDisabledText,
186 UpdateDismissHoverBg,
187 UpdateDismissPressedBg,
188
189 // Accent aliases (derived)
190 HighlightColorOrange,
191 HighlightColorGreen,
192
193 // None
194 NoRole
195 };
196 Q_ENUM(ThemeToken)
197
198 static ThemeManager* instance();
199
205 static void init(const QString &theme = QStringLiteral("default"));
206
207 void cleanup();
208
212 ThemeInfo info() const;
213
225 static QList<ThemeInfo> availableThemes();
226
235 QColor color(ThemeToken role) const;
236
237 bool colorIsAvailable(ThemeToken role) const;
238
258 QHash<ThemeToken, QColor> previewTheme(const QString &internalName,
259 bool wantDark) const;
260
279 QString loadStyleSheet(const QString &name) const;
280
294 static QString styleSheet(const QString &name);
295
307 static void setValidationState(QWidget *w, const QString &state);
308
314 static bool isDark();
315
336 bool isDarkMode() const;
337
344 ThemeMode mode() const;
345
352 void setMode(ThemeMode mode);
353
364 QColor graphColor(int idx) const;
365
366 QColor graphDefaultColor() const;
367
373 qsizetype graphColorCount() const;
374
375 QFont regularFont() const;
376 QFont monospaceFont() const;
377
378signals:
379 void themeChanged();
380
381protected:
382 explicit ThemeManager(QObject *parent = nullptr);
384
385private:
386 static ThemeManager* instance_;
387 static QMutex mutex_;
388
389 ThemeInfo info_;
390
391 // Flattened token -> color value, one map per mode
392 QHash<ThemeManager::ThemeToken, ThemeColorPair> themeColors_;
393 QList<ThemeColorPair> graphColors_;
394
395 QFont regular_font_;
396 QFont monospace_font_;
397
398 QHash<QString, ThemeSectionInfo> sections_;
399
400 // mapping caches for role and palette resolution
401 QHash<QString, ThemeToken> colorRoleCache_;
402 QHash<QString, QPalette::ColorRole> paletteRoleCache_;
403
404 // Light/dark selection: user's explicit choice plus the detector
405 // that tracks the OS preference when mode == System. The detector
406 // is a private member — external code never sees it. Lifetime is
407 // managed via Qt's parent-child ownership: constructed with `this`
408 // as parent, destroyed automatically with the ThemeManager.
409 ThemeMode mode_ = ThemeMode::System;
410 SystemThemeDetector *detector_ = nullptr;
411
412 // Pristine OS palette snapshot used as the baseline when the palette
413 // builder constructs a new theme palette. Captured once at ctor
414 // entry (before any setPalette() call could pollute QApplication's
415 // live palette), and refreshed from `qApp->style()->standardPalette()`
416 // whenever the effective light/dark scheme changes (mode flip or OS
417 // notification). Using this snapshot instead of
418 // QApplication::palette() prevents the previous theme's palette
419 // overrides from leaking into the next theme's baseline — without it,
420 // switching from a theme with palette overrides (e.g. "inverted") to
421 // one without (e.g. "default") would leave the prior overrides
422 // visible. Only consulted on platforms where the OS palette is
423 // trusted (macOS, Windows Qt ≥ 6.8); ignored elsewhere.
424 QPalette osBaseline_;
425
436 QPalette baselineForBuild() const;
437
444 void reapplyForSchemeChange();
445
451 static ThemeMode modeFromPrefs(int gui_color_scheme);
452
459 void applyToStyleHints();
460
467 void applyApplicationStyleSheet();
468
477 bool loadTheme(const QString &themeName = QStringLiteral("default"));
478
479};
480
481#endif /* THEME_MANAGER_H */
Definition system_theme_detector.h:38
Definition theme_manager.h:74
static bool isDark()
Definition theme_manager.cpp:222
static void init(const QString &theme=QStringLiteral("default"))
Definition theme_manager.cpp:174
void setMode(ThemeMode mode)
Definition theme_manager.cpp:267
QHash< ThemeToken, QColor > previewTheme(const QString &internalName, bool wantDark) const
Definition theme_manager.cpp:432
QColor graphColor(int idx) const
Returns the graph color for the given index, cycling through available graph colors if necessary.
Definition theme_manager.cpp:560
static QString styleSheet(const QString &name)
Definition theme_manager.cpp:416
QString loadStyleSheet(const QString &name) const
Definition theme_manager.cpp:411
ThemeMode
Definition theme_manager.h:87
ThemeMode mode() const
Definition theme_manager.cpp:262
qsizetype graphColorCount() const
Returns the number of graph colors defined in the theme.
Definition theme_manager.cpp:587
bool isDarkMode() const
Definition theme_manager.cpp:227
static void setValidationState(QWidget *w, const QString &state)
Definition theme_manager.cpp:421
static QList< ThemeInfo > availableThemes()
Definition theme_manager.cpp:191
ThemeInfo info() const
Definition theme_manager.cpp:186
QColor color(ThemeToken role) const
Definition theme_manager.cpp:374
Definition theme_manager.h:39
Definition theme_manager.h:26
int version
Schema version (currently 1)
Definition theme_manager.h:29
QString name
Display name, e.g. "Wireshark Default".
Definition theme_manager.h:27
QString author
Theme author or organization.
Definition theme_manager.h:31
QString description
One-line description shown in preferences.
Definition theme_manager.h:30
QString internalName
Internal name, e.g. "default".
Definition theme_manager.h:28
Definition theme_manager.h:34