Files
magpie/internal/shortcut/shortcut.go
T
yetone fff1607a30 portable: a portable magpie on Windows keeps WebView2's profile in data\webview2 and makes no Start-menu shortcut, and an installed one makes the shortcut once and never puts back one the user deleted (#508, suixinex: 便携模式时还是会创建 C:\Users\<username>\AppData\Roaming\magpie.exe\ 文件夹; 开始菜单的快捷方式删除后再打开magpie会重新添加) — the AppData\Roaming\magpie.exe folder is WebView2's user data folder: Wails v3 passes WindowsOptions.WebviewUserDataPath to go-webview2 as its DataPath, and left empty it is %APPDATA%\<exe name>, which portable mode (appdir) never set. The app's Windows options (windowsOptions) now give it appdir.WebView(): data\webview2 when portable, "" (WebView2's default, so the pages' storage and cookies there are kept) when installed. The shortcut came back because shortcut.Ensure ran at every start of the desktop app and wrote Magpie.lnk whenever it was missing or stale, with nothing recording that it had been made. Ensure now does nothing when portable (no shortcut, no App Paths entry); installed, it makes the shortcut on the first start and records that in start-menu-shortcut in magpie's folder (with the exe), keeps one that is still there pointed at the exe (moved exe, update), and makes none once the file is recorded and the shortcut is gone. A user who deleted it under an older magpie isn't given it back on updating either: a missing shortcut with no record but an App Paths\magpie.exe entry already there (every older magpie wrote it at each start) counts as deleted and is recorded as such. The Start menu, the shortcut writer and App Paths are behind function variables, so the decision is tested with fakes on any OS. Checked for other leaks: autostart only refreshes an entry the user turned on (Refresh never enables), the magpie:// link registration is needed for import links and stays, as appdir's doc says, as do temp folders; the update stages beside the exe, caches already follow appdir. Not changed: the Mac's WKWebView and Linux's WebKitGTK keep their own data in the profile (Wails has no option for it), which #508 didn't report. Nothing ran on Windows (the test box isn't reachable): no real WebView2 was started with the data path and no real shortcut was made or deleted. TestEnsureOnce, TestEnsureAfterOlderMagpie, TestEnsurePortable (fake Start menu in a temp folder), TestWebView and TestWindowsOptionsWebViewData fail without the change. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build and vet ./..., GOOS=windows go test -c for shortcut, gui and appdir, go test -tags nogui -count=1 ./... under a temp HOME pass (TestUsageKeepsTimeToFirstToken and TestUserPath failed once on timing, pass rerun alone; untouched).
2026-10-02 14:39:49 +08:00

130 lines
4.3 KiB
Go

// Package shortcut puts magpie in the Start menu on Windows. There it is
// one exe with no installer, so nothing else would: the first start of the
// desktop app makes a Magpie shortcut in the user's Start menu opening the
// exe it runs as, and Windows search finds it by that. It is made once:
// magpie remembers it did, and a shortcut the user deleted stays deleted
// (#508); one still there is pointed at the exe again when that was moved.
// A portable magpie (a data folder beside it) makes none and registers
// nothing, leaving no trace on the machine. Elsewhere it does nothing —
// the Mac has the app in Applications, Linux its own ways.
package shortcut
import (
"errors"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/yetone/magpie/internal/appdir"
)
// Name is what the shortcut is called, and what Windows search finds.
const Name = "Magpie"
// StateName is the file in magpie's folder recording that the shortcut was
// made, and for which exe.
const StateName = "start-menu-shortcut"
// what Ensure asks of the system; the tests put fakes in
var (
// startMenu is where the shortcut goes (…\Start Menu\Programs\Magpie.lnk)
startMenu = startMenuLink
// writeLink has the shortcut at lnk open exe, and reports whether it had to
writeLink = writeShortcut
// register has Win+R and `start magpie` find exe
register = registerAppPath
// registered reports whether a magpie registered itself there before
registered = registeredAppPath
)
// Ensure makes the shortcut on the first start, keeps it pointed at this
// magpie after that while it is there, and never makes it again once the
// user deleted it. A portable magpie makes none. It fails quietly — magpie
// runs the same without one — and is skipped with MAGPIE_NO_SHORTCUT=1
// (tests, sandboxes).
func Ensure() {
if os.Getenv("MAGPIE_NO_SHORTCUT") == "1" {
return
}
exe, err := os.Executable()
if err != nil {
return
}
if p, err := filepath.EvalSymlinks(exe); err == nil {
exe = p
}
if err := ensure(Target(exe), appdir.Portable(), filepath.Join(appdir.Config(), StateName)); err != nil {
log.Println("start menu shortcut:", err)
}
}
// errNoStartMenu is startMenu's answer where there is none (off Windows).
var errNoStartMenu = errors.New("no Start menu here")
// ensure is Ensure for exe, portable being the data folder of a portable
// magpie ("" installed) and state the file remembering the shortcut was
// made.
func ensure(exe, portable, state string) error {
if portable != "" {
return nil
}
lnk, err := startMenu()
if errors.Is(err, errNoStartMenu) {
return nil
}
// a magpie from before the state file registered itself at every start
before := registered()
register(exe)
if err != nil {
return err
}
_, gone := os.Stat(lnk)
if gone != nil {
if _, err := os.Stat(state); err == nil {
// made once already: the user deleted it, and it stays deleted
return nil
}
if before {
// an older magpie made it at its every start, and it is gone:
// the user deleted it, so it isn't made again now either
return remember(state, exe)
}
}
if _, err := writeLink(lnk, exe); err != nil {
return err
}
return remember(state, exe)
}
// remember records in state that the shortcut was made, for exe.
func remember(state, exe string) error {
if err := os.MkdirAll(filepath.Dir(state), 0o755); err != nil {
return err
}
return os.WriteFile(state, []byte(exe+"\n"), 0o644)
}
// aside is what an update moves the exe it replaces to, or stages the new
// one as: magpie.exe.old, magpie.exe.old-2…, magpie.exe.new
var aside = regexp.MustCompile(`(?i)(\.exe)\.(old(-\d+)?|new)$`)
// Target is what the shortcut opens for exe: exe itself, but for a magpie
// left running from where an update moved it aside, the exe in its place,
// which is what the next start runs. An update replaces the exe where it
// is, so the shortcut stays good through it.
func Target(exe string) string {
return aside.ReplaceAllString(exe, "$1")
}
// Stale reports whether a shortcut opening target in dir is not one for
// exe: there is none yet, or the exe was moved. Windows paths are the same
// in any case.
func Stale(target, dir, exe string) bool {
same := func(a, b string) bool {
return a != "" && strings.EqualFold(filepath.Clean(a), filepath.Clean(b))
}
return !same(target, exe) || !same(dir, filepath.Dir(exe))
}