Skip to content

Commit

Permalink
Merge branch 'dark_mode' into 'master'
Browse files Browse the repository at this point in the history
Support dark mode on Windows

Closes #7985

See merge request OpenMW/openmw!4101
  • Loading branch information
psi29a committed May 20, 2024
2 parents fd3ae0d + 9f470b4 commit cd116eb
Show file tree
Hide file tree
Showing 41 changed files with 969 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
Feature #7953: Allow to change SVG icons colors depending on color scheme
Feature #7964: Add Lua read access to MW Dialogue records
Feature #7971: Make save's Time Played value display hours instead of days
Feature #7985: Support dark mode on Windows
Task #5896: Do not use deprecated MyGUI properties
Task #6085: Replace boost::filesystem with std::filesystem
Task #6149: Dehardcode Lua API_REVISION
Expand Down
5 changes: 4 additions & 1 deletion apps/launcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ source_group(launcher FILES ${LAUNCHER} ${LAUNCHER_HEADER})

set(QT_USE_QTGUI 1)

set (LAUNCHER_RES ${CMAKE_SOURCE_DIR}/files/launcher/launcher.qrc)

# Set some platform specific settings
if(WIN32)
set(LAUNCHER_RES ${LAUNCHER_RES} ${CMAKE_SOURCE_DIR}/files/windows/QWindowsVistaDark/dark.qrc)
set(GUI_TYPE WIN32)
set(QT_USE_QTMAIN TRUE)
endif(WIN32)

QT_ADD_RESOURCES(RCC_SRCS ${CMAKE_SOURCE_DIR}/files/launcher/launcher.qrc)
QT_ADD_RESOURCES(RCC_SRCS ${LAUNCHER_RES})
QT_WRAP_UI(UI_HDRS ${LAUNCHER_UI})

include_directories(${CMAKE_CURRENT_BINARY_DIR})
Expand Down
3 changes: 2 additions & 1 deletion apps/launcher/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <components/files/configurationmanager.hpp>
#include <components/files/qtconversion.hpp>
#include <components/l10n/qttranslations.hpp>
#include <components/platform/application.hpp>
#include <components/platform/platform.hpp>

#ifdef MAC_OS_X_VERSION_MIN_REQUIRED
Expand All @@ -33,7 +34,7 @@ int runLauncher(int argc, char* argv[])

