Sources/Orgstar/BufferTabs.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Sources/Orgstar/BufferTabs.swift history · blame · raw

80 lines · 3297 bytes

3 symbols in this file
 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    let close: (String) -> Void
35    @Environment(\.orgTheme) private var theme
36    @Environment(\.colorScheme) private var colorScheme
37
38    var body: some View {
39        ScrollView(.horizontal, showsIndicators: false) {
40            HStack(spacing: 0) {
41                ForEach(pane.shown, id: \.self) { path in
42                    tab(path)
43                }
44            }
45        }
46        .frame(height: 28)
47        .background { Rectangle().fill(theme.color("modeline-background", in: colorScheme).map(AnyShapeStyle.init) ?? AnyShapeStyle(.bar)) }
48        .overlay(alignment: .bottom) { Divider() }
49    }
50
51    private func tab(_ path: String) -> some View {
52        let current = path == pane.path
53        return HStack(spacing: 6) {
54            Text(session.bufferName(path))
55                .lineLimit(1)
56                .foregroundStyle(current ? .primary : .secondary)
57                .accessibilityAddTraits(current ? [.isButton, .isSelected] : .isButton)
58                .accessibilityAction { session.open(path, in: pane) }
59            Button {
60                close(path)
61            } label: {
62                Image(systemName: session.dirtyPaths.contains(path) ? "circle.fill" : "xmark")
63                    .font(.system(size: session.dirtyPaths.contains(path) ? 6 : 9, weight: .semibold))
64                    .frame(width: 12, height: 12)
65            }
66            .buttonStyle(.borderless)
67            .help("Close")
68            .accessibilityLabel("Close \(session.bufferName(path))" + (session.dirtyPaths.contains(path) ? ", unsaved" : ""))
69        }
70        .font(.callout)
71        .padding(.horizontal, 12)
72        .frame(maxHeight: .infinity)
73        // A shape, not a color: a color background runs up under the toolbar.
74        .background { Rectangle().fill(current ? theme.color("background") ?? Color(nsColor: .textBackgroundColor) : .clear) }
75        .overlay(alignment: .trailing) { Divider() }
76        .contentShape(Rectangle())
77        .onTapGesture { session.open(path, in: pane) }
78        .help(path)
79    }
80}