Mercurial > minori
annotate src/gui/theme.cc @ 282:19eb6c4dca78
autotools: ax_have_qt sucks, use pkgconf instead
this is particularly useful because m4_ax_have_qt really
sucked when cross compiling because of qmake
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 08 May 2024 15:54:10 -0400 |
parents | 657fda1b9cac |
children | 9a88e1725fd2 |
rev | line source |
---|---|
102 | 1 #include "core/config.h" |
2 #include "core/session.h" | |
279 | 3 #include "gui/theme.h" |
102 | 4 #include <QApplication> |
258 | 5 #include <QDebug> |
102 | 6 #include <QFile> |
258 | 7 #include <QStyleFactory> |
102 | 8 #include <QTextStream> |
279 | 9 #include <QStyle> |
102 | 10 #ifdef MACOSX |
11 # include "sys/osx/dark_theme.h" | |
279 | 12 #elif defined(WIN32) |
102 | 13 # include "sys/win32/dark_theme.h" |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
14 #else |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
15 # ifdef GLIB |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
16 # include "sys/glib/dark_theme.h" |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
17 # endif |
102 | 18 #endif |
19 | |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
20 /* Weird quirks of this implementation: |
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
21 * 1. Dark mode stuff in Qt changes a lot and Qt 5 and Qt 6 are massively different |
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
22 * 2. Some widgets, i.e. QTabWidget, QTabBar, etc., just completely IGNORE the QPalette setting |
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
23 * on different platforms and the only way to fix it is by using Fusion |
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
24 * 3. Windows dark mode support in Qt 6.5 (with Fusion) is completely unavoidable |
258 | 25 */ |
102 | 26 |
27 namespace Theme { | |
28 | |
279 | 29 ThemeManager::ThemeManager(Theme theme) { |
102 | 30 this->theme = theme; |
31 } | |
32 | |
279 | 33 Theme ThemeManager::GetTheme() const { |
102 | 34 return theme; |
35 } | |
36 | |
279 | 37 bool ThemeManager::IsInDarkTheme() const { |
38 switch (theme) { | |
39 case Theme::Default: | |
102 | 40 #ifdef MACOSX |
279 | 41 if (osx::DarkThemeAvailable()) |
42 return osx::IsInDarkTheme(); | |
102 | 43 #elif defined(WIN32) |
279 | 44 if (win32::DarkThemeAvailable()) |
45 return win32::IsInDarkTheme(); | |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
46 #else |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
47 # ifdef GLIB |
279 | 48 return glib::IsInDarkTheme(); |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
49 # endif |
102 | 50 #endif |
279 | 51 default: break; |
52 } | |
53 return (theme == Theme::Dark); | |
102 | 54 } |
55 | |
279 | 56 void ThemeManager::SetToDarkTheme() { |
102 | 57 /* macOS >= 10.14 has its own global dark theme, |
58 use it :) */ | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
59 #ifdef MACOSX |
102 | 60 if (osx::DarkThemeAvailable()) |
61 osx::SetToDarkTheme(); | |
62 else | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
63 #elif defined(WIN32) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
64 if (win32::DarkThemeAvailable()) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
65 win32::SetTitleBarsToBlack(true); |
102 | 66 #endif |
279 | 67 SetStyleSheet(Theme::Dark); |
102 | 68 } |
69 | |
279 | 70 void ThemeManager::SetToLightTheme() { |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
71 #ifdef MACOSX |
102 | 72 if (osx::DarkThemeAvailable()) |
73 osx::SetToLightTheme(); | |
74 else | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
75 #elif defined(WIN32) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
76 if (win32::DarkThemeAvailable()) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
77 win32::SetTitleBarsToBlack(false); |
102 | 78 #endif |
279 | 79 SetStyleSheet(Theme::Light); |
102 | 80 } |
81 | |
279 | 82 Theme ThemeManager::GetCurrentOSTheme() const { |
83 return IsInDarkTheme() ? Theme::Dark : Theme::Light; | |
102 | 84 } |
85 | |
86 /* this function is private, and should stay that way */ | |
279 | 87 void ThemeManager::SetStyleSheet(Theme theme) { |
102 | 88 switch (theme) { |
279 | 89 case Theme::Dark: { |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
90 const QColor darkGray(53, 53, 53); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
91 const QColor gray(128, 128, 128); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
92 const QColor black(25, 25, 25); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
93 const QColor blue(42, 130, 218); |
102 | 94 |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
95 QPalette pal(QApplication::style()->standardPalette()); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
96 pal.setColor(QPalette::Window, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
97 pal.setColor(QPalette::WindowText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
98 pal.setColor(QPalette::Base, black); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
99 pal.setColor(QPalette::AlternateBase, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
100 pal.setColor(QPalette::ToolTipBase, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
101 pal.setColor(QPalette::ToolTipText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
102 pal.setColor(QPalette::Text, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
103 pal.setColor(QPalette::Button, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
104 pal.setColor(QPalette::ButtonText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
105 pal.setColor(QPalette::Link, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
106 pal.setColor(QPalette::Highlight, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
107 pal.setColor(QPalette::HighlightedText, Qt::black); |
102 | 108 |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
109 pal.setColor(QPalette::Active, QPalette::Button, gray.darker()); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
110 pal.setColor(QPalette::Disabled, QPalette::ButtonText, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
111 pal.setColor(QPalette::Disabled, QPalette::WindowText, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
112 pal.setColor(QPalette::Disabled, QPalette::Text, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
113 pal.setColor(QPalette::Disabled, QPalette::Light, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
114 qApp->setPalette(pal); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
115 |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
116 #ifdef WIN32 |
258 | 117 qApp->setStyleSheet([] { |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
118 QFile f(":/dark.qss"); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
119 if (!f.exists()) |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
120 return QStringLiteral(""); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
121 f.open(QFile::ReadOnly | QFile::Text); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
122 QTextStream ts(&f); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
123 return ts.readAll(); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
124 }()); |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
125 #endif |
102 | 126 break; |
127 } | |
128 default: | |
279 | 129 /* this sucks. */ |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
130 QPalette pal(QApplication::style()->standardPalette()); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
131 #ifdef WIN32 /* fuck you Qt 6 */ |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
132 pal.setColor(QPalette::Window, QColor(0xF0, 0xF0, 0xF0)); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
133 #endif |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
134 qApp->setPalette(pal); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
135 |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
136 qApp->setStyleSheet(""); |
102 | 137 break; |
138 } | |
139 } | |
140 | |
279 | 141 void ThemeManager::SetTheme(Theme theme) { |
102 | 142 switch (theme) { |
279 | 143 case Theme::Light: SetToLightTheme(); break; |
144 case Theme::Dark: SetToDarkTheme(); break; | |
145 case Theme::Default: | |
146 if (GetCurrentOSTheme() == Theme::Light) | |
102 | 147 SetToLightTheme(); |
148 else | |
149 SetToDarkTheme(); | |
150 break; | |
151 } | |
152 this->theme = theme; | |
153 } | |
154 | |
279 | 155 void ThemeManager::RepaintCurrentTheme() { |
156 SetTheme(theme); | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
157 } |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
158 |
258 | 159 } // namespace Theme |