The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.
Windows reopen where they were, on their own display, with the last resize kept at quit (#201)
Quinn Battersbyac1b3d1c183d
diff --git a/Sources/Cra/App/AppDelegate.swift b/Sources/Cra/App/AppDelegate.swiftindex c53c336..278b166 100644--- a/Sources/Cra/App/AppDelegate.swift+++ b/Sources/Cra/App/AppDelegate.swift@@ -302,6 +302,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {if store.settings.warnBeforeQuitting, !isRelaunching, !confirmQuit() { return .terminateCancel }// Little Windows are not in the sidebar; keep their pages in the Library.for w in littleWindows { w.archivePage() }+ for w in mainWindows { w.saveFrameNow() }store.saveAll()return .terminateNow}@@ -311,6 +312,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {/// Sparkle quits Cra to install an update and starts it again: no quit confirmation.func prepareForUpdateRelaunch() {isRelaunching = true+ for w in mainWindows { w.saveFrameNow() }store.saveAll()}diff --git a/Sources/Cra/UI/Windows/MainWindowController.swift b/Sources/Cra/UI/Windows/MainWindowController.swiftindex 6fa9915..a23ab37 100644--- a/Sources/Cra/UI/Windows/MainWindowController.swift+++ b/Sources/Cra/UI/Windows/MainWindowController.swift@@ -85,7 +85,6 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserMstyleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],backing: .buffered, defer: false)super.init(window: window)- window.delegate = selfwindow.titlebarAppearsTransparent = truewindow.titleVisibility = .hiddenwindow.isMovableByWindowBackground = true@@ -93,7 +92,14 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserMwindow.isReleasedWhenClosed = falsewindow.tabbingMode = .disallowedwindow.title = "Cra"- if session.frame == nil { window.center() }+ // The initializer can shift a saved global frame onto the current screen.+ // Apply it after construction so a window on another display stays there.+ if session.frame != nil, NSScreen.screens.contains(where: { $0.visibleFrame.intersects(frame) }) {+ window.setFrame(frame, display: false)+ } else {+ window.center()+ }+ window.delegate = selfsidebarUI.windowID = session.idlet store = app.store@@ -2998,15 +3004,20 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM/// it is stored once a resize or move settles, not on every step of it.private func saveFrame() {frameSaveWork?.cancel()- let work = DispatchWorkItem { [weak self] in- guard let self, let window = self.window, !window.styleMask.contains(.fullScreen) else { return }- self.store.updateWindow(self.windowID) { $0.frame = NSStringFromRect(window.frame) }- }+ let work = DispatchWorkItem { [weak self] in self?.saveFrameNow() }frameSaveWork = workDispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: work)}private var frameSaveWork: DispatchWorkItem?+ /// Quit can arrive before the move/resize debounce fires.+ func saveFrameNow() {+ frameSaveWork?.cancel()+ frameSaveWork = nil+ guard let window, !window.styleMask.contains(.fullScreen) else { return }+ store.updateWindow(windowID) { $0.frame = NSStringFromRect(window.frame) }+ }+func windowShouldClose(_ sender: NSWindow) -> Bool {if sender === window { commandBar.close() }return truediff --git a/Tests/smoke-launch.sh b/Tests/smoke-launch.shindex 4f2c8ba..d3b8bf4 100755--- a/Tests/smoke-launch.sh+++ b/Tests/smoke-launch.sh@@ -1,6 +1,6 @@#!/bin/bash# Launch the built app three times on a throwaway data folder (no pages, a page opened, the page-# restored) and quit it each time like ⌘Q, just after a settings write. Fails if it dies, hangs,+# restored) and quit it each time like ⌘Q, just after a settings write or resize. Fails if it dies, hangs,# exits with an error or leaves a crash report: the only check that sees a CHECK inside Chromium.# Tests/smoke-launch.sh [path/to/Cra.app] (default build/Cra.app)set -euo pipefail@@ -26,9 +26,22 @@ fail() {exit 1}-# Quits like ⌘Q: an Apple event to this pid only, never to the installed Cra.+# Seeds an off-centre frame on the secondary display when present, or quits like+# ⌘Q: an Apple event to this pid only, never to the installed Cra.cat > "$TEST_DIR/quit.swift" <<'EOF'import AppKit+if CommandLine.arguments[1] == "frame" {+ let _ = NSApplication.shared+ let screen = NSScreen.screens.last!.visibleFrame+ let size = NSSize(width: min(1000, max(640, screen.width - 160)),+ height: min(700, max(420, screen.height - 180)))+ let frame = NSRect(x: screen.minX + floor((screen.width - size.width) / 3),+ y: screen.minY + floor((screen.height - size.height) / 3),+ width: size.width, height: size.height)+ let json = try! JSONSerialization.data(withJSONObject: ["windows": [["frame": NSStringFromRect(frame)]]])+ print(String(decoding: json, as: UTF8.self))+ exit(0)+}let pid = pid_t(CommandLine.arguments[1])!exit(NSRunningApplication(processIdentifier: pid)?.terminate() == true ? 0 : 1)EOF@@ -38,12 +51,14 @@ xcrun swiftc "$TEST_DIR/quit.swift" -o "$TEST_DIR/quit"cat > "$DATA/settings.json" <<'EOF'{"hasSeenImportPrompt": true, "warnBeforeQuitting": false, "toolbarDefaultMigrated": true, "showToolbar": false}EOF+"$TEST_DIR/quit" frame > "$DATA/windows.json"cat > "$TEST_DIR/page.html" <<'EOF'<!doctype html><title>Smoke</title><p>Cra smoke test</p>EOFrun() {local n="$1" what="$2"+ cp "$DATA/windows.json" "$TEST_DIR/expected-$n.json"CRA_DATA_DIR="$DATA" CRA_COMMAND_FILE="$TEST_DIR/commands" CRA_TEST_BACKGROUND=1 \"$APP/Contents/MacOS/$EXE" > "$TEST_DIR/run-$n.log" 2>&1 &PID=$!@@ -57,10 +72,37 @@ run() {[ "$what" = "opens a page" ] && echo "cra://open?url=file://$TEST_DIR/page.html" >> "$TEST_DIR/commands"sleep 8kill -0 "$PID" 2>/dev/null || fail "launch $n ($what): the app died before quitting"+ echo "cra://debug?windows=1" >> "$TEST_DIR/commands"+ for _ in $(seq 50); do+ grep -q 'Cra windows: MainWindowController' "$TEST_DIR/run-$n.log" && break+ sleep 0.1+ done+ python3 - "$TEST_DIR/expected-$n.json" "$TEST_DIR/run-$n.log" <<'PY' || fail "launch $n ($what): the window moved or resized on launch"+import json, re, sys+expected = json.load(open(sys.argv[1]))["windows"][0]["frame"]+match = re.search(r"Cra windows: MainWindowController .*? frame=(.*?) parent=", open(sys.argv[2]).read())+def numbers(frame):+ return [float(n) for n in re.findall(r"-?[0-9]+(?:\.[0-9]+)?", frame)]+assert match, "No browser window"+assert numbers(match[1]) == numbers(expected), (expected, match[1])+PY# A settings write just before quitting (#36 crashed on one at quit).echo "cra://action/toggleToolbar" >> "$TEST_DIR/commands"sleep 0.3- "$TEST_DIR/quit" "$PID" || fail "launch $n ($what): the app did not take the quit event"+ if [ "$n" = 2 ]; then+ # Both commands run in the same poll, before the 300 ms frame debounce.+ local resized+ resized=$(python3 - "$TEST_DIR/expected-$n.json" <<'PY'+import json, re, sys+value = json.load(open(sys.argv[1]))["windows"][0]["frame"]+x, y, width, height = [float(n) for n in re.findall(r"-?[0-9]+(?:\.[0-9]+)?", value)]+print(f"{max(640, width - 87):g}x{max(420, height - 53):g}")+PY+)+ printf '%s\n' "cra://debug?windowSize=$resized" 'cra://debug?quit=1' >> "$TEST_DIR/commands"+ else+ "$TEST_DIR/quit" "$PID" || fail "launch $n ($what): the app did not take the quit event"+ fifor _ in $(seq 60); dokill -0 "$PID" 2>/dev/null || breaksleep 0.5@@ -71,6 +113,18 @@ run() {PID=""rm -f "$TEST_DIR/commands"[ "$status" = 0 ] || fail "launch $n ($what): exited with status $status"+ if [ "$n" = 2 ]; then+ python3 - "$DATA/windows.json" "$TEST_DIR/expected-$n.json" <<'PY' || fail "the last resize was lost at quit"+import json, re, sys+def frame(path):+ value = json.load(open(path))["windows"][0]["frame"]+ return [float(n) for n in re.findall(r"-?[0-9]+(?:\.[0-9]+)?", value)]+before = frame(sys.argv[2])+width, height = max(640, before[2] - 87), max(420, before[3] - 53)+expected = [before[0], before[1] + before[3] - height, width, height]+assert frame(sys.argv[1]) == expected, (frame(sys.argv[1]), expected)+PY+ fiecho "Smoke: launch $n $what and quits cleanly"}