238 lines
9.0 KiB
Swift
238 lines
9.0 KiB
Swift
import HelderCore
|
|
import SwiftUI
|
|
|
|
/// Column B: the file tree, flattened to its visible rows so thousands of files stay lazy,
|
|
/// with the tail block under it while a `.log` file is tailed.
|
|
public struct ExplorerColumn: View {
|
|
let model: WorkspaceModel
|
|
@State private var columnFrame = CGRect.zero
|
|
/// A cursor that follows the pointer must not scroll the list under it.
|
|
@State private var hoveredPath: String?
|
|
|
|
public init(model: WorkspaceModel) {
|
|
self.model = model
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
header
|
|
GeometryReader { proxy in
|
|
VStack(spacing: 0) {
|
|
if let store = model.explorer {
|
|
tree(store)
|
|
} else {
|
|
Spacer()
|
|
}
|
|
if let path = model.terminal.tailedPath {
|
|
TailBlock(model: model, path: path)
|
|
.frame(height: proxy.size.height * Self.tailFraction)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
.background(Palette.column(.explorer, active: isActive))
|
|
.background(GlobalFrameReader(frame: $columnFrame))
|
|
.simultaneousGesture(TapGesture().onEnded { model.pointerFocused(.explorer) })
|
|
}
|
|
|
|
private static let tailFraction = 0.4
|
|
|
|
private var isActive: Bool { model.layout.activePanel == .explorer }
|
|
private var showHidden: Bool { model.layout.showHidden }
|
|
|
|
private var header: some View {
|
|
HStack(spacing: 8) {
|
|
Text("Explorer").typeRole(Typography.panelLabel).foregroundStyle(Palette.text4)
|
|
Spacer(minLength: 8)
|
|
Text(model.name)
|
|
.typeRole(Typography.panelLabel)
|
|
.foregroundStyle(Palette.text4)
|
|
.lineLimit(1)
|
|
.truncationMode(.middle)
|
|
}
|
|
.padding(.horizontal, 12)
|
|
.frame(height: Metrics.panelHeader)
|
|
.hairline(.bottom)
|
|
}
|
|
|
|
private func tree(_ store: ExplorerStore) -> some View {
|
|
let statuses = model.sourceControl?.fileStatuses ?? [:]
|
|
return GeometryReader { viewport in
|
|
ScrollViewReader { proxy in
|
|
ScrollView {
|
|
LazyVStack(alignment: .leading, spacing: 0) {
|
|
ForEach(store.rows(showHidden: showHidden), id: \.path) { row in
|
|
TreeRowView(
|
|
row: row,
|
|
isOpen: store.expanded.contains(row.path),
|
|
selected: isSelected(row, store),
|
|
status: row.kind == .file ? statuses[row.path] : nil,
|
|
onClick: { click(row, store) },
|
|
onHover: { hoveredPath = row.path; store.selectedPath = row.path },
|
|
onContextMenu: { point in showMenu(for: ExplorerStore.target(for: row), at: point, store) }
|
|
)
|
|
.reportsRowFrame(.tree(row.path), to: model)
|
|
.id(row.path)
|
|
}
|
|
}
|
|
.padding(.top, 6)
|
|
.padding(.bottom, 14)
|
|
.frame(minHeight: viewport.size.height, alignment: .top)
|
|
// The bare space under the rows is the project folder itself.
|
|
.background(RightClickArea { point in showMenu(for: .projectRoot, at: point, store) })
|
|
}
|
|
.onChange(of: store.selectedPath) { _, path in
|
|
guard let path, path != hoveredPath else { return }
|
|
proxy.scrollTo(path)
|
|
}
|
|
}
|
|
}
|
|
.task { await store.loadIfNeeded() }
|
|
}
|
|
|
|
/// The open file, the keyboard cursor, or the row whose menu is open.
|
|
private func isSelected(_ row: TreeRow, _ store: ExplorerStore) -> Bool {
|
|
if case let .contextMenu(request) = model.overlay.current, request.target == ExplorerStore.target(for: row) {
|
|
return true
|
|
}
|
|
if isActive && store.selectedPath == row.path { return true }
|
|
return row.kind == .file && model.editor?.activePath == row.path
|
|
}
|
|
|
|
private func click(_ row: TreeRow, _ store: ExplorerStore) {
|
|
store.selectedPath = row.path
|
|
if row.kind == .directory {
|
|
store.toggle(row.path)
|
|
} else {
|
|
model.perform(.open(row.path, OpenOptions()))
|
|
}
|
|
}
|
|
|
|
/// Rows anchor the menu to the column's right edge; the empty space under them anchors it at the click.
|
|
private func showMenu(for target: ContextTarget, at point: CGPoint, _ store: ExplorerStore) {
|
|
model.pointerFocused(.explorer)
|
|
if target != .projectRoot { store.selectedPath = target.path }
|
|
let anchor = target == .projectRoot ? point : CGPoint(x: columnFrame.maxX, y: point.y)
|
|
model.perform(.showContextMenu(target, anchor: anchor))
|
|
}
|
|
}
|
|
|
|
/// `tail -f` on one log file: a header with the file name and a close button over a live terminal.
|
|
private struct TailBlock: View {
|
|
let model: WorkspaceModel
|
|
let path: String
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
HStack(spacing: 8) {
|
|
Text("Tail").typeRole(Typography.panelLabel).foregroundStyle(Palette.text4)
|
|
Spacer(minLength: 8)
|
|
Text(PathUtils.basename(path))
|
|
.typeRole(Typography.panelLabel)
|
|
.foregroundStyle(Palette.text4)
|
|
.lineLimit(1)
|
|
.truncationMode(.middle)
|
|
.help(path)
|
|
CloseTailButton { model.perform(.closeTail) }
|
|
}
|
|
.padding(.leading, 12)
|
|
.padding(.trailing, 8)
|
|
.frame(height: Metrics.panelHeader)
|
|
.hairline(.top)
|
|
.zIndex(1)
|
|
TerminalPane(kind: .tail, store: model.terminal, scale: model.layout.textScale)
|
|
.id(model.terminal.tailGeneration)
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct CloseTailButton: View {
|
|
let action: () -> Void
|
|
@State private var isHovered = false
|
|
|
|
var body: some View {
|
|
Button(action: action) {
|
|
GitIconView(icon: .x, size: 14)
|
|
.foregroundStyle(isHovered ? Palette.text1 : Palette.text4)
|
|
.padding(4)
|
|
.contentShape(Rectangle())
|
|
}
|
|
.buttonStyle(.plain)
|
|
.pointerHand()
|
|
.tracksHover($isHovered)
|
|
.tip("Stop tailing", edge: .trailing)
|
|
}
|
|
}
|
|
|
|
/// One tree row: indent, chevron, folder glyph or type badge, name, change letter.
|
|
private struct TreeRowView: View {
|
|
let row: TreeRow
|
|
let isOpen: Bool
|
|
let selected: Bool
|
|
let status: GitStatus?
|
|
let onClick: () -> Void
|
|
let onHover: () -> Void
|
|
let onContextMenu: (CGPoint) -> Void
|
|
@State private var hovered = false
|
|
|
|
private var isDirectory: Bool { row.kind == .directory }
|
|
|
|
var body: some View {
|
|
HStack(spacing: 7) {
|
|
Group {
|
|
if isDirectory {
|
|
ChevronGlyph(open: isOpen).foregroundStyle(isOpen ? Palette.amber : Palette.text4)
|
|
} else {
|
|
Color.clear
|
|
}
|
|
}
|
|
.frame(width: 10, height: 10)
|
|
if isDirectory {
|
|
FolderGlyph(open: isOpen).frame(width: 16, height: 16)
|
|
} else {
|
|
TypeBadge(path: row.path)
|
|
}
|
|
Text(PathUtils.basename(row.path))
|
|
.typeRole(selected ? Typography.rowSelected : Typography.row)
|
|
.strikethrough(status == .deleted, color: Palette.text4)
|
|
.foregroundStyle(labelColor)
|
|
.lineLimit(1)
|
|
.truncationMode(.tail)
|
|
Spacer(minLength: 0)
|
|
if let status {
|
|
Text(status.rawValue)
|
|
.typeRole(RowStyle.changeLetterRole)
|
|
.foregroundStyle(letterColor(status))
|
|
.padding(.leading, 8)
|
|
}
|
|
}
|
|
.padding(.leading, Metrics.explorerBaseIndent + CGFloat(row.depth) * Metrics.explorerIndent)
|
|
.padding(.trailing, 12)
|
|
.frame(height: Metrics.explorerRow)
|
|
.selectableRow(isSelected: selected, isHovered: hovered)
|
|
.contentShape(Rectangle())
|
|
.pointerHand()
|
|
.onTapGesture(perform: onClick)
|
|
.onHover { inside in
|
|
hovered = inside
|
|
if inside { onHover() }
|
|
}
|
|
.overlay(RightClickArea(onRightClick: onContextMenu))
|
|
.help(isDirectory ? "" : row.path)
|
|
}
|
|
|
|
private func letterColor(_ status: GitStatus) -> Color {
|
|
switch status {
|
|
case .deleted: Palette.amberDeep
|
|
case .unmerged: Palette.gitConflict
|
|
default: Palette.addedText
|
|
}
|
|
}
|
|
|
|
private var labelColor: Color {
|
|
if status == .deleted { return Palette.text4 }
|
|
return selected || (isDirectory && isOpen) ? Palette.text1 : Palette.text3
|
|
}
|
|
}
|