Refeccd on Discord: "每次更新后,第一次在菜单栏点击唤起主界面会出现闪退现象
(实际程序未退出,只是隐藏了)" (macOS, automatic updates).
Cause: 182803ea7 made the panel lazily, on its first open, and
trayOwnClicks (mpOwnClicks, the local monitor that sends the item's
action on mouse-down and drops the event) was installed in makePanel.
So the first click after every start went through the button's own
mouse-down tracking: the panel came up, the tracking loop took focus
back, and HideOnFocusLost hid it at once. Later clicks were fine,
because by then the monitor was in. Every update restarts magpie, so
it showed after each one.
Now trayOwnClicks runs right after the tray item is made, in app.go,
before any click can arrive; makePanel no longer installs it (the
monitor is dispatch_once, so nothing is installed twice).
Run (mica):
- go test -count=1 ./internal/gui/: ok (153.7s).
- Quick tests, go vet (darwin, GOOS=windows, GOOS=linux -tags nogui)
on mote: ok; gofmt -l clean.
- TestTrayOwnsClicksBeforeThePanel is red with the call moved back
into makePanel (broken by hand), green with the fix.
Not tried: a real tray click after a restart (no click can be driven
over ssh); the reporter's case is reasoned from the code path above.
79 lines
3.1 KiB
Go
79 lines
3.1 KiB
Go
package gui
|
|
|
|
import (
|
|
"os"
|
|
"regexp"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A double-click on the tray icon opens the main window on Windows, the one
|
|
// tray that reports it (isaac_bai on Discord); the Mac and Linux keep one
|
|
// click for the panel, since a double-click there is two clicks.
|
|
func TestTrayDoubleClickOpensTheWindowOnWindowsOnly(t *testing.T) {
|
|
for goos, want := range map[string]bool{"windows": true, "darwin": false, "linux": false} {
|
|
if got := trayDoubleClicks(goos); got != want {
|
|
t.Errorf("%s: double-click opens the window = %v, want %v", goos, got, want)
|
|
}
|
|
}
|
|
src, err := os.ReadFile("app.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// the handler is set where it applies, opens the window, and the click
|
|
// handler asks first whether it is a double-click's release
|
|
if !regexp.MustCompile(`(?s)if trayDoubleClicks\(runtime\.GOOS\) \{\s*h\.tray\.OnDoubleClick\(func\(\) \{\s*double\.doubled\(time\.Now\(\)\)\s*h\.ShowMain\(""\)`).Match(src) {
|
|
t.Error("app.go doesn't open the window on a double-click where trayDoubleClicks says")
|
|
}
|
|
if !regexp.MustCompile(`(?s)h\.tray\.OnClick\(func\(\) \{\s*if double\.swallow\(time\.Now\(\)\) \{\s*return`).Match(src) {
|
|
t.Error("the tray's click handler doesn't let a double-click's release go first")
|
|
}
|
|
}
|
|
|
|
// Windows sends a double-click as up, double-click, up: the last up is the
|
|
// double-click's and opens nothing, once; a click after it, or one long
|
|
// after, is a click again.
|
|
func TestTrayDoubleClickReleaseSwallowedOnce(t *testing.T) {
|
|
t0 := time.Date(2026, 11, 3, 20, 28, 0, 0, time.UTC)
|
|
var d trayDouble
|
|
if d.swallow(t0) {
|
|
t.Fatal("a click with no double-click before it was swallowed")
|
|
}
|
|
d.doubled(t0)
|
|
if !d.swallow(t0.Add(120 * time.Millisecond)) {
|
|
t.Fatal("the double-click's own release opened the panel again")
|
|
}
|
|
if d.swallow(t0.Add(time.Second)) {
|
|
t.Fatal("the click after a double-click's release was swallowed too")
|
|
}
|
|
// a release that never came: the user's next click, long after, opens
|
|
d.doubled(t0)
|
|
if d.swallow(t0.Add(trayDoubleRelease + time.Second)) {
|
|
t.Fatal("a click long after a double-click was swallowed")
|
|
}
|
|
if d.swallow(t0.Add(trayDoubleRelease + 2*time.Second)) {
|
|
t.Fatal("a late click left the double-click waiting")
|
|
}
|
|
}
|
|
|
|
// The Mac's menu bar item takes its clicks itself (trayOwnClicks) from the
|
|
// moment it is made, not once the panel is: the panel is made on its first
|
|
// open there, and that first click, going through the button's own
|
|
// tracking, showed the panel and hid it again (Refeccd on Discord).
|
|
func TestTrayOwnsClicksBeforeThePanel(t *testing.T) {
|
|
app, err := os.ReadFile("app.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !regexp.MustCompile(`(?s)h\.tray = h\.app\.SystemTray\.New\(\)\n(\s*//[^\n]*\n)*\s*trayOwnClicks\(\)`).Match(app) {
|
|
t.Error("app.go doesn't install trayOwnClicks as soon as the tray is made")
|
|
}
|
|
lw, err := os.ReadFile("lightweight.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if regexp.MustCompile(`(?s)func \(h \*host\) makePanel\(\).*?trayOwnClicks\(\).*?\n}\n`).Match(lw) {
|
|
t.Error("makePanel still installs trayOwnClicks, which then waits for the first open")
|
|
}
|
|
}
|