From 87e974e0801d98614f6bd2eeb464cad9ca0d09e5 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Wed, 6 Jan 2021 11:56:01 +1100 Subject: [PATCH] Refactor/Tidy up Ffenestri darwin --- v2/internal/ffenestri/ffenestri_darwin.c | 96 +------------------ v2/internal/ffenestri/ffenestri_darwin.go | 14 +++ v2/internal/ffenestri/ffenestri_darwin.h | 110 +++++++++++++++++++--- 3 files changed, 112 insertions(+), 108 deletions(-) diff --git a/v2/internal/ffenestri/ffenestri_darwin.c b/v2/internal/ffenestri/ffenestri_darwin.c index bd4d350b6..e4c60b889 100644 --- a/v2/internal/ffenestri/ffenestri_darwin.c +++ b/v2/internal/ffenestri/ffenestri_darwin.c @@ -1,101 +1,7 @@ #ifdef FFENESTRI_DARWIN -#define OBJC_OLD_DISPATCH_PROTOTYPES 1 -#include -#include -#include "json.h" -#include "hashmap.h" -#include "stdlib.h" - -// Macros to make it slightly more sane -#define msg objc_msgSend - -#define c(str) (id)objc_getClass(str) -#define s(str) sel_registerName(str) -#define u(str) sel_getUid(str) -#define str(input) msg(c("NSString"), s("stringWithUTF8String:"), input) -#define strunicode(input) msg(c("NSString"), s("stringWithFormat:"), str("%C"), (unsigned short)input) -#define cstr(input) (const char *)msg(input, s("UTF8String")) -#define url(input) msg(c("NSURL"), s("fileURLWithPath:"), str(input)) - -#define ALLOC(classname) msg(c(classname), s("alloc")) -#define ALLOC_INIT(classname) msg(msg(c(classname), s("alloc")), s("init")) -#define GET_FRAME(receiver) ((CGRect(*)(id, SEL))objc_msgSend_stret)(receiver, s("frame")) -#define GET_BOUNDS(receiver) ((CGRect(*)(id, SEL))objc_msgSend_stret)(receiver, s("bounds")) -#define GET_BACKINGSCALEFACTOR(receiver) ((CGFloat(*)(id, SEL))msg)(receiver, s("backingScaleFactor")) - -#define STREQ(a,b) strcmp(a, b) == 0 -#define STRCOPY(a) concat(a, "") -#define STR_HAS_CHARS(input) input != NULL && strlen(input) > 0 -#define MEMFREE(input) free((void*)input); input = NULL; - -#define ON_MAIN_THREAD(str) dispatch( ^{ str; } ) -#define MAIN_WINDOW_CALL(str) msg(app->mainWindow, s((str))) - -#define NSBackingStoreBuffered 2 - -#define NSWindowStyleMaskBorderless 0 -#define NSWindowStyleMaskTitled 1 -#define NSWindowStyleMaskClosable 2 -#define NSWindowStyleMaskMiniaturizable 4 -#define NSWindowStyleMaskResizable 8 -#define NSWindowStyleMaskFullscreen 1 << 14 - -#define NSVisualEffectMaterialWindowBackground 12 -#define NSVisualEffectBlendingModeBehindWindow 0 -#define NSVisualEffectStateFollowsWindowActiveState 0 -#define NSVisualEffectStateActive 1 -#define NSVisualEffectStateInactive 2 - -#define NSViewWidthSizable 2 -#define NSViewHeightSizable 16 - -#define NSWindowBelow -1 -#define NSWindowAbove 1 - -#define NSSquareStatusItemLength -2.0 -#define NSVariableStatusItemLength -1.0 - -#define NSWindowTitleHidden 1 -#define NSWindowStyleMaskFullSizeContentView 1 << 15 - -#define NSEventModifierFlagCommand 1 << 20 -#define NSEventModifierFlagOption 1 << 19 -#define NSEventModifierFlagControl 1 << 18 -#define NSEventModifierFlagShift 1 << 17 - -#define NSControlStateValueMixed -1 -#define NSControlStateValueOff 0 -#define NSControlStateValueOn 1 - -// Unbelievably, if the user swaps their button preference -// then right buttons are reported as left buttons -#define NSEventMaskLeftMouseDown 1 << 1 -#define NSEventMaskLeftMouseUp 1 << 2 -#define NSEventMaskRightMouseDown 1 << 3 -#define NSEventMaskRightMouseUp 1 << 4 - -#define NSEventTypeLeftMouseDown 1 -#define NSEventTypeLeftMouseUp 2 -#define NSEventTypeRightMouseDown 3 -#define NSEventTypeRightMouseUp 4 - -#define NSNoImage 0 -#define NSImageOnly 1 -#define NSImageLeft 2 -#define NSImageRight 3 -#define NSImageBelow 4 -#define NSImageAbove 5 -#define NSImageOverlaps 6 - -#define NSAlertStyleWarning 0 -#define NSAlertStyleInformational 1 -#define NSAlertStyleCritical 2 - -#define NSAlertFirstButtonReturn 1000 -#define NSAlertSecondButtonReturn 1001 -#define NSAlertThirdButtonReturn 1002 +#include "ffenestri_darwin.h" // References to assets #include "assets.h" diff --git a/v2/internal/ffenestri/ffenestri_darwin.go b/v2/internal/ffenestri/ffenestri_darwin.go index 536f03663..9a438557c 100644 --- a/v2/internal/ffenestri/ffenestri_darwin.go +++ b/v2/internal/ffenestri/ffenestri_darwin.go @@ -6,6 +6,20 @@ package ffenestri #include "ffenestri_darwin.h" +extern void TitlebarAppearsTransparent(void *); +extern void HideTitle(void *); +extern void HideTitleBar(void *); +extern void FullSizeContent(void *); +extern void UseToolbar(void *); +extern void HideToolbarSeparator(void *); +extern void DisableFrame(void *); +extern void SetAppearance(void *, const char *); +extern void WebviewIsTransparent(void *); +extern void WindowBackgroundIsTranslucent(void *); +extern void SetMenu(void *, const char *); +extern void SetTray(void *, const char *, const char *, const char *); +extern void SetContextMenus(void *, const char *); + */ import "C" import ( diff --git a/v2/internal/ffenestri/ffenestri_darwin.h b/v2/internal/ffenestri/ffenestri_darwin.h index 3ca846044..90eb16f8c 100644 --- a/v2/internal/ffenestri/ffenestri_darwin.h +++ b/v2/internal/ffenestri/ffenestri_darwin.h @@ -2,18 +2,102 @@ #ifndef FFENESTRI_DARWIN_H #define FFENESTRI_DARWIN_H -extern void TitlebarAppearsTransparent(void *); -extern void HideTitle(void *); -extern void HideTitleBar(void *); -extern void FullSizeContent(void *); -extern void UseToolbar(void *); -extern void HideToolbarSeparator(void *); -extern void DisableFrame(void *); -extern void SetAppearance(void *, const char *); -extern void WebviewIsTransparent(void *); -extern void WindowBackgroundIsTranslucent(void *); -extern void SetMenu(void *, const char *); -extern void SetTray(void *, const char *, const char *, const char *); -extern void SetContextMenus(void *, const char *); + +#define OBJC_OLD_DISPATCH_PROTOTYPES 1 +#include +#include +#include "json.h" +#include "hashmap.h" +#include "stdlib.h" + +// Macros to make it slightly more sane +#define msg objc_msgSend + +#define c(str) (id)objc_getClass(str) +#define s(str) sel_registerName(str) +#define u(str) sel_getUid(str) +#define str(input) msg(c("NSString"), s("stringWithUTF8String:"), input) +#define strunicode(input) msg(c("NSString"), s("stringWithFormat:"), str("%C"), (unsigned short)input) +#define cstr(input) (const char *)msg(input, s("UTF8String")) +#define url(input) msg(c("NSURL"), s("fileURLWithPath:"), str(input)) + +#define ALLOC(classname) msg(c(classname), s("alloc")) +#define ALLOC_INIT(classname) msg(msg(c(classname), s("alloc")), s("init")) +#define GET_FRAME(receiver) ((CGRect(*)(id, SEL))objc_msgSend_stret)(receiver, s("frame")) +#define GET_BOUNDS(receiver) ((CGRect(*)(id, SEL))objc_msgSend_stret)(receiver, s("bounds")) +#define GET_BACKINGSCALEFACTOR(receiver) ((CGFloat(*)(id, SEL))msg)(receiver, s("backingScaleFactor")) + +#define STREQ(a,b) strcmp(a, b) == 0 +#define STRCOPY(a) concat(a, "") +#define STR_HAS_CHARS(input) input != NULL && strlen(input) > 0 +#define MEMFREE(input) free((void*)input); input = NULL; + +#define ON_MAIN_THREAD(str) dispatch( ^{ str; } ) +#define MAIN_WINDOW_CALL(str) msg(app->mainWindow, s((str))) + +#define NSBackingStoreBuffered 2 + +#define NSWindowStyleMaskBorderless 0 +#define NSWindowStyleMaskTitled 1 +#define NSWindowStyleMaskClosable 2 +#define NSWindowStyleMaskMiniaturizable 4 +#define NSWindowStyleMaskResizable 8 +#define NSWindowStyleMaskFullscreen 1 << 14 + +#define NSVisualEffectMaterialWindowBackground 12 +#define NSVisualEffectBlendingModeBehindWindow 0 +#define NSVisualEffectStateFollowsWindowActiveState 0 +#define NSVisualEffectStateActive 1 +#define NSVisualEffectStateInactive 2 + +#define NSViewWidthSizable 2 +#define NSViewHeightSizable 16 + +#define NSWindowBelow -1 +#define NSWindowAbove 1 + +#define NSSquareStatusItemLength -2.0 +#define NSVariableStatusItemLength -1.0 + +#define NSWindowTitleHidden 1 +#define NSWindowStyleMaskFullSizeContentView 1 << 15 + +#define NSEventModifierFlagCommand 1 << 20 +#define NSEventModifierFlagOption 1 << 19 +#define NSEventModifierFlagControl 1 << 18 +#define NSEventModifierFlagShift 1 << 17 + +#define NSControlStateValueMixed -1 +#define NSControlStateValueOff 0 +#define NSControlStateValueOn 1 + +// Unbelievably, if the user swaps their button preference +// then right buttons are reported as left buttons +#define NSEventMaskLeftMouseDown 1 << 1 +#define NSEventMaskLeftMouseUp 1 << 2 +#define NSEventMaskRightMouseDown 1 << 3 +#define NSEventMaskRightMouseUp 1 << 4 + +#define NSEventTypeLeftMouseDown 1 +#define NSEventTypeLeftMouseUp 2 +#define NSEventTypeRightMouseDown 3 +#define NSEventTypeRightMouseUp 4 + +#define NSNoImage 0 +#define NSImageOnly 1 +#define NSImageLeft 2 +#define NSImageRight 3 +#define NSImageBelow 4 +#define NSImageAbove 5 +#define NSImageOverlaps 6 + +#define NSAlertStyleWarning 0 +#define NSAlertStyleInformational 1 +#define NSAlertStyleCritical 2 + +#define NSAlertFirstButtonReturn 1000 +#define NSAlertSecondButtonReturn 1001 +#define NSAlertThirdButtonReturn 1002 + #endif \ No newline at end of file