Sources/Orgstar/WindowModel.swift

104 lines · 4011 bytes

  1import Foundation
  2import OrgApp
  3
  4/// One editor window's panes: how they divide the window and which has focus. The panes
  5/// themselves belong to the session, which keeps them in step with the buffers.
  6@MainActor
  7@Observable
  8final class WindowModel: Identifiable {
  9    let id = UUID()
 10    let session: DocumentSession
 11    private(set) var layout: PaneLayout
 12    private(set) var panes: [PaneID: EditorPane]
 13    var focused: PaneID
 14
 15    /// A window around `pane`, which the session already has.
 16    init(session: DocumentSession, pane: EditorPane) {
 17        self.session = session
 18        layout = .pane(pane.id)
 19        panes = [pane.id: pane]
 20        focused = pane.id
 21    }
 22
 23    func pane(_ id: PaneID) -> EditorPane? { panes[id] }
 24    var focusedPane: EditorPane { panes[focused] ?? panes[layout.panes[0]] ?? panes.values.first ?? session.focused }
 25    var paneCount: Int { layout.panes.count }
 26
 27    /// A new pane after the focused one, showing the same buffer, as `split-window` does. The
 28    /// focus stays where it was.
 29    @discardableResult
 30    func split(_ axis: PaneLayout.Axis) -> EditorPane {
 31        let current = focusedPane
 32        let pane = session.makePane()
 33        panes[pane.id] = pane
 34        layout = layout.splitting(current.id, axis: axis, new: pane.id)
 35        if let path = current.path { session.open(path, in: pane) }
 36        return pane
 37    }
 38
 39    /// Removes `id` from the window; its buffers stay open. The only pane stays.
 40    func close(_ id: PaneID) {
 41        guard let pane = panes[id], let rest = layout.removing(id) else { return }
 42        layout = rest
 43        panes[id] = nil
 44        session.closePane(pane)
 45        if focused == id { focus(rest.panes[0]) }
 46    }
 47
 48    /// Closes `path`'s tab in `pane`, or the empty pane when `path` is nil. With other panes in
 49    /// the window and no other tab here, the pane closes too, and the buffer closes only if no
 50    /// other pane shows it. False when unsaved changes kept it open.
 51    @discardableResult
 52    func closeTab(_ path: String?, in pane: EditorPane, commands: AppCommands) -> Bool {
 53        let closesPane = paneCount > 1 && pane.shown.allSatisfy { $0 == path }
 54        let elsewhere = path.map { path in session.panes.contains { $0 !== pane && $0.shown.contains(path) } } ?? true
 55        if let path, !(closesPane && elsewhere) {
 56            guard UnsavedChanges.resolve(session, paths: [path]) else { return false }
 57        }
 58        if closesPane {
 59            commands.store.releasePane(pane.id)
 60            close(pane.id)
 61            activate(commands)
 62        }
 63        if let path, !(closesPane && elsewhere) { session.kill(path) }
 64        return true
 65    }
 66
 67    /// Gives the focused pane's text view the keyboard, after a pane command moved the focus.
 68    func activate(_ commands: AppCommands) {
 69        let pane = focusedPane
 70        session.focused = pane
 71        if let path = pane.path, let textView = commands.store.textView(for: pane.id, path: path) {
 72            textView.window?.makeFirstResponder(textView)
 73            commands.editor = commands.store.editor(for: path)
 74        } else {
 75            commands.editor = nil
 76        }
 77    }
 78
 79    /// Removes every pane but the focused one.
 80    func closeOthers() {
 81        for id in layout.panes where id != focused { close(id) }
 82    }
 83
 84    /// Focuses the pane after the focused one in reading order, wrapping.
 85    func focusNext() {
 86        focus(layout.next(after: focused))
 87    }
 88
 89    func focus(_ id: PaneID) {
 90        guard let pane = panes[id] else { return }
 91        focused = id
 92        session.focused = pane
 93    }
 94
 95    func setFraction(_ fraction: Double, ofSplitBetween before: PaneID, and after: PaneID) {
 96        layout = layout.settingFraction(fraction, ofSplitBetween: before, and: after)
 97    }
 98
 99    /// Takes every pane out of the session, as the window closes. The panes stay listed here
100    /// so views still on screen can read them.
101    func closeAll() {
102        for pane in panes.values { session.closePane(pane) }
103    }
104}