Added single instance

This commit is contained in:
Apologieze
2025-03-17 19:49:39 -04:00
parent fc25f206da
commit 19f476b932
4 changed files with 74 additions and 4 deletions

1
go.mod
View File

@@ -13,6 +13,7 @@ require (
github.com/dweymouth/fyne-tooltip v0.2.1
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2
github.com/nightlyone/lockfile v1.0.0
)
require (

2
go.sum
View File

@@ -275,6 +275,8 @@ github.com/nicksnyder/go-i18n/v2 v2.4.0 h1:3IcvPOAvnCKwNm0TB0dLDTuawWEj+ax/RERNC
github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7gMc814+6wVyEI4=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nightlyone/lockfile v1.0.0 h1:RHep2cFKK4PonZJDdEl4GmkabuhbsRMgk/k3uAmxBiA=
github.com/nightlyone/lockfile v1.0.0/go.mod h1:rywoIealpdNse2r832aiD9jRk8ErCatROs6LzC841CI=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=

View File

@@ -29,7 +29,7 @@ import (
const (
AppID = "fr.apologize.benri"
AppName = "AnimeGUI"
AppName = "Benri"
)
var (
@@ -48,11 +48,19 @@ var (
)
func main() {
appW = app.NewWithID(AppID)
config.CreateConfig(appW.Preferences())
if config.Setting.TrayIcon {
// Initialize lockfile
lock := initLock()
defer lock.Unlock()
}
go pollingNewAppDetection()
go dowloadMPV()
appW = app.NewWithID(AppID)
window = appW.NewWindow(AppName)
window.Resize(fyne.NewSize(1000, 700))
window.CenterOnScreen()
@@ -69,8 +77,10 @@ func main() {
if !changedToken {
fmt.Println(window.Title(), AppName)
initMainApp()
appW.Run()
}
}
func updateAnimeNames(data *binding.ExternalStringList) (first bool) {
@@ -336,7 +346,6 @@ func initMainApp() {
imageContainer.Refresh()
}
config.CreateConfig(appW.Preferences())
initSettingDialog()
initPlayMorePopUp()

58
src/singleInstance.go Normal file
View File

@@ -0,0 +1,58 @@
package main
import (
"fmt"
"github.com/charmbracelet/log"
"github.com/nightlyone/lockfile"
"os"
"time"
)
var (
lockPath = os.TempDir() + "/" + AppName + ".lock"
notifyPath = os.TempDir() + "/" + AppName + ".notify"
)
func initLock() lockfile.Lockfile {
lock, err := lockfile.New(lockPath)
if err != nil {
fmt.Printf("Cannot init lockfile: %v\n", err)
os.Exit(1)
}
// Try to acquire the lock
err = lock.TryLock()
if err != nil {
fmt.Println("Application is already running")
// Touch the notification file to signal the first instance
currentTime := []byte(fmt.Sprintf("%d", time.Now().UnixNano()))
_ = os.WriteFile(notifyPath, currentTime, 0644)
os.Exit(0)
}
return lock
}
func pollingNewAppDetection() {
var lastModTime time.Time
info, err := os.Stat(notifyPath)
if err == nil {
lastModTime = info.ModTime()
}
for {
// Check if notification file exists
info, err := os.Stat(notifyPath)
if err == nil && info.ModTime().After(lastModTime) {
log.Info("New instance detected, bringing to front")
window.Show()
window.RequestFocus()
lastModTime = info.ModTime()
}
time.Sleep(500 * time.Millisecond)
}
}