5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 05:30:52 +08:00

Mac support

This commit is contained in:
Lea Anthony 2025-01-22 19:29:23 +11:00
parent 547e30f025
commit 276a1a22a5
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
3 changed files with 58 additions and 1 deletions

View File

@ -10,6 +10,8 @@ package application
#include "application_darwin.h"
#include "application_darwin_delegate.h"
#include "webview_window_darwin.h"
#include "custom_protocol.h"
#include <stdlib.h>
extern void registerListener(unsigned int event);
@ -177,6 +179,8 @@ import (
"github.com/wailsapp/wails/v3/pkg/events"
)
var openUrlBuffer = make(chan string, 100)
type macosApp struct {
applicationMenu unsafe.Pointer
parent *App
@ -229,6 +233,9 @@ func (m *macosApp) setApplicationMenu(menu *Menu) {
}
func (m *macosApp) run() error {
C.StartCustomProtocolHandler()
if m.parent.options.SingleInstance != nil {
cUniqueID := C.CString(m.parent.options.SingleInstance.UniqueID)
defer C.free(unsafe.Pointer(cUniqueID))
@ -262,9 +269,25 @@ func (m *macosApp) GetFlags(options Options) map[string]any {
func newPlatformApp(app *App) *macosApp {
C.init()
return &macosApp{
result := &macosApp{
parent: app,
}
go result.startUrlOpenProcessor()
return result
}
func (m *macosApp) startUrlOpenProcessor() {
for url := range openUrlBuffer {
if m.parent.options.Mac.OnUrlOpen != nil {
m.parent.options.Mac.OnUrlOpen(url)
}
}
}
//export HandleCustomProtocol
func HandleCustomProtocol(url *C.char) {
goUrl := C.GoString(url)
openUrlBuffer <- goUrl
}
//export processApplicationEvent

View File

@ -0,0 +1,14 @@
#ifndef CustomProtocol_h
#define CustomProtocol_h
#import <Cocoa/Cocoa.h>
extern void HandleCustomProtocol(char*);
@interface CustomProtocolSchemeHandler : NSObject
+ (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
@end
void StartCustomProtocolHandler(void);
#endif /* CustomProtocol_h */

View File

@ -0,0 +1,20 @@
#include "CustomProtocol.h"
@implementation CustomProtocolSchemeHandler
+ (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
[event paramDescriptorForKeyword:keyDirectObject];
NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
HandleCustomProtocol((char*)[[[event paramDescriptorForKeyword:keyDirectObject] stringValue] UTF8String]);
}
@end
void StartCustomProtocolHandler(void) {
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:[CustomProtocolSchemeHandler class]
andSelector:@selector(handleGetURLEvent:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID: kAEGetURL];
}