Mercurial > minori
annotate src/gui/theme.cc @ 277:a796e97cc86d
dep/animone: x11: correctly check for connection failure
if there's no X server running then the previous code segfaults(!)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 22 Apr 2024 19:11:06 -0400 |
parents | 862d0d8619f6 |
children | 657fda1b9cac |
rev | line source |
---|---|
102 | 1 #include "core/config.h" |
2 #include "core/session.h" | |
3 #include <QApplication> | |
258 | 4 #include <QDebug> |
102 | 5 #include <QFile> |
258 | 6 #include <QStyleFactory> |
102 | 7 #include <QTextStream> |
8 #ifdef MACOSX | |
9 # include "sys/osx/dark_theme.h" | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
10 #elif WIN32 |
102 | 11 # include "sys/win32/dark_theme.h" |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
12 #else |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
13 # ifdef GLIB |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
14 # include "sys/glib/dark_theme.h" |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
15 # endif |
102 | 16 #endif |
17 | |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
18 /* 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
|
19 * 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
|
20 * 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
|
21 * 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
|
22 * 3. Windows dark mode support in Qt 6.5 (with Fusion) is completely unavoidable |
258 | 23 */ |
102 | 24 |
25 namespace Theme { | |
26 | |
27 Theme::Theme(Themes theme) { | |
28 this->theme = theme; | |
29 } | |
30 | |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
108
diff
changeset
|
31 Themes Theme::GetTheme() const { |
102 | 32 return theme; |
33 } | |
34 | |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
108
diff
changeset
|
35 bool Theme::IsInDarkTheme() const { |
102 | 36 if (theme != Themes::OS) |
37 return (theme == Themes::DARK); | |
38 #ifdef MACOSX | |
39 if (osx::DarkThemeAvailable()) | |
40 return osx::IsInDarkTheme(); | |
41 #elif defined(WIN32) | |
42 if (win32::DarkThemeAvailable()) | |
43 return win32::IsInDarkTheme(); | |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
44 #else |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
45 # ifdef GLIB |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
46 return glib::IsInDarkTheme(); |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
47 # endif |
102 | 48 #endif |
49 return (theme == Themes::DARK); | |
50 } | |
51 | |
52 void Theme::SetToDarkTheme() { | |
53 /* macOS >= 10.14 has its own global dark theme, | |
54 use it :) */ | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
55 #ifdef MACOSX |
102 | 56 if (osx::DarkThemeAvailable()) |
57 osx::SetToDarkTheme(); | |
58 else | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
59 #elif defined(WIN32) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
60 if (win32::DarkThemeAvailable()) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
61 win32::SetTitleBarsToBlack(true); |
102 | 62 #endif |
63 SetStyleSheet(Themes::DARK); | |
64 } | |
65 | |
66 void Theme::SetToLightTheme() { | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
67 #ifdef MACOSX |
102 | 68 if (osx::DarkThemeAvailable()) |
69 osx::SetToLightTheme(); | |
70 else | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
71 #elif defined(WIN32) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
72 if (win32::DarkThemeAvailable()) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
73 win32::SetTitleBarsToBlack(false); |
102 | 74 #endif |
75 SetStyleSheet(Themes::LIGHT); | |
76 } | |
77 | |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
108
diff
changeset
|
78 Themes Theme::GetCurrentOSTheme() const { |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
79 return IsInDarkTheme() ? Themes::DARK : Themes::LIGHT; |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
80 |
102 | 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 } | |
86 | |
87 /* this function is private, and should stay that way */ | |
88 void Theme::SetStyleSheet(Themes theme) { | |
89 switch (theme) { | |
90 case Themes::DARK: { | |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
91 const QColor darkGray(53, 53, 53); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
92 const QColor gray(128, 128, 128); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
93 const QColor black(25, 25, 25); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
94 const QColor blue(42, 130, 218); |
102 | 95 |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
96 QPalette pal(QApplication::style()->standardPalette()); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
97 pal.setColor(QPalette::Window, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
98 pal.setColor(QPalette::WindowText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
99 pal.setColor(QPalette::Base, black); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
100 pal.setColor(QPalette::AlternateBase, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
101 pal.setColor(QPalette::ToolTipBase, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
102 pal.setColor(QPalette::ToolTipText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
103 pal.setColor(QPalette::Text, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
104 pal.setColor(QPalette::Button, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
105 pal.setColor(QPalette::ButtonText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
106 pal.setColor(QPalette::Link, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
107 pal.setColor(QPalette::Highlight, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
108 pal.setColor(QPalette::HighlightedText, Qt::black); |
102 | 109 |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
110 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
|
111 pal.setColor(QPalette::Disabled, QPalette::ButtonText, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
112 pal.setColor(QPalette::Disabled, QPalette::WindowText, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
113 pal.setColor(QPalette::Disabled, QPalette::Text, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
114 pal.setColor(QPalette::Disabled, QPalette::Light, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
115 qApp->setPalette(pal); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
116 |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
117 #ifdef WIN32 |
258 | 118 qApp->setStyleSheet([] { |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
119 QFile f(":/dark.qss"); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
120 if (!f.exists()) |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
121 return QStringLiteral(""); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
122 f.open(QFile::ReadOnly | QFile::Text); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
123 QTextStream ts(&f); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
124 return ts.readAll(); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
125 }()); |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
126 #endif |
102 | 127 break; |
128 } | |
129 default: | |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
130 /* this sucks, it relies on the standard palette which |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
131 * may or may not be a dark style itself. */ |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
132 QPalette pal(QApplication::style()->standardPalette()); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
133 #ifdef WIN32 /* fuck you Qt 6 */ |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
134 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
|
135 #endif |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
136 qApp->setPalette(pal); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
137 |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
138 qApp->setStyleSheet(""); |
102 | 139 break; |
140 } | |
141 } | |
142 | |
143 void Theme::SetTheme(Themes theme) { | |
144 switch (theme) { | |
258 | 145 case Themes::LIGHT: SetToLightTheme(); break; |
146 case Themes::DARK: SetToDarkTheme(); break; | |
102 | 147 case Themes::OS: |
148 if (GetCurrentOSTheme() == Themes::LIGHT) | |
149 SetToLightTheme(); | |
150 else | |
151 SetToDarkTheme(); | |
152 break; | |
153 } | |
154 this->theme = theme; | |
155 } | |
156 | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
157 void Theme::RepaintCurrentTheme() { |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
158 Theme::SetTheme(theme); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
159 } |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
160 |
258 | 161 } // namespace Theme |