Files
helder/Sources/HelderCore/Session/ColumnLayout.swift
T
2026-10-30 13:21:45 +01:00

80 lines
3.4 KiB
Swift

import Foundation
/// Which side of the window has focus; drives the fluid widths below 1600pt.
public enum FocusZone: String, Sendable, Equatable {
case neutral
case editor
case terminal
}
/// Pixel widths of the two fixed columns; the editor takes the remainder.
public struct ColumnWidths: Sendable, Equatable {
public var explorer: Double
public var agent: Double
public init(explorer: Double, agent: Double) {
self.explorer = explorer
self.agent = agent
}
}
public enum ColumnLayout {
/// At and above this width every column fits and focus changes nothing.
public static let roomyWidth = 1600.0
public static let minEditorWidth = 280.0
public static let splitterWidth = 1.0
public static let explorerRange = 160.0...520.0
/// Fluid widths for a window. Roomy: 16.4% · flex · 27.3% (the design's 288 · flex · 480
/// on a 1760 frame). Tight: 15%, and the agent column takes 30%, or 20% with the editor
/// focused, or 50% with a terminal focused.
public static func fluid(windowWidth w: Double, focus: FocusZone) -> ColumnWidths {
if w >= roomyWidth {
return ColumnWidths(explorer: (w * 0.164).rounded(), agent: (w * 0.273).rounded())
}
let agentShare = focus == .terminal ? 0.5 : focus == .editor ? 0.2 : 0.3
return ColumnWidths(explorer: (w * 0.15).rounded(), agent: (w * agentShare).rounded())
}
/// Scales the fixed columns down so the flex column keeps `minEditorWidth`; unchanged when they fit.
/// Hidden columns keep their width for when they come back.
public static func fitted(_ widths: ColumnWidths, windowWidth: Double, visible: ColumnVisibility) -> ColumnWidths {
let agentIsFixed = visible.agent && visible.editor
let flexMinimum = visible.editor || visible.agent ? minEditorWidth : 0
let budget = windowWidth - Double(visible.splitterCount) * splitterWidth - flexMinimum
let total = (visible.explorer ? widths.explorer : 0) + (agentIsFixed ? widths.agent : 0)
guard total > budget, total > 0 else { return widths }
let k = max(0, budget) / total
return ColumnWidths(
explorer: visible.explorer ? (widths.explorer * k).rounded(.down) : widths.explorer,
agent: agentIsFixed ? (widths.agent * k).rounded(.down) : widths.agent
)
}
/// The widest the agent column may be dragged: until the editor would drop below its minimum.
public static func maxAgentWidth(windowWidth: Double, widths: ColumnWidths, visible: ColumnVisibility) -> Double {
let others = visible.explorer ? widths.explorer : 0
return max(minEditorWidth, windowWidth - Double(visible.splitterCount) * splitterWidth - others - minEditorWidth)
}
}
/// Which of the three columns are shown. A hidden column keeps its width and its contents.
public struct ColumnVisibility: Sendable, Equatable {
public var explorer: Bool
public var editor: Bool
public var agent: Bool
public init(explorer: Bool = true, editor: Bool = true, agent: Bool = true) {
self.explorer = explorer
self.editor = editor
self.agent = agent
}
public static let all = ColumnVisibility()
/// One splitter between each pair of neighbouring visible columns.
public var splitterCount: Int {
max(0, [explorer, editor, agent].filter { $0 }.count - 1)
}
}