Files
2026-05-13 20:40:14 +08:00

157 lines
3.5 KiB
Go

package main
import (
"context"
"sgg-ai-skill-manager/internal/domain"
"sgg-ai-skill-manager/internal/service"
"sgg-ai-skill-manager/internal/tray"
wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
type App struct {
ctx context.Context
service *service.Service
initErr error
trayStop func()
}
func NewApp() *App {
svc, err := service.NewDefault()
return &App{service: svc, initErr: err}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
if a.service != nil {
a.service.StartAutoUpdate(ctx)
}
a.startTray(ctx)
}
func (a *App) shutdown(context.Context) {
if a.trayStop != nil {
a.trayStop()
a.trayStop = nil
}
}
func (a *App) startTray(ctx context.Context) {
stop, err := tray.Start(ctx, tray.Config{
Tooltip: appTitle,
IconPath: defaultTrayIconPath,
OnOpen: func() {
wailsruntime.WindowShow(ctx)
wailsruntime.WindowUnminimise(ctx)
},
})
if err != nil {
wailsruntime.LogErrorf(ctx, "failed to start tray icon: %v", err)
return
}
a.trayStop = stop
}
func (a *App) LoadConfig() (domain.Config, error) {
if err := a.ready(); err != nil {
return domain.Config{}, err
}
return a.service.LoadConfig()
}
func (a *App) SaveConfig(req domain.SaveConfigRequest) error {
if err := a.ready(); err != nil {
return err
}
return a.service.SaveConfig(a.context(), req)
}
func (a *App) TestConnection(req domain.SaveConfigRequest) (domain.TestConnectionResult, error) {
if err := a.ready(); err != nil {
return domain.TestConnectionResult{}, err
}
return a.service.TestConnection(a.context(), req)
}
func (a *App) ListRemoteSkills() ([]domain.RemoteSkill, error) {
if err := a.ready(); err != nil {
return nil, err
}
return a.service.ListRemoteSkills(a.context())
}
func (a *App) ListLocalSkills() ([]domain.SkillState, error) {
if err := a.ready(); err != nil {
return nil, err
}
return a.service.ListLocalSkills(a.context())
}
func (a *App) DownloadSkill(repo domain.RemoteSkill) (domain.SkillState, error) {
if err := a.ready(); err != nil {
return domain.SkillState{}, err
}
return a.service.DownloadSkill(a.context(), repo)
}
func (a *App) UpdateSkill(org, repo string) (domain.SkillState, error) {
if err := a.ready(); err != nil {
return domain.SkillState{}, err
}
return a.service.UpdateSkill(a.context(), org, repo)
}
func (a *App) InstallSkill(org, repo, targetID string) (domain.SkillState, error) {
if err := a.ready(); err != nil {
return domain.SkillState{}, err
}
return a.service.InstallSkill(a.context(), org, repo, targetID)
}
func (a *App) UninstallSkill(org, repo, targetID string) (domain.SkillState, error) {
if err := a.ready(); err != nil {
return domain.SkillState{}, err
}
return a.service.UninstallSkill(a.context(), org, repo, targetID)
}
func (a *App) DeleteSkill(org, repo string) error {
if err := a.ready(); err != nil {
return err
}
return a.service.DeleteSkill(a.context(), org, repo)
}
func (a *App) OpenInVSCode(org, repo string) error {
if err := a.ready(); err != nil {
return err
}
return a.service.OpenInVSCode(a.context(), org, repo)
}
func (a *App) OpenFolder(org, repo string) error {
if err := a.ready(); err != nil {
return err
}
return a.service.OpenFolder(a.context(), org, repo)
}
func (a *App) RunAutoUpdate() error {
if err := a.ready(); err != nil {
return err
}
return a.service.RunAutoUpdate(a.context())
}
func (a *App) ready() error {
return a.initErr
}
func (a *App) context() context.Context {
if a.ctx == nil {
return context.Background()
}
return a.ctx
}