site/ is the website: a static page plus a worker that turns the newest release of yetone/magpie-releases into an update feed (/api/latest, with every file's SHA-256), stable download links and an install script. The app asks the feed at start-up and every six hours, downloads a newer version in the background, and installs it on Restart to Update or on quit. A download must match its checksum and be signed by the same team as the app it replaces. `magpie update` does the same from a terminal and replaces a bare binary in place.
198 lines
5.7 KiB
Go
198 lines
5.7 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
"log"
|
|
"os"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/wailsapp/wails/v3/pkg/application"
|
|
"github.com/wailsapp/wails/v3/pkg/events"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
//go:embed tray.png
|
|
var trayIcon []byte // black glyph, tinted by the macOS menu bar
|
|
|
|
//go:embed icon.png
|
|
var appIcon []byte // coloured, for other trays and the about box
|
|
|
|
type host struct {
|
|
app *application.App
|
|
panel *application.WebviewWindow
|
|
main *application.WebviewWindow
|
|
tray *application.SystemTray
|
|
|
|
panelHeight int
|
|
query string // what the windows' URLs carry (a forced theme)
|
|
}
|
|
|
|
func (h *host) HidePanel() { h.panel.Hide() }
|
|
func (h *host) ShowMain(view string) {
|
|
h.panel.Hide()
|
|
if view != "" {
|
|
h.main.SetURL("/?view=" + view + h.query)
|
|
}
|
|
h.main.Show()
|
|
h.main.Focus()
|
|
}
|
|
func (h *host) Quit() { h.app.Quit() }
|
|
func (h *host) OpenURL(url string) { _ = h.app.Browser.OpenURL(url) }
|
|
|
|
const panelWidth, panelMin, panelMax = 440, 220, 720
|
|
|
|
// FitPanel grows or shrinks the panel to its content and keeps it anchored
|
|
// under the tray icon.
|
|
func (h *host) FitPanel(height int) {
|
|
height = max(panelMin, min(panelMax, height))
|
|
if h.panelHeight == height {
|
|
return
|
|
}
|
|
h.panelHeight = height
|
|
h.panel.SetSize(panelWidth, height)
|
|
if h.panel.IsVisible() {
|
|
_ = h.tray.PositionWindow(h.panel, 6)
|
|
}
|
|
}
|
|
|
|
// Run starts the desktop app: a menu bar icon whose click drops down a compact
|
|
// panel, plus a regular window for when you want it to stay around.
|
|
// showMain opens the window immediately; otherwise only the tray icon appears.
|
|
func Run(version string, showMain bool) error {
|
|
// The gateway runs inside the app. If another magpie already has the
|
|
// port, that one serves and this one only shows its status.
|
|
var gw *gateway.Server
|
|
if !gateway.Running() {
|
|
gw = gateway.New()
|
|
go func() {
|
|
if err := gw.ListenAndServe(context.Background()); err != nil {
|
|
log.Println("gateway:", err)
|
|
}
|
|
}()
|
|
}
|
|
// Model lists are fetched, never compiled in: whatever the agents can see
|
|
// comes from the models.dev catalog plus each vendor's own /models answer.
|
|
// Keep both halves warm without making the user click anything.
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
if catalog.Stale() {
|
|
if err := catalog.Sync(ctx); err != nil {
|
|
log.Println("catalog:", err)
|
|
}
|
|
}
|
|
cancel()
|
|
// A signed-in agent's list exists only at the vendor; fill it in the
|
|
// first time so the picker never shows a stale snapshot.
|
|
for _, p := range provider.All() {
|
|
if p.Account == nil || !p.Ready() {
|
|
continue
|
|
}
|
|
if _, ok := p.Fetched(); ok {
|
|
continue
|
|
}
|
|
c, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
if _, err := p.Fetch(c); err != nil {
|
|
log.Println(p.ID + ": " + err.Error())
|
|
}
|
|
cancel()
|
|
}
|
|
}()
|
|
// MAGPIE_THEME=light|dark forces the palette; handy for screenshots.
|
|
theme := ""
|
|
if t := os.Getenv("MAGPIE_THEME"); t != "" {
|
|
theme = "&theme=" + t
|
|
}
|
|
Version = version
|
|
h := &host{query: theme}
|
|
h.app = application.New(application.Options{
|
|
Name: "magpie",
|
|
Description: "one place to pick every coding agent's model",
|
|
Icon: appIcon,
|
|
Assets: application.AssetOptions{Handler: Handler(h, gw)},
|
|
Mac: application.MacOptions{ActivationPolicy: application.ActivationPolicyAccessory},
|
|
Windows: application.WindowsOptions{DisableQuitOnLastWindowClosed: true},
|
|
// A version downloaded but not restarted into is installed on the
|
|
// way out, so the next launch is the new one.
|
|
OnShutdown: func() { updates.install() },
|
|
})
|
|
|
|
h.panel = h.app.Window.NewWithOptions(application.WebviewWindowOptions{
|
|
Name: "panel",
|
|
Title: "magpie",
|
|
URL: "/?mode=panel" + theme,
|
|
Width: panelWidth,
|
|
Height: 520,
|
|
Hidden: true,
|
|
Frameless: true,
|
|
AlwaysOnTop: true,
|
|
DisableResize: true,
|
|
HideOnEscape: true,
|
|
HideOnFocusLost: true,
|
|
BackgroundType: application.BackgroundTypeTranslucent,
|
|
Mac: application.MacWindow{
|
|
Backdrop: application.MacBackdropTranslucent,
|
|
CornerRadius: 12,
|
|
},
|
|
Windows: application.WindowsWindow{HiddenOnTaskbar: true},
|
|
})
|
|
|
|
h.main = h.app.Window.NewWithOptions(application.WebviewWindowOptions{
|
|
Name: "main",
|
|
Title: "magpie",
|
|
URL: "/?" + theme,
|
|
Width: 660,
|
|
Height: 600,
|
|
MinWidth: 560,
|
|
MinHeight: 420,
|
|
Hidden: true,
|
|
Mac: application.MacWindow{
|
|
TitleBar: application.MacTitleBarHiddenInset,
|
|
InvisibleTitleBarHeight: 50,
|
|
},
|
|
})
|
|
// Closing the window keeps the tray alive; quitting is a menu action.
|
|
h.main.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
|
|
h.main.Hide()
|
|
e.Cancel()
|
|
})
|
|
|
|
menu := h.app.NewMenu()
|
|
menu.Add("Open magpie").OnClick(func(*application.Context) { h.ShowMain("") })
|
|
menu.AddSeparator()
|
|
menu.Add("Version " + version).SetEnabled(false)
|
|
restart := menu.Add("Restart to Update").SetHidden(true)
|
|
restart.OnClick(func(*application.Context) {
|
|
if restartToUpdate() {
|
|
h.app.Quit()
|
|
}
|
|
})
|
|
menu.Add("Quit magpie").OnClick(func(*application.Context) { h.app.Quit() })
|
|
updates.onReady = func(v string) {
|
|
application.InvokeSync(func() {
|
|
restart.SetLabel("Restart to Update to " + v).SetHidden(false)
|
|
menu.Update()
|
|
})
|
|
}
|
|
updates.start()
|
|
|
|
h.tray = h.app.SystemTray.New()
|
|
h.tray.SetTooltip("magpie")
|
|
if runtime.GOOS == "darwin" {
|
|
h.tray.SetTemplateIcon(trayIcon)
|
|
} else {
|
|
h.tray.SetIcon(appIcon)
|
|
}
|
|
h.tray.SetMenu(menu)
|
|
h.tray.AttachWindow(h.panel).WindowOffset(6)
|
|
|
|
if showMain {
|
|
h.app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) { h.ShowMain("") })
|
|
}
|
|
return h.app.Run()
|
|
}
|