2022-06-25 12:19:59 +02:00
|
|
|
#if defined(OS_MACOS)
|
|
|
|
|
|
|
|
#include <CoreFoundation/CFBundle.h>
|
|
|
|
#include <ApplicationServices/ApplicationServices.h>
|
2022-06-30 15:28:51 +02:00
|
|
|
#include <Foundation/NSUserDefaults.h>
|
2023-02-08 13:51:56 +01:00
|
|
|
#include <AppKit/NSScreen.h>
|
2024-02-24 22:46:52 +01:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#include <CoreText/CoreText.h>
|
2022-06-25 12:19:59 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2023-12-30 23:52:25 +01:00
|
|
|
|
|
|
|
#define GLFW_EXPOSE_NATIVE_COCOA
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <GLFW/glfw3native.h>
|
|
|
|
|
2023-05-14 21:50:58 +02:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2023-05-14 20:20:22 +02:00
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
2023-05-27 17:45:41 +02:00
|
|
|
void errorMessageMacos(const char *cMessage) {
|
|
|
|
CFStringRef strMessage = CFStringCreateWithCString(NULL, cMessage, kCFStringEncodingUTF8);
|
|
|
|
CFUserNotificationDisplayAlert(0, kCFUserNotificationStopAlertLevel, NULL, NULL, NULL, strMessage, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:03:04 +02:00
|
|
|
void openFile(const char *path);
|
2024-02-24 22:46:52 +01:00
|
|
|
void registerFont(const char *fontName, const char *fontPath);
|
2023-05-14 20:20:22 +02:00
|
|
|
|
2023-05-14 22:03:04 +02:00
|
|
|
void openWebpageMacos(const char *url) {
|
2022-06-25 12:19:59 +02:00
|
|
|
CFURLRef urlRef = CFURLCreateWithBytes(NULL, (uint8_t*)(url), strlen(url), kCFStringEncodingASCII, NULL);
|
|
|
|
LSOpenCFURLRef(urlRef, NULL);
|
|
|
|
CFRelease(urlRef);
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:03:04 +02:00
|
|
|
bool isMacosSystemDarkModeEnabled(void) {
|
2022-06-30 15:09:57 +02:00
|
|
|
NSString * appleInterfaceStyle = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
|
|
|
|
|
|
|
|
if (appleInterfaceStyle && [appleInterfaceStyle length] > 0) {
|
|
|
|
return [[appleInterfaceStyle lowercaseString] containsString:@"dark"];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:03:04 +02:00
|
|
|
float getBackingScaleFactor(void) {
|
2023-02-08 13:51:56 +01:00
|
|
|
return [[NSScreen mainScreen] backingScaleFactor];
|
|
|
|
}
|
|
|
|
|
2024-02-11 14:12:14 +01:00
|
|
|
void setupMacosWindowStyle(GLFWwindow *window, bool borderlessWindowMode) {
|
2023-12-30 23:52:25 +01:00
|
|
|
NSWindow* cocoaWindow = glfwGetCocoaWindow(window);
|
2024-02-03 22:39:31 +01:00
|
|
|
|
2023-12-30 23:52:25 +01:00
|
|
|
cocoaWindow.titleVisibility = NSWindowTitleHidden;
|
2024-01-30 11:21:34 +01:00
|
|
|
|
2024-02-11 14:12:14 +01:00
|
|
|
if (borderlessWindowMode) {
|
|
|
|
cocoaWindow.titlebarAppearsTransparent = YES;
|
|
|
|
cocoaWindow.styleMask |= NSWindowStyleMaskFullSizeContentView;
|
|
|
|
|
|
|
|
[cocoaWindow setOpaque:NO];
|
|
|
|
[cocoaWindow setHasShadow:YES];
|
|
|
|
[cocoaWindow setBackgroundColor:[NSColor colorWithWhite: 0 alpha: 0.001f]];
|
|
|
|
}
|
2024-02-03 22:39:31 +01:00
|
|
|
}
|
2024-01-30 11:21:34 +01:00
|
|
|
|
2024-02-03 22:39:31 +01:00
|
|
|
bool isMacosFullScreenModeEnabled(GLFWwindow *window) {
|
|
|
|
NSWindow* cocoaWindow = glfwGetCocoaWindow(window);
|
|
|
|
return (cocoaWindow.styleMask & NSWindowStyleMaskFullScreen) == NSWindowStyleMaskFullScreen;
|
2023-12-30 23:52:25 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 22:46:52 +01:00
|
|
|
void enumerateFontsMacos(void) {
|
|
|
|
CFArrayRef fontDescriptors = CTFontManagerCopyAvailableFontFamilyNames();
|
|
|
|
CFIndex count = CFArrayGetCount(fontDescriptors);
|
|
|
|
|
|
|
|
for (CFIndex i = 0; i < count; i++) {
|
|
|
|
CFStringRef fontName = (CFStringRef)CFArrayGetValueAtIndex(fontDescriptors, i);
|
|
|
|
|
|
|
|
// Get font path
|
|
|
|
CFDictionaryRef attributes = (__bridge CFDictionaryRef)@{ (__bridge NSString *)kCTFontNameAttribute : (__bridge NSString *)fontName };
|
|
|
|
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes(attributes);
|
|
|
|
CFURLRef fontURL = CTFontDescriptorCopyAttribute(descriptor, kCTFontURLAttribute);
|
|
|
|
CFStringRef fontPath = CFURLCopyFileSystemPath(fontURL, kCFURLPOSIXPathStyle);
|
|
|
|
|
|
|
|
registerFont([(__bridge NSString *)fontName UTF8String], [(__bridge NSString *)fontPath UTF8String]);
|
|
|
|
|
|
|
|
CFRelease(descriptor);
|
|
|
|
CFRelease(fontURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFRelease(fontDescriptors);
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:27:57 +02:00
|
|
|
void macosHandleTitlebarDoubleClickGesture(GLFWwindow *window) {
|
|
|
|
NSWindow* cocoaWindow = glfwGetCocoaWindow(window);
|
|
|
|
|
|
|
|
// Consult user preferences: "System Settings -> Desktop & Dock -> Double-click a window's title bar to"
|
|
|
|
NSString* action = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleActionOnDoubleClick"];
|
|
|
|
|
|
|
|
if (action == nil || [action isEqualToString:@"None"]) {
|
|
|
|
// Nothing to do
|
|
|
|
} else if ([action isEqualToString:@"Minimize"]) {
|
|
|
|
if ([cocoaWindow isMiniaturizable]) {
|
|
|
|
[cocoaWindow miniaturize:nil];
|
|
|
|
}
|
|
|
|
} else if ([action isEqualToString:@"Maximize"]) {
|
|
|
|
// `[NSWindow zoom:_ sender]` takes over pumping the main runloop for the duration of the resize,
|
|
|
|
// and would interfere with our renderer's frame logic. Schedule it for the next frame
|
|
|
|
|
|
|
|
CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^{
|
|
|
|
if ([cocoaWindow isZoomable]) {
|
|
|
|
[cocoaWindow zoom:nil];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool macosIsWindowBeingResizedByUser(GLFWwindow *window) {
|
|
|
|
NSWindow* cocoaWindow = glfwGetCocoaWindow(window);
|
|
|
|
|
|
|
|
return cocoaWindow.inLiveResize;
|
|
|
|
}
|
|
|
|
|
2023-05-14 18:35:35 +02:00
|
|
|
@interface HexDocument : NSDocument
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation HexDocument
|
|
|
|
|
2023-05-14 20:20:22 +02:00
|
|
|
- (BOOL) readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError {
|
2023-05-14 22:03:04 +02:00
|
|
|
NSString* urlString = [url absoluteString];
|
|
|
|
const char* utf8String = [urlString UTF8String];
|
|
|
|
|
2024-02-23 20:17:33 +01:00
|
|
|
const char *prefix = "file://";
|
|
|
|
if (strncmp(utf8String, prefix, strlen(prefix)) == 0)
|
|
|
|
utf8String += strlen(prefix);
|
|
|
|
|
2023-05-14 22:03:04 +02:00
|
|
|
openFile(utf8String);
|
2023-05-14 20:20:22 +02:00
|
|
|
|
2023-05-14 18:35:35 +02:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2023-01-04 14:55:58 +01:00
|
|
|
#endif
|