added suggested changes

Signed-off-by: Tayou <tayou@gmx.net>
This commit is contained in:
Tayou 2022-11-01 15:41:08 +01:00
parent fef60a9da0
commit a3f5ea3598
No known key found for this signature in database
GPG Key ID: 02CA43C1CB6E9887
7 changed files with 68 additions and 71 deletions

View File

@ -2,10 +2,8 @@
/* /*
* Prism Launcher * Prism Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net> * Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (C) 2022 Tayou <tayou@gmx.net>
*
* PolyMC - Minecraft Launcher
* Copyright (C) 2022 Lenny McLennington <lenny@sneed.church> * Copyright (C) 2022 Lenny McLennington <lenny@sneed.church>
* Copyright (C) 2022 Tayou <tayou@gmx.net>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -71,6 +69,8 @@
#include "ui/pagedialog/PageDialog.h" #include "ui/pagedialog/PageDialog.h"
#include "ui/themes/ThemeManager.h"
#include "ApplicationMessage.h" #include "ApplicationMessage.h"
#include <iostream> #include <iostream>
@ -747,11 +747,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
} }
// Themes // Themes
{ m_themeManager = std::make_unique<ThemeManager>(m_mainWindow);
m_themeManager = new ThemeManager(m_mainWindow);
m_themeManager->InitializeThemes();
}
// initialize and load all instances // initialize and load all instances
{ {
@ -860,10 +856,6 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
performMainStartupAction(); performMainStartupAction();
} }
ThemeManager* Application::getThemeManager() {
return Application::m_themeManager;
}
bool Application::createSetupWizard() bool Application::createSetupWizard()
{ {
bool javaRequired = [&]() bool javaRequired = [&]()
@ -1109,7 +1101,7 @@ std::shared_ptr<JavaInstallList> Application::javalist()
return m_javalist; return m_javalist;
} }
std::vector<ITheme *> Application::getValidApplicationThemes() QList<ITheme*> Application::getValidApplicationThemes()
{ {
return m_themeManager->getValidApplicationThemes(); return m_themeManager->getValidApplicationThemes();
} }

View File

@ -45,8 +45,6 @@
#include <QUrl> #include <QUrl>
#include <updater/GoUpdate.h> #include <updater/GoUpdate.h>
#include "ui/themes/ThemeManager.h"
#include <BaseInstance.h> #include <BaseInstance.h>
#include "minecraft/launch/MinecraftServerTarget.h" #include "minecraft/launch/MinecraftServerTarget.h"
@ -71,6 +69,7 @@ class BaseDetachedToolFactory;
class TranslationsModel; class TranslationsModel;
class ITheme; class ITheme;
class MCEditTool; class MCEditTool;
class ThemeManager;
namespace Meta { namespace Meta {
class Index; class Index;
@ -121,7 +120,7 @@ public:
void setIconTheme(const QString& name); void setIconTheme(const QString& name);
std::vector<ITheme *> getValidApplicationThemes(); QList<ITheme*> getValidApplicationThemes();
void setApplicationTheme(const QString& name, bool initial); void setApplicationTheme(const QString& name, bool initial);
@ -201,8 +200,6 @@ public:
void ShowGlobalSettings(class QWidget * parent, QString open_page = QString()); void ShowGlobalSettings(class QWidget * parent, QString open_page = QString());
ThemeManager* getThemeManager();
signals: signals:
void updateAllowedChanged(bool status); void updateAllowedChanged(bool status);
void globalSettingsAboutToOpen(); void globalSettingsAboutToOpen();
@ -262,7 +259,7 @@ private:
std::shared_ptr<GenericPageProvider> m_globalSettingsProvider; std::shared_ptr<GenericPageProvider> m_globalSettingsProvider;
std::unique_ptr<MCEditTool> m_mcedit; std::unique_ptr<MCEditTool> m_mcedit;
QSet<QString> m_features; QSet<QString> m_features;
ThemeManager* m_themeManager; std::unique_ptr<ThemeManager> m_themeManager;
QMap<QString, std::shared_ptr<BaseProfilerFactory>> m_profilers; QMap<QString, std::shared_ptr<BaseProfilerFactory>> m_profilers;

View File

@ -66,7 +66,7 @@ static bool readThemeJson(const QString &path, QPalette &palette, double &fadeAm
QColor color(colorValue); QColor color(colorValue);
if(!color.isValid()) if(!color.isValid())
{ {
themeWarningLog << "Color value" << colorValue << "for" << colorName << "was not recognized."; themeWarningLog() << "Color value" << colorValue << "for" << colorName << "was not recognized.";
return QColor(); return QColor();
} }
return color; return color;
@ -82,7 +82,7 @@ static bool readThemeJson(const QString &path, QPalette &palette, double &fadeAm
} }
else else
{ {
themeDebugLog << "Color value for" << colorName << "was not present."; themeDebugLog() << "Color value for" << colorName << "was not present.";
} }
}; };
@ -108,13 +108,13 @@ static bool readThemeJson(const QString &path, QPalette &palette, double &fadeAm
} }
catch (const Exception &e) catch (const Exception &e)
{ {
themeWarningLog << "Couldn't load theme json: " << e.cause(); themeWarningLog() << "Couldn't load theme json: " << e.cause();
return false; return false;
} }
} }
else else
{ {
themeDebugLog << "No theme json present."; themeDebugLog() << "No theme json present.";
return false; return false;
} }
return true; return true;
@ -160,7 +160,7 @@ static bool writeThemeJson(const QString &path, const QPalette &palette, double
} }
catch (const Exception &e) catch (const Exception &e)
{ {
themeWarningLog << "Failed to write theme json to" << path; themeWarningLog() << "Failed to write theme json to" << path;
return false; return false;
} }
} }
@ -179,7 +179,7 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
if(!FS::ensureFolderPathExists(path) || !FS::ensureFolderPathExists(pathResources)) if(!FS::ensureFolderPathExists(path) || !FS::ensureFolderPathExists(pathResources))
{ {
themeWarningLog << "X couldn't create folder for theme!"; themeWarningLog() << "couldn't create folder for theme!";
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
return; return;
@ -192,7 +192,7 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
if (!readThemeJson(themeFilePath, m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath, jsonDataIncomplete)) if (!readThemeJson(themeFilePath, m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath, jsonDataIncomplete))
{ {
themeDebugLog << "Did not read theme json file correctly, writing new one to: " << themeFilePath; themeDebugLog() << "Did not read theme json file correctly, writing new one to: " << themeFilePath;
m_name = "Custom"; m_name = "Custom";
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
m_fadeColor = baseTheme->fadeColor(); m_fadeColor = baseTheme->fadeColor();
@ -223,13 +223,13 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
} }
catch (const Exception &e) catch (const Exception &e)
{ {
themeWarningLog << "X Couldn't load css:" << e.cause() << "from" << cssFilePath; themeWarningLog() << "Couldn't load css:" << e.cause() << "from" << cssFilePath;
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
} }
} }
else else
{ {
themeDebugLog << "X No theme css present."; themeDebugLog() << "No theme css present.";
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
try try
{ {
@ -237,7 +237,7 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
} }
catch (const Exception &e) catch (const Exception &e)
{ {
themeWarningLog << "X Couldn't write css:" << e.cause() << "to" << cssFilePath; themeWarningLog() << "Couldn't write css:" << e.cause() << "to" << cssFilePath;
} }
} }
} else { } else {
@ -250,7 +250,7 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
if(!FS::ensureFilePathExists(path)) if(!FS::ensureFilePathExists(path))
{ {
themeWarningLog << m_name << " Theme file path doesn't exist!"; themeWarningLog() << m_name << " Theme file path doesn't exist!";
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
return; return;
@ -264,7 +264,7 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
} }
catch (const Exception &e) catch (const Exception &e)
{ {
themeWarningLog << "Couldn't load qss:" << e.cause() << "from" << path; themeWarningLog() << "Couldn't load qss:" << e.cause() << "from" << path;
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
} }
} }

View File

@ -39,8 +39,7 @@
#pragma once #pragma once
#include "ITheme.h" #include "ITheme.h"
#include <QFile> #include <QFileInfo>
#include <QDir>
class CustomTheme: public ITheme class CustomTheme: public ITheme
{ {

View File

@ -45,25 +45,25 @@
SystemTheme::SystemTheme() SystemTheme::SystemTheme()
{ {
themeDebugLog << "Determining System Theme..."; themeDebugLog() << "Determining System Theme...";
const auto & style = QApplication::style(); const auto & style = QApplication::style();
systemPalette = style->standardPalette(); systemPalette = style->standardPalette();
QString lowerThemeName = style->objectName(); QString lowerThemeName = style->objectName();
themeDebugLog << "System theme seems to be:" << lowerThemeName; themeDebugLog() << "System theme seems to be:" << lowerThemeName;
QStringList styles = QStyleFactory::keys(); QStringList styles = QStyleFactory::keys();
for(auto &st: styles) for(auto &st: styles)
{ {
themeDebugLog << "Considering theme from theme factory:" << st.toLower(); themeDebugLog() << "Considering theme from theme factory:" << st.toLower();
if(st.toLower() == lowerThemeName) if(st.toLower() == lowerThemeName)
{ {
systemTheme = st; systemTheme = st;
themeDebugLog << "System theme has been determined to be:" << systemTheme; themeDebugLog() << "System theme has been determined to be:" << systemTheme;
return; return;
} }
} }
// fall back to fusion if we can't find the current theme. // fall back to fusion if we can't find the current theme.
systemTheme = "Fusion"; systemTheme = "Fusion";
themeDebugLog << "System theme not found, defaulted to Fusion"; themeDebugLog() << "System theme not found, defaulted to Fusion";
} }
void SystemTheme::apply(bool initial) void SystemTheme::apply(bool initial)

View File

@ -29,20 +29,25 @@
#include "Application.h" #include "Application.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <versionhelpers.h> #include <windows.h>
// this is needed for versionhelpers.h, it is also included in WinDarkmode, but we can't rely on that.
// Ultimately this should be included in versionhelpers, but that is outside of the project.
#include "ui/WinDarkmode.h" #include "ui/WinDarkmode.h"
#include <versionhelpers.h>
#endif #endif
ThemeManager::ThemeManager(MainWindow* mainWindow) { ThemeManager::ThemeManager(MainWindow* mainWindow) {
m_mainWindow = mainWindow; m_mainWindow = mainWindow;
InitializeThemes();
} }
/// @brief Adds the Theme to the list of themes /// @brief Adds the Theme to the list of themes
/// @param theme The Theme to add /// @param theme The Theme to add
/// @return Theme ID /// @return Theme ID
QString ThemeManager::AddTheme(ITheme * theme) { QString ThemeManager::AddTheme(std::unique_ptr<ITheme> theme) {
m_themes.insert(std::make_pair(theme->id(), std::unique_ptr<ITheme>(theme))); QString id = theme->id();
return theme->id(); m_themes.emplace(id, std::move(theme));
return id;
} }
/// @brief Gets the Theme from the List via ID /// @brief Gets the Theme from the List via ID
@ -62,20 +67,20 @@ void ThemeManager::InitializeThemes() {
auto searchPaths = QIcon::themeSearchPaths(); auto searchPaths = QIcon::themeSearchPaths();
searchPaths.append("iconthemes"); searchPaths.append("iconthemes");
QIcon::setThemeSearchPaths(searchPaths); QIcon::setThemeSearchPaths(searchPaths);
themeDebugLog << "<> Icon themes initialized."; themeDebugLog() << "<> Icon themes initialized.";
} }
// Initialize widget themes // Initialize widget themes
{ {
themeDebugLog << "<> Initializing Widget Themes"; themeDebugLog() << "<> Initializing Widget Themes";
themeDebugLog "Loading Built-in Theme:" << AddTheme(new SystemTheme()); themeDebugLog() << "Loading Built-in Theme:" << AddTheme(std::make_unique<SystemTheme>());
auto darkThemeId = AddTheme(new DarkTheme()); auto darkThemeId = AddTheme(std::make_unique<DarkTheme>());
themeDebugLog "Loading Built-in Theme:" << darkThemeId; themeDebugLog() << "Loading Built-in Theme:" << darkThemeId;
themeDebugLog "Loading Built-in Theme:" << AddTheme(new BrightTheme()); themeDebugLog() << "Loading Built-in Theme:" << AddTheme(std::make_unique<BrightTheme>());
// TODO: need some way to differentiate same name themes in different subdirectories (maybe smaller grey text next to theme name in dropdown? Dunno how to do that though) // TODO: need some way to differentiate same name themes in different subdirectories (maybe smaller grey text next to theme name in dropdown?)
QString themeFolder = (new QDir("./themes/"))->absoluteFilePath(""); QString themeFolder = QDir("./themes/").absoluteFilePath("");
themeDebugLog << "Theme Folder Path: " << themeFolder; themeDebugLog() << "Theme Folder Path: " << themeFolder;
QDirIterator directoryIterator(themeFolder, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); QDirIterator directoryIterator(themeFolder, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (directoryIterator.hasNext()) { while (directoryIterator.hasNext()) {
@ -83,34 +88,30 @@ void ThemeManager::InitializeThemes() {
QFileInfo themeJson(dir.absoluteFilePath("theme.json")); QFileInfo themeJson(dir.absoluteFilePath("theme.json"));
if (themeJson.exists()) { if (themeJson.exists()) {
// Load "theme.json" based themes // Load "theme.json" based themes
themeDebugLog "✓ Loading JSON Theme from:" << themeJson.absoluteFilePath(); themeDebugLog() << "Loading JSON Theme from:" << themeJson.absoluteFilePath();
CustomTheme* theme = new CustomTheme(GetTheme(darkThemeId), themeJson, true); AddTheme(std::make_unique<CustomTheme>(GetTheme(darkThemeId), themeJson, true));
AddTheme(theme);
} else { } else {
// Load pure QSS Themes // Load pure QSS Themes
QDirIterator stylesheetFileIterator(dir.absoluteFilePath(""), {"*.qss", "*.css"}, QDir::Files); QDirIterator stylesheetFileIterator(dir.absoluteFilePath(""), {"*.qss", "*.css"}, QDir::Files);
while (stylesheetFileIterator.hasNext()) { while (stylesheetFileIterator.hasNext()) {
QFile customThemeFile(stylesheetFileIterator.next()); QFile customThemeFile(stylesheetFileIterator.next());
QFileInfo customThemeFileInfo(customThemeFile); QFileInfo customThemeFileInfo(customThemeFile);
themeDebugLog "✓ Loading QSS Theme from:" << customThemeFileInfo.absoluteFilePath(); themeDebugLog() << "Loading QSS Theme from:" << customThemeFileInfo.absoluteFilePath();
CustomTheme* theme = new CustomTheme(GetTheme(darkThemeId), customThemeFileInfo, false); AddTheme(std::make_unique<CustomTheme>(GetTheme(darkThemeId), customThemeFileInfo, false));
AddTheme(theme);
} }
} }
} }
themeDebugLog << "<> Widget themes initialized."; themeDebugLog() << "<> Widget themes initialized.";
} }
} }
std::vector<ITheme *> ThemeManager::getValidApplicationThemes() QList<ITheme*> ThemeManager::getValidApplicationThemes()
{ {
std::vector<ITheme *> ret; QList<ITheme*> ret;
auto iter = m_themes.cbegin(); ret.reserve(m_themes.size());
while (iter != m_themes.cend()) for (auto&& [id, theme] : m_themes) {
{ ret.append(theme.get());
ret.push_back((*iter).second.get());
iter++;
} }
return ret; return ret;
} }
@ -133,7 +134,8 @@ void ThemeManager::setApplicationTheme(const QString& name, bool initial)
auto themeIter = m_themes.find(name); auto themeIter = m_themes.find(name);
if(themeIter != m_themes.end()) if(themeIter != m_themes.end())
{ {
auto & theme = (*themeIter).second; auto & theme = themeIter->second;
themeDebugLog() << "applying theme" << theme->name();
theme->apply(initial); theme->apply(initial);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
if (m_mainWindow && IsWindows10OrGreater()) { if (m_mainWindow && IsWindows10OrGreater()) {
@ -147,6 +149,6 @@ void ThemeManager::setApplicationTheme(const QString& name, bool initial)
} }
else else
{ {
qWarning() << "Tried to set invalid theme:" << name; themeWarningLog() << "Tried to set invalid theme:" << name;
} }
} }

View File

@ -15,21 +15,28 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#pragma once
#include <QString> #include <QString>
#include "ui/themes/ITheme.h" #include "ui/themes/ITheme.h"
#include "ui/MainWindow.h" #include "ui/MainWindow.h"
#define themeDebugLog qDebug() << "[Themes]" inline auto themeDebugLog() {
#define themeWarningLog qWarning() << "[Themes]" return qDebug() << "[Theme]";
}
inline auto themeWarningLog() {
return qWarning() << "[Theme]";
}
class ThemeManager { class ThemeManager {
public: public:
ThemeManager(MainWindow* mainWindow); ThemeManager(MainWindow* mainWindow);
// maybe make private? Or put in ctor?
void InitializeThemes(); void InitializeThemes();
std::vector<ITheme *> getValidApplicationThemes(); QList<ITheme*> getValidApplicationThemes();
void setIconTheme(const QString& name); void setIconTheme(const QString& name);
void applyCurrentlySelectedTheme(); void applyCurrentlySelectedTheme();
void setApplicationTheme(const QString& name, bool initial); void setApplicationTheme(const QString& name, bool initial);
@ -38,7 +45,7 @@ private:
std::map<QString, std::unique_ptr<ITheme>> m_themes; std::map<QString, std::unique_ptr<ITheme>> m_themes;
MainWindow* m_mainWindow; MainWindow* m_mainWindow;
QString AddTheme(ITheme * theme); QString AddTheme(std::unique_ptr<ITheme> theme);
ITheme* GetTheme(QString themeId); ITheme* GetTheme(QString themeId);
}; };