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"
+ − 10 #else
+ − 11 # include "sys/win32/dark_theme.h"
+ − 12 #endif
+ − 13
+ − 14 /* This is, believe it or not, one of the hardest things I've implemented :/
+ − 15 1. Dark mode stuff in Qt changes a lot and Qt 5 and Qt 6 are massively different
+ − 16 2. Some widgets, i.e. QTabWidget, QTabBar, etc., just completely IGNORE the QPalette setting
+ − 17 3. I don't want to use the Fusion style on every single platform
+ − 18 4. Windows dark mode support in Qt 6.5 (with Fusion) is completely unavoidable
+ − 19 (not a joke btw, it's retarded)
+ − 20 These three already make it really hard, but along with that, I don't even remember if
+ − 21 OS X dark mode support even works still; I remember the background of some of the widgets
+ − 22 would refuse to update for whatever reason. */
+ − 23
+ − 24 namespace Theme {
+ − 25
+ − 26 Theme::Theme(Themes theme) {
+ − 27 this->theme = theme;
+ − 28 }
+ − 29
+ − 30 Themes Theme::GetTheme() {
+ − 31 return theme;
+ − 32 }
+ − 33
+ − 34 bool Theme::IsInDarkMode() {
+ − 35 if (theme != Themes::OS)
+ − 36 return (theme == Themes::DARK);
+ − 37 #ifdef MACOSX
+ − 38 if (osx::DarkThemeAvailable())
+ − 39 return osx::IsInDarkTheme();
+ − 40 #elif defined(WIN32)
+ − 41 if (win32::DarkThemeAvailable())
+ − 42 return win32::IsInDarkTheme();
+ − 43 #endif
+ − 44 return (theme == Themes::DARK);
+ − 45 }
+ − 46
+ − 47 void Theme::SetToDarkTheme() {
+ − 48 /* macOS >= 10.14 has its own global dark theme,
+ − 49 use it :) */
+ − 50 #if MACOSX
+ − 51 if (osx::DarkThemeAvailable())
+ − 52 osx::SetToDarkTheme();
+ − 53 else
+ − 54 #endif
+ − 55 SetStyleSheet(Themes::DARK);
+ − 56 }
+ − 57
+ − 58 void Theme::SetToLightTheme() {
+ − 59 #if MACOSX
+ − 60 if (osx::DarkThemeAvailable())
+ − 61 osx::SetToLightTheme();
+ − 62 else
+ − 63 #endif
+ − 64 SetStyleSheet(Themes::LIGHT);
+ − 65 }
+ − 66
+ − 67 Themes Theme::GetCurrentOSTheme() {
+ − 68 #if MACOSX
+ − 69 if (osx::DarkThemeAvailable())
+ − 70 return osx::IsInDarkTheme() ? Themes::DARK : Themes::LIGHT;
+ − 71 #elif defined(WIN32)
+ − 72 if (win32::DarkThemeAvailable())
+ − 73 return win32::IsInDarkTheme() ? Themes::DARK : Themes::LIGHT;
+ − 74 #endif
+ − 75 /* Currently OS detection only supports Windows and macOS.
+ − 76 Please don't be shy if you're willing to port it to other OSes
+ − 77 (or desktop environments, or window managers) */
+ − 78 return Themes::LIGHT;
+ − 79 }
+ − 80
+ − 81 /* this function is private, and should stay that way */
+ − 82 void Theme::SetStyleSheet(Themes theme) {
+ − 83 switch (theme) {
+ − 84 case Themes::DARK: {
+ − 85 QColor darkGray(53, 53, 53);
+ − 86 QColor gray(128, 128, 128);
+ − 87 QColor black(25, 25, 25);
+ − 88 QColor blue(42, 130, 218);
+ − 89
+ − 90 QPalette darkPalette;
+ − 91 darkPalette.setColor(QPalette::Window, darkGray);
+ − 92 darkPalette.setColor(QPalette::WindowText, Qt::white);
+ − 93 darkPalette.setColor(QPalette::Base, black);
+ − 94 darkPalette.setColor(QPalette::AlternateBase, darkGray);
+ − 95 darkPalette.setColor(QPalette::ToolTipBase, blue);
+ − 96 darkPalette.setColor(QPalette::ToolTipText, Qt::white);
+ − 97 darkPalette.setColor(QPalette::Text, Qt::white);
+ − 98 darkPalette.setColor(QPalette::Button, darkGray);
+ − 99 darkPalette.setColor(QPalette::ButtonText, Qt::white);
+ − 100 darkPalette.setColor(QPalette::Link, blue);
+ − 101 darkPalette.setColor(QPalette::Highlight, blue);
+ − 102 darkPalette.setColor(QPalette::HighlightedText, Qt::black);
+ − 103
+ − 104 darkPalette.setColor(QPalette::Active, QPalette::Button, gray.darker());
+ − 105 darkPalette.setColor(QPalette::Disabled, QPalette::ButtonText, gray);
+ − 106 darkPalette.setColor(QPalette::Disabled, QPalette::WindowText, gray);
+ − 107 darkPalette.setColor(QPalette::Disabled, QPalette::Text, gray);
+ − 108 darkPalette.setColor(QPalette::Disabled, QPalette::Light, darkGray);
+ − 109 qApp->setPalette(darkPalette);
+ − 110 break;
+ − 111 }
+ − 112 default:
+ − 113 qApp->setPalette(QApplication::style()->standardPalette());
+ − 114 break;
+ − 115 }
+ − 116 }
+ − 117
+ − 118 void Theme::SetTheme(Themes theme) {
+ − 119 switch (theme) {
+ − 120 case Themes::LIGHT:
+ − 121 SetToLightTheme();
+ − 122 break;
+ − 123 case Themes::DARK:
+ − 124 SetToDarkTheme();
+ − 125 break;
+ − 126 case Themes::OS:
+ − 127 if (GetCurrentOSTheme() == Themes::LIGHT)
+ − 128 SetToLightTheme();
+ − 129 else
+ − 130 SetToDarkTheme();
+ − 131 break;
+ − 132 }
+ − 133 this->theme = theme;
+ − 134 }
+ − 135
+ − 136 } // namespace DarkTheme