Sources/OrgstarMobile/EditorSplitScreen.swift
185 lines · 8014 bytes
1#if os(iOS)
2import OrgApp
3import SwiftUI
4import UIKit
5
6/// An editor's panes in a scene, as a Mac window has them: one to start, split right or below
7/// (`app.split-right`, `app.split-below`), each showing a buffer in its own view, with its own
8/// caret and scroll, over the buffer every scene shares.
9@MainActor
10@Observable
11final class EditorSplit {
12 private(set) var layout: PaneLayout
13 private(set) var panes: [PaneID: EditorPane] = [:]
14 private(set) var paths: [PaneID: String] = [:]
15 var focused: PaneID
16 @ObservationIgnored nonisolated(unsafe) private var release: (() -> Void)?
17
18 init(path: String, session: DocumentSession, store: MobileEditorStore) {
19 let pane = session.makePane()
20 layout = .pane(pane.id)
21 focused = pane.id
22 panes = [pane.id: pane]
23 paths = [pane.id: path]
24 release = { [weak self, weak session] in
25 guard let self, let session else { return }
26 for (id, pane) in panes {
27 if let path = paths[id] { store.release(id, path: path) }
28 session.closePane(pane)
29 }
30 }
31 }
32
33 deinit {
34 guard let release else { return }
35 Task { @MainActor in release() }
36 }
37
38 var isSplit: Bool { layout.panes.count > 1 }
39
40 /// Runs a pane command; returns why it did nothing, or nil.
41 func run(_ id: String, session: DocumentSession, store: MobileEditorStore) -> String? {
42 switch id {
43 case "app.split-right", "app.split-below":
44 guard let path = paths[focused] else { return nil }
45 let pane = session.makePane()
46 panes[pane.id] = pane
47 paths[pane.id] = path
48 layout = layout.splitting(focused, axis: id == "app.split-right" ? .horizontal : .vertical, new: pane.id)
49 case "app.pane-next":
50 focused = layout.next(after: focused)
51 case "app.pane-close":
52 guard isSplit else { return String(localized: "This is the only pane.", comment: "Close Pane with one pane") }
53 close(focused, session: session, store: store)
54 case "app.pane-only":
55 for id in layout.panes where id != focused { close(id, session: session, store: store) }
56 default:
57 return nil
58 }
59 return nil
60 }
61
62 /// Sets the first side's share of the split between `before` and `after`, as dragging its
63 /// divider does.
64 func setFraction(_ fraction: Double, ofSplitBetween before: PaneID, and after: PaneID) {
65 layout = layout.settingFraction(fraction, ofSplitBetween: before, and: after)
66 }
67
68 private func close(_ id: PaneID, session: DocumentSession, store: MobileEditorStore) {
69 guard let rest = layout.removing(id) else { return }
70 let next = layout.next(after: id)
71 layout = rest
72 if let path = paths[id] { store.release(id, path: path) }
73 if let pane = panes[id] { session.closePane(pane) }
74 panes[id] = nil
75 paths[id] = nil
76 if focused == id { focused = next }
77 }
78}
79
80/// The editor route's screen: its panes, with the focused one's title and toolbar.
81struct EditorSplitScreen: View {
82 let path: String
83 let offset: Int?
84 let workspace: WorkspaceModel
85 let session: DocumentSession
86 @State private var split: EditorSplit?
87 @State private var message: String?
88 @Environment(MobileModel.self) private var model
89
90 var body: some View {
91 Group {
92 if let split {
93 layoutView(split.layout, split: split)
94 .navigationTitle(split.paths[split.focused].map { session.bufferName($0) + (session.dirtyPaths.contains($0) ? " •" : "") } ?? "")
95 .navigationBarTitleDisplayMode(.inline)
96 // The menu bar's editor commands act on the focused pane.
97 .focusedSceneValue(\.editorActions, split.paths[split.focused].flatMap { model.editors.existing($0) }.map(EditorActions.init))
98 } else {
99 ProgressView()
100 }
101 }
102 .onAppear {
103 if split == nil { split = EditorSplit(path: path, session: session, store: model.editors) }
104 }
105 }
106
107 private func layoutView(_ layout: PaneLayout, split: EditorSplit) -> AnyView {
108 switch layout {
109 case .pane(let id):
110 guard let pane = split.panes[id], let file = split.paths[id] else { return AnyView(EmptyView()) }
111 return AnyView(
112 EditorScreen(
113 path: file, offset: id == split.layout.panes.first ? offset : nil, workspace: workspace, session: session,
114 givenPane: pane, inSplit: split.isSplit, isFocused: split.focused == id,
115 onFocus: { split.focused = id },
116 onPaneCommand: { split.run($0, session: session, store: model.editors) }
117 )
118 .id(id)
119 .overlay(alignment: .top) {
120 if split.isSplit, split.focused == id { Rectangle().fill(Color.accentColor).frame(height: 2) }
121 }
122 )
123 case .split(let axis, let first, let second, let fraction):
124 return AnyView(GeometryReader { geometry in
125 let total = axis == .horizontal ? geometry.size.width : geometry.size.height
126 let firstSize = max(120, min(max(120, total - 120), total * fraction))
127 let divider = PaneDivider(axis: axis, firstSize: firstSize, total: total) { size in
128 split.setFraction(size / max(total, 1), ofSplitBetween: first.panes[first.panes.count - 1], and: second.panes[0])
129 }
130 if axis == .horizontal {
131 HStack(spacing: 0) {
132 layoutView(first, split: split).frame(width: firstSize)
133 divider
134 layoutView(second, split: split).frame(maxWidth: .infinity)
135 }
136 } else {
137 VStack(spacing: 0) {
138 layoutView(first, split: split).frame(height: firstSize)
139 divider
140 layoutView(second, split: split).frame(maxHeight: .infinity)
141 }
142 }
143 })
144 }
145 }
146}
147
148/// The line between two panes, with a wider area to drag; dragging resizes the first pane.
149private struct PaneDivider: View {
150 let axis: PaneLayout.Axis
151 let firstSize: CGFloat
152 let total: CGFloat
153 let resize: (CGFloat) -> Void
154 @State private var start: CGFloat?
155
156 var body: some View {
157 let horizontal = axis == .horizontal
158 Rectangle()
159 .fill(Color(uiColor: .separator))
160 .frame(width: horizontal ? 1 : nil, height: horizontal ? nil : 1)
161 .frame(maxWidth: horizontal ? 1 : .infinity, maxHeight: horizontal ? .infinity : 1)
162 .overlay {
163 Capsule().fill(Color(uiColor: .tertiaryLabel))
164 .frame(width: horizontal ? 4 : 36, height: horizontal ? 36 : 4)
165 }
166 .overlay {
167 Color.clear
168 .frame(width: horizontal ? 20 : nil, height: horizontal ? nil : 20)
169 .contentShape(Rectangle())
170 .gesture(DragGesture(minimumDistance: 1).onChanged { drag in
171 let base = start ?? firstSize
172 start = base
173 resize(base + (horizontal ? drag.translation.width : drag.translation.height))
174 }.onEnded { _ in start = nil })
175 .hoverEffect(.highlight)
176 }
177 .accessibilityElement()
178 .accessibilityLabel(Text("Pane Divider"))
179 .accessibilityValue(Text(Double(firstSize / max(total, 1)), format: .percent.precision(.fractionLength(0))))
180 .accessibilityAdjustableAction { direction in
181 resize(firstSize + (direction == .increment ? 0.1 : -0.1) * total)
182 }
183 }
184}
185#endif