Sources/Orgstar/EditorWindowView.swift

53 lines · 2152 bytes

 1import OrgApp
 2import SwiftUI
 3
 4/// A window of panes alone, as a second Emacs frame: no sidebar or search. It opens on the
 5/// buffer the focused pane showed, and its panes leave the session when it closes.
 6struct EditorWindowView: View {
 7    let session: DocumentSession
 8    let commands: AppCommands
 9    let clock: ClockModel
10    @AppStorage("showTabBar") private var showTabBar = false
11    @State private var window: WindowModel?
12    @Environment(\.openWindow) private var openWindow
13
14    /// App commands run in the main window; it comes back if it was closed.
15    private func run(_ id: String) {
16        if !commands.mainWindowOpen { openWindow(id: "main") }
17        commands.run(id)
18    }
19
20    var body: some View {
21        Group {
22            if let window {
23                PaneLayoutView(layout: window.layout, window: window) { pane in
24                    PaneCell(pane: pane, window: window, session: session, commands: commands, showTabBar: showTabBar) { run($0) }
25                }
26                .safeAreaInset(edge: .bottom, spacing: 0) {
27                    VStack(spacing: 0) {
28                        ModeLine(session: session, commands: commands, clock: clock) { run($0) }
29                        EchoArea(commands: commands)
30                    }
31                }
32            }
33        }
34        .navigationTitle(window?.focusedPane.path.map(session.bufferName) ?? "orgstar")
35        .onAppear {
36            guard window == nil else { return }
37            let pane = session.makePane()
38            if let path = session.focused.path { session.open(path, in: pane) }
39            let model = WindowModel(session: session, pane: pane)
40            window = model
41            commands.store.windows.append(model)
42            model.focus(pane.id)
43        }
44        .onDisappear {
45            guard let model = window else { return }
46            for id in model.layout.panes { commands.store.releasePane(id) }
47            model.closeAll()
48            commands.store.windows.removeAll { $0 === model }
49            commands.editor = nil
50            if let main = commands.store.windows.first { main.focus(main.focused) }
51        }
52    }
53}