87 lines
4.0 KiB
Swift
87 lines
4.0 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 three fixed columns; the editor takes the remainder.
|
|
public struct ColumnWidths: Sendable, Equatable {
|
|
public var sourceControl: Double
|
|
public var explorer: Double
|
|
public var agent: Double
|
|
|
|
public init(sourceControl: Double, explorer: Double, agent: Double) {
|
|
self.sourceControl = sourceControl
|
|
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 sourceControlRange = 160.0...460.0
|
|
public static let explorerRange = 160.0...520.0
|
|
|
|
/// Fluid widths for a window. Roomy: 13.4% · 16.4% · flex · 27.3% (the design's
|
|
/// 236 · 288 · flex · 480 on a 1760 frame). Tight: 15% · 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(sourceControl: (w * 0.134).rounded(), explorer: (w * 0.164).rounded(), agent: (w * 0.273).rounded())
|
|
}
|
|
let agentShare = focus == .terminal ? 0.5 : focus == .editor ? 0.2 : 0.3
|
|
return ColumnWidths(sourceControl: (w * 0.15).rounded(), 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.sourceControl ? widths.sourceControl : 0) + (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(
|
|
sourceControl: visible.sourceControl ? (widths.sourceControl * k).rounded(.down) : widths.sourceControl,
|
|
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.sourceControl ? widths.sourceControl : 0) + (visible.explorer ? widths.explorer : 0)
|
|
return max(minEditorWidth, windowWidth - Double(visible.splitterCount) * splitterWidth - others - minEditorWidth)
|
|
}
|
|
}
|
|
|
|
/// Which of the four columns are shown. A hidden column keeps its width and its contents.
|
|
public struct ColumnVisibility: Sendable, Equatable {
|
|
public var sourceControl: Bool
|
|
public var explorer: Bool
|
|
public var editor: Bool
|
|
public var agent: Bool
|
|
|
|
public init(sourceControl: Bool = true, explorer: Bool = true, editor: Bool = true, agent: Bool = true) {
|
|
self.sourceControl = sourceControl
|
|
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, [sourceControl, explorer, editor, agent].filter { $0 }.count - 1)
|
|
}
|
|
}
|