comparison src/gui/theme.cc @ 279:657fda1b9cac

*: clean up enums
author Paper <paper@paper.us.eu.org>
date Fri, 19 Apr 2024 13:24:06 -0400
parents 862d0d8619f6
children 9a88e1725fd2
comparison
equal deleted inserted replaced
274:f6a756c19bfb 279:657fda1b9cac
1 #include "core/config.h" 1 #include "core/config.h"
2 #include "core/session.h" 2 #include "core/session.h"
3 #include "gui/theme.h"
3 #include <QApplication> 4 #include <QApplication>
4 #include <QDebug> 5 #include <QDebug>
5 #include <QFile> 6 #include <QFile>
6 #include <QStyleFactory> 7 #include <QStyleFactory>
7 #include <QTextStream> 8 #include <QTextStream>
9 #include <QStyle>
8 #ifdef MACOSX 10 #ifdef MACOSX
9 # include "sys/osx/dark_theme.h" 11 # include "sys/osx/dark_theme.h"
10 #elif WIN32 12 #elif defined(WIN32)
11 # include "sys/win32/dark_theme.h" 13 # include "sys/win32/dark_theme.h"
12 #else 14 #else
13 # ifdef GLIB 15 # ifdef GLIB
14 # include "sys/glib/dark_theme.h" 16 # include "sys/glib/dark_theme.h"
15 # endif 17 # endif
22 * 3. Windows dark mode support in Qt 6.5 (with Fusion) is completely unavoidable 24 * 3. Windows dark mode support in Qt 6.5 (with Fusion) is completely unavoidable
23 */ 25 */
24 26
25 namespace Theme { 27 namespace Theme {
26 28
27 Theme::Theme(Themes theme) { 29 ThemeManager::ThemeManager(Theme theme) {
28 this->theme = theme; 30 this->theme = theme;
29 } 31 }
30 32
31 Themes Theme::GetTheme() const { 33 Theme ThemeManager::GetTheme() const {
32 return theme; 34 return theme;
33 } 35 }
34 36
35 bool Theme::IsInDarkTheme() const { 37 bool ThemeManager::IsInDarkTheme() const {
36 if (theme != Themes::OS) 38 switch (theme) {
37 return (theme == Themes::DARK); 39 case Theme::Default:
38 #ifdef MACOSX 40 #ifdef MACOSX
39 if (osx::DarkThemeAvailable()) 41 if (osx::DarkThemeAvailable())
40 return osx::IsInDarkTheme(); 42 return osx::IsInDarkTheme();
41 #elif defined(WIN32) 43 #elif defined(WIN32)
42 if (win32::DarkThemeAvailable()) 44 if (win32::DarkThemeAvailable())
43 return win32::IsInDarkTheme(); 45 return win32::IsInDarkTheme();
44 #else 46 #else
45 # ifdef GLIB 47 # ifdef GLIB
46 return glib::IsInDarkTheme(); 48 return glib::IsInDarkTheme();
47 # endif 49 # endif
48 #endif 50 #endif
49 return (theme == Themes::DARK); 51 default: break;
52 }
53 return (theme == Theme::Dark);
50 } 54 }
51 55
52 void Theme::SetToDarkTheme() { 56 void ThemeManager::SetToDarkTheme() {
53 /* macOS >= 10.14 has its own global dark theme, 57 /* macOS >= 10.14 has its own global dark theme,
54 use it :) */ 58 use it :) */
55 #ifdef MACOSX 59 #ifdef MACOSX
56 if (osx::DarkThemeAvailable()) 60 if (osx::DarkThemeAvailable())
57 osx::SetToDarkTheme(); 61 osx::SetToDarkTheme();
58 else 62 else
59 #elif defined(WIN32) 63 #elif defined(WIN32)
60 if (win32::DarkThemeAvailable()) 64 if (win32::DarkThemeAvailable())
61 win32::SetTitleBarsToBlack(true); 65 win32::SetTitleBarsToBlack(true);
62 #endif 66 #endif
63 SetStyleSheet(Themes::DARK); 67 SetStyleSheet(Theme::Dark);
64 } 68 }
65 69
66 void Theme::SetToLightTheme() { 70 void ThemeManager::SetToLightTheme() {
67 #ifdef MACOSX 71 #ifdef MACOSX
68 if (osx::DarkThemeAvailable()) 72 if (osx::DarkThemeAvailable())
69 osx::SetToLightTheme(); 73 osx::SetToLightTheme();
70 else 74 else
71 #elif defined(WIN32) 75 #elif defined(WIN32)
72 if (win32::DarkThemeAvailable()) 76 if (win32::DarkThemeAvailable())
73 win32::SetTitleBarsToBlack(false); 77 win32::SetTitleBarsToBlack(false);
74 #endif 78 #endif
75 SetStyleSheet(Themes::LIGHT); 79 SetStyleSheet(Theme::Light);
76 } 80 }
77 81
78 Themes Theme::GetCurrentOSTheme() const { 82 Theme ThemeManager::GetCurrentOSTheme() const {
79 return IsInDarkTheme() ? Themes::DARK : Themes::LIGHT; 83 return IsInDarkTheme() ? Theme::Dark : Theme::Light;
80
81 /* Currently OS detection only supports Windows and macOS.
82 Please don't be shy if you're willing to port it to other OSes
83 (or desktop environments, or window managers) */
84 return Themes::LIGHT;
85 } 84 }
86 85
87 /* this function is private, and should stay that way */ 86 /* this function is private, and should stay that way */
88 void Theme::SetStyleSheet(Themes theme) { 87 void ThemeManager::SetStyleSheet(Theme theme) {
89 switch (theme) { 88 switch (theme) {
90 case Themes::DARK: { 89 case Theme::Dark: {
91 const QColor darkGray(53, 53, 53); 90 const QColor darkGray(53, 53, 53);
92 const QColor gray(128, 128, 128); 91 const QColor gray(128, 128, 128);
93 const QColor black(25, 25, 25); 92 const QColor black(25, 25, 25);
94 const QColor blue(42, 130, 218); 93 const QColor blue(42, 130, 218);
95 94
125 }()); 124 }());
126 #endif 125 #endif
127 break; 126 break;
128 } 127 }
129 default: 128 default:
130 /* this sucks, it relies on the standard palette which 129 /* this sucks. */
131 * may or may not be a dark style itself. */
132 QPalette pal(QApplication::style()->standardPalette()); 130 QPalette pal(QApplication::style()->standardPalette());
133 #ifdef WIN32 /* fuck you Qt 6 */ 131 #ifdef WIN32 /* fuck you Qt 6 */
134 pal.setColor(QPalette::Window, QColor(0xF0, 0xF0, 0xF0)); 132 pal.setColor(QPalette::Window, QColor(0xF0, 0xF0, 0xF0));
135 #endif 133 #endif
136 qApp->setPalette(pal); 134 qApp->setPalette(pal);
138 qApp->setStyleSheet(""); 136 qApp->setStyleSheet("");
139 break; 137 break;
140 } 138 }
141 } 139 }
142 140
143 void Theme::SetTheme(Themes theme) { 141 void ThemeManager::SetTheme(Theme theme) {
144 switch (theme) { 142 switch (theme) {
145 case Themes::LIGHT: SetToLightTheme(); break; 143 case Theme::Light: SetToLightTheme(); break;
146 case Themes::DARK: SetToDarkTheme(); break; 144 case Theme::Dark: SetToDarkTheme(); break;
147 case Themes::OS: 145 case Theme::Default:
148 if (GetCurrentOSTheme() == Themes::LIGHT) 146 if (GetCurrentOSTheme() == Theme::Light)
149 SetToLightTheme(); 147 SetToLightTheme();
150 else 148 else
151 SetToDarkTheme(); 149 SetToDarkTheme();
152 break; 150 break;
153 } 151 }
154 this->theme = theme; 152 this->theme = theme;
155 } 153 }
156 154
157 void Theme::RepaintCurrentTheme() { 155 void ThemeManager::RepaintCurrentTheme() {
158 Theme::SetTheme(theme); 156 SetTheme(theme);
159 } 157 }
160 158
161 } // namespace Theme 159 } // namespace Theme