Commit 73230beed3

73230beed303498a3ed5f7f1886cd9f3d9beacd1

parent: d6defe0dc0

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-11 04:55 UTC

Panes: review fixes

Splits are keyed by the panes either side of their divider. The main window registers a fresh model when it opens and releases its panes when it closes; an editor window reopens it before an app command. Narrowing and sparse trees move every view's caret out of hidden text, and so does a view taking focus. The palette and Quick Open act on the pane that had the focus when they opened.

Layout: unified · split

Sources/OrgApp/PaneLayout.swift +6 −4
@@ -46,15 +46,17 @@ public indirect enum PaneLayout: Equatable, Sendable {
4646 }
4747 }
4848
49 /// The split whose first pane in reading order is `id`, with `fraction` for its first side.
50 public func settingFraction(_ fraction: Double, ofSplitStartingWith id: PaneID) -> PaneLayout {
49 /// The split whose divider lies between `before` (the last pane of its first side) and
50 /// `after` (the first pane of its second side), with `fraction` for its first side. Each
51 /// split is one such adjacency in reading order, so the pair names it.
52 public func settingFraction(_ fraction: Double, ofSplitBetween before: PaneID, and after: PaneID) -> PaneLayout {
5153 switch self {
5254 case .pane: self
5355 case .split(let axis, let first, let second, let old):
54 if first.panes.first == id {
56 if first.panes.last == before, second.panes.first == after {
5557 .split(axis: axis, first: first, second: second, fraction: min(max(fraction, 0.1), 0.9))
5658 } else {
57 .split(axis: axis, first: first.settingFraction(fraction, ofSplitStartingWith: id), second: second.settingFraction(fraction, ofSplitStartingWith: id), fraction: old)
59 .split(axis: axis, first: first.settingFraction(fraction, ofSplitBetween: before, and: after), second: second.settingFraction(fraction, ofSplitBetween: before, and: after), fraction: old)
5860 }
5961 }
6062 }
Sources/OrgEditorAppKit/OrgEditor+Folding.swift +13 −3
@@ -29,10 +29,15 @@ extension OrgEditor {
2929 view.wrapperFolds = Set(wrapperHidden.keys)
3030 updateHiddenAndLayout()
3131 redrawViews()
32 caretsOutOfHiddenText()
33 }
34
35 /// Every view's caret moved to just before the hidden text it sits in, if any.
36 func caretsOutOfHiddenText() {
3237 for view in textViews {
3338 let caret = view.selectedRange().location
3439 guard let range = hidden.containing(caret) else { continue }
35 if view === active { setCaret(range.lowerBound - 1) } else { view.setSelectedRange(NSRange(location: max(0, range.lowerBound - 1), length: 0)) }
40 if view === active { setCaret(max(0, range.lowerBound - 1)) } else { view.setSelectedRange(NSRange(location: max(0, range.lowerBound - 1), length: 0)) }
3641 }
3742 }
3843
@@ -131,8 +136,7 @@ extension OrgEditor {
131136 restyleOutsideEditing(0..<utf16Length)
132137 }
133138 redrawViews()
134 let caret = textView.selectedRange().location
135 if let range = hidden.containing(caret) { setCaret(max(0, range.lowerBound - 1)) }
139 caretsOutOfHiddenText()
136140 }
137141
138142 /// Leaves a sparse tree for ordinary folds: headings with something shown below them
@@ -194,6 +198,12 @@ extension OrgEditor {
194198 redrawViews()
195199 if let narrowed {
196200 setCaret(max(narrowed.lowerBound, min(caret, narrowed.upperBound)))
201 for view in textViews where view !== active {
202 let other = view.selectedRange().location
203 if !(narrowed.lowerBound...narrowed.upperBound).contains(other) {
204 view.setSelectedRange(NSRange(location: max(narrowed.lowerBound, min(other, narrowed.upperBound)), length: 0))
205 }
206 }
197207 textView.scrollRangeToVisible(NSRange(location: narrowed.lowerBound, length: 0))
198208 } else {
199209 textView.scrollRangeToVisible(NSRange(location: caret, length: 0))
Sources/OrgEditorAppKit/OrgEditor.swift +5 −1
@@ -137,8 +137,12 @@ public final class OrgEditor: NSObject {
137137 view.onFocusChange = { [weak self, unowned view] in
138138 guard let self, let layout = view.textLayoutManager else { return }
139139 let id = ObjectIdentifier(layout)
140 if view.window?.firstResponder === view {
140 if view.window?.firstResponder === view, self.active !== view {
141141 self.active = view
142 // Its caret may sit in text another view folded or narrowed away since.
143 self.selectionChanged()
144 self.onFocus?(view)
145 } else if view.window?.firstResponder === view {
142146 self.onFocus?(view)
143147 }
144148 if let window = view.window, window.isKeyWindow, window.firstResponder === view {
Sources/Orgstar/ContentView.swift +52 −9
@@ -104,6 +104,21 @@ struct ContentView: View {
104104 EditorSettings(keymap: keymap, modal: preset.isModal, optionAsMeta: OptionAsMeta(rawValue: optionAsMeta), options: options, showMarkup: showMarkup, showLineNumbers: showLineNumbers, theme: theme)
105105 }
106106
107 /// The pane that had the focus when a sheet opened, to act on when it closes.
108 @State private var sheetTarget: EditorPane?
109
110 /// Puts the focus back on the pane a sheet was opened from.
111 private func restoreSheetTarget() {
112 guard let target = sheetTarget, session.panes.contains(where: { $0 === target }) else { return }
113 sheetTarget = nil
114 session.focused = target
115 if let window = commands.store.window(of: target.id) { window.focused = target.id }
116 if let path = target.path, let textView = commands.store.textView(for: target.id, path: path) {
117 textView.window?.makeFirstResponder(textView)
118 commands.editor = commands.store.editor(for: path)
119 }
120 }
121
107122 /// The window holding the focused pane: this one, or an editor window.
108123 private var paneWindow: WindowModel? {
109124 commands.store.window(of: session.focused.id) ?? window
@@ -270,11 +285,29 @@ struct ContentView: View {
270285 .onAppear {
271286 commands.externalHandler = runExternal
272287 commands.knownTags = { [index = workspace.index] in (try? index.tags()) ?? [] }
273 if window == nil {
274 let model = WindowModel(session: session, pane: session.panes[0])
275 window = model
276 commands.store.windows.append(model)
277 }
288 commands.mainWindowOpen = true
289 if let stale = window { commands.store.windows.removeAll { $0 === stale } }
290 // The session's first pane the first time; a new one showing the focused buffer
291 // when the window opens again.
292 let pane: EditorPane
293 if commands.store.window(of: session.panes[0].id) == nil, window == nil {
294 pane = session.panes[0]
295 } else {
296 pane = session.makePane()
297 if let path = session.focused.path { session.open(path, in: pane) }
298 }
299 let model = WindowModel(session: session, pane: pane)
300 window = model
301 commands.store.windows.insert(model, at: 0)
302 model.focus(pane.id)
303 }
304 .onDisappear {
305 commands.mainWindowOpen = false
306 guard let model = window else { return }
307 for id in model.layout.panes { commands.store.releasePane(id) }
308 model.closeAll()
309 commands.store.windows.removeAll { $0 === model }
310 commands.editor = nil
278311 }
279312 .onChange(of: saveMode, initial: true) { _, mode in session.saveMode = mode }
280313 .onChange(of: options, initial: true) { _, options in session.options = options }
@@ -306,10 +339,16 @@ struct ContentView: View {
306339 .sheet(isPresented: $commands.showingPalette) {
307340 PaletteView(commands: commands) { id in
308341 commands.showingPalette = false
309 // After the sheet closes, so app commands can present their own.
310 DispatchQueue.main.async { perform(id) }
342 // After the sheet closes, so app commands can present their own, and in the
343 // pane that had the focus when it opened: closing a sheet makes this window key.
344 DispatchQueue.main.async {
345 restoreSheetTarget()
346 perform(id)
347 }
311348 }
312349 }
350 .onChange(of: commands.showingPalette) { _, showing in if showing { sheetTarget = session.focused } }
351 .onChange(of: commands.showingQuickOpen) { _, showing in if showing { sheetTarget = session.focused } }
313352 .onChange(of: session.conflict) { _, conflict in
314353 if conflict != nil { commands.showingConflict = true }
315354 }
@@ -333,7 +372,7 @@ struct ContentView: View {
333372 hotKey?.unregister()
334373 hotKey = enabled ? GlobalHotKey { perform("app.capture") } : nil
335374 }
336 .onChange(of: commands.runRequest?.serial) {
375 .onChange(of: commands.runRequest?.serial, initial: true) {
337376 if let id = commands.runRequest?.id { perform(id) }
338377 }
339378 .onChange(of: ConfigSync.shared.problems, initial: true) { _, problems in
@@ -366,8 +405,12 @@ struct ContentView: View {
366405 }
367406 .sheet(isPresented: $commands.showingQuickOpen) {
368407 QuickOpenView(workspace: workspace) { entry in
369 selectedFile = entry.path
370408 commands.showingQuickOpen = false
409 DispatchQueue.main.async {
410 restoreSheetTarget()
411 selectedFile = entry.path
412 if selectedFile == entry.path { session.open(entry.path) }
413 }
371414 }
372415 }
373416 .alert("orgstar", isPresented: errorShown) {
Sources/Orgstar/EditorWindowView.swift +10 −2
@@ -9,16 +9,23 @@ struct EditorWindowView: View {
99 let clock: ClockModel
1010 @AppStorage("showTabBar") private var showTabBar = false
1111 @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 }
1219
1320 var body: some View {
1421 Group {
1522 if let window {
1623 PaneLayoutView(layout: window.layout, window: window) { pane in
17 PaneCell(pane: pane, window: window, session: session, commands: commands, showTabBar: showTabBar) { commands.run($0) }
24 PaneCell(pane: pane, window: window, session: session, commands: commands, showTabBar: showTabBar) { run($0) }
1825 }
1926 .safeAreaInset(edge: .bottom, spacing: 0) {
2027 VStack(spacing: 0) {
21 ModeLine(session: session, commands: commands, clock: clock) { commands.run($0) }
28 ModeLine(session: session, commands: commands, clock: clock) { run($0) }
2229 EchoArea(commands: commands)
2330 }
2431 }
@@ -39,6 +46,7 @@ struct EditorWindowView: View {
3946 for id in model.layout.panes { commands.store.releasePane(id) }
4047 model.closeAll()
4148 commands.store.windows.removeAll { $0 === model }
49 commands.editor = nil
4250 if let main = commands.store.windows.first { main.focus(main.focused) }
4351 }
4452 }
Sources/Orgstar/OrgstarApp.swift +4
@@ -24,6 +24,9 @@ final class AppCommands {
2424 var editor: OrgEditor?
2525 /// The editors and text views of every pane in every window.
2626 let store = EditorStore()
27 /// The main window is on screen. App commands run there, so an editor window reopens it
28 /// before asking for one.
29 var mainWindowOpen = false
2730 /// What every editor is set up with, from the settings; the main window keeps it current.
2831 var editorSettings = EditorSettings()
2932 /// Runs work outside the text (a table recalculation in Emacs); the main window sets it.
@@ -332,6 +335,7 @@ struct OrgstarApp: App {
332335 }
333336 }
334337 .defaultSize(width: 900, height: 700)
338 .commandsRemoved()
335339 Window("Agenda", id: "agenda") {
336340 if case .success(let model) = workspace {
337341 AgendaView(workspace: model, session: session, commands: commands, scheduler: scheduler, clock: clock)
Sources/Orgstar/PaneLayoutView.swift +2 −1
@@ -16,7 +16,7 @@ struct PaneLayoutView<Content: View>: View {
1616 let total = axis == .horizontal ? geometry.size.width : geometry.size.height
1717 let firstSize = max(80, min(max(80, total - 80), total * fraction))
1818 let divider = SplitDivider(axis: axis, firstSize: firstSize) { size in
19 window.setFraction(size / max(total, 1), ofSplitStartingWith: first.panes[0])
19 window.setFraction(size / max(total, 1), ofSplitBetween: first.panes[first.panes.count - 1], and: second.panes[0])
2020 }
2121 if axis == .horizontal {
2222 HStack(spacing: 0) {
@@ -81,6 +81,7 @@ struct PaneCell: View {
8181 .contentShape(Rectangle())
8282 .onTapGesture {
8383 window.focus(pane.id)
84 NSApp.keyWindow?.makeFirstResponder(nil)
8485 commands.editor = nil
8586 }
8687 }
Sources/Orgstar/WindowModel.swift +5 −5
@@ -21,7 +21,7 @@ final class WindowModel: Identifiable {
2121 }
2222
2323 func pane(_ id: PaneID) -> EditorPane? { panes[id] }
24 var focusedPane: EditorPane { panes[focused] ?? panes[layout.panes[0]]! }
24 var focusedPane: EditorPane { panes[focused] ?? panes[layout.panes[0]] ?? panes.values.first ?? session.focused }
2525 var paneCount: Int { layout.panes.count }
2626
2727 /// A new pane after the focused one, showing the same buffer, as `split-window` does. The
@@ -61,13 +61,13 @@ final class WindowModel: Identifiable {
6161 session.focused = pane
6262 }
6363
64 func setFraction(_ fraction: Double, ofSplitStartingWith id: PaneID) {
65 layout = layout.settingFraction(fraction, ofSplitStartingWith: id)
64 func setFraction(_ fraction: Double, ofSplitBetween before: PaneID, and after: PaneID) {
65 layout = layout.settingFraction(fraction, ofSplitBetween: before, and: after)
6666 }
6767
68 /// Takes every pane out of the session, as the window closes.
68 /// Takes every pane out of the session, as the window closes. The panes stay listed here
69 /// so views still on screen can read them.
6970 func closeAll() {
7071 for pane in panes.values { session.closePane(pane) }
71 panes = [:]
7272 }
7373}
Tests/OrgAppTests/PaneLayoutTests.swift +6 −2
@@ -22,9 +22,13 @@ struct PaneLayoutTests {
2222 @Test func settingAFractionFindsTheSplitByItsFirstPane() {
2323 let a = PaneID(), b = PaneID(), c = PaneID()
2424 let layout = PaneLayout.pane(a).splitting(a, axis: .horizontal, new: b).splitting(b, axis: .vertical, new: c)
25 let inner = layout.settingFraction(0.3, ofSplitStartingWith: b)
25 let inner = layout.settingFraction(0.3, ofSplitBetween: b, and: c)
2626 #expect(inner == .split(axis: .horizontal, first: .pane(a), second: .split(axis: .vertical, first: .pane(b), second: .pane(c), fraction: 0.3), fraction: 0.5))
27 #expect(layout.settingFraction(2, ofSplitStartingWith: a) == .split(axis: .horizontal, first: .pane(a), second: .split(axis: .vertical, first: .pane(b), second: .pane(c), fraction: 0.5), fraction: 0.9))
27 #expect(layout.settingFraction(2, ofSplitBetween: a, and: b) == .split(axis: .horizontal, first: .pane(a), second: .split(axis: .vertical, first: .pane(b), second: .pane(c), fraction: 0.5), fraction: 0.9))
28 // A nested split on the first side shares its first pane with the outer split.
29 let nestedFirst = PaneLayout.pane(a).splitting(a, axis: .horizontal, new: b).splitting(a, axis: .vertical, new: c)
30 #expect(nestedFirst.settingFraction(0.3, ofSplitBetween: a, and: c) == .split(axis: .horizontal, first: .split(axis: .vertical, first: .pane(a), second: .pane(c), fraction: 0.3), second: .pane(b), fraction: 0.5))
31 #expect(nestedFirst.settingFraction(0.3, ofSplitBetween: c, and: b) == .split(axis: .horizontal, first: .split(axis: .vertical, first: .pane(a), second: .pane(c), fraction: 0.5), second: .pane(b), fraction: 0.3))
2832 }
2933
3034 @Test func onlyAndNext() {
Tests/OrgEditorAppKitTests/MultiViewTests.swift +18
@@ -81,6 +81,24 @@ struct MultiViewTests {
8181 #expect(two.second.string == "XY* a\nbody\n")
8282 }
8383
84 @Test func narrowingMovesEveryViewsCaretIntoTheRegion() {
85 let two = Two("* a\nbody\n* b\nmore\n")
86 two.second.setSelectedRange(NSRange(location: 0, length: 0))
87 two.editor.setCaret(9)
88 two.editor.run("org.narrow.subtree")
89 #expect(two.editor.narrowed?.lowerBound == 9)
90 #expect(two.second.selectedRange().location >= 9)
91 }
92
93 @Test func aViewTakingFocusLeavesHiddenText() {
94 let two = Two("* a\nbody\n* b\nmore\n")
95 two.second.setSelectedRange(NSRange(location: 5, length: 0))
96 two.editor.setCaret(0)
97 _ = two.editor.cycleAtCaret()
98 two.windows[1].makeFirstResponder(two.second)
99 #expect(two.editor.hidden.containing(two.second.selectedRange().location) == nil)
100 }
101
84102 @Test func removingAViewDetachesItsLayoutManager() {
85103 let two = Two("* a\n")
86104 let storage = two.editor.textView.textLayoutManager!.textContentManager!
docs/plans/2026-10-10-windows-and-panes.md +6
@@ -155,3 +155,9 @@ public indirect enum PaneLayout: Equatable, Sendable {
155155- `WindowModel` (app) holds a window's `PaneLayout`, its panes and the focused one; `AppCommands.store.windows` lists them so pane commands find the window of the focused pane.
156156- A split shows the same buffer in the new pane and keeps the focus, as `split-window` does. A pane with no buffer shows a placeholder.
157157- Checked: `swift test` (691 tests); the main window with three panes and a fold shared by all of them, by snapshot; the window and pane commands run to completion under the development hooks.
158
159## Review
160
161A fresh review of the branch found, and the fix pass closed: a nested split's inner divider resized the outer split (splits are now keyed by the two panes either side of the divider, with a test); the main window had no close-time cleanup, so reopening it drove a stale model (it now registers a fresh model on appear and releases its panes on disappear); the editor window's app commands did nothing with the main window closed (it reopens it first, and a request made before the window appeared runs when it does); narrowing and sparse trees left other views' carets in hidden text, and a view taking focus kept such a caret (every view's caret is moved, and a view taking focus runs the selection check); the palette and Quick Open acted on the main window's pane when opened from an editor window (they act on the pane that had the focus when they opened); the empty-pane placeholder left the keyboard with another pane; `WindowModel.closeAll` emptied the panes under views still on screen.
162
163Deferred, minor: `kill` and `showPrevious` may fall back to a buffer another pane already shows (Emacs prefers one not visible); a split's new view starts at the top rather than at the point, and `truncatesLines` is per view and not copied to a new view; detached text views keep observing key-window changes (pre-existing, now common); `Theme.focusedLayouts` can keep a released primary view's id; `previousSelection` is per editor, so the first caret fix after switching views may jump the wrong way; `.onChange(of: session.path)` runs the sync-conflict scan on every focus change between panes showing different files.