restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
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 sourceControl
|
||||
case explorer
|
||||
case editor
|
||||
case terminal
|
||||
}
|
||||
|
||||
/// The column layout one project remembers between launches.
|
||||
public struct ColumnLayoutSnapshot: Codable, Sendable, Equatable {
|
||||
public var sourceControlWidth: Double
|
||||
public var explorerWidth: Double
|
||||
public var agentWidth: Double
|
||||
public var fluid: Bool
|
||||
public var gitVisible: Bool
|
||||
|
||||
public init(sourceControlWidth: Double, explorerWidth: Double, agentWidth: Double, fluid: Bool, gitVisible: Bool) {
|
||||
self.sourceControlWidth = sourceControlWidth
|
||||
self.explorerWidth = explorerWidth
|
||||
self.agentWidth = agentWidth
|
||||
self.fluid = fluid
|
||||
self.gitVisible = gitVisible
|
||||
}
|
||||
|
||||
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(sourceControl: 236, 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
|
||||
/// ⌘G: the Source Control column is shown.
|
||||
public private(set) var gitVisible = true
|
||||
/// ⌘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
|
||||
let saved = persistence?.load(root: root)
|
||||
gitVisible = saved?.gitVisible ?? true
|
||||
fluid = saved?.fluid ?? true
|
||||
if let saved, !saved.fluid {
|
||||
widths = ColumnWidths(sourceControl: clamp(saved.sourceControlWidth, ColumnLayout.sourceControlRange),
|
||||
explorer: clamp(saved.explorerWidth, ColumnLayout.explorerRange),
|
||||
agent: max(saved.agentWidth, ColumnLayout.minEditorWidth))
|
||||
}
|
||||
refit()
|
||||
}
|
||||
|
||||
public var snapshot: ColumnLayoutSnapshot {
|
||||
ColumnLayoutSnapshot(sourceControlWidth: widths.sourceControl, explorerWidth: widths.explorer,
|
||||
agentWidth: widths.agent, fluid: fluid, gitVisible: gitVisible)
|
||||
}
|
||||
|
||||
public func toggleGit() {
|
||||
gitVisible.toggle()
|
||||
if !gitVisible && activePanel == .sourceControl { activePanel = nil }
|
||||
refit()
|
||||
save()
|
||||
}
|
||||
|
||||
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) {
|
||||
if panel == .sourceControl && !gitVisible { 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 dragSourceControl(by delta: Double) {
|
||||
fluid = false
|
||||
widths.sourceControl = clamp(widths.sourceControl + delta, ColumnLayout.sourceControlRange)
|
||||
}
|
||||
|
||||
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, sourceControlVisible: gitVisible)
|
||||
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] {
|
||||
gitVisible ? Panel.allCases : [.explorer, .editor, .terminal]
|
||||
}
|
||||
|
||||
/// The horizontal extent of a column in window coordinates; nil for a hidden column.
|
||||
public func span(of panel: Panel) -> ClosedRange<Double>? {
|
||||
let splitter = ColumnLayout.splitterWidth
|
||||
let sc = gitVisible ? widths.sourceControl + splitter : 0
|
||||
let editorStart = sc + widths.explorer + splitter
|
||||
let agentStart = max(editorStart, windowWidth - widths.agent)
|
||||
switch panel {
|
||||
case .sourceControl: return gitVisible ? 0...widths.sourceControl : nil
|
||||
case .explorer: return sc...(sc + widths.explorer)
|
||||
case .editor: return editorStart...max(editorStart, agentStart - splitter)
|
||||
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
|
||||
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, sourceControlVisible: gitVisible)
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user