refactor(app): 重构应用启动和托盘功能

- 移除 App.startup 中的系统托盘创建逻辑
- 在 main.go 中添加系统托盘创建逻辑- 更新前端 App.vue,添加实时盈亏显示和相关事件监听- 调整 stock.vue,引入通知功能
This commit is contained in:
spark 2025-01-14 11:31:15 +08:00
parent 2fcd89ab97
commit 1ea534b3c0
4 changed files with 45 additions and 19 deletions

15
app.go
View File

@ -36,14 +36,6 @@ func NewApp() *App {
func (a *App) startup(ctx context.Context) { func (a *App) startup(ctx context.Context) {
// Perform your setup here // Perform your setup here
a.ctx = ctx a.ctx = ctx
// 创建系统托盘
go systray.Run(func() {
onReady(a)
}, func() {
onExit(a)
})
} }
// domReady is called after front-end resources have been loaded // domReady is called after front-end resources have been loaded
@ -121,9 +113,10 @@ func MonitorStockPrices(a *App) {
} }
} }
title := "go-stock " + time.Now().Format(time.DateTime) + fmt.Sprintf(" %.2f¥", total) //title := "go-stock " + time.Now().Format(time.DateTime) + fmt.Sprintf(" %.2f¥", total)
runtime.WindowSetTitle(a.ctx, title) go runtime.EventsEmit(a.ctx, "realtime_profit", fmt.Sprintf(" %.2f", total))
systray.SetTooltip(title) //runtime.WindowSetTitle(a.ctx, title)
//systray.SetTooltip(title)
} }
func GetStockInfos(follows ...data.FollowedStock) *[]data.StockInfo { func GetStockInfos(follows ...data.FollowedStock) *[]data.StockInfo {

View File

@ -1,6 +1,7 @@
<script setup> <script setup>
import stockInfo from './components/stock.vue' import stockInfo from './components/stock.vue'
import { import {
EventsOn,
Quit, Quit,
WindowFullscreen, WindowGetPosition, WindowFullscreen, WindowGetPosition,
WindowHide, WindowHide,
@ -9,18 +10,19 @@ import {
} from '../wailsjs/runtime' } from '../wailsjs/runtime'
import {h, ref} from "vue"; import {h, ref} from "vue";
import { RouterLink } from 'vue-router' import { RouterLink } from 'vue-router'
import {darkTheme, NIcon, NText} from 'naive-ui' import {darkTheme, NIcon, NText,} from 'naive-ui'
import { import {
SettingsOutline, SettingsOutline,
ReorderTwoOutline, ReorderTwoOutline,
ExpandOutline, ExpandOutline,
RefreshOutline, PowerOutline, BarChartOutline, MoveOutline, RefreshOutline, PowerOutline, BarChartOutline, MoveOutline, WalletOutline,
} from '@vicons/ionicons5' } from '@vicons/ionicons5'
const content = ref('数据来源于网络,仅供参考\n投资有风险,入市需谨慎') const content = ref('数据来源于网络,仅供参考\n投资有风险,入市需谨慎')
const isFullscreen = ref(false) const isFullscreen = ref(false)
const activeKey = ref('stock') const activeKey = ref('stock')
const containerRef= ref({}) const containerRef= ref({})
const realtimeProfit= ref("")
const menuOptions = ref([ const menuOptions = ref([
{ {
label: () => label: () =>
@ -37,7 +39,15 @@ const menuOptions = ref([
{ default: () => '我的自选',} { default: () => '我的自选',}
), ),
key: 'stock', key: 'stock',
icon: renderIcon(BarChartOutline) icon: renderIcon(BarChartOutline),
children:[
{
label: ()=> h(NText, {type:realtimeProfit.value>0?'error':'success'},{ default: () => '当日盈亏 '+realtimeProfit.value+"¥"}),
key: 'realtimeProfit',
show: realtimeProfit.value,
icon: renderIcon(WalletOutline),
},
]
}, },
{ {
label: () => label: () =>
@ -122,11 +132,18 @@ function dragstart(e) {
} }
} }
window.addEventListener('mousemove', dragstart) window.addEventListener('mousemove', dragstart)
EventsOn("realtime_profit",(data)=>{
realtimeProfit.value=data
})
</script> </script>
<template> <template>
<n-config-provider :theme="darkTheme" ref="containerRef"> <n-config-provider :theme="darkTheme" ref="containerRef">
<n-message-provider >
<n-notification-provider>
<n-modal-provider>
<n-watermark <n-watermark
:content="content" :content="content"
cross cross
@ -141,8 +158,7 @@ window.addEventListener('mousemove', dragstart)
style="height: 100%" style="height: 100%"
> >
<n-flex justify="center"> <n-flex justify="center">
<n-message-provider >
<n-modal-provider>
<n-grid x-gap="12" :cols="1"> <n-grid x-gap="12" :cols="1">
<n-gi style="padding-bottom: 70px"> <n-gi style="padding-bottom: 70px">
<RouterView /> <RouterView />
@ -158,10 +174,12 @@ window.addEventListener('mousemove', dragstart)
</n-card> </n-card>
</n-gi> </n-gi>
</n-grid> </n-grid>
</n-modal-provider>
</n-message-provider>
</n-flex> </n-flex>
</n-watermark> </n-watermark>
</n-modal-provider>
</n-notification-provider>
</n-message-provider>
</n-config-provider> </n-config-provider>
</template> </template>
<style> <style>

View File

@ -10,12 +10,13 @@ import {
SetCostPriceAndVolume, SetStockSort, SetCostPriceAndVolume, SetStockSort,
UnFollow UnFollow
} from '../../wailsjs/go/main/App' } from '../../wailsjs/go/main/App'
import {NButton, NFlex, NForm, NFormItem, NInputNumber, NText, useMessage, useModal} from 'naive-ui' import {NButton, NFlex, NForm, NFormItem, NInputNumber, NText, useMessage, useModal,useNotification} from 'naive-ui'
import {EventsOn, WindowFullscreen, WindowReload, WindowUnfullscreen} from '../../wailsjs/runtime' import {EventsOn, WindowFullscreen, WindowReload, WindowUnfullscreen} from '../../wailsjs/runtime'
import {Add, Search,StarOutline} from '@vicons/ionicons5' import {Add, Search,StarOutline} from '@vicons/ionicons5'
const message = useMessage() const message = useMessage()
const modal = useModal() const modal = useModal()
const notify = useNotification()
const stocks=ref([]) const stocks=ref([])
const results=ref({}) const results=ref({})
@ -124,6 +125,8 @@ EventsOn("refreshFollowList",(data)=>{
// }) // })
}) })
//A //A
function isTradingTime() { function isTradingTime() {
const now = new Date(); const now = new Date();

12
main.go
View File

@ -4,6 +4,7 @@ import (
"embed" "embed"
"encoding/json" "encoding/json"
"github.com/duke-git/lancet/v2/convertor" "github.com/duke-git/lancet/v2/convertor"
"github.com/getlantern/systray"
"github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/logger" "github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/menu" "github.com/wailsapp/wails/v2/pkg/menu"
@ -73,12 +74,23 @@ func main() {
FileMenu.AddText("隐藏到托盘区", keys.CmdOrCtrl("h"), func(_ *menu.CallbackData) { FileMenu.AddText("隐藏到托盘区", keys.CmdOrCtrl("h"), func(_ *menu.CallbackData) {
runtime.WindowHide(app.ctx) runtime.WindowHide(app.ctx)
}) })
FileMenu.AddText("显示", keys.CmdOrCtrl("v"), func(_ *menu.CallbackData) {
runtime.WindowShow(app.ctx)
})
} }
//FileMenu.AddText("退出", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) { //FileMenu.AddText("退出", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) {
// runtime.Quit(app.ctx) // runtime.Quit(app.ctx)
//}) //})
// 创建系统托盘
go systray.Run(func() {
onReady(app)
}, func() {
onExit(app)
})
// Create application with options // Create application with options
err := wails.Run(&options.App{ err := wails.Run(&options.App{
Title: "go-stock", Title: "go-stock",