Files
helder/Sources/HelderUI/Explorer/RowParts.swift
T
2026-10-30 13:21:45 +01:00

211 lines
7.7 KiB
Swift

// Pieces of the Explorer rows.
import AppKit
import HelderCore
import SwiftUI
enum RowStyle {
static let folderFill = Color(hex: 0x7A838C, alpha: 0.12)
static let openFolderFill = Color(hex: 0x7A838C, alpha: 0.16)
static let openFolderFrontFill = Color(hex: 0x7A838C, alpha: 0.22)
static let changeLetterRole = TypeRole(.monoSemiBold, 10)
static let gitLetterRole = TypeRole(.monoSemiBold, 11)
static let dirHeadRole = TypeRole(.monoRegular, 11)
static let groupLabelRole = Typography.panelLabel
}
// MARK: Glyphs (drawn on 16-unit grids)
struct ChevronGlyph: View {
let open: Bool
var body: some View {
Path { p in
p.move(to: CGPoint(x: 3.5, y: 2))
p.addLine(to: CGPoint(x: 7, y: 5))
p.addLine(to: CGPoint(x: 3.5, y: 8))
}
.stroke(style: StrokeStyle(lineWidth: 1.4, lineCap: .round, lineJoin: .round))
.frame(width: 10, height: 10)
.scaleEffect(0.9)
.rotationEffect(.degrees(open ? 90 : 0))
}
}
struct FolderGlyph: View {
let open: Bool
var size: CGFloat = 14
var body: some View {
Canvas { context, canvas in
let k = canvas.width / 16
func pt(_ x: CGFloat, _ y: CGFloat) -> CGPoint { CGPoint(x: x * k, y: y * k) }
let line = StrokeStyle(lineWidth: 1.1, lineJoin: .round)
if open {
var back = Path()
back.move(to: pt(2, 12.5)); back.addLine(to: pt(2, 4.5)); back.addLine(to: pt(5.6, 4.5))
back.addLine(to: pt(6.8, 5.9)); back.addLine(to: pt(13.5, 5.9)); back.addLine(to: pt(13.5, 7.4))
context.fill(back, with: .color(RowStyle.openFolderFill))
context.stroke(back, with: .color(Palette.text4), style: line)
var front = Path()
front.move(to: pt(1.4, 12.6)); front.addLine(to: pt(3.3, 7.5)); front.addLine(to: pt(14.7, 7.5))
front.addLine(to: pt(12.8, 12.6)); front.closeSubpath()
context.fill(front, with: .color(RowStyle.openFolderFrontFill))
context.stroke(front, with: .color(Palette.text4), style: line)
} else {
var shape = Path()
shape.move(to: pt(1.5, 4.5)); shape.addLine(to: pt(5.5, 4.5)); shape.addLine(to: pt(6.7, 5.9))
shape.addLine(to: pt(14, 5.9)); shape.addLine(to: pt(14, 13)); shape.addLine(to: pt(1.5, 13))
shape.closeSubpath()
context.fill(shape, with: .color(RowStyle.folderFill))
context.stroke(shape, with: .color(Palette.text4), style: line)
}
}
.frame(width: size, height: size)
}
}
/// The plus, minus, push and arrow-up icons.
enum IconGlyph {
case plus, minus, push, arrowUp
func path(in size: CGFloat) -> Path {
switch self {
case .plus:
let k = size / 14
var p = Path()
p.move(to: CGPoint(x: 7 * k, y: 2.5 * k)); p.addLine(to: CGPoint(x: 7 * k, y: 11.5 * k))
p.move(to: CGPoint(x: 2.5 * k, y: 7 * k)); p.addLine(to: CGPoint(x: 11.5 * k, y: 7 * k))
return p
case .minus:
let k = size / 14
var p = Path()
p.move(to: CGPoint(x: 2.5 * k, y: 7 * k)); p.addLine(to: CGPoint(x: 11.5 * k, y: 7 * k))
return p
case .push:
let k = size / 16
var p = Path()
p.move(to: CGPoint(x: 8 * k, y: 13 * k)); p.addLine(to: CGPoint(x: 8 * k, y: 4 * k))
p.move(to: CGPoint(x: 4.5 * k, y: 7.5 * k)); p.addLine(to: CGPoint(x: 8 * k, y: 4 * k))
p.addLine(to: CGPoint(x: 11.5 * k, y: 7.5 * k))
p.move(to: CGPoint(x: 3.5 * k, y: 2.5 * k)); p.addLine(to: CGPoint(x: 12.5 * k, y: 2.5 * k))
return p
case .arrowUp:
let k = size / 16
var p = Path()
p.move(to: CGPoint(x: 8 * k, y: 13 * k)); p.addLine(to: CGPoint(x: 8 * k, y: 3.5 * k))
p.move(to: CGPoint(x: 4.5 * k, y: 7 * k)); p.addLine(to: CGPoint(x: 8 * k, y: 3.5 * k))
p.addLine(to: CGPoint(x: 11.5 * k, y: 7 * k))
return p
}
}
}
struct IconView: View {
let glyph: IconGlyph
var size: CGFloat = 13
var body: some View {
glyph.path(in: size)
.stroke(style: StrokeStyle(lineWidth: glyph == .push || glyph == .arrowUp ? 1.4 : 1.5, lineCap: .round, lineJoin: .round))
.frame(width: size, height: size)
}
}
/// A borderless icon button that turns amber under the pointer.
struct QuickActionButton: View {
let glyph: IconGlyph
let help: String
let action: () -> Void
@State private var hovered = false
var body: some View {
Button(action: action) {
IconView(glyph: glyph)
.foregroundStyle(hovered ? Palette.amber : Palette.text4)
.frame(width: 18, height: 18)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.pointerHand()
.onHover { hovered = $0 }
.help(help)
}
}
// MARK: Right click
/// Reports a right click (or control-click) with its point in window coordinates, top-left origin.
/// It only claims right clicks, so left clicks and hover still reach the SwiftUI row beneath.
struct RightClickArea: NSViewRepresentable {
let onRightClick: (CGPoint) -> Void
func makeNSView(context: Context) -> CatcherView {
let view = CatcherView()
view.onRightClick = onRightClick
return view
}
func updateNSView(_ view: CatcherView, context: Context) {
view.onRightClick = onRightClick
}
final class CatcherView: NSView {
var onRightClick: ((CGPoint) -> Void)?
override func hitTest(_ point: NSPoint) -> NSView? {
guard let event = NSApp.currentEvent, Self.isContextClick(event) else { return nil }
return super.hitTest(point)
}
override func rightMouseDown(with event: NSEvent) { report(event) }
override func mouseDown(with event: NSEvent) {
if Self.isContextClick(event) { report(event) } else { super.mouseDown(with: event) }
}
private func report(_ event: NSEvent) {
guard let content = window?.contentView else { return }
let p = content.convert(event.locationInWindow, from: nil)
onRightClick?(CGPoint(x: p.x, y: content.isFlipped ? p.y : content.bounds.height - p.y))
}
private static func isContextClick(_ event: NSEvent) -> Bool {
event.type == .rightMouseDown || (event.type == .leftMouseDown && event.modifierFlags.contains(.control))
}
}
}
/// Reads a view's frame in window coordinates, for anchoring menus to a column's right edge.
struct GlobalFrameReader: View {
@Binding var frame: CGRect
var body: some View {
GeometryReader { proxy in
Color.clear
.onAppear { frame = proxy.frame(in: .global) }
.onChange(of: proxy.frame(in: .global)) { _, new in frame = new }
}
}
}
/// Tells the model where a list row sits in the window, for the ⌘→ menu; nil once the row is gone.
private struct RowFrameReporter: ViewModifier {
let key: RowKey
let model: WorkspaceModel
func body(content: Content) -> some View {
content.background(GeometryReader { proxy in
Color.clear
.onAppear { model.rowFrameChanged(key, proxy.frame(in: .global)) }
.onChange(of: proxy.frame(in: .global)) { _, frame in model.rowFrameChanged(key, frame) }
.onDisappear { model.rowFrameChanged(key, nil) }
})
}
}
extension View {
func reportsRowFrame(_ key: RowKey, to model: WorkspaceModel) -> some View {
modifier(RowFrameReporter(key: key, model: model))
}
}