309 lines
12 KiB
Swift
309 lines
12 KiB
Swift
import Foundation
|
||
import HelderCore
|
||
import HelderServices
|
||
import Observation
|
||
|
||
/// Which column has keyboard focus: drives the active-panel tint and arrow-key navigation.
|
||
public enum Panel: String, Sendable, CaseIterable {
|
||
case explorer
|
||
case editor
|
||
case terminal
|
||
}
|
||
|
||
/// The column layout one project remembers between launches.
|
||
public struct ColumnLayoutSnapshot: Codable, Sendable, Equatable {
|
||
public var explorerWidth: Double
|
||
public var agentWidth: Double
|
||
public var fluid: Bool
|
||
public var explorerVisible: Bool
|
||
public var fileVisible: Bool
|
||
public var claudeVisible: Bool
|
||
|
||
public init(explorerWidth: Double, agentWidth: Double, fluid: Bool, visibility: ColumnVisibility = .all) {
|
||
self.explorerWidth = explorerWidth
|
||
self.agentWidth = agentWidth
|
||
self.fluid = fluid
|
||
explorerVisible = visibility.explorer
|
||
fileVisible = visibility.editor
|
||
claudeVisible = visibility.agent
|
||
}
|
||
|
||
/// A missing visibility flag means shown. Snapshots from before the Git view also hold
|
||
/// `sourceControlWidth` and `gitVisible`; they are ignored.
|
||
public init(from decoder: Decoder) throws {
|
||
let c = try decoder.container(keyedBy: CodingKeys.self)
|
||
explorerWidth = try c.decode(Double.self, forKey: .explorerWidth)
|
||
agentWidth = try c.decode(Double.self, forKey: .agentWidth)
|
||
fluid = try c.decode(Bool.self, forKey: .fluid)
|
||
explorerVisible = try c.decodeIfPresent(Bool.self, forKey: .explorerVisible) ?? true
|
||
fileVisible = try c.decodeIfPresent(Bool.self, forKey: .fileVisible) ?? true
|
||
claudeVisible = try c.decodeIfPresent(Bool.self, forKey: .claudeVisible) ?? true
|
||
}
|
||
|
||
public var visibility: ColumnVisibility {
|
||
ColumnVisibility(explorer: explorerVisible, editor: fileVisible, agent: claudeVisible)
|
||
}
|
||
|
||
public static func key(root: String) -> String { "helder.layout:" + root }
|
||
}
|
||
|
||
/// Where column layouts are kept, one per project root.
|
||
public protocol LayoutPersisting: Sendable {
|
||
func load(root: String) -> ColumnLayoutSnapshot?
|
||
func save(_ snapshot: ColumnLayoutSnapshot, root: String)
|
||
/// View → Zoom, shared by every project.
|
||
func loadZoom() -> Double
|
||
func saveZoom(_ level: Double)
|
||
}
|
||
|
||
extension LayoutPersisting {
|
||
public func loadZoom() -> Double { 0 }
|
||
public func saveZoom(_ level: Double) {}
|
||
}
|
||
|
||
/// UserDefaults-backed layouts, next to the session and the agent split.
|
||
public struct UserDefaultsLayoutStore: LayoutPersisting, @unchecked Sendable {
|
||
private let defaults: UserDefaults
|
||
|
||
public init(defaults: UserDefaults = .standard) {
|
||
self.defaults = defaults
|
||
}
|
||
|
||
public func load(root: String) -> ColumnLayoutSnapshot? {
|
||
defaults.data(forKey: ColumnLayoutSnapshot.key(root: root)).flatMap { try? JSONDecoder().decode(ColumnLayoutSnapshot.self, from: $0) }
|
||
}
|
||
|
||
public func save(_ snapshot: ColumnLayoutSnapshot, root: String) {
|
||
if let data = try? JSONEncoder().encode(snapshot) { defaults.set(data, forKey: ColumnLayoutSnapshot.key(root: root)) }
|
||
}
|
||
|
||
public func loadZoom() -> Double { defaults.double(forKey: "helder.zoom") }
|
||
public func saveZoom(_ level: Double) { defaults.set(level, forKey: "helder.zoom") }
|
||
}
|
||
|
||
@Observable
|
||
@MainActor
|
||
public final class LayoutStore {
|
||
public private(set) var widths = ColumnWidths(explorer: 288, agent: 480)
|
||
/// The agent pane's share of the right column; persisted across launches.
|
||
public private(set) var agentFraction: Double
|
||
public private(set) var windowWidth: Double = 1680
|
||
/// The title bar's pane toggles; ⌘1–⌘3 flip them.
|
||
public private(set) var visibility = ColumnVisibility.all
|
||
/// ⌘G: the full-screen Git view over the whole window. Not persisted; the columns stay mounted under it.
|
||
public private(set) var isGitViewOpen = false
|
||
/// ⌘L: columns re-fit on resize and focus. A manual splitter drag turns this off.
|
||
public private(set) var fluid = true
|
||
/// ⌘.: dotfiles show in the tree and the search.
|
||
public private(set) var showHidden = false
|
||
public private(set) var focusZone = FocusZone.neutral
|
||
public private(set) var activePanel: Panel?
|
||
/// macOS hides the traffic lights in full screen, so the title bar reclaims their inset.
|
||
public var isFullScreen = false
|
||
/// View → Zoom: half steps, each whole step 20% larger, as Chromium zooms.
|
||
public private(set) var zoomLevel: Double = 0
|
||
|
||
@ObservationIgnored private let session: SessionStoring
|
||
@ObservationIgnored private let persistence: LayoutPersisting?
|
||
@ObservationIgnored private var root: String?
|
||
|
||
public init(session: SessionStoring, persistence: LayoutPersisting? = nil) {
|
||
self.session = session
|
||
self.persistence = persistence
|
||
agentFraction = session.loadLayout().agentFraction
|
||
zoomLevel = Self.clampZoom(persistence?.loadZoom() ?? 0)
|
||
}
|
||
|
||
public static let zoomRange: ClosedRange<Double> = -3...5
|
||
|
||
/// The factor the code and terminal text sizes are multiplied by.
|
||
public var textScale: Double { pow(1.2, zoomLevel) }
|
||
|
||
public func zoom(_ step: ZoomStep) {
|
||
let next: Double = switch step {
|
||
case .reset: 0
|
||
case .bigger: zoomLevel + 0.5
|
||
case .smaller: zoomLevel - 0.5
|
||
}
|
||
let clamped = Self.clampZoom(next)
|
||
guard clamped != zoomLevel else { return }
|
||
zoomLevel = clamped
|
||
persistence?.saveZoom(clamped)
|
||
}
|
||
|
||
private static func clampZoom(_ level: Double) -> Double {
|
||
min(max(level, zoomRange.lowerBound), zoomRange.upperBound)
|
||
}
|
||
|
||
/// Loads the project's saved columns; a project without one starts fluid with every column shown.
|
||
public func restore(root: String) {
|
||
self.root = root
|
||
activePanel = nil
|
||
focusZone = .neutral
|
||
isGitViewOpen = false
|
||
let saved = persistence?.load(root: root)
|
||
visibility = saved?.visibility ?? .all
|
||
fluid = saved?.fluid ?? true
|
||
if let saved, !saved.fluid {
|
||
widths = ColumnWidths(explorer: clamp(saved.explorerWidth, ColumnLayout.explorerRange),
|
||
agent: max(saved.agentWidth, ColumnLayout.minEditorWidth))
|
||
}
|
||
refit()
|
||
}
|
||
|
||
public var snapshot: ColumnLayoutSnapshot {
|
||
ColumnLayoutSnapshot(explorerWidth: widths.explorer, agentWidth: widths.agent, fluid: fluid, visibility: visibility)
|
||
}
|
||
|
||
public func isVisible(_ panel: Panel) -> Bool {
|
||
switch panel {
|
||
case .explorer: visibility.explorer
|
||
case .editor: visibility.editor
|
||
case .terminal: visibility.agent
|
||
}
|
||
}
|
||
|
||
/// Shows or hides one column; a hidden column loses keyboard focus but keeps everything inside it.
|
||
public func togglePane(_ panel: Panel) {
|
||
setVisible(panel, !isVisible(panel))
|
||
}
|
||
|
||
public func setVisible(_ panel: Panel, _ visible: Bool) {
|
||
guard isVisible(panel) != visible else { return }
|
||
switch panel {
|
||
case .explorer: visibility.explorer = visible
|
||
case .editor: visibility.editor = visible
|
||
case .terminal: visibility.agent = visible
|
||
}
|
||
if !visible && activePanel == panel { activePanel = nil }
|
||
refit()
|
||
save()
|
||
}
|
||
|
||
public func openGitView() {
|
||
isGitViewOpen = true
|
||
}
|
||
|
||
public func closeGitView() {
|
||
isGitViewOpen = false
|
||
}
|
||
|
||
public func toggleGitView() {
|
||
isGitViewOpen.toggle()
|
||
}
|
||
|
||
public func toggleFluid() {
|
||
fluid.toggle()
|
||
refit()
|
||
save()
|
||
}
|
||
|
||
public func toggleHidden() {
|
||
showHidden.toggle()
|
||
}
|
||
|
||
public func windowResized(width: Double) {
|
||
windowWidth = width
|
||
refit()
|
||
}
|
||
|
||
/// Focus moves the fluid widths below 1600pt: the editor or the terminals get more room.
|
||
public func focus(_ panel: Panel) {
|
||
guard isVisible(panel) else { return }
|
||
activePanel = panel
|
||
switch panel {
|
||
case .editor: focusZone = .editor
|
||
case .terminal: focusZone = .terminal
|
||
default: break
|
||
}
|
||
refit()
|
||
}
|
||
|
||
/// Opening a file widens the editor without moving the keyboard cursor out of the list it came from.
|
||
public func setFocusZone(_ zone: FocusZone) {
|
||
focusZone = zone
|
||
refit()
|
||
}
|
||
|
||
public func dragExplorer(by delta: Double) {
|
||
fluid = false
|
||
widths.explorer = clamp(widths.explorer + delta, ColumnLayout.explorerRange)
|
||
}
|
||
|
||
/// The agent splitter sits on the column's left edge, so dragging right shrinks it.
|
||
public func dragAgent(by delta: Double) {
|
||
fluid = false
|
||
let maxWidth = ColumnLayout.maxAgentWidth(windowWidth: windowWidth, widths: widths, visible: visibility)
|
||
widths.agent = clamp(widths.agent - delta, ColumnLayout.minEditorWidth...maxWidth)
|
||
}
|
||
|
||
/// Call when a splitter drag ends: the locked widths are saved for this project.
|
||
public func endDrag() {
|
||
save()
|
||
}
|
||
|
||
public func setAgentFraction(_ fraction: Double) {
|
||
agentFraction = LayoutState.clampAgentFraction(fraction)
|
||
session.saveLayout(LayoutState(agentFraction: agentFraction))
|
||
}
|
||
|
||
/// Moves the agent/shell divider by `delta` points in a right column `height` points tall.
|
||
public func dragAgentSplit(by delta: Double, height: Double) {
|
||
guard height > 0 else { return }
|
||
setAgentFraction((agentFraction * height + delta) / height)
|
||
}
|
||
|
||
// MARK: Column geometry
|
||
|
||
/// The visible columns, left to right.
|
||
public var columns: [Panel] {
|
||
Panel.allCases.filter(isVisible)
|
||
}
|
||
|
||
/// The horizontal extent of a column in window coordinates; nil for a hidden column.
|
||
/// The editor is the flex column; with the editor hidden the agent column fills the rest.
|
||
public func span(of panel: Panel) -> ClosedRange<Double>? {
|
||
guard isVisible(panel) else { return nil }
|
||
let splitter = ColumnLayout.splitterWidth
|
||
let v = visibility
|
||
let flexStart = v.explorer ? widths.explorer + splitter : 0
|
||
let agentStart = v.editor ? max(flexStart, windowWidth - widths.agent) : flexStart
|
||
switch panel {
|
||
case .explorer: return 0...widths.explorer
|
||
case .editor: return flexStart...max(flexStart, v.agent ? agentStart - splitter : windowWidth)
|
||
case .terminal: return agentStart...max(agentStart, windowWidth)
|
||
}
|
||
}
|
||
|
||
/// The column under a window x; nil within `splitterMargin` of a divider, so a splitter drag never refocuses.
|
||
public func panel(atX x: Double, splitterMargin: Double = 0) -> Panel? {
|
||
columns.first { panel in
|
||
guard let span = span(of: panel) else { return false }
|
||
return x >= span.lowerBound + splitterMargin && x <= span.upperBound - splitterMargin
|
||
}
|
||
}
|
||
|
||
/// The column `step` places left (−1) or right (+1) of `panel`, clamped at the ends. No focus counts as the editor.
|
||
public func neighbour(of panel: Panel?, step: Int) -> Panel {
|
||
let list = columns
|
||
guard !list.isEmpty else { return panel ?? .editor }
|
||
let index = list.firstIndex(of: panel ?? .editor) ?? list.firstIndex(of: .editor) ?? 0
|
||
return list[min(max(index + step, 0), list.count - 1)]
|
||
}
|
||
|
||
private func refit() {
|
||
var next = fluid ? ColumnLayout.fluid(windowWidth: windowWidth, focus: focusZone) : widths
|
||
next = ColumnLayout.fitted(next, windowWidth: windowWidth, visible: visibility)
|
||
if next != widths { widths = next }
|
||
}
|
||
|
||
private func save() {
|
||
guard let root else { return }
|
||
persistence?.save(snapshot, root: root)
|
||
}
|
||
|
||
private func clamp(_ value: Double, _ range: ClosedRange<Double>) -> Double {
|
||
min(max(value, range.lowerBound), range.upperBound)
|
||
}
|
||
}
|