From 63a05954f8c47ac400b636e2f8ebab4883309d2c Mon Sep 17 00:00:00 2001 From: ArvinLovegood Date: Fri, 21 Mar 2025 17:53:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E9=94=81=E5=92=8C=E5=BA=94=E7=94=A8=E6=8B=96=E5=8A=A8?= =?UTF-8?q?/=E9=80=80=E5=87=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 main.go 中添加了 SingleInstanceLock 配置,确保只有一个应用实例运行- 在 App 结构中添加了 OnSecondInstanceLaunch 函数,用于处理第二次启动时的通知 - 优化了应用退出流程,确保 systray 正确退出 - 调整了窗口默认大小和最小宽度 --- app.go | 45 ++++++++++++++++++++++++++------------- frontend/index.html | 2 +- frontend/src/App.vue | 50 ++++++++++++++++++++++---------------------- main.go | 9 +++++--- 4 files changed, 63 insertions(+), 43 deletions(-) diff --git a/app.go b/app.go index ac8d785..87655a1 100644 --- a/app.go +++ b/app.go @@ -14,6 +14,8 @@ import ( "github.com/duke-git/lancet/v2/strutil" "github.com/getlantern/systray" "github.com/go-resty/resty/v2" + "github.com/go-toast/toast" + "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/runtime" "go-stock/backend/data" "go-stock/backend/db" @@ -118,18 +120,18 @@ func (a *App) domReady(ctx context.Context) { } }() - go func() { - ticker := time.NewTicker(time.Second * time.Duration(60)) - defer ticker.Stop() - for range ticker.C { - telegraph := refreshTelegraphList() - if telegraph != nil && config.EnableNews { - go runtime.EventsEmit(a.ctx, "telegraph", telegraph) - } - } - - }() if config.EnableNews { + go func() { + ticker := time.NewTicker(time.Second * time.Duration(60)) + defer ticker.Stop() + for range ticker.C { + telegraph := refreshTelegraphList() + if telegraph != nil { + go runtime.EventsEmit(a.ctx, "telegraph", telegraph) + } + } + + }() go runtime.EventsEmit(a.ctx, "telegraph", refreshTelegraphList()) } go MonitorStockPrices(a) @@ -479,6 +481,7 @@ func (a *App) shutdown(ctx context.Context) { defer PanicHandler() // Perform your teardown here systray.Quit() + os.Exit(0) } // Greet returns a greeting for the given name @@ -673,6 +676,7 @@ func getMsgTypeName(msgType int) string { func onExit(a *App) { // 清理操作 logger.SugaredLogger.Infof("onExit") + systray.Quit() runtime.Quit(a.ctx) } @@ -696,15 +700,12 @@ func onReady(a *App) { case <-mQuitOrig.ClickedCh: logger.SugaredLogger.Infof("退出应用程序") runtime.Quit(a.ctx) - //systray.Quit() case <-show.ClickedCh: logger.SugaredLogger.Infof("显示应用程序") runtime.WindowShow(a.ctx) - //runtime.WindowShow(a.ctx) case <-hide.ClickedCh: logger.SugaredLogger.Infof("隐藏应用程序") runtime.WindowHide(a.ctx) - } } }() @@ -780,3 +781,19 @@ func (a *App) FollowFund(fundCode string) string { func (a *App) UnFollowFund(fundCode string) string { return data.NewFundApi().UnFollowFund(fundCode) } + +func OnSecondInstanceLaunch(secondInstanceData options.SecondInstanceData) { + notification := toast.Notification{ + AppID: "go-stock", + Title: "go-stock", + Message: "程序已经在运行了", + Icon: "", + Duration: "short", + Audio: toast.Default, + } + err := notification.Push() + if err != nil { + logger.SugaredLogger.Error(err) + } + time.Sleep(time.Second * 3) +} diff --git a/frontend/index.html b/frontend/index.html index f27a275..db29dec 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -6,7 +6,7 @@ go-stock:AI赋能股票分析 - +
diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 1e241d7..ea8bffa 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -129,15 +129,15 @@ const menuOptions = ref([ key: 'hide', icon: renderIcon(ReorderTwoOutline), }, - { - label: ()=> h("a", { - href: 'javascript:void(0)', - style: 'cursor: move;', - onClick: toggleStartMoveWindow, - }, { default: () => '移动' }), - key: 'move', - icon: renderIcon(MoveOutline), - }, + // { + // label: ()=> h("a", { + // href: 'javascript:void(0)', + // style: 'cursor: move;', + // onClick: toggleStartMoveWindow, + // }, { default: () => '移动' }), + // key: 'move', + // icon: renderIcon(MoveOutline), + // }, { label: ()=> h("a", { href: '#', @@ -161,22 +161,22 @@ function toggleFullscreen(e) { } isFullscreen.value=!isFullscreen.value } -const drag = ref(false) -const lastPos= ref({x:0,y:0}) -function toggleStartMoveWindow(e) { - drag.value=!drag.value - lastPos.value={x:e.clientX,y:e.clientY} -} -function dragstart(e) { - if (drag.value) { - let x=e.clientX-lastPos.value.x - let y=e.clientY-lastPos.value.y - WindowGetPosition().then((pos) => { - WindowSetPosition(pos.x+x,pos.y+y) - }) - } -} -window.addEventListener('mousemove', dragstart) +// const drag = ref(false) +// const lastPos= ref({x:0,y:0}) +// function toggleStartMoveWindow(e) { +// drag.value=!drag.value +// lastPos.value={x:e.clientX,y:e.clientY} +// } +// function dragstart(e) { +// if (drag.value) { +// let x=e.clientX-lastPos.value.x +// let y=e.clientY-lastPos.value.y +// WindowGetPosition().then((pos) => { +// WindowSetPosition(pos.x+x,pos.y+y) +// }) +// } +// } +// window.addEventListener('mousemove', dragstart) EventsOn("realtime_profit",(data)=>{ realtimeProfit.value=data diff --git a/main.go b/main.go index ff8eb86..bd3f951 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,6 @@ import ( "go-stock/backend/db" log "go-stock/backend/logger" "go-stock/backend/models" - "os" goruntime "runtime" "runtime/debug" @@ -126,7 +125,7 @@ func main() { width, height, err = getScreenResolution() if err != nil { log.SugaredLogger.Error("get screen resolution error") - width = 1366 + width = 1456 height = 768 } @@ -135,7 +134,7 @@ func main() { Title: "go-stock", Width: width * 4 / 5, Height: height * 4 / 5, - MinWidth: 1024, + MinWidth: 1456, MinHeight: 768, MaxWidth: width, MaxHeight: height, @@ -156,6 +155,10 @@ func main() { OnBeforeClose: app.beforeClose, OnShutdown: app.shutdown, WindowStartState: options.Normal, + SingleInstanceLock: &options.SingleInstanceLock{ + UniqueId: "go-stock", + OnSecondInstanceLaunch: OnSecondInstanceLaunch, + }, Bind: []interface{}{ app, },