game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
theme_manager.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/theme_manager.h
4// ==============================================================================
5// Theme management system for Qt UI - centralized theming with presets and
6// customizable accent colors
7// ==============================================================================
8
9#pragma once
10
11#include <QColor>
12#include <QFont>
13#include <QMenu>
14#include <QObject>
15#include <QPalette>
16#include <QString>
17#include <QtGlobal>
18#include <map>
19#include <vector>
20
21// Forward declarations
22namespace core
23{
25}
26
27// ==============================================================================
28// ThemeColors - All color roles used throughout the application
29// ==============================================================================
30
32{
33 // Base backgrounds (darkest to lightest)
34 QColor windowBackground; // Main window background
35 QColor panelBackground; // Sidebar, panels
36 QColor cardBackground; // Cards, elevated surfaces
37 QColor inputBackground; // Input fields, list items
38 QColor hoverBackground; // Hover state background
39
40 // Text colors
41 QColor textPrimary; // Main text
42 QColor textSecondary; // Secondary info
43 QColor textMuted; // Disabled, hints
44 QColor textOnAccent; // Text on accent-colored backgrounds
45
46 // Accent colors (customizable by user)
47 QColor accent; // Primary accent color
48 QColor accentHover; // Accent hover state
49 QColor accentPressed; // Accent pressed state
50
51 // Semantic colors (theme-specific, not customizable)
52 QColor success; // Running, installed, positive actions
53 QColor error; // Errors, delete, negative actions
54 QColor warning; // Warnings, caution
55 QColor info; // Info badges, secondary highlights
56 QColor favorite; // Favorite star
57
58 // Borders and separators
59 QColor border; // Default border color
60 QColor borderLight; // Subtle borders
61
62 // Scrollbar colors
66};
67
68// ==============================================================================
69// Theme - Complete theme definition
70// ==============================================================================
71
72struct Theme
73{
74 QString id; // Unique identifier (e.g., "cachyos-emerald")
75 QString displayName; // Display name (e.g., "CachyOS Emerald")
76 ThemeColors colors; // All theme colors
77 QColor defaultAccent; // Theme's default accent (can be overridden by user)
78};
79
80// ==============================================================================
81// ThemeManager - Singleton managing application theming
82// ==============================================================================
83
84class ThemeManager : public QObject
85{
86 Q_OBJECT
87
88 public:
89 static ThemeManager& instance();
90
91 // Initialize with settings controller for persistence
92 void initialize(core::SettingsController* settingsController);
93
94 // =========================================================================
95 // Theme Selection
96 // =========================================================================
97
98 /// Get list of available theme IDs
99 std::vector<QString> getAvailableThemeIds() const;
100
101 /// Get theme by ID (returns nullptr if not found)
102 const Theme* getTheme(const QString& id) const;
103
104 /// Get current theme
105 const Theme* currentTheme() const;
106
107 /// Get current theme ID
108 QString currentThemeId() const;
109
110 /// Set active theme by ID
111 void setTheme(const QString& themeId);
112
113 // =========================================================================
114 // Accent Color Customization
115 // =========================================================================
116
117 /// Set custom accent color (overrides theme default)
118 void setAccentColor(const QColor& color);
119
120 /// Get current accent color (custom or theme default)
121 QColor accentColor() const;
122
123 /// Check if using custom accent color
124 bool hasCustomAccent() const;
125
126 /// Reset accent to theme default
128
129 // =========================================================================
130 // UI Scaling
131 // =========================================================================
132
133 /// Get current zoom level (75-150, default 100)
134 int zoomLevel() const;
135
136 /// Set zoom level (75-150)
137 void setZoomLevel(int percent);
138
139 /// Get current font family (empty = system default)
140 QString fontFamily() const;
141
142 /// Set font family
143 void setFontFamily(const QString& family);
144
145 /// Get scaled font size for given base size
146 int scaledFontSize(int baseSize) const;
147
148 /// Get scaled dimension for layouts/spacing
149 int scaledSize(int baseSize) const;
150
151 /// Get scaled font (applies zoom + custom family if set)
152 QFont scaledFont(const QFont& baseFont) const;
153
154 /// Get the base application font (with zoom and custom family applied)
155 QFont applicationFont() const;
156
157 // =========================================================================
158 // Color Access
159 // =========================================================================
160
161 /// Get effective colors (with accent customization applied)
162 const ThemeColors& colors() const;
163
164 // =========================================================================
165 // Application Styling
166 // =========================================================================
167
168 /// Apply current theme to the entire application
169 void applyToApplication();
170
171 /// Build QPalette from current theme
172 QPalette buildPalette() const;
173
174 /// Build global stylesheet for widgets not covered by QPalette
175 QString buildStyleSheet() const;
176
177 /// Get standalone menu stylesheet (for dynamically created menus)
178 QString menuStyleSheet() const;
179
180 /// Apply complete menu styling (stylesheet + palette) to a QMenu
181 void styleMenu(QMenu* menu) const;
182
183 // =========================================================================
184 // Persistence
185 // =========================================================================
186
187 /// Save theme settings to settings controller
188 void saveToSettings();
189
190 /// Load theme settings from settings controller
191 void loadFromSettings();
192
193 // =========================================================================
194 // Color Utilities (public for use by UI components)
195 // =========================================================================
196
197 /// Ensure foreground has sufficient contrast against background
198 static QColor ensureContrast(const QColor& fg, const QColor& bg, int minDiff = 120);
199
200 /// Lighten a color by increasing its lightness
201 static QColor lightenColor(const QColor& color, int amount);
202
203 /// Darken a color by decreasing its lightness
204 static QColor darkenColor(const QColor& color, int amount);
205
206 signals:
207 /// Emitted when theme or accent color changes
209
210 /// Emitted when zoom level or font changes
212
213 public:
214 // Deleted copy operations: kept public so a call-site copy attempt
215 // produces a clear "deleted function" diagnostic. Explicit `public:`
216 // ends the preceding `signals:` section so moc doesn't mistake these
217 // for signals. Singleton: instances only via ThemeManager::instance().
218 ThemeManager(const ThemeManager&) = delete;
220
221 private:
222 ThemeManager();
223 ~ThemeManager() override = default;
224
225 // Register all built-in themes
226 void registerBuiltinThemes();
227
228 // Connect to system theme change signals (Qt 6.5+)
229 void connectSystemThemeSignals();
230
231 // Handle system color scheme change
232 void onSystemColorSchemeChanged();
233
234 // Resolve the effective theme for "auto" mode
235 QString resolveAutoThemeId() const;
236
237 // Whether currently in auto mode
238 bool isAutoMode() const;
239
240 // Theme factory methods
241 Theme createAutoTheme(); // Auto (System) - follows system, switches dark/light
242 Theme createSystemTheme(); // Follows system color scheme
243 Theme createStandardDarkTheme(); // Simple dark theme with blue accent
244 Theme createStandardLightTheme(); // Simple light theme with blue accent
245 Theme createCachyOSEmeraldTheme();
246 Theme createNordDarkTheme();
247 Theme createNordLightTheme(); // Nord Light (Snow Storm)
248 Theme createDraculaTheme();
249 Theme createCatppuccinMochaTheme();
250 Theme createCatppuccinLatteTheme(); // Catppuccin light variant
251 Theme createGruvboxDarkTheme();
252 Theme createGruvboxLightTheme(); // Gruvbox light variant
253 Theme createTokyoNightTheme();
254 Theme createSolarizedLightTheme(); // Solarized light
255
256 // Helper to detect system dark mode
257 bool isSystemDarkMode() const;
258
259 // Recalculate effective colors when accent changes
260 void updateEffectiveColors();
261
262 // Force refresh all widget styles (called after theme/accent changes)
263 void refreshAllWidgetStyles();
264
265 // Generate theme-aware SVG indicator icons (checkbox/radio)
266 void generateIndicatorIcons();
267
268 // Theme storage
269 std::map<QString, Theme> themes_;
270 QString currentThemeId_;
271
272 // Custom accent (empty = use theme default)
273 QColor customAccentColor_;
274 bool useCustomAccent_ = false;
275
276 // Effective colors with accent applied
277 ThemeColors effectiveColors_;
278
279 // UI scaling
280 int zoomLevel_ = 100; // 75-150, default 100
281 QString fontFamily_; // Custom font (empty = system default)
282 mutable int baseFontSize_ = 0; // System default font size (cached lazily)
283
284 // Cached paths for theme-aware indicator icons
285 QString checkmarkIconPath_;
286 QString radioDotIconPath_;
287
288 // Settings controller for persistence
289 core::SettingsController* settingsController_ = nullptr;
290};
static ThemeManager & instance()
void applyToApplication()
Apply current theme to the entire application.
QPalette buildPalette() const
Build QPalette from current theme.
static QColor ensureContrast(const QColor &fg, const QColor &bg, int minDiff=120)
Ensure foreground has sufficient contrast against background.
void resetAccentToDefault()
Reset accent to theme default.
static QColor darkenColor(const QColor &color, int amount)
Darken a color by decreasing its lightness.
QString currentThemeId() const
Get current theme ID.
QString buildStyleSheet() const
Build global stylesheet for widgets not covered by QPalette.
QString fontFamily() const
Get current font family (empty = system default).
bool hasCustomAccent() const
Check if using custom accent color.
void loadFromSettings()
Load theme settings from settings controller.
void styleMenu(QMenu *menu) const
Apply complete menu styling (stylesheet + palette) to a QMenu.
void setAccentColor(const QColor &color)
Set custom accent color (overrides theme default).
int scaledSize(int baseSize) const
Get scaled dimension for layouts/spacing.
void initialize(core::SettingsController *settingsController)
void setFontFamily(const QString &family)
Set font family.
void themeChanged()
Emitted when theme or accent color changes.
static QColor lightenColor(const QColor &color, int amount)
Lighten a color by increasing its lightness.
const Theme * getTheme(const QString &id) const
Get theme by ID (returns nullptr if not found).
void setZoomLevel(int percent)
Set zoom level (75-150).
int scaledFontSize(int baseSize) const
Get scaled font size for given base size.
QString menuStyleSheet() const
Get standalone menu stylesheet (for dynamically created menus).
int zoomLevel() const
Get current zoom level (75-150, default 100).
void setTheme(const QString &themeId)
Set active theme by ID.
void zoomChanged()
Emitted when zoom level or font changes.
const Theme * currentTheme() const
Get current theme.
const ThemeColors & colors() const
Get effective colors (with accent customization applied).
ThemeManager(const ThemeManager &)=delete
QFont scaledFont(const QFont &baseFont) const
Get scaled font (applies zoom + custom family if set).
std::vector< QString > getAvailableThemeIds() const
Get list of available theme IDs.
void saveToSettings()
Save theme settings to settings controller.
QColor accentColor() const
Get current accent color (custom or theme default).
QFont applicationFont() const
Get the base application font (with zoom and custom family applied).
ThemeManager & operator=(const ThemeManager &)=delete
QColor favorite
QColor panelBackground
QColor inputBackground
QColor borderLight
QColor cardBackground
QColor textSecondary
QColor scrollbarTrack
QColor textPrimary
QColor accentHover
QColor scrollbarThumbHover
QColor windowBackground
QColor scrollbarThumb
QColor textMuted
QColor hoverBackground
QColor accentPressed
QColor textOnAccent
ThemeColors colors
QString displayName
QString id
QColor defaultAccent