view src/gui/dark_theme.cc @ 101:c537996cf67b

*: multitude of config changes 1. theme is now configurable from the settings menu (but you have to restart for it to apply) 2. config is now stored in an INI file, with no method of conversion from json (this repo is private-ish anyway)
author Paper <mrpapersonic@gmail.com>
date Fri, 03 Nov 2023 14:06:02 -0400
parents 9b2b41f83a5e
children
line wrap: on
line source

#include "core/config.h"
#include "core/session.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
#ifdef MACOSX
#	include "sys/osx/dark_theme.h"
#else
#	include "sys/win32/dark_theme.h"
#endif

namespace DarkTheme {

bool IsInDarkMode() {
	if (session.config.theme != Themes::OS)
		return (session.config.theme == Themes::DARK);
#ifdef MACOSX
	if (osx::DarkThemeAvailable()) {
		if (osx::IsInDarkTheme()) {
			return true;
		} else {
			return false;
		}
	}
#elif defined(WIN32)
	if (win32::DarkThemeAvailable()) {
		if (win32::IsInDarkTheme()) {
			return true;
		} else {
			return false;
		}
	}
#endif
	return (session.config.theme == Themes::DARK);
}

/* this function is private, and should stay that way */
void SetStyleSheet(enum Themes theme) {
	switch (theme) {
		case Themes::DARK: {
			QFile f(":qdarkstyle/dark/darkstyle.qss");
			if (!f.exists())
				return; // fail
			f.open(QFile::ReadOnly | QFile::Text);
			QTextStream ts(&f);
			qApp->setStyleSheet(ts.readAll());
			break;
		}
		default: qApp->setStyleSheet(""); break;
	}
}

void SetToDarkTheme() {
	/* macOS >= 10.14 has its own global dark theme,
	   use it :) */
#if MACOSX
	if (osx::DarkThemeAvailable())
		osx::SetToDarkTheme();
	else
#endif
		SetStyleSheet(Themes::DARK);
}

void SetToLightTheme() {
#if MACOSX
	if (osx::DarkThemeAvailable())
		osx::SetToLightTheme();
	else
#endif
		SetStyleSheet(Themes::LIGHT);
}

enum Themes GetCurrentOSTheme() {
#if MACOSX
	if (osx::DarkThemeAvailable())
		return osx::IsInDarkTheme() ? Themes::DARK : Themes::LIGHT;
#elif defined(WIN32)
	if (win32::DarkThemeAvailable())
		return win32::IsInDarkTheme() ? Themes::DARK : Themes::LIGHT;
#endif
	/* Currently OS detection only supports Windows and macOS.
	   Please don't be shy if you're willing to port it to other OSes
	   (or desktop environments, or window managers) */
	return Themes::LIGHT;
}

void SetTheme(enum Themes theme) {
	switch (theme) {
		case Themes::LIGHT: SetToLightTheme(); break;
		case Themes::DARK: SetToDarkTheme(); break;
		case Themes::OS:
			if (GetCurrentOSTheme() == Themes::LIGHT)
				SetToLightTheme();
			else
				SetToDarkTheme();
			break;
	}
}

} // namespace DarkTheme