Sources/Orgstar/EditorWindowView.swift
52 lines · 2100 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 @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 ModeLine(session: session, commands: commands)
28 EchoArea(commands: commands)
29 }
30 }
31 }
32 }
33 .navigationTitle(window?.focusedPane.path.map(session.bufferName) ?? "orgstar")
34 .onAppear {
35 guard window == nil else { return }
36 let pane = session.makePane()
37 if let path = session.focused.path { session.open(path, in: pane) }
38 let model = WindowModel(session: session, pane: pane)
39 window = model
40 commands.store.windows.append(model)
41 model.focus(pane.id)
42 }
43 .onDisappear {
44 guard let model = window else { return }
45 for id in model.layout.panes { commands.store.releasePane(id) }
46 model.closeAll()
47 commands.store.windows.removeAll { $0 === model }
48 commands.editor = nil
49 if let main = commands.store.windows.first { main.focus(main.focused) }
50 }
51 }
52}