63
+ − 1 #include "core/config.h"
+ − 2 #include "core/session.h"
46
+ − 3 #include <QApplication>
+ − 4 #include <QFile>
+ − 5 #include <QTextStream>
+ − 6 #ifdef MACOSX
+ − 7 # include "sys/osx/dark_theme.h"
+ − 8 #else
+ − 9 # include "sys/win32/dark_theme.h"
+ − 10 #endif
+ − 11
+ − 12 namespace DarkTheme {
+ − 13
+ − 14 bool IsInDarkMode() {
+ − 15 if (session.config.theme != Themes::OS)
+ − 16 return (session.config.theme == Themes::DARK);
+ − 17 #ifdef MACOSX
+ − 18 if (osx::DarkThemeAvailable()) {
+ − 19 if (osx::IsInDarkTheme()) {
+ − 20 return true;
+ − 21 } else {
+ − 22 return false;
+ − 23 }
+ − 24 }
+ − 25 #elif defined(WIN32)
+ − 26 if (win32::DarkThemeAvailable()) {
+ − 27 if (win32::IsInDarkTheme()) {
+ − 28 return true;
+ − 29 } else {
+ − 30 return false;
+ − 31 }
+ − 32 }
+ − 33 #endif
+ − 34 return (session.config.theme == Themes::DARK);
+ − 35 }
+ − 36
+ − 37 /* this function is private, and should stay that way */
+ − 38 void SetStyleSheet(enum Themes theme) {
+ − 39 switch (theme) {
+ − 40 case Themes::DARK: {
+ − 41 QFile f(":qdarkstyle/dark/darkstyle.qss");
+ − 42 if (!f.exists())
+ − 43 return; // fail
+ − 44 f.open(QFile::ReadOnly | QFile::Text);
+ − 45 QTextStream ts(&f);
+ − 46 qApp->setStyleSheet(ts.readAll());
+ − 47 break;
+ − 48 }
+ − 49 default: qApp->setStyleSheet(""); break;
+ − 50 }
+ − 51 }
+ − 52
+ − 53 void SetToDarkTheme() {
+ − 54 /* macOS >= 10.14 has its own global dark theme,
+ − 55 use it :) */
+ − 56 #if MACOSX
+ − 57 if (osx::DarkThemeAvailable())
+ − 58 osx::SetToDarkTheme();
+ − 59 else
+ − 60 #endif
63
+ − 61 SetStyleSheet(Themes::DARK);
46
+ − 62 }
+ − 63
+ − 64 void SetToLightTheme() {
+ − 65 #if MACOSX
+ − 66 if (osx::DarkThemeAvailable())
+ − 67 osx::SetToLightTheme();
+ − 68 else
+ − 69 #endif
62
+ − 70 SetStyleSheet(Themes::LIGHT);
46
+ − 71 }
+ − 72
+ − 73 enum Themes GetCurrentOSTheme() {
+ − 74 #if MACOSX
+ − 75 if (osx::DarkThemeAvailable())
+ − 76 return osx::IsInDarkTheme() ? Themes::DARK : Themes::LIGHT;
+ − 77 #elif defined(WIN32)
+ − 78 if (win32::DarkThemeAvailable())
+ − 79 return win32::IsInDarkTheme() ? Themes::DARK : Themes::LIGHT;
+ − 80 #endif
+ − 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 void SetTheme(enum Themes theme) {
+ − 88 switch (theme) {
+ − 89 case Themes::LIGHT: SetToLightTheme(); break;
+ − 90 case Themes::DARK: SetToDarkTheme(); break;
+ − 91 case Themes::OS:
+ − 92 if (GetCurrentOSTheme() == Themes::LIGHT)
+ − 93 SetToLightTheme();
+ − 94 else
+ − 95 SetToDarkTheme();
+ − 96 break;
+ − 97 }
+ − 98 }
+ − 99
+ − 100 } // namespace DarkTheme