Mercurial > minori
annotate src/gui/theme.cc @ 353:2f094656e775
sys/x11/settings: misc fixups
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 14 Jul 2024 23:27:43 -0400 |
parents | c844f8bb87ce |
children |
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> |
291 | 10 #include <QtGlobal> |
102 | 11 #ifdef MACOSX |
12 # include "sys/osx/dark_theme.h" | |
279 | 13 #elif defined(WIN32) |
102 | 14 # include "sys/win32/dark_theme.h" |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
15 #else |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
16 # ifdef GLIB |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
17 # include "sys/glib/dark_theme.h" |
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
18 # endif |
351
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
19 # ifdef HAVE_XCB |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
20 # include "sys/x11/dark_theme.h" |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
21 # endif |
102 | 22 #endif |
23 | |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
24 /* 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
|
25 * 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
|
26 * 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
|
27 * 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
|
28 * 3. Windows dark mode support in Qt 6.5 (with Fusion) is completely unavoidable |
258 | 29 */ |
102 | 30 |
31 namespace Theme { | |
32 | |
279 | 33 ThemeManager::ThemeManager(Theme theme) { |
102 | 34 this->theme = theme; |
35 } | |
36 | |
279 | 37 Theme ThemeManager::GetTheme() const { |
102 | 38 return theme; |
39 } | |
40 | |
279 | 41 bool ThemeManager::IsInDarkTheme() const { |
42 switch (theme) { | |
43 case Theme::Default: | |
102 | 44 #ifdef MACOSX |
279 | 45 if (osx::DarkThemeAvailable()) |
46 return osx::IsInDarkTheme(); | |
102 | 47 #elif defined(WIN32) |
279 | 48 if (win32::DarkThemeAvailable()) |
49 return win32::IsInDarkTheme(); | |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
50 #else |
351
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
51 # ifdef HAVE_XCB |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
52 if (x11::IsInDarkTheme()) |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
53 return true; |
232
ff0061e75f0f
theme: add OS detection with glib
Paper <mrpapersonic@gmail.com>
parents:
230
diff
changeset
|
54 # endif |
351
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
55 # ifdef GLIB |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
56 if (glib::IsInDarkTheme()) |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
57 return true; |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
58 # endif |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
59 break; |
102 | 60 #endif |
351
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
61 default: |
c844f8bb87ce
gui/theme: add xsettings backend
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
62 break; |
279 | 63 } |
64 return (theme == Theme::Dark); | |
102 | 65 } |
66 | |
279 | 67 void ThemeManager::SetToDarkTheme() { |
102 | 68 /* macOS >= 10.14 has its own global dark theme, |
69 use it :) */ | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
70 #ifdef MACOSX |
102 | 71 if (osx::DarkThemeAvailable()) |
72 osx::SetToDarkTheme(); | |
73 else | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
74 #elif defined(WIN32) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
75 if (win32::DarkThemeAvailable()) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
76 win32::SetTitleBarsToBlack(true); |
102 | 77 #endif |
279 | 78 SetStyleSheet(Theme::Dark); |
102 | 79 } |
80 | |
279 | 81 void ThemeManager::SetToLightTheme() { |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
82 #ifdef MACOSX |
102 | 83 if (osx::DarkThemeAvailable()) |
84 osx::SetToLightTheme(); | |
85 else | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
86 #elif defined(WIN32) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
87 if (win32::DarkThemeAvailable()) |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
88 win32::SetTitleBarsToBlack(false); |
102 | 89 #endif |
279 | 90 SetStyleSheet(Theme::Light); |
102 | 91 } |
92 | |
279 | 93 Theme ThemeManager::GetCurrentOSTheme() const { |
94 return IsInDarkTheme() ? Theme::Dark : Theme::Light; | |
102 | 95 } |
96 | |
97 /* this function is private, and should stay that way */ | |
279 | 98 void ThemeManager::SetStyleSheet(Theme theme) { |
102 | 99 switch (theme) { |
279 | 100 case Theme::Dark: { |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
101 const QColor darkGray(53, 53, 53); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
102 const QColor gray(128, 128, 128); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
103 const QColor black(25, 25, 25); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
104 const QColor blue(42, 130, 218); |
102 | 105 |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
106 QPalette pal; |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
107 |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
108 pal.setColor(QPalette::Window, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
109 pal.setColor(QPalette::WindowText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
110 pal.setColor(QPalette::Base, black); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
111 pal.setColor(QPalette::AlternateBase, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
112 pal.setColor(QPalette::ToolTipBase, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
113 pal.setColor(QPalette::ToolTipText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
114 pal.setColor(QPalette::Text, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
115 pal.setColor(QPalette::Button, darkGray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
116 pal.setColor(QPalette::ButtonText, Qt::white); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
117 pal.setColor(QPalette::Link, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
118 pal.setColor(QPalette::Highlight, blue); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
119 pal.setColor(QPalette::HighlightedText, Qt::black); |
102 | 120 |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
121 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
|
122 pal.setColor(QPalette::Disabled, QPalette::ButtonText, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
123 pal.setColor(QPalette::Disabled, QPalette::WindowText, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
124 pal.setColor(QPalette::Disabled, QPalette::Text, gray); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
125 pal.setColor(QPalette::Disabled, QPalette::Light, darkGray); |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
126 |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
127 qApp->setPalette(pal); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
128 |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
129 #ifdef WIN32 |
258 | 130 qApp->setStyleSheet([] { |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
131 QFile f(":/dark.qss"); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
132 if (!f.exists()) |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
133 return QStringLiteral(""); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
134 f.open(QFile::ReadOnly | QFile::Text); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
135 QTextStream ts(&f); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
136 return ts.readAll(); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
229
diff
changeset
|
137 }()); |
229
adc20fa321c1
theme: force Fusion style on platforms other than Win32 or OS X
Paper <paper@paper.us.eu.org>
parents:
183
diff
changeset
|
138 #endif |
102 | 139 break; |
140 } | |
141 default: | |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
142 /* this sucks */ |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
143 QPalette pal(QApplication::style()->standardPalette()); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
144 #ifdef WIN32 /* fuck you Qt 6 */ |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
145 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
|
146 #endif |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
147 qApp->setPalette(pal); |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
148 |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
149 qApp->setStyleSheet(""); |
102 | 150 break; |
151 } | |
152 } | |
153 | |
279 | 154 void ThemeManager::SetTheme(Theme theme) { |
102 | 155 switch (theme) { |
279 | 156 case Theme::Light: SetToLightTheme(); break; |
157 case Theme::Dark: SetToDarkTheme(); break; | |
158 case Theme::Default: | |
159 if (GetCurrentOSTheme() == Theme::Light) | |
102 | 160 SetToLightTheme(); |
161 else | |
162 SetToDarkTheme(); | |
163 break; | |
164 } | |
165 this->theme = theme; | |
166 } | |
167 | |
279 | 168 void ThemeManager::RepaintCurrentTheme() { |
169 SetTheme(theme); | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
170 } |
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
171 |
258 | 172 } // namespace Theme |