|
| 1 | +import AppKit |
| 2 | +import Foundation |
| 3 | + |
| 4 | +/// Brings a closed tab back into a native window tab. Reopening reuses the restoration path that |
| 5 | +/// cold launch already uses, so a reopened table tab recovers its filters, sort, and column |
| 6 | +/// layout instead of reimplementing that here. |
| 7 | +@MainActor |
| 8 | +internal enum RecentlyClosedTabReopener { |
| 9 | + internal static func reopenMostRecent() { |
| 10 | + guard let entry = RecentlyClosedTabStore.shared.mostRecentEntry else { return } |
| 11 | + reopen(id: entry.id) |
| 12 | + } |
| 13 | + |
| 14 | + internal static func reopen(id: UUID) { |
| 15 | + guard let entry = RecentlyClosedTabStore.shared.consume(id: id) else { return } |
| 16 | + |
| 17 | + // Closing the last tab of the last window leaves the window standing but empty. Reopening |
| 18 | + // into a new window tab there would strand that empty tab alongside the restored one, so |
| 19 | + // the empty window is filled in place, matching how New Tab reuses it. |
| 20 | + if let coordinator = emptyWindowCoordinator(for: entry.connectionId) { |
| 21 | + restore(entry, into: coordinator) |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + guard WindowManager.shared.hasOpenWindow(for: entry.connectionId) else { |
| 26 | + Task { await LaunchIntentRouter.shared.route(.reopenClosedTab(entry)) } |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + openWindowTab(for: entry) |
| 31 | + NSApp.activate(ignoringOtherApps: true) |
| 32 | + } |
| 33 | + |
| 34 | + private static func emptyWindowCoordinator(for connectionId: UUID) -> MainContentCoordinator? { |
| 35 | + let empty = MainContentCoordinator.allActiveCoordinators().filter { |
| 36 | + $0.connectionId == connectionId && $0.tabManager.tabs.isEmpty |
| 37 | + } |
| 38 | + return empty.first { $0.contentWindow?.isKeyWindow == true } ?? empty.first |
| 39 | + } |
| 40 | + |
| 41 | + private static func restore(_ entry: RecentlyClosedTabEntry, into coordinator: MainContentCoordinator) { |
| 42 | + let tab = makeTab(for: entry) |
| 43 | + coordinator.tabManager.adoptTab(tab, claimFocus: tab.tabType == .query) |
| 44 | + |
| 45 | + if tab.tabType == .table, let tableName = tab.tableContext.tableName { |
| 46 | + coordinator.restoreLastHiddenColumnsForTable() |
| 47 | + coordinator.restoreFiltersForTable(tableName) |
| 48 | + coordinator.lazyLoadCurrentTabIfNeeded(trigger: .restore) |
| 49 | + } |
| 50 | + |
| 51 | + coordinator.contentWindow?.makeKeyAndOrderFront(nil) |
| 52 | + NSApp.activate(ignoringOtherApps: true) |
| 53 | + } |
| 54 | + |
| 55 | + private static func makeTab(for entry: RecentlyClosedTabEntry) -> QueryTab { |
| 56 | + QueryTab( |
| 57 | + from: entry.tab, |
| 58 | + defaultPageSize: AppSettingsManager.shared.dataGrid.defaultPageSize |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + internal static func openWindowTab(for entry: RecentlyClosedTabEntry) { |
| 63 | + let tab = makeTab(for: entry) |
| 64 | + let payload = EditorTabPayload( |
| 65 | + connectionId: entry.connectionId, |
| 66 | + tabType: tab.tabType, |
| 67 | + tableName: tab.tableContext.tableName, |
| 68 | + databaseName: tab.tableContext.databaseName, |
| 69 | + schemaName: tab.tableContext.schemaName, |
| 70 | + isView: tab.tableContext.isView, |
| 71 | + skipAutoExecute: true, |
| 72 | + sourceFileURL: tab.content.sourceFileURL, |
| 73 | + erDiagramSchemaKey: tab.display.erDiagramSchemaKey, |
| 74 | + tabTitle: tab.title, |
| 75 | + intent: .restoreOrDefault |
| 76 | + ) |
| 77 | + RestorationGroupRegistry.register( |
| 78 | + .init(tabs: [tab], selectedTabId: tab.id, loadTiming: .immediate), |
| 79 | + for: payload.id |
| 80 | + ) |
| 81 | + WindowManager.shared.openTab(payload: payload) |
| 82 | + } |
| 83 | +} |
0 commit comments