Sources/Orgstar/BufferTabs.swift

d85099bc95bf1766aee990d94671dacda0c0686e
orgstar/Sources/Orgstar/BufferTabs.swift history · blame · raw

78 lines · 3253 bytes

 1import AppKit
 2import OrgApp
 3import SwiftUI
 4
 5/// A divider that resizes the pane before it by dragging. An `HSplitView` inside the window's
 6/// split view loops on its size constraints in short windows, so this sets a width instead.
 7struct PaneDivider: View {
 8    @Binding var width: Double
 9    let range: ClosedRange<Double>
10    @State private var start: Double?
11
12    var body: some View {
13        Rectangle()
14            .fill(Color(nsColor: .separatorColor))
15            .frame(width: 1)
16            .frame(maxHeight: .infinity)
17            .overlay(Color.clear.frame(width: 7).contentShape(Rectangle()).onHover { inside in
18                if inside { NSCursor.resizeLeftRight.push() } else { NSCursor.pop() }
19            }
20            .gesture(DragGesture(minimumDistance: 1).onChanged { drag in
21                let base = start ?? width
22                start = base
23                width = min(max(base + drag.translation.width, range.lowerBound), range.upperBound)
24            }.onEnded { _ in start = nil }))
25            .accessibilityHidden(true)
26    }
27}
28
29/// A tab for each buffer the pane has shown: click to show it, × to close it, a dot for
30/// unsaved edits.
31struct BufferTabs: View {
32    let session: DocumentSession
33    let pane: EditorPane
34    @Environment(\.orgTheme) private var theme
35
36    var body: some View {
37        ScrollView(.horizontal, showsIndicators: false) {
38            HStack(spacing: 0) {
39                ForEach(pane.shown, id: \.self) { path in
40                    tab(path)
41                }
42            }
43        }
44        .frame(height: 28)
45        .background { Rectangle().fill(theme.color("modeline-background").map(AnyShapeStyle.init) ?? AnyShapeStyle(.bar)) }
46        .overlay(alignment: .bottom) { Divider() }
47    }
48
49    private func tab(_ path: String) -> some View {
50        let current = path == pane.path
51        return HStack(spacing: 6) {
52            Text(session.bufferName(path))
53                .lineLimit(1)
54                .foregroundStyle(current ? .primary : .secondary)
55                .accessibilityAddTraits(current ? [.isButton, .isSelected] : .isButton)
56                .accessibilityAction { session.open(path, in: pane) }
57            Button {
58                if UnsavedChanges.resolve(session, paths: [path]) { session.kill(path) }
59            } label: {
60                Image(systemName: session.dirtyPaths.contains(path) ? "circle.fill" : "xmark")
61                    .font(.system(size: session.dirtyPaths.contains(path) ? 6 : 9, weight: .semibold))
62                    .frame(width: 12, height: 12)
63            }
64            .buttonStyle(.borderless)
65            .help("Close")
66            .accessibilityLabel("Close \(session.bufferName(path))" + (session.dirtyPaths.contains(path) ? ", unsaved" : ""))
67        }
68        .font(.callout)
69        .padding(.horizontal, 12)
70        .frame(maxHeight: .infinity)
71        // A shape, not a color: a color background runs up under the toolbar.
72        .background { Rectangle().fill(current ? theme.color("background") ?? Color(nsColor: .textBackgroundColor) : .clear) }
73        .overlay(alignment: .trailing) { Divider() }
74        .contentShape(Rectangle())
75        .onTapGesture { session.open(path, in: pane) }
76        .help(path)
77    }
78}