79 lines
3.0 KiB
Swift
79 lines
3.0 KiB
Swift
/// Per-project editor session, restored on launch when `session.restoreOnLaunch` is on.
|
|
public struct SessionState: Codable, Sendable, Equatable {
|
|
public var active: String?
|
|
public var modes: [String: ViewMode]
|
|
/// Which git row opened each file; only the Diff overlay follows it.
|
|
public var sides: [String: DiffSide]
|
|
|
|
public init(active: String? = nil, modes: [String: ViewMode] = [:], sides: [String: DiffSide] = [:]) {
|
|
self.active = active
|
|
self.modes = modes
|
|
self.sides = sides
|
|
}
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case active
|
|
case modes = "tabMode"
|
|
case sides = "tabSide"
|
|
}
|
|
|
|
/// A stored mode this build does not know is dropped, not the whole map.
|
|
public init(from decoder: Decoder) throws {
|
|
let c = try decoder.container(keyedBy: CodingKeys.self)
|
|
active = try? c.decodeIfPresent(String.self, forKey: .active)
|
|
let raw = (try? c.decodeIfPresent([String: String].self, forKey: .modes)) ?? [:]
|
|
modes = raw.compactMapValues(ViewMode.init(rawValue:))
|
|
sides = (try? c.decodeIfPresent([String: DiffSide].self, forKey: .sides)) ?? [:]
|
|
}
|
|
|
|
/// The storage key; one session per project root.
|
|
public static func key(root: String) -> String { "helder.session:" + root }
|
|
}
|
|
|
|
/// Window-wide layout that survives a restart.
|
|
public struct LayoutState: Codable, Sendable, Equatable {
|
|
/// The agent pane's share of the right column's height.
|
|
public var agentFraction: Double
|
|
|
|
public init(agentFraction: Double = LayoutState.defaultAgentFraction) {
|
|
self.agentFraction = LayoutState.clampAgentFraction(agentFraction)
|
|
}
|
|
|
|
public static let key = "helder.topFrac"
|
|
public static let defaultAgentFraction = 0.52
|
|
public static let agentFractionRange = 0.18...0.82
|
|
|
|
public static func clampAgentFraction(_ value: Double) -> Double {
|
|
min(max(value, agentFractionRange.lowerBound), agentFractionRange.upperBound)
|
|
}
|
|
}
|
|
|
|
public struct RecentProject: Codable, Sendable, Equatable, Identifiable {
|
|
public var path: String
|
|
public var name: String
|
|
public var id: String { path }
|
|
|
|
public init(path: String, name: String) {
|
|
self.path = path
|
|
self.name = name
|
|
}
|
|
}
|
|
|
|
public enum RecentLists {
|
|
/// Recent files per project, in `.helder/recent.json`.
|
|
public static let maxFiles = 100
|
|
/// Recent projects, machine-wide.
|
|
public static let maxProjects = 20
|
|
public static let recentFilesName = "recent.json"
|
|
|
|
/// Moves `path` to the front, de-duplicated and capped.
|
|
public static func pushing(_ path: String, onto list: [String], limit: Int = maxFiles) -> [String] {
|
|
Array(([path] + list.filter { $0 != path }).prefix(limit))
|
|
}
|
|
|
|
/// Moves a project to the front, keyed on its path, capped at 20.
|
|
public static func pushing(_ project: RecentProject, onto list: [RecentProject]) -> [RecentProject] {
|
|
Array(([project] + list.filter { $0.path != project.path }).prefix(maxProjects))
|
|
}
|
|
}
|