diff --git a/common/CocoaTools.h b/common/CocoaTools.h index 0ea11e747c..3afe75b447 100644 --- a/common/CocoaTools.h +++ b/common/CocoaTools.h @@ -22,6 +22,10 @@ namespace CocoaTools { bool CreateMetalLayer(WindowInfo* wi); void DestroyMetalLayer(WindowInfo* wi); + /// Add a handler to be run when macOS changes between dark and light themes + void AddThemeChangeHandler(void* ctx, void(handler)(void* ctx)); + /// Remove a handler previously added using AddThemeChangeHandler with the given context + void RemoveThemeChangeHandler(void* ctx); } #endif // __APPLE__ diff --git a/common/CocoaTools.mm b/common/CocoaTools.mm index d0b6e5e117..e81d2aaf21 100644 --- a/common/CocoaTools.mm +++ b/common/CocoaTools.mm @@ -20,9 +20,12 @@ #include "CocoaTools.h" #include "Console.h" #include "WindowInfo.h" +#include #include #include +// MARK: - Metal Layers + bool CocoaTools::CreateMetalLayer(WindowInfo* wi) { if (![NSThread isMainThread]) @@ -64,3 +67,61 @@ void CocoaTools::DestroyMetalLayer(WindowInfo* wi) [view setLayer:nil]; [view setWantsLayer:NO]; } + +// MARK: - Theme Change Handlers + +@interface PCSX2KVOHelper : NSObject + +- (void)addCallback:(void*)ctx run:(void(*)(void*))callback; +- (void)removeCallback:(void*)ctx; + +@end + +@implementation PCSX2KVOHelper +{ + std::vector> _callbacks; +} + +- (void)addCallback:(void*)ctx run:(void(*)(void*))callback +{ + _callbacks.push_back(std::make_pair(ctx, callback)); +} + +- (void)removeCallback:(void*)ctx +{ + auto new_end = std::remove_if(_callbacks.begin(), _callbacks.end(), [ctx](const auto& entry){ + return ctx == entry.first; + }); + _callbacks.erase(new_end, _callbacks.end()); +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + for (const auto& callback : _callbacks) + callback.second(callback.first); +} + +@end + +static PCSX2KVOHelper* s_themeChangeHandler; + +void CocoaTools::AddThemeChangeHandler(void* ctx, void(handler)(void* ctx)) +{ + assert([NSThread isMainThread]); + if (!s_themeChangeHandler) + { + s_themeChangeHandler = [[PCSX2KVOHelper alloc] init]; + NSApplication* app = [NSApplication sharedApplication]; + [app addObserver:s_themeChangeHandler + forKeyPath:@"effectiveAppearance" + options:0 + context:nil]; + } + [s_themeChangeHandler addCallback:ctx run:handler]; +} + +void CocoaTools::RemoveThemeChangeHandler(void* ctx) +{ + assert([NSThread isMainThread]); + [s_themeChangeHandler removeCallback:ctx]; +} diff --git a/pcsx2-qt/MainWindow.cpp b/pcsx2-qt/MainWindow.cpp index cb64cccafd..3bac98d1b8 100644 --- a/pcsx2-qt/MainWindow.cpp +++ b/pcsx2-qt/MainWindow.cpp @@ -24,6 +24,7 @@ #include #include "common/Assertions.h" +#include "common/CocoaTools.h" #include "common/FileSystem.h" #include "pcsx2/CDVD/CDVDaccess.h" @@ -84,12 +85,25 @@ MainWindow::~MainWindow() // we compare here, since recreate destroys the window later if (g_main_window == this) g_main_window = nullptr; +#ifdef __APPLE__ + CocoaTools::RemoveThemeChangeHandler(this); +#endif } void MainWindow::initialize() { setStyleFromSettings(); setIconThemeFromStyle(); +#ifdef __APPLE__ + CocoaTools::AddThemeChangeHandler(this, [](void* ctx){ + // This handler is called *before* the style change has propagated far enough for Qt to see it + // Use RunOnUIThread to delay until it has + QtHost::RunOnUIThread([ctx = static_cast(ctx)]{ + ctx->setStyleFromSettings(); // Qt won't notice the style change without us touching the palette in some way + ctx->setIconThemeFromStyle(); + }); + }); +#endif m_ui.setupUi(this); setupAdditionalUi(); connectSignals();