Sources/Orgstar/EditorWindowView.swift
53 lines · 2181 bytes
2 symbols in this file
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 @AppStorage("showTabBar") private var showTabBar = false
10 @State private var window: WindowModel?
11 @Environment(\.openWindow) private var openWindow
12
13 /// App commands run in the main window; it comes back if it was closed.
14 private func run(_ id: String) {
15 if !commands.mainWindowOpen { openWindow(id: "main") }
16 commands.run(id)
17 }
18
19 var body: some View {
20 Group {
21 if let window {
22 PaneLayoutView(layout: window.layout, window: window) { pane in
23 PaneCell(pane: pane, window: window, session: session, commands: commands, showTabBar: showTabBar) { run($0) }
24 }
25 .safeAreaInset(edge: .bottom, spacing: 0) {
26 VStack(spacing: 0) {
27 CryptHoldBar(session: session) { run("app.crypt.save") }
28 ModeLine(session: session, commands: commands)
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}