Repository navigation
feat: hierarchical workspace groups with templates, scripts, and project management #2156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
65ec7b7
bb81d43
76093f0
5527c36
4448ba5
d84739d
6d7f4df
4614ccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2331,6 +2331,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent | |
| installMenuBarVisibilityObserver() | ||
| syncMenuBarExtraVisibility() | ||
| updateController.startUpdaterIfNeeded() | ||
| NSApp.servicesProvider = self | ||
| ScriptRepository.shared.seedDefaultScripts() | ||
| TemplateRepository.shared.seedDefaultTemplates() | ||
| } | ||
| titlebarAccessoryController.start() | ||
| windowDecorationsController.start() | ||
|
|
@@ -4231,6 +4234,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent | |
| mainWindowContexts.values.first(where: { $0.windowId == windowId })?.tabManager | ||
| } | ||
|
|
||
| /// Returns all TabManagers across all windows. | ||
| func allTabManagers() -> [TabManager] { | ||
| mainWindowContexts.values.map(\.tabManager) | ||
| } | ||
|
|
||
| func windowId(for tabManager: TabManager) -> UUID? { | ||
| mainWindowContexts.values.first(where: { $0.tabManager === tabManager })?.windowId | ||
| } | ||
|
|
@@ -4764,13 +4772,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent | |
| alert.addButton(withTitle: String(localized: "common.close", defaultValue: "Close")) | ||
| alert.addButton(withTitle: String(localized: "common.cancel", defaultValue: "Cancel")) | ||
|
|
||
| let alertWindow = alert.window | ||
| if let closeButton = alert.buttons.first { | ||
| alertWindow.defaultButtonCell = closeButton.cell as? NSButtonCell | ||
| alertWindow.initialFirstResponder = closeButton | ||
| DispatchQueue.main.async { | ||
| _ = alertWindow.makeFirstResponder(closeButton) | ||
| } | ||
| // Mark the Close button as destructive so the system renders it with | ||
| // red text, fixing a contrast issue in dark mode. | ||
| if #available(macOS 12.0, *) { | ||
| alert.buttons[0].hasDestructiveAction = true | ||
| } | ||
|
|
||
| return alert.runModal() == .alertFirstButtonReturn | ||
|
|
@@ -5186,6 +5191,26 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent | |
| return nil | ||
| } | ||
|
|
||
| /// Resolve the best current `TabManager` for menu/UI reads without mutating | ||
| /// the app-wide active window pointers. Use this in SwiftUI computed properties | ||
| /// and CommandMenu closures to avoid triggering @Published re-evaluation loops. | ||
| func preferredTabManager(preferredWindow: NSWindow? = nil) -> TabManager? { | ||
| if let preferredWindow, | ||
| let context = contextForMainWindow(preferredWindow) { | ||
| return context.tabManager | ||
| } | ||
| if let context = contextForMainWindow(NSApp.keyWindow) { | ||
| return context.tabManager | ||
| } | ||
| if let context = contextForMainWindow(NSApp.mainWindow) { | ||
| return context.tabManager | ||
| } | ||
| if let activeManager = tabManager { | ||
| return activeManager | ||
| } | ||
| return mainWindowContexts.values.first?.tabManager | ||
| } | ||
|
|
||
| /// Re-sync app-level active window pointers from the currently focused main terminal window. | ||
| /// This keeps menu/shortcut actions window-scoped even if the cached `tabManager` drifts. | ||
| @discardableResult | ||
|
|
@@ -11330,6 +11355,129 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent | |
| } | ||
| #endif | ||
|
|
||
| // MARK: - Finder Services | ||
|
|
||
| /// macOS Services handler: "Open in cmux" | ||
| /// Accepts folder paths from Finder right-click > Services. | ||
| @objc func openInCmux( | ||
| _ pasteboard: NSPasteboard, | ||
| userData: String, | ||
| error: AutoreleasingUnsafeMutablePointer<NSString> | ||
| ) { | ||
| guard let items = pasteboard.readObjects(forClasses: [NSURL.self], options: [ | ||
| .urlReadingFileURLsOnly: true | ||
| ]) as? [URL] else { return } | ||
|
|
||
| // Use the first window's tab manager, or create a window if none exists | ||
| let tm: TabManager | ||
| if let firstWindowSummary = listMainWindowSummaries().first, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Finder service selects the target window via listMainWindowSummaries().first, which is built from Dictionary values and therefore unordered. With multiple windows open, this can route Finder service opens to an arbitrary background window instead of the key/main window. Prompt for AI agents |
||
| let existingTm = tabManagerFor(windowId: firstWindowSummary.windowId) { | ||
| tm = existingTm | ||
| } else if let firstURL = items.first { | ||
| let path = resolvedDirectoryPath(for: firstURL) | ||
| _ = createMainWindow(initialWorkingDirectory: path) | ||
| NSApp.activate(ignoringOtherApps: true) | ||
| return | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Finder Services return immediately after creating a cold-start window, so only the first selected item is opened and template services never apply the template on cold start. Prompt for AI agents |
||
| } else { | ||
| return | ||
| } | ||
|
|
||
| for url in items { | ||
| var isDir: ObjCBool = false | ||
| let path: String | ||
| if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDir), isDir.boolValue { | ||
| path = url.path | ||
| } else { | ||
| path = url.deletingLastPathComponent().path | ||
| } | ||
|
|
||
| let dirURL = URL(fileURLWithPath: path) | ||
| let canonicalPath = dirURL.resolvingSymlinksInPath().path | ||
|
|
||
| // Dedupe: check if a parent workspace already targets this directory | ||
| var found = false | ||
| for wsId in tm.items { | ||
| guard let ws = tm.workspace(for: wsId), | ||
| ws.hasChildren else { continue } | ||
| let wsPath = URL(fileURLWithPath: ws.currentDirectory) | ||
| .resolvingSymlinksInPath().path | ||
| if wsPath == canonicalPath { | ||
| tm.selectedTabId = ws.id | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if found { continue } | ||
|
|
||
| // Create project workspace | ||
| let projectName = dirURL.lastPathComponent | ||
| let ws = tm.addWorkspace(workingDirectory: path, select: true) | ||
| ws.title = projectName | ||
| } | ||
|
|
||
| NSApp.activate(ignoringOtherApps: true) | ||
| } | ||
|
|
||
| // MARK: - Finder Services (Builder / Fixer) | ||
|
|
||
| @objc func openInCmuxBuilder( | ||
| _ pasteboard: NSPasteboard, | ||
| userData: String, | ||
| error: AutoreleasingUnsafeMutablePointer<NSString> | ||
| ) { | ||
| openInCmuxWithTemplate(pasteboard, templateName: "Builder", error: error) | ||
| } | ||
|
|
||
| @objc func openInCmuxFixer( | ||
| _ pasteboard: NSPasteboard, | ||
| userData: String, | ||
| error: AutoreleasingUnsafeMutablePointer<NSString> | ||
| ) { | ||
| openInCmuxWithTemplate(pasteboard, templateName: "Fixer", error: error) | ||
| } | ||
|
|
||
| private func openInCmuxWithTemplate( | ||
| _ pasteboard: NSPasteboard, | ||
| templateName: String, | ||
| error: AutoreleasingUnsafeMutablePointer<NSString> | ||
| ) { | ||
| guard let items = pasteboard.readObjects(forClasses: [NSURL.self], options: [ | ||
| .urlReadingFileURLsOnly: true | ||
| ]) as? [URL] else { return } | ||
|
|
||
| guard let firstWindowSummary = listMainWindowSummaries().first, | ||
| let tm = tabManagerFor(windowId: firstWindowSummary.windowId) else { | ||
| // No window exists — create one for the first directory | ||
| if let firstURL = items.first { | ||
| let path = resolvedDirectoryPath(for: firstURL) | ||
| _ = createMainWindow(initialWorkingDirectory: path) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| let template = try? TemplateRepository.shared.getTemplate(named: templateName) | ||
|
|
||
| for url in items { | ||
| let path = resolvedDirectoryPath(for: url) | ||
| if let template { | ||
| tm.openTemplate(template, directory: path) | ||
| } else { | ||
| // Fallback: plain workspace if template not found | ||
| let ws = tm.addWorkspace(workingDirectory: path, select: true) | ||
| ws.title = URL(fileURLWithPath: path).lastPathComponent | ||
| } | ||
| } | ||
|
|
||
| NSApp.activate(ignoringOtherApps: true) | ||
| } | ||
|
|
||
| private func resolvedDirectoryPath(for url: URL) -> String { | ||
| var isDir: ObjCBool = false | ||
| if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDir), isDir.boolValue { | ||
| return url.path | ||
| } | ||
| return url.deletingLastPathComponent().path | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
|
|
@@ -12673,5 +12821,4 @@ private extension NSWindow { | |
| } | ||
| return hitWebView === webView | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import Foundation | ||
|
|
||
| /// Fatal errors during `.cmux.yaml` parsing. Thrown for unrecoverable failures. | ||
| enum CmuxConfigError: Error, Equatable { | ||
| case invalidYaml(details: String) | ||
| case fileNotReadable(path: String) | ||
| } | ||
|
|
||
| /// Non-fatal warnings during `.cmux.yaml` parsing. | ||
| /// The config is still applied, but with the warned items degraded. | ||
| enum CmuxConfigWarning: Equatable { | ||
| /// A referenced startup script was not found in the script repository. | ||
| case scriptNotFound(name: String) | ||
| /// A sub-group tried to nest groups beyond the 2-level limit. | ||
| case maxDepthExceeded(groupName: String) | ||
| } | ||
|
|
||
| /// Result of parsing a `.cmux.yaml` file. | ||
| /// Contains the project definition plus any non-fatal warnings. | ||
| struct ConfigParseResult { | ||
| let projectName: String | ||
| let projectColor: String? | ||
| let warnings: [CmuxConfigWarning] | ||
| /// Tab definitions with their startup script names (to be resolved later). | ||
| let tabDefinitions: [ConfigTabDefinition] | ||
| } | ||
|
|
||
| /// A tab definition from `.cmux.yaml`, not yet resolved to a workspace. | ||
| struct ConfigTabDefinition { | ||
| let title: String | ||
| let startupScript: String? | ||
| let groupPath: [String] // empty = top-level, ["sub"] = inside sub-group "sub" | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
openProjectPathbypasses socket authentication, socmux opencan fail in password-protected socket mode.Prompt for AI agents