Sources/Orgstar/BufferTabs.swift

9954efcbf435b2e8d78a4b59bfbbbb67c1b6c379
orgstar/Sources/Orgstar/BufferTabs.swift

88 lines · 3677 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            .accessibilityElement()
26            .accessibilityLabel("Outline width")
27            .accessibilityValue("\(Int(width)) points")
28            .accessibilityAdjustableAction { direction in
29                let step = direction == .increment ? 20.0 : -20.0
30                width = min(max(width + step, range.lowerBound), range.upperBound)
31            }
32    }
33}
34
35/// A tab for each buffer the pane has shown: click to show it, × to close it, a dot for
36/// unsaved edits.
37struct BufferTabs: View {
38    let session: DocumentSession
39    let pane: EditorPane
40    let close: (String) -> Void
41    @Environment(\.orgTheme) private var theme
42    @Environment(\.colorScheme) private var colorScheme
43
44    var body: some View {
45        ScrollView(.horizontal, showsIndicators: false) {
46            HStack(spacing: 0) {
47                ForEach(pane.shown, id: \.self) { path in
48                    tab(path)
49                }
50            }
51        }
52        .frame(height: 28)
53        .background { Rectangle().fill(theme.color("modeline-background", in: colorScheme).map(AnyShapeStyle.init) ?? AnyShapeStyle(.bar)) }
54        .overlay(alignment: .bottom) { Divider() }
55    }
56
57    private func tab(_ path: String) -> some View {
58        let current = path == pane.path
59        return HStack(spacing: 2) {
60            Button(session.bufferName(path)) { session.open(path, in: pane) }
61                .buttonStyle(.plain)
62                .lineLimit(1)
63                .foregroundStyle(current ? .primary : .secondary)
64                .accessibilityAddTraits(current ? .isSelected : [])
65            Button {
66                close(path)
67            } label: {
68                Image(systemName: session.dirtyPaths.contains(path) ? "circle.fill" : "xmark")
69                    .font(.system(size: session.dirtyPaths.contains(path) ? 6 : 9, weight: .semibold))
70                    .frame(width: 20, height: 20)
71                    .contentShape(Rectangle())
72            }
73            .buttonStyle(.borderless)
74            .help("Close")
75            .accessibilityLabel("Close \(session.bufferName(path))" + (session.dirtyPaths.contains(path) ? ", unsaved" : ""))
76        }
77        .font(.callout)
78        .padding(.leading, 12)
79        .padding(.trailing, 6)
80        .frame(maxHeight: .infinity)
81        // A shape, not a color: a color background runs up under the toolbar.
82        .background { Rectangle().fill(current ? theme.color("background") ?? Color(nsColor: .textBackgroundColor) : .clear) }
83        .overlay(alignment: .trailing) { Divider() }
84        .contentShape(Rectangle())
85        .onTapGesture { session.open(path, in: pane) }
86        .help(path)
87    }
88}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form