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