Common: Split some routines into CocoaTools
This commit is contained in:
@ -254,9 +254,9 @@ elseif(APPLE)
|
||||
include(AddMetalSources)
|
||||
|
||||
set(MAC_SOURCES
|
||||
cocoa_tools.h
|
||||
metal_device.h
|
||||
metal_device.mm
|
||||
metal_layer.h
|
||||
metal_stream_buffer.h
|
||||
metal_stream_buffer.mm
|
||||
platform_misc_mac.mm
|
||||
|
||||
@ -1,30 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||
|
||||
#include <string_view>
|
||||
|
||||
struct WindowInfo;
|
||||
|
||||
#ifdef __OBJC__
|
||||
#import <AppKit/AppKit.h>
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
namespace CocoaTools {
|
||||
NSString* StringViewToNSString(const std::string_view& str);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
namespace CocoaTools {
|
||||
/// 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);
|
||||
|
||||
/// Creates metal layer on specified window surface.
|
||||
bool CreateMetalLayer(WindowInfo* wi);
|
||||
|
||||
/// Destroys metal layer on specified window surface.
|
||||
void DestroyMetalLayer(WindowInfo* wi);
|
||||
} // namespace CocoaTools
|
||||
12
src/util/metal_layer.h
Normal file
12
src/util/metal_layer.h
Normal file
@ -0,0 +1,12 @@
|
||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||
|
||||
struct WindowInfo;
|
||||
|
||||
namespace CocoaTools {
|
||||
/// Creates metal layer on specified window surface.
|
||||
bool CreateMetalLayer(WindowInfo* wi);
|
||||
|
||||
/// Destroys metal layer on specified window surface.
|
||||
void DestroyMetalLayer(WindowInfo* wi);
|
||||
} // namespace CocoaTools
|
||||
@ -3,7 +3,7 @@
|
||||
|
||||
#include "platform_misc.h"
|
||||
#include "window_info.h"
|
||||
#include "cocoa_tools.h"
|
||||
#include "metal_layer.h"
|
||||
|
||||
#include "common/log.h"
|
||||
#include "common/small_string.h"
|
||||
@ -80,74 +80,6 @@ bool PlatformMisc::PlaySoundAsync(const char* path)
|
||||
return result;
|
||||
}
|
||||
|
||||
NSString* CocoaTools::StringViewToNSString(const std::string_view& str)
|
||||
{
|
||||
if (str.empty())
|
||||
return nil;
|
||||
|
||||
return [[[NSString alloc] initWithBytes:str.data()
|
||||
length:static_cast<NSUInteger>(str.length())
|
||||
encoding:NSUTF8StringEncoding] autorelease];
|
||||
}
|
||||
|
||||
// From https://github.com/PCSX2/pcsx2/blob/1b673d9dd0829a48f5f0b6604c1de2108e981399/common/CocoaTools.mm
|
||||
|
||||
@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];
|
||||
}
|
||||
|
||||
bool CocoaTools::CreateMetalLayer(WindowInfo *wi)
|
||||
{
|
||||
// Punt off to main thread if we're not calling from it already.
|
||||
|
||||
@ -17,7 +17,7 @@
|
||||
#endif
|
||||
|
||||
#if defined(VK_USE_PLATFORM_METAL_EXT)
|
||||
#include "util/cocoa_tools.h"
|
||||
#include "util/metal_layer.h"
|
||||
#endif
|
||||
|
||||
Log_SetChannel(VulkanDevice);
|
||||
|
||||
Reference in New Issue
Block a user