Sources/Orgstar/EditorStore.swift

06941b2cf25c6ccc1629723cd7951910ce15e67a
orgstar/Sources/Orgstar/EditorStore.swift history · blame · raw

80 lines · 3345 bytes

 1import AppKit
 2import OrgApp
 3import OrgEditorAppKit
 4
 5/// The editors of the open buffers and the text views the panes show them in, across every
 6/// window: one `OrgEditor` per file, one text view per pane that shows it.
 7@MainActor
 8final class EditorStore {
 9    /// The editor of each open file.
10    private(set) var editors: [String: OrgEditor] = [:]
11    /// Each pane's text view per file, with its scroll view, kept while the pane shows
12    /// another buffer so its caret and scroll position stay.
13    private(set) var views: [PaneID: [String: (scroll: NSScrollView, textView: NSTextView)]] = [:]
14    /// The pane each text view belongs to.
15    private var paneOf: [ObjectIdentifier: EditorPane] = [:]
16    /// The pane using an editor's first text view, which can't be removed from the editor.
17    private var primaryOwner: [String: PaneID] = [:]
18    /// The editor windows, for the pane commands.
19    var windows: [WindowModel] = []
20
21    func editor(for path: String) -> OrgEditor? { editors[path] }
22
23    func pane(of textView: NSTextView) -> EditorPane? { paneOf[ObjectIdentifier(textView)] }
24
25    func window(of pane: PaneID) -> WindowModel? { windows.first { $0.pane(pane) != nil } }
26
27    /// The text view `pane` shows `path` in, made with `make` the first time.
28    func view(for pane: EditorPane, path: String, make: () -> OrgEditor?) -> (editor: OrgEditor, scroll: NSScrollView, textView: NSTextView)? {
29        let editor: OrgEditor
30        if let existing = editors[path] {
31            editor = existing
32        } else {
33            guard let made = make() else { return nil }
34            editors[path] = made
35            editor = made
36        }
37        if let shown = views[pane.id]?[path] { return (editor, shown.scroll, shown.textView) }
38        let textView: NSTextView
39        if primaryOwner[path] == nil {
40            primaryOwner[path] = pane.id
41            textView = editor.textViews[0]
42        } else {
43            textView = editor.addView()
44        }
45        let scroll = editor.makeScrollView(for: textView)
46        scroll.autoresizingMask = [.width, .height]
47        views[pane.id, default: [:]][path] = (scroll, textView)
48        paneOf[ObjectIdentifier(textView)] = pane
49        return (editor, scroll, textView)
50    }
51
52    /// The text view `pane` shows `path` in, if it has one.
53    func textView(for pane: PaneID, path: String) -> NSTextView? { views[pane]?[path]?.textView }
54
55    /// Drops `pane`'s text views; its buffers stay.
56    func releasePane(_ pane: PaneID) {
57        for (path, shown) in views[pane] ?? [:] { drop(shown, path: path, pane: pane) }
58        views[pane] = nil
59    }
60
61    /// Drops every text view of `path` and its editor, as the buffer closes.
62    func releasePath(_ path: String) {
63        for pane in views.keys {
64            if let shown = views[pane]?[path] { drop(shown, path: path, pane: pane) }
65            views[pane]?[path] = nil
66        }
67        editors[path] = nil
68        primaryOwner[path] = nil
69    }
70
71    private func drop(_ shown: (scroll: NSScrollView, textView: NSTextView), path: String, pane: PaneID) {
72        shown.scroll.removeFromSuperview()
73        paneOf[ObjectIdentifier(shown.textView)] = nil
74        if primaryOwner[path] == pane {
75            primaryOwner[path] = nil
76        } else {
77            editors[path]?.removeView(shown.textView)
78        }
79    }
80}