try
{
QApplication app(argc, argv);
Platform::Application app(argc, argv);

QString resourcesPath(".");
if (!variables["resources"].empty())
Expand Down
1 change: 1 addition & 0 deletions apps/opencs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ set (OPENCS_UI
source_group (openmw-cs FILES main.cpp ${OPENCS_SRC} ${OPENCS_HDR})

if(WIN32)
set(OPENCS_RES ${OPENCS_RES} ${CMAKE_SOURCE_DIR}/files/windows/QWindowsVistaDark/dark.qrc)
set(QT_USE_QTMAIN TRUE)
set(OPENCS_RC_FILE ${CMAKE_SOURCE_DIR}/files/windows/opencs.rc)
else(WIN32)
Expand Down
37 changes: 6 additions & 31 deletions apps/opencs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
#include <exception>
#include <string>

#include <QApplication>
#include <QIcon>
#include <QSurfaceFormat>

#include <osg/DisplaySettings>

#include <components/debug/debugging.hpp>
#include <components/debug/debuglog.hpp>
#include <components/misc/scalableicon.hpp>
#include <components/platform/application.hpp>
#include <components/platform/platform.hpp>

#include "model/doc/messages.hpp"
Expand All @@ -26,33 +24,6 @@ Q_DECLARE_METATYPE(std::string)
class QEvent;
class QObject;

class Application : public QApplication
{
private:
bool notify(QObject* receiver, QEvent* event) override
{
try
{
if (event->type() == QEvent::ThemeChange || event->type() == QEvent::PaletteChange)
Misc::ScalableIcon::updateAllIcons();

return QApplication::notify(receiver, event);
}
catch (const std::exception& exception)
{
Log(Debug::Error) << "An exception has been caught: " << exception.what();
}

return false;
}

public:
Application(int& argc, char* argv[])
: QApplication(argc, argv)
{
}
};

void setQSurfaceFormat()
{
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
Expand All @@ -76,14 +47,18 @@ int runApplication(int argc, char* argv[])

Q_INIT_RESOURCE(resources);

#ifdef WIN32
Q_INIT_RESOURCE(dark);
#endif

qRegisterMetaType<std::string>("std::string");
qRegisterMetaType<CSMWorld::UniversalId>("CSMWorld::UniversalId");
qRegisterMetaType<CSMDoc::Message>("CSMDoc::Message");

setQSurfaceFormat();
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

Application application(argc, argv);
Platform::Application application(argc, argv);

application.setWindowIcon(QIcon(":openmw-cs"));

Expand Down
5 changes: 4 additions & 1 deletion apps/wizard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ source_group(wizard FILES ${WIZARD} ${WIZARD_HEADER})

set(QT_USE_QTGUI 1)

set (WIZARD_RES ${CMAKE_SOURCE_DIR}/files/wizard/wizard.qrc)

# Set some platform specific settings
if(WIN32)
set(WIZARD_RES ${WIZARD_RES} ${CMAKE_SOURCE_DIR}/files/windows/QWindowsVistaDark/dark.qrc)
set(GUI_TYPE WIN32)
set(QT_USE_QTMAIN TRUE)
endif(WIN32)

QT_ADD_RESOURCES(RCC_SRCS ${CMAKE_SOURCE_DIR}/files/wizard/wizard.qrc)
QT_ADD_RESOURCES(RCC_SRCS ${WIZARD_RES})
QT_WRAP_UI(UI_HDRS ${WIZARD_UI})

include_directories(${CMAKE_CURRENT_BINARY_DIR})
Expand Down
4 changes: 2 additions & 2 deletions apps/wizard/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <QApplication>
#include <QDir>

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>

#include <components/files/qtconversion.hpp>
#include <components/l10n/qttranslations.hpp>
#include <components/platform/application.hpp>

#include "mainwizard.hpp"

Expand All @@ -23,7 +23,7 @@ int main(int argc, char* argv[])
configurationManager.addCommonOptions(description);
configurationManager.readConfiguration(variables, description, true);

QApplication app(argc, argv);
Platform::Application app(argc, argv);

// Now we make sure the current dir is set to application path
QDir dir(QCoreApplication::applicationDirPath());
Expand Down
4 changes: 4 additions & 0 deletions components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ if (USE_QT)
qtconfigpath
)

add_component_qt_dir (platform
application
)

QT_WRAP_UI(ESM_UI_HDR ${ESM_UI})
endif()

Expand Down
98 changes: 98 additions & 0 deletions components/platform/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "application.hpp"

#include <QFile>
#include <QStyle>
#include <QStyleHints>

#include <components/debug/debuglog.hpp>
#include <components/misc/scalableicon.hpp>

namespace Platform
{
Application::Application(int& argc, char* argv[])
: QApplication(argc, argv)
{
#if defined(WIN32) && QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
init();
}

void Application::init()
{
connect(this, &Application::darkModeChanged, this, &Application::updateStyle);

const auto* hints = QGuiApplication::styleHints();
const auto currentStyle = QApplication::style()->objectName();
mInitialStyle = currentStyle.toStdString();
mCurrentStyle = currentStyle.toStdString();
if (hints->colorScheme() == Qt::ColorScheme::Dark)
{
mDarkMode = true;
if (currentStyle == "windowsvista")
{
mCurrentStyle = "windows";
setStyle("windows");

QFile file(":/dark/dark.qss");
file.open(QIODevice::ReadOnly);
setStyleSheet(file.readAll());
}
}
}

void Application::updateStyle(bool isDark)
{
if (mInitialStyle != "windowsvista")
return;

if (isDark)
{
mCurrentStyle = "windows";
setStyle("windows");

QFile file(":/dark/dark.qss");
file.open(QIODevice::ReadOnly);
setStyleSheet(file.readAll());
}
else
{
mCurrentStyle = mInitialStyle;
setStyleSheet("");
setStyle(mInitialStyle.c_str());
}
#endif
}

bool Application::notify(QObject* receiver, QEvent* event)
{
try
{
if (event->type() == QEvent::ThemeChange || event->type() == QEvent::PaletteChange)
{
#if defined(WIN32) && QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
const auto* hints = QGuiApplication::styleHints();
const auto currentStyle = QApplication::style()->objectName();
bool isDark = hints->colorScheme() == Qt::ColorScheme::Dark;
if (isDark != mDarkMode)
{
mDarkMode = isDark;

bool result = QApplication::notify(receiver, event);

emit darkModeChanged(isDark);

return result;
}
#endif
Misc::ScalableIcon::updateAllIcons();
}

return QApplication::notify(receiver, event);
}
catch (const std::exception& exception)
{
Log(Debug::Error) << "An exception has been caught: " << exception.what();
}

return false;
}
}
30 changes: 30 additions & 0 deletions components/platform/application.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <QApplication>
#include <QEvent>

namespace Platform
{
class Application : public QApplication
{
Q_OBJECT

public:
Application(int& argc, char* argv[]);

#if defined(WIN32) && QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
void init();

private slots:
void updateStyle(bool isDark);
#endif

signals:
void darkModeChanged(bool);

private:
bool notify(QObject* receiver, QEvent* event) override;

std::string mCurrentStyle{};
std::string mInitialStyle{};
bool mDarkMode{ false };
};
}
9 changes: 4 additions & 5 deletions files/opencs/menu-close.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion files/opencs/menu-exit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion files/opencs/qt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions files/windows/QWindowsVistaDark/checkbox-checked-disabled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions files/windows/QWindowsVistaDark/checkbox-checked.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions files/windows/QWindowsVistaDark/checkbox-empty-disabled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions files/windows/QWindowsVistaDark/checkbox-empty.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions files/windows/QWindowsVistaDark/checkbox-half-disabled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions files/windows/QWindowsVistaDark/checkbox-half.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cd116eb

Please sign in to comment.