144 lines
6.0 KiB
Swift
144 lines
6.0 KiB
Swift
import AppKit
|
|
import HelderCore
|
|
import HelderServices
|
|
import SwiftUI
|
|
|
|
/// The app's window scene: one project per window.
|
|
public struct HelderScene: Scene {
|
|
public static let windowID = "workspace"
|
|
|
|
public init() {}
|
|
|
|
public var body: some Scene {
|
|
WindowGroup(id: Self.windowID) {
|
|
WorkspaceWindow(root: LaunchState.claimRoot())
|
|
}
|
|
.windowStyle(.hiddenTitleBar)
|
|
.defaultSize(width: Metrics.defaultWindow.width, height: Metrics.defaultWindow.height)
|
|
.commands { HelderCommands() }
|
|
}
|
|
}
|
|
|
|
/// The menu bar. Shortcuts the key router owns (⌘F, ⌘G, ⌘L, ⌘.) are shown here too; the router
|
|
/// consumes them first, so the menu item fires only when a field or an overlay let the key through.
|
|
struct HelderCommands: Commands {
|
|
@FocusedValue(\.workspace) private var workspace
|
|
|
|
var body: some Commands {
|
|
CommandGroup(replacing: .newItem) {
|
|
Button("New Window") { openNewInstance(nil) }
|
|
.keyboardShortcut("n", modifiers: [.command, .shift])
|
|
Button("Open Folder…") { openFolder() }
|
|
.keyboardShortcut("o", modifiers: .command)
|
|
Menu("Open Recent") {
|
|
let recents = workspace?.recentProjects.filter { $0.path != workspace?.root?.path } ?? []
|
|
if recents.isEmpty {
|
|
Text("No Recent Projects")
|
|
} else {
|
|
ForEach(recents) { project in
|
|
Button(project.name) { openRecent(project.path) }
|
|
}
|
|
}
|
|
}
|
|
Button("Open Recent Project…") { workspace?.perform(.showRecentProjects) }
|
|
.keyboardShortcut("o", modifiers: [.command, .shift])
|
|
.disabled(workspace?.root == nil)
|
|
}
|
|
CommandGroup(replacing: .saveItem) {
|
|
Button(workspace?.editor?.activePath == nil ? "Close" : "Close File") {
|
|
if let workspace { workspace.perform(.closeFileOrWindow) } else { NSApp.keyWindow?.performClose(nil) }
|
|
}
|
|
.keyboardShortcut("w", modifiers: .command)
|
|
Button("Close Window") { NSApp.keyWindow?.performClose(nil) }
|
|
.keyboardShortcut("w", modifiers: [.command, .shift])
|
|
Divider()
|
|
Button("Save") { workspace?.perform(.save) }
|
|
.keyboardShortcut("s", modifiers: .command)
|
|
.disabled(workspace?.editor?.activePath == nil)
|
|
}
|
|
// ⌘M cycles the editor's view, so Minimize gives up its shortcut.
|
|
CommandGroup(replacing: .windowSize) {
|
|
Button("Minimize") { NSApp.keyWindow?.miniaturize(nil) }
|
|
Button("Zoom") { NSApp.keyWindow?.zoom(nil) }
|
|
}
|
|
CommandGroup(before: .toolbar) {
|
|
Button("Refresh") { workspace?.perform(.refresh) }
|
|
.keyboardShortcut("r", modifiers: .command)
|
|
.disabled(workspace?.root == nil)
|
|
Divider()
|
|
Button(workspace?.layout.gitVisible == false ? "Show Source Control" : "Hide Source Control") {
|
|
workspace?.perform(.toggleGit)
|
|
}
|
|
.keyboardShortcut("g", modifiers: .command)
|
|
Button(workspace?.layout.fluid == false ? "Fluid Column Widths" : "Lock Column Widths") {
|
|
workspace?.perform(.toggleAutoFit)
|
|
}
|
|
.keyboardShortcut("l", modifiers: .command)
|
|
Button(workspace?.layout.showHidden == true ? "Hide Hidden Files" : "Show Hidden Files") {
|
|
workspace?.perform(.toggleHidden)
|
|
}
|
|
.keyboardShortcut(".", modifiers: .command)
|
|
Divider()
|
|
Button("Search") { workspace?.perform(.showSearch(seed: "")) }
|
|
.keyboardShortcut("f", modifiers: .command)
|
|
Button("Project Note") { workspace?.perform(.showNote) }
|
|
.keyboardShortcut("n", modifiers: .command)
|
|
Button("Keyboard Shortcuts") { workspace?.perform(.showShortcuts) }
|
|
Divider()
|
|
Button("Actual Size") { workspace?.perform(.zoom(.reset)) }
|
|
.keyboardShortcut("0", modifiers: .command)
|
|
Button("Zoom In") { workspace?.perform(.zoom(.bigger)) }
|
|
.keyboardShortcut("+", modifiers: .command)
|
|
Button("Zoom Out") { workspace?.perform(.zoom(.smaller)) }
|
|
.keyboardShortcut("-", modifiers: .command)
|
|
Divider()
|
|
Button(workspace?.layout.isFullScreen == true ? "Exit Full Screen" : "Enter Full Screen") {
|
|
NSApp.keyWindow?.toggleFullScreen(nil)
|
|
}
|
|
.keyboardShortcut("f", modifiers: [.command, .control])
|
|
}
|
|
CommandGroup(replacing: .help) {
|
|
Button("Keyboard Shortcuts") { workspace?.perform(.showShortcuts) }
|
|
Divider()
|
|
Button("Open Log") { LiveSystemActions().openLog() }
|
|
Button("Reveal Log in Finder") { LiveSystemActions().revealLog() }
|
|
}
|
|
}
|
|
|
|
private func openNewInstance(_ root: URL?) {
|
|
LiveSystemActions().openNewInstance(root)
|
|
}
|
|
|
|
/// Without a key window there is no project to switch, so the choice opens in a new process.
|
|
private func openFolder() {
|
|
if let workspace {
|
|
workspace.perform(.openFolder)
|
|
return
|
|
}
|
|
Task { @MainActor in
|
|
if let url = await LiveSystemActions().chooseFolder() { openNewInstance(url) }
|
|
}
|
|
}
|
|
|
|
private func openRecent(_ path: String) {
|
|
if let workspace {
|
|
workspace.perform(.openRecentProject(path))
|
|
} else {
|
|
openNewInstance(URL(fileURLWithPath: path))
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Hands the launch folder (`$HELDER_PROJECT` or an argument) to the first window; a later window
|
|
/// in this process (the Dock reopening one after the last closed) starts on the launcher.
|
|
@MainActor
|
|
enum LaunchState {
|
|
private static var claimedInitial = false
|
|
|
|
static func claimRoot() -> URL? {
|
|
guard !claimedInitial else { return nil }
|
|
claimedInitial = true
|
|
return ProjectLocator.initialRoot()
|
|
}
|
|
}
|