Skip to content

Commit

Permalink
darwin: allow pass nsdirectory to processApplicationEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
5aaee9 committed Oct 21, 2023
1 parent 831e769 commit e93492d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
35 changes: 33 additions & 2 deletions v3/pkg/application/application_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,27 @@ static void show(void) {
[NSApp unhide:nil];
}
static const char* serializationNSDictionary(void *dict) {
NSDictionary *nsDict = (__bridge NSDictionary *)dict;
if ([NSJSONSerialization isValidJSONObject:nsDict]) {
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:nsDict options:kNilOptions error:&error];
NSString *resultStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
return [resultStr UTF8String];
}
return nil;
}
*/
import "C"
import (
"github.com/wailsapp/wails/v3/internal/operatingsystem"
"encoding/json"
"unsafe"

"github.com/wailsapp/wails/v3/internal/operatingsystem"

"github.com/wailsapp/wails/v3/internal/assetserver/webview"
"github.com/wailsapp/wails/v3/pkg/events"
)
Expand Down Expand Up @@ -237,8 +252,24 @@ func newPlatformApp(app *App) *macosApp {
}

//export processApplicationEvent
func processApplicationEvent(eventID C.uint, _ unsafe.Pointer) {
func processApplicationEvent(eventID C.uint, data unsafe.Pointer) {
event := newApplicationEvent(int(eventID))

if data != nil {
dataCStrJSON := C.serializationNSDictionary(data)
if dataCStrJSON != nil {
dataJSON := C.GoString(dataCStrJSON)
var result map[string]any
err := json.Unmarshal([]byte(dataJSON), &result)

if err != nil {
panic(err)
}

event.Context().setData(result)
}
}

switch event.Id {
case uint(events.Mac.ApplicationDidChangeTheme):
isDark := globalApplication.IsDarkMode()
Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/application_darwin_delegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ - (void)themeChanged:(NSNotification *)notification {
- (BOOL)applicationShouldHandleReopen:(NSNotification *)notification
hasVisibleWindows:(BOOL)flag {
if( hasListeners(EventApplicationShouldHandleReopen) ) {
processApplicationEvent(EventApplicationShouldHandleReopen, NULL);
processApplicationEvent(EventApplicationShouldHandleReopen, @{@"hasVisibleWindows": @(flag)});
}

return TRUE;
Expand Down
16 changes: 14 additions & 2 deletions v3/pkg/application/context_application_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (c ApplicationEventContext) setIsDarkMode(mode bool) {
c.data["isDarkMode"] = mode
}

func (c ApplicationEventContext) IsDarkMode() bool {
mode, ok := c.data["isDarkMode"]
func (c ApplicationEventContext) getBool(key string) bool {
mode, ok := c.data[key]
if !ok {
return false
}
Expand All @@ -43,6 +43,18 @@ func (c ApplicationEventContext) IsDarkMode() bool {
return result
}

func (c ApplicationEventContext) IsDarkMode() bool {
return c.getBool("isDarkMode")
}

func (c ApplicationEventContext) HasVisibleWindows() bool {
return c.getBool("hasVisibleWindows")
}

func (c ApplicationEventContext) setData(data map[string]any) {
c.data = data
}

func newApplicationEventContext() *ApplicationEventContext {
return &ApplicationEventContext{
data: make(map[string]any),
Expand Down

0 comments on commit e93492d

Please sign in to comment.