Sources/Orgstar/PaneLayoutView.swift
95 lines · 4249 bytes
3 symbols in this file
1import OrgApp
2import SwiftUI
3
4/// A window's panes as its layout divides them, with a draggable divider at each split.
5struct PaneLayoutView<Content: View>: View {
6 let layout: PaneLayout
7 let window: WindowModel
8 @ViewBuilder let content: (EditorPane) -> Content
9
10 var body: some View {
11 switch layout {
12 case .pane(let id):
13 if let pane = window.pane(id) { content(pane) } else { Color.clear }
14 case .split(let axis, let first, let second, let fraction):
15 GeometryReader { geometry in
16 let total = axis == .horizontal ? geometry.size.width : geometry.size.height
17 let firstSize = max(80, min(max(80, total - 80), total * fraction))
18 let divider = SplitDivider(axis: axis, firstSize: firstSize) { size in
19 window.setFraction(size / max(total, 1), ofSplitBetween: first.panes[first.panes.count - 1], and: second.panes[0])
20 }
21 if axis == .horizontal {
22 HStack(spacing: 0) {
23 PaneLayoutView(layout: first, window: window, content: content).frame(width: firstSize)
24 divider
25 PaneLayoutView(layout: second, window: window, content: content).frame(maxWidth: .infinity)
26 }
27 } else {
28 VStack(spacing: 0) {
29 PaneLayoutView(layout: first, window: window, content: content).frame(height: firstSize)
30 divider
31 PaneLayoutView(layout: second, window: window, content: content).frame(maxHeight: .infinity)
32 }
33 }
34 }
35 }
36 }
37}
38
39/// The line between two panes; dragging it resizes the first.
40struct SplitDivider: View {
41 let axis: PaneLayout.Axis
42 let firstSize: CGFloat
43 let resize: (CGFloat) -> Void
44 @State private var start: CGFloat?
45
46 var body: some View {
47 let horizontal = axis == .horizontal
48 Rectangle()
49 .fill(Color(nsColor: .separatorColor))
50 .frame(width: horizontal ? 1 : nil, height: horizontal ? nil : 1)
51 .frame(maxWidth: horizontal ? 1 : .infinity, maxHeight: horizontal ? .infinity : 1)
52 .overlay(Color.clear.frame(width: horizontal ? 7 : nil, height: horizontal ? nil : 7).contentShape(Rectangle()).onHover { inside in
53 if inside { (horizontal ? NSCursor.resizeLeftRight : NSCursor.resizeUpDown).push() } else { NSCursor.pop() }
54 }
55 .gesture(DragGesture(minimumDistance: 1).onChanged { drag in
56 let base = start ?? firstSize
57 start = base
58 resize(base + (horizontal ? drag.translation.width : drag.translation.height))
59 }.onEnded { _ in start = nil }))
60 .accessibilityHidden(true)
61 }
62}
63
64/// One pane: its tabs, its editor, or a placeholder while it shows nothing. A thin bar marks
65/// the focused pane when there are several.
66struct PaneCell: View {
67 let pane: EditorPane
68 let window: WindowModel
69 let session: DocumentSession
70 let commands: AppCommands
71 let showTabBar: Bool
72 let onAppCommand: (String) -> Void
73
74 var body: some View {
75 VStack(spacing: 0) {
76 if showTabBar { BufferTabs(session: session, pane: pane) { window.closeTab($0, in: pane, commands: commands) } }
77 if pane.path != nil {
78 PaneView(pane: pane, session: session, commands: commands, settings: commands.editorSettings, onAppCommand: onAppCommand)
79 } else {
80 ContentUnavailableView("No buffer", systemImage: "rectangle.dashed", description: Text("Choose a file, or press ⌘P."))
81 .contentShape(Rectangle())
82 .onTapGesture {
83 window.focus(pane.id)
84 NSApp.keyWindow?.makeFirstResponder(nil)
85 commands.editor = nil
86 }
87 }
88 }
89 .overlay(alignment: .top) {
90 if window.paneCount > 1, window.focused == pane.id {
91 Rectangle().fill(Color.accentColor).frame(height: 2).accessibilityHidden(true)
92 }
93 }
94 }
95}