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