Skip to content

Commit

Permalink
darwin: add event ApplicationShouldHandleReopen (wailsapp#2991)
Browse files Browse the repository at this point in the history
* darwin: add ApplicationShouldHandleReopen

* docs: update changelog with mr id

* events: update id

* feat: always return true

* Merge v3-alpha and regenerate events

* darwin: allow pass nsdirectory to processApplicationEvent

---------

Co-authored-by: Lea Anthony <[email protected]>
  • Loading branch information
5aaee9 and leaanthony authored Oct 22, 2023
1 parent d1c3f8a commit f8250fb
Show file tree
Hide file tree
Showing 10 changed files with 639 additions and 504 deletions.
1 change: 1 addition & 0 deletions mkdocs-website/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [darwin] add Event ApplicationShouldHandleReopen to able handle dock icon click by @5aaee9 in [#2991](https://github.com/wailsapp/wails/pull/2991)
- [darwin] add getPrimaryScreen/getScreens to impl by @tmclane in [#2618](https://github.com/wailsapp/wails/pull/2618)

### Fixed
Expand Down
72 changes: 72 additions & 0 deletions v3/examples/hide-window/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
_ "embed"
"log"
"runtime"

"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/icons"
"github.com/wailsapp/wails/v3/pkg/events"
)

func main() {
app := application.New(application.Options{
Name: "Hide Window Demo",
Description: "A test of Hidden window and display it",
Assets: application.AlphaAssets,
Mac: application.MacOptions{
// ActivationPolicy: application.ActivationPolicyAccessory,
ApplicationShouldTerminateAfterLastWindowClosed: false,
},
})

systemTray := app.NewSystemTray()

window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Width: 500,
Height: 800,
Frameless: false,
AlwaysOnTop: false,
Hidden: false,
DisableResize: false,
ShouldClose: func(window *application.WebviewWindow) bool {
println("close")
window.Hide()
return false
},
Windows: application.WindowsWindow{
HiddenOnTaskbar: true,
},
})

if runtime.GOOS == "darwin" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
}


// Click Dock icon tigger application show
app.On(events.Mac.ApplicationShouldHandleReopen, func(event *application.Event) {
println("reopen")
window.Show()
})

myMenu := app.NewMenu()
myMenu.Add("Show").OnClick(func(ctx *application.Context) {
window.Show()
})

myMenu.Add("Quit").OnClick(func(ctx *application.Context) {
app.Quit()
})

systemTray.SetMenu(myMenu)
systemTray.OnClick(func() {
app.CurrentWindow().Show()
})

err := app.Run()
if err != nil {
log.Fatal(err)
}
}
1 change: 1 addition & 0 deletions v3/internal/runtime/desktop/api/event_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const EventTypes = {
ApplicationWillUnhide: "mac:ApplicationWillUnhide",
ApplicationWillUpdate: "mac:ApplicationWillUpdate",
ApplicationDidChangeTheme: "mac:ApplicationDidChangeTheme!",
ApplicationShouldHandleReopen: "mac:ApplicationShouldHandleReopen!",
WindowDidBecomeKey: "mac:WindowDidBecomeKey",
WindowDidBecomeMain: "mac:WindowDidBecomeMain",
WindowDidBeginSheet: "mac:WindowDidBeginSheet",
Expand Down
39 changes: 37 additions & 2 deletions v3/pkg/application/application_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,29 @@ static void show(void) {
[NSApp unhide:nil];
}
static const char* serializationNSDictionary(void *dict) {
@autoreleasepool {
NSDictionary *nsDict = (__bridge NSDictionary *)dict;
if ([NSJSONSerialization isValidJSONObject:nsDict]) {
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:nsDict options:kNilOptions error:&error];
NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
return strdup([result 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 +254,26 @@ 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 {
defer C.free(unsafe.Pointer(dataCStrJSON))

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
8 changes: 8 additions & 0 deletions v3/pkg/application/application_darwin_delegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ - (void)themeChanged:(NSNotification *)notification {
processApplicationEvent(EventApplicationDidChangeTheme, NULL);
}
}
- (BOOL)applicationShouldHandleReopen:(NSNotification *)notification
hasVisibleWindows:(BOOL)flag {
if( hasListeners(EventApplicationShouldHandleReopen) ) {
processApplicationEvent(EventApplicationShouldHandleReopen, @{@"hasVisibleWindows": @(flag)});
}

return TRUE;
}
// GENERATED EVENTS START
- (void)applicationDidBecomeActive:(NSNotification *)notification {
if( hasListeners(EventApplicationDidBecomeActive) ) {
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
Loading

0 comments on commit f8250fb

Please sign in to comment.