Sources/OrgstarMobile/EditorSplitScreen.swift
131 lines · 5057 bytes
5 symbols in this file
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; false when it does nothing here.
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 "Attempt to delete the only 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 private func close(_ id: PaneID, session: DocumentSession, store: MobileEditorStore) {
63 guard let rest = layout.removing(id) else { return }
64 let next = layout.next(after: id)
65 layout = rest
66 if let path = paths[id] { store.release(id, path: path) }
67 if let pane = panes[id] { session.closePane(pane) }
68 panes[id] = nil
69 paths[id] = nil
70 if focused == id { focused = next }
71 }
72}
73
74/// The editor route's screen: its panes, with the focused one's title and toolbar.
75struct EditorSplitScreen: View {
76 let path: String
77 let offset: Int?
78 let workspace: WorkspaceModel
79 let session: DocumentSession
80 @State private var split: EditorSplit?
81 @State private var message: String?
82 @Environment(MobileModel.self) private var model
83
84 var body: some View {
85 Group {
86 if let split {
87 layoutView(split.layout, split: split)
88 .navigationTitle(split.paths[split.focused].map { session.bufferName($0) + (session.dirtyPaths.contains($0) ? " •" : "") } ?? "")
89 .navigationBarTitleDisplayMode(.inline)
90 } else {
91 ProgressView()
92 }
93 }
94 .onAppear {
95 if split == nil { split = EditorSplit(path: path, session: session, store: model.editors) }
96 }
97 }
98
99 private func layoutView(_ layout: PaneLayout, split: EditorSplit) -> AnyView {
100 switch layout {
101 case .pane(let id):
102 guard let pane = split.panes[id], let file = split.paths[id] else { return AnyView(EmptyView()) }
103 return AnyView(
104 EditorScreen(
105 path: file, offset: id == split.layout.panes.first ? offset : nil, workspace: workspace, session: session,
106 givenPane: pane, inSplit: split.isSplit, isFocused: split.focused == id,
107 onFocus: { split.focused = id },
108 onPaneCommand: { command in _ = split.run(command, session: session, store: model.editors) }
109 )
110 .id(id)
111 .overlay(alignment: .top) {
112 if split.isSplit, split.focused == id { Rectangle().fill(Color.accentColor).frame(height: 2) }
113 }
114 )
115 case .split(let axis, let first, let second, _):
116 if axis == .horizontal {
117 return AnyView(HStack(spacing: 0) {
118 layoutView(first, split: split)
119 Divider()
120 layoutView(second, split: split)
121 })
122 }
123 return AnyView(VStack(spacing: 0) {
124 layoutView(first, split: split)
125 Divider()
126 layoutView(second, split: split)
127 })
128 }
129 }
130}
131#endif