Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

Open the main window at launch even when only Settings was restored (issue #58)

Gauthier Piarrettef649bb47f631
diff --git a/Scripts/launch-window-smoke.sh b/Scripts/launch-window-smoke.sh
new file mode 100755
index 0000000..f871a1e
--- /dev/null
+++ b/Scripts/launch-window-smoke.sh
@@ -0,0 +1,33 @@
+#!/bin/zsh
+# Launch-window smoke (issue #58): the app must open its main window at launch even when the
+# previous session ended with only the Settings window open. 0.8.0 added a Settings scene and
+# macOS window restoration brought it back alone at relaunch; the library was reachable only via
+# Cmd-N. This reproduces the report's shape on an empty HIGHBALL_HOME: launch, open Settings
+# (Cmd-,), close the main window, quit, relaunch, and assert a main window is visible.
+# Needs the screen (Accessibility permission for System Events). Records
+# private/launch-window-smoke/latest.json.
+#
+# Usage: Scripts/launch-window-smoke.sh [path/to/Highball.app]
+set -u
+ROOT="$(cd "$(dirname "$0")/.." && pwd)"
+APP="${1:-$ROOT/dist/Highball.app}"; BIN="$APP/Contents/MacOS/Highball"
+[ -x "$BIN" ] || { echo "no app at $APP (run Scripts/make-app.sh)"; exit 2; }
+OUT="$ROOT/private/launch-window-smoke"; mkdir -p "$OUT"
+H="${TMPDIR:-/tmp}/hb-launch-window-smoke"; rm -rf "$H"; mkdir -p "$H"
+wins(){ osascript -e 'tell application "System Events" to tell process "Highball" to get name of every window' 2>/dev/null | tr -d '\n'; }
+record(){ python3 -c "import json,time;json.dump({'passed':'$1'=='true','epoch':int(time.time()),'date':time.strftime('%Y-%m-%d'),'windows':'$2'},open('$OUT/latest.json','w'),indent=2)"; }
+pkill -f "$BIN" 2>/dev/null; sleep 1
+HIGHBALL_HOME="$H" "$BIN" >/dev/null 2>&1 & sleep 6
+osascript -e 'tell application "Highball" to activate' >/dev/null 2>&1; sleep 1
+echo "after launch: $(wins)"
+osascript -e 'tell application "System Events" to tell process "Highball" to keystroke "," using command down' >/dev/null 2>&1; sleep 2
+echo "after Cmd-,: $(wins)"
+osascript -e 'tell application "System Events" to tell process "Highball" to click button 1 of window "Highball"' >/dev/null 2>&1; sleep 1
+echo "after closing main: $(wins)"
+osascript -e 'tell application "System Events" to tell process "Highball" to keystroke "q" using command down' >/dev/null 2>&1; sleep 3
+pgrep -f "$BIN" >/dev/null && pkill -9 -f "$BIN"; sleep 1
+HIGHBALL_HOME="$H" "$BIN" >/dev/null 2>&1 & sleep 7
+w=$(wins); echo "after relaunch: $w"
+pkill -f "$BIN" 2>/dev/null; rm -rf "$H"
+if echo "$w" | grep -q 'Highball'; then record true "$w"; echo "LAUNCH WINDOW SMOKE PASSED (main window present after relaunch)"; exit 0
+else record false "$w"; echo "LAUNCH WINDOW SMOKE FAILED: no main window after relaunch (windows: $w)"; exit 1; fi
diff --git a/Scripts/release.sh b/Scripts/release.sh
index cee4213..123b88c 100755
--- a/Scripts/release.sh
+++ b/Scripts/release.sh
@@ -59,6 +59,22 @@ then
sleep 5
fi
+# Launch-window regressions (issue #58: only Settings came back after a relaunch) ship silently
+# because every other check starts the app fresh. A release wants a recent
+# Scripts/launch-window-smoke.sh result: Settings open, main closed, quit, relaunch, main is back.
+if ! python3 - <<'PY'
+import json, sys, time
+d = json.load(open("private/launch-window-smoke/latest.json"))
+sys.exit(0 if d.get("passed") and time.time() - d.get("epoch", 0) < 14*86400 else 1)
+PY
+then
+ echo "" >&2
+ echo "WARNING: no passing launch-window-smoke result from the last 14 days." >&2
+ echo " Run Scripts/launch-window-smoke.sh: the app must open its main window on relaunch." >&2
+ echo " Continuing in 5s…" >&2
+ sleep 5
+fi
+
# Game regressions (a launcher going silent, a map load freezing) ship via engine, app and OS
# changes alike, so a release wants a recent Scripts/game-smoke.sh result: every verified title
# installed here launched, showed its window and kept drawing. Same rule: warn, don't block.
diff --git a/Sources/HighballApp/HighballApp.swift b/Sources/HighballApp/HighballApp.swift
index 1cf5bec..b52695e 100644
--- a/Sources/HighballApp/HighballApp.swift
+++ b/Sources/HighballApp/HighballApp.swift
@@ -9,8 +9,10 @@ struct HighballApp: App {
@State private var state = AppState()
var body: some Scene {
- Settings { SettingsView().environment(state).preferredColorScheme(.dark) }
- WindowGroup("Highball") {
+ // The main window group is declared first: it is the scene SwiftUI opens at launch.
+ // Settings comes after it and is non-restorable (SettingsView), so a session that ended
+ // with only Settings open cannot come back as Settings alone (issue #58).
+ WindowGroup("Highball", id: "main") {
ContentView()
.environment(state)
.tint(HB.amber)
@@ -41,6 +43,7 @@ struct HighballApp: App {
}
}
}
+ Settings { SettingsView().environment(state).preferredColorScheme(.dark) }
}
}
@@ -102,8 +105,19 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
let icon = NSImage(contentsOf: iconURL) {
NSApp.applicationIconImage = icon
}
+ // Issue #58: if window restoration brought back only the Settings window, open the main
+ // one. SettingsView asks for it as it appears; this is the fallback once launch settles.
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { MainWindow.ensureOpen() }
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { true }
+
+ /// Dock click with only Settings on screen: SwiftUI sees a visible window and opens nothing,
+ /// so bring the main window back ourselves (issue #58).
+ func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
+ guard !MainWindow.isOpen, MainWindow.opener != nil else { return true }
+ MainWindow.ensureOpen()
+ return false
+ }
}
struct ContentView: View {
diff --git a/Sources/HighballApp/SettingsView.swift b/Sources/HighballApp/SettingsView.swift
index ec03d68..fb509e8 100644
--- a/Sources/HighballApp/SettingsView.swift
+++ b/Sources/HighballApp/SettingsView.swift
@@ -5,6 +5,7 @@ import HighballKit
/// canvas "Advanced: Settings"). Nothing here is needed to play a game.
struct SettingsView: View {
@Environment(AppState.self) private var state
+ @Environment(\.openWindow) private var openWindow
var body: some View {
@Bindable var state = state
@@ -17,6 +18,14 @@ struct SettingsView: View {
TroubleshootingPane().tabItem { Label(L("Troubleshooting"), systemImage: "wrench.and.screwdriver") }
.tag(SettingsTab.troubleshooting)
}
+ // Issue #58: this window must never be the only one back after a relaunch. Restoration
+ // is off for it, and if it does appear without the main window (a restored session,
+ // or the main window closed earlier), open the main window.
+ .background(WindowAccessor { window in window.isRestorable = false })
+ .onAppear {
+ MainWindow.opener = { openWindow(id: "main") }
+ MainWindow.ensureOpen()
+ }
// The strip lives on the main window; a compact echo here so a repair or a new
// environment started from Settings is not silent (review #14).
if state.busy {
diff --git a/Sources/HighballApp/WindowAccessor.swift b/Sources/HighballApp/WindowAccessor.swift
new file mode 100644
index 0000000..45a9738
--- /dev/null
+++ b/Sources/HighballApp/WindowAccessor.swift
@@ -0,0 +1,43 @@
+import SwiftUI
+import AppKit
+
+/// Hands a SwiftUI view's hosting NSWindow to a callback once it exists. Used to mark the
+/// Settings window non-restorable (issue #58): macOS window restoration otherwise brings a
+/// Settings window that was open at quit back at the next launch, and SwiftUI then skips
+/// opening the default main window because a window was already restored.
+struct WindowAccessor: NSViewRepresentable {
+ let configure: (NSWindow) -> Void
+
+ func makeNSView(context: Context) -> NSView {
+ let view = NSView()
+ DispatchQueue.main.async { if let window = view.window { configure(window) } }
+ return view
+ }
+
+ func updateNSView(_ nsView: NSView, context: Context) {
+ DispatchQueue.main.async { if let window = nsView.window { configure(window) } }
+ }
+}
+
+/// The main (library) window must exist whenever the app is in front of the user. SwiftUI opens
+/// it at launch on its own, except when window restoration restored something else first; and a
+/// dock click with only Settings visible does not reopen it either. Views that can appear without
+/// a main window register an opener here, and the delegate and Settings ask for one when needed.
+@MainActor
+enum MainWindow {
+ /// Set from a view's onAppear: `{ openWindow(id: "main") }`.
+ static var opener: (() -> Void)?
+
+ static func isMain(_ window: NSWindow) -> Bool {
+ (window.identifier?.rawValue.hasPrefix("main") ?? false) || window.title == "Highball"
+ }
+
+ static var isOpen: Bool {
+ NSApp.windows.contains { isMain($0) && ($0.isVisible || $0.isMiniaturized) }
+ }
+
+ /// Opens the main window if none is open; a no-op when one already is.
+ static func ensureOpen() {
+ if !isOpen { opener?() }
+ }
+}