mirror of https://github.com/PCSX2/pcsx2.git
Qt: Respond to dark/light mode changes
This commit is contained in:
parent
beab9870cf
commit
f9c2327bf5
|
@ -22,6 +22,10 @@ namespace CocoaTools
|
||||||
{
|
{
|
||||||
bool CreateMetalLayer(WindowInfo* wi);
|
bool CreateMetalLayer(WindowInfo* wi);
|
||||||
void DestroyMetalLayer(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__
|
#endif // __APPLE__
|
||||||
|
|
|
@ -20,9 +20,12 @@
|
||||||
#include "CocoaTools.h"
|
#include "CocoaTools.h"
|
||||||
#include "Console.h"
|
#include "Console.h"
|
||||||
#include "WindowInfo.h"
|
#include "WindowInfo.h"
|
||||||
|
#include <vector>
|
||||||
#include <Cocoa/Cocoa.h>
|
#include <Cocoa/Cocoa.h>
|
||||||
#include <QuartzCore/QuartzCore.h>
|
#include <QuartzCore/QuartzCore.h>
|
||||||
|
|
||||||
|
// MARK: - Metal Layers
|
||||||
|
|
||||||
bool CocoaTools::CreateMetalLayer(WindowInfo* wi)
|
bool CocoaTools::CreateMetalLayer(WindowInfo* wi)
|
||||||
{
|
{
|
||||||
if (![NSThread isMainThread])
|
if (![NSThread isMainThread])
|
||||||
|
@ -64,3 +67,61 @@ void CocoaTools::DestroyMetalLayer(WindowInfo* wi)
|
||||||
[view setLayer:nil];
|
[view setLayer:nil];
|
||||||
[view setWantsLayer:NO];
|
[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<std::pair<void*, void(*)(void*)>> _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<NSKeyValueChangeKey,id> *)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];
|
||||||
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include <QtWidgets/QStyleFactory>
|
#include <QtWidgets/QStyleFactory>
|
||||||
|
|
||||||
#include "common/Assertions.h"
|
#include "common/Assertions.h"
|
||||||
|
#include "common/CocoaTools.h"
|
||||||
#include "common/FileSystem.h"
|
#include "common/FileSystem.h"
|
||||||
|
|
||||||
#include "pcsx2/CDVD/CDVDaccess.h"
|
#include "pcsx2/CDVD/CDVDaccess.h"
|
||||||
|
@ -84,12 +85,25 @@ MainWindow::~MainWindow()
|
||||||
// we compare here, since recreate destroys the window later
|
// we compare here, since recreate destroys the window later
|
||||||
if (g_main_window == this)
|
if (g_main_window == this)
|
||||||
g_main_window = nullptr;
|
g_main_window = nullptr;
|
||||||
|
#ifdef __APPLE__
|
||||||
|
CocoaTools::RemoveThemeChangeHandler(this);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::initialize()
|
void MainWindow::initialize()
|
||||||
{
|
{
|
||||||
setStyleFromSettings();
|
setStyleFromSettings();
|
||||||
setIconThemeFromStyle();
|
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<MainWindow*>(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);
|
m_ui.setupUi(this);
|
||||||
setupAdditionalUi();
|
setupAdditionalUi();
|
||||||
connectSignals();
|
connectSignals();
|
||||||
|
|
Loading…
Reference in New Issue