Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 73 additions & 18 deletions Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3716,6 +3716,20 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
quantized.forEach { hasher.combine($0) }
}

private func persistWorkspaceSessionSnapshots(
from snapshot: AppSessionSnapshot,
synchronously: Bool
) {
let writeBlock = {
SessionPersistenceStore.saveWorkspaceSnapshots(from: snapshot)
}
if synchronously {
sessionPersistenceQueue.sync(execute: writeBlock)
} else {
sessionPersistenceQueue.async(execute: writeBlock)
}
}

private func persistSessionSnapshot(
_ snapshot: AppSessionSnapshot?,
removeWhenEmpty: Bool,
Expand All @@ -3724,7 +3738,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
) {
guard snapshot != nil || removeWhenEmpty || persistedGeometryData != nil else { return }

let writeBlock = {
let writePrimarySnapshotBlock = { () -> SessionSnapshotWriteResult in
Self.removeLegacyPersistedWindowGeometry()
if let persistedGeometryData {
UserDefaults.standard.set(
Expand All @@ -3733,16 +3747,26 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
)
}
if let snapshot {
_ = SessionPersistenceStore.save(snapshot)
return SessionPersistenceStore.saveResult(snapshot)
} else if removeWhenEmpty {
SessionPersistenceStore.removeSnapshot()
}
return .unchanged
}

let writeAllSnapshotsBlock = {
guard let snapshot else {
_ = writePrimarySnapshotBlock()
return
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
guard writePrimarySnapshotBlock() != .failed else { return }
SessionPersistenceStore.saveWorkspaceSnapshots(from: snapshot)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

if synchronously {
writeBlock()
sessionPersistenceQueue.sync(execute: writeAllSnapshotsBlock)
} else {
sessionPersistenceQueue.async(execute: writeBlock)
sessionPersistenceQueue.async(execute: writeAllSnapshotsBlock)
}
}

Expand Down Expand Up @@ -3771,19 +3795,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
let windows: [SessionWindowSnapshot] = contexts
.prefix(SessionPersistencePolicy.maxWindowsPerSnapshot)
.map { context in
let window = context.window ?? windowForMainWindowId(context.windowId)
return SessionWindowSnapshot(
frame: window.map { SessionRectSnapshot($0.frame) },
display: displaySnapshot(for: window),
tabManager: context.tabManager.sessionSnapshot(
includeScrollback: includeScrollback,
restorableAgentIndex: restorableAgentIndex
),
sidebar: SessionSidebarSnapshot(
isVisible: context.sidebarState.isVisible,
selection: SessionSidebarSelection(selection: context.sidebarSelectionState.selection),
width: SessionPersistencePolicy.sanitizedSidebarWidth(Double(context.sidebarState.persistedWidth))
)
sessionWindowSnapshot(
for: context,
includeScrollback: includeScrollback,
restorableAgentIndex: restorableAgentIndex
)
}

Expand All @@ -3795,6 +3810,27 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
)
}

private func sessionWindowSnapshot(
for context: MainWindowContext,
includeScrollback: Bool,
restorableAgentIndex: RestorableAgentSessionIndex?
) -> SessionWindowSnapshot {
let window = context.window ?? windowForMainWindowId(context.windowId)
return SessionWindowSnapshot(
frame: window.map { SessionRectSnapshot($0.frame) },
display: displaySnapshot(for: window),
tabManager: context.tabManager.sessionSnapshot(
includeScrollback: includeScrollback,
restorableAgentIndex: restorableAgentIndex
),
sidebar: SessionSidebarSnapshot(
isVisible: context.sidebarState.isVisible,
selection: SessionSidebarSelection(selection: context.sidebarSelectionState.selection),
width: SessionPersistencePolicy.sanitizedSidebarWidth(Double(context.sidebarState.persistedWidth))
)
)
}

#if DEBUG
private func debugLogSessionSaveSnapshot(
_ snapshot: AppSessionSnapshot,
Expand Down Expand Up @@ -14097,6 +14133,22 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
persistWindowGeometry(from: window)
}

let shouldPersistOnUnregister = Self.shouldPersistSnapshotOnWindowUnregister(isTerminatingApp: isTerminatingApp)
let closingWorkspaceSnapshot = shouldPersistOnUnregister
? contextForMainTerminalWindow(window, reindex: false).map { context in
AppSessionSnapshot(
version: SessionSnapshotSchema.currentVersion,
createdAt: Date().timeIntervalSince1970,
windows: [
sessionWindowSnapshot(
for: context,
includeScrollback: false,
restorableAgentIndex: RestorableAgentSessionIndex.load()
),
]
)
}
: nil
guard let removed = unregisterMainWindowContext(for: window) else { return }
publishCmuxWindowLifecycle(name: "window.closed", windowId: removed.windowId, origin: "appkit_close")
commandPaletteVisibilityByWindowId.removeValue(forKey: removed.windowId)
Expand Down Expand Up @@ -14130,7 +14182,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
// During app termination we already persisted a full snapshot (with scrollback)
// in applicationShouldTerminate/applicationWillTerminate. Saving again here would
// overwrite it as windows tear down one-by-one, dropping closed windows and replay.
if Self.shouldPersistSnapshotOnWindowUnregister(isTerminatingApp: isTerminatingApp) {
if shouldPersistOnUnregister {
if let closingWorkspaceSnapshot {
persistWorkspaceSessionSnapshots(from: closingWorkspaceSnapshot, synchronously: false)
}
_ = saveSessionSnapshot(includeScrollback: false, removeWhenEmpty: false)
}
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/CmuxConfigExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,10 @@ struct CmuxConfigExecutor {
}

let resolvedCwd = CmuxConfigStore.resolveCwd(wsDef.cwd, relativeTo: baseCwd)
let newWorkspace = tabManager.addWorkspace(workingDirectory: resolvedCwd)
let newWorkspace = tabManager.addWorkspace(
workingDirectory: resolvedCwd,
restoreWorkspaceSession: false
)
newWorkspace.setCustomTitle(workspaceName)
if let color = wsDef.color {
newWorkspace.setCustomColor(color)
Expand Down
Loading
Loading