From 45b11fa8f4eeb48fe214e377a1d49c0bc79765a0 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sat, 7 Sep 2024 22:35:51 -0400 Subject: [PATCH] app now only allows a single instance --- AniListUserFunctions.go | 4 ++-- MALUserFunctions.go | 4 ++-- SimklUserFunctions.go | 4 ++-- app.go | 17 ++++++++++++++++- main.go | 4 ++++ 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/AniListUserFunctions.go b/AniListUserFunctions.go index 045923c..22c94ae 100644 --- a/AniListUserFunctions.go +++ b/AniListUserFunctions.go @@ -52,7 +52,7 @@ func (a *App) AniListLogin() { refreshToken, err := aniRing.Get("anilistRefreshToken") if err != nil || len(accessToken.Data) == 0 { getAniListCodeUrl := "https://anilist.co/api/v2/oauth/authorize?client_id=" + Environment.ANILIST_APP_ID + "&redirect_uri=" + Environment.ANILIST_CALLBACK_URI + "&response_type=code" - runtime.BrowserOpenURL(a.ctx, getAniListCodeUrl) + runtime.BrowserOpenURL(*wailsContext, getAniListCodeUrl) serverDone := &sync.WaitGroup{} serverDone.Add(1) @@ -97,7 +97,7 @@ func (a *App) handleAniListCallback(wg *sync.WaitGroup) { Key: "anilistRefreshToken", Data: []byte(aniListJwt.RefreshToken), }) - _, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ + _, err := runtime.MessageDialog(*wailsContext, runtime.MessageDialogOptions{ Title: "AniList Authorization", Message: "It is now safe to close your browser tab", }) diff --git a/MALUserFunctions.go b/MALUserFunctions.go index 1efb883..16a560c 100644 --- a/MALUserFunctions.go +++ b/MALUserFunctions.go @@ -97,7 +97,7 @@ func (a *App) MyAnimeListLogin() { if err != nil || len(accessToken.Data) == 0 { verifier, _ := verifier() getMyAnimeListCodeUrl := "https://myanimelist.net/v1/oauth2/authorize?response_type=code&client_id=" + Environment.MAL_CLIENT_ID + "&redirect_uri=" + Environment.MAL_CALLBACK_URI + "&code_challenge=" + verifier.Value + "&code_challenge_method=plain" - runtime.BrowserOpenURL(a.ctx, getMyAnimeListCodeUrl) + runtime.BrowserOpenURL(*wailsContext, getMyAnimeListCodeUrl) serverDone := &sync.WaitGroup{} serverDone.Add(1) a.handleMyAnimeListCallback(serverDone, verifier) @@ -144,7 +144,7 @@ func (a *App) handleMyAnimeListCallback(wg *sync.WaitGroup, verifier *CodeVerifi Key: "MyAnimeListRefreshToken", Data: []byte(myAnimeListJwt.RefreshToken), }) - _, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ + _, err := runtime.MessageDialog(*wailsContext, runtime.MessageDialogOptions{ Title: "MyAnimeList Authorization", Message: "It is now safe to close your browser tab", }) diff --git a/SimklUserFunctions.go b/SimklUserFunctions.go index 24207dd..f435a22 100644 --- a/SimklUserFunctions.go +++ b/SimklUserFunctions.go @@ -47,7 +47,7 @@ func (a *App) SimklLogin() { scope, err := simklRing.Get("SimklScope") if err != nil || len(accessToken.Data) == 0 { getSimklCodeUrl := "https://simkl.com/oauth/authorize?response_type=code&client_id=" + Environment.SIMKL_CLIENT_ID + "&redirect_uri=" + Environment.SIMKL_CALLBACK_URI - runtime.BrowserOpenURL(a.ctx, getSimklCodeUrl) + runtime.BrowserOpenURL(*wailsContext, getSimklCodeUrl) serverDone := &sync.WaitGroup{} serverDone.Add(1) @@ -87,7 +87,7 @@ func (a *App) handleSimklCallback(wg *sync.WaitGroup) { Key: "SimklScope", Data: []byte(simklJwt.Scope), }) - _, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ + _, err := runtime.MessageDialog(*wailsContext, runtime.MessageDialogOptions{ Title: "Simkl Authorization", Message: "It is now safe to close your browser tab", }) diff --git a/app.go b/app.go index 17a3abb..4505391 100644 --- a/app.go +++ b/app.go @@ -2,8 +2,13 @@ package main import ( "context" + "github.com/wailsapp/wails/v2/pkg/options" + "github.com/wailsapp/wails/v2/pkg/runtime" + "strings" ) +var wailsContext *context.Context + // App struct type App struct { ctx context.Context @@ -17,6 +22,16 @@ func NewApp() *App { // startup is called when the app starts. The context is saved // so we can call the runtime methods func (a *App) startup(ctx context.Context) { - a.ctx = ctx + wailsContext = &ctx //runtime.WindowMaximise(ctx) } + +func (a *App) onSecondInstanceLaunch(secondInstanceData options.SecondInstanceData) { + var secondInstanceArgs = secondInstanceData.Args + + println("user opened second instance", strings.Join(secondInstanceData.Args, ",")) + println("user opened second from", secondInstanceData.WorkingDirectory) + runtime.WindowUnminimise(*wailsContext) + runtime.Show(*wailsContext) + go runtime.EventsEmit(*wailsContext, "launchArgs", secondInstanceArgs) +} diff --git a/main.go b/main.go index 83ad324..d062198 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,10 @@ func main() { }, BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1}, OnStartup: app.startup, + SingleInstanceLock: &options.SingleInstanceLock{ + UniqueId: "49c93b6d-663d-4b7a-9cb0-8a469ea9182b", + OnSecondInstanceLaunch: app.onSecondInstanceLaunch, + }, Bind: []interface{}{ app, },