diff --git a/v2/pkg/mac/mac_darwin.go b/v2/pkg/mac/login_darwin.go similarity index 100% rename from v2/pkg/mac/mac_darwin.go rename to v2/pkg/mac/login_darwin.go diff --git a/v2/pkg/mac/notification_darwin.go b/v2/pkg/mac/notification_darwin.go new file mode 100644 index 000000000..a06ecb53a --- /dev/null +++ b/v2/pkg/mac/notification_darwin.go @@ -0,0 +1,30 @@ +// Package mac provides MacOS related utility functions for Wails applications +package mac + +import ( + "fmt" + + "github.com/pkg/errors" + "github.com/wailsapp/wails/v2/internal/shell" +) + +// StartAtLogin will either add or remove this application to/from the login +// items, depending on the given boolean flag. The limitation is that the +// currently running app must be in an app bundle. +func ShowNotification(title string, subtitle string, message string, sound string) error { + command := fmt.Sprintf("display notification \"%s\"", message) + if len(title) > 0 { + command += fmt.Sprintf(" with title \"%s\"", title) + } + if len(subtitle) > 0 { + command += fmt.Sprintf(" subtitle \"%s\"", subtitle) + } + if len(sound) > 0 { + command += fmt.Sprintf(" sound name \"%s\"", sound) + } + _, stde, err := shell.RunCommand("/tmp", "osascript", "-e", command) + if err != nil { + errors.Wrap(err, stde) + } + return nil +} diff --git a/v2/pkg/mac/notification_darwin_test.go b/v2/pkg/mac/notification_darwin_test.go new file mode 100644 index 000000000..7d14c00e5 --- /dev/null +++ b/v2/pkg/mac/notification_darwin_test.go @@ -0,0 +1,34 @@ +package mac + +import "testing" + +func TestShowNotification(t *testing.T) { + type args struct { + title string + subtitle string + message string + sound string + } + tests := []struct { + name string + title string + subtitle string + message string + sound string + wantErr bool + }{ + {"No message", "", "", "", "", false}, + {"Title only", "I am a Title", "", "", "", false}, + {"SubTitle only", "", "I am a subtitle", "", "", false}, + {"Message only", "", "", "I am a message!", "", false}, + {"Sound only", "", "", "", "submarine.aiff", false}, + {"Full", "Title", "Subtitle", "This is a long message to show that text gets wrapped in a notification", "submarine.aiff", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ShowNotification(tt.title, tt.subtitle, tt.message, tt.sound); (err != nil) != tt.wantErr { + t.Errorf("ShowNotification() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}