forked from CodeEditApp/CodeEdit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorLayoutView.swift
More file actions
99 lines (85 loc) · 3.04 KB
/
EditorLayoutView.swift
File metadata and controls
99 lines (85 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// EditorLayoutView.swift
// CodeEdit
//
// Created by Wouter Hennen on 20/02/2023.
//
import SwiftUI
struct EditorLayoutView: View {
var layout: EditorLayout
@FocusState.Binding var focus: Editor?
@Environment(\.window.value)
private var window
@Environment(\.isAtEdge)
private var isAtEdge
var toolbarHeight: CGFloat {
window?.contentView?.safeAreaInsets.top ?? .zero
}
var body: some View {
VStack {
switch layout {
case .one(let detailEditor):
EditorAreaView(editor: detailEditor, focus: $focus)
.transformEnvironment(\.edgeInsets) { insets in
switch isAtEdge {
case .all:
insets.top += toolbarHeight
insets.bottom += StatusBarView.height + 5
case .top:
insets.top += toolbarHeight
case .bottom:
insets.bottom += StatusBarView.height + 5
default:
return
}
}
case .vertical(let data), .horizontal(let data):
SubEditorLayoutView(data: data, focus: $focus)
}
}
}
struct SubEditorLayoutView: View {
@Environment(\.colorScheme)
private var colorScheme
@ObservedObject var data: SplitViewData
@FocusState.Binding var focus: Editor?
var body: some View {
SplitView(axis: data.axis, dividerStyle: .editorDivider) {
splitView
}
.edgesIgnoringSafeArea([.top, .bottom])
}
var splitView: some View {
ForEach(Array(data.editorLayouts.enumerated()), id: \.offset) { index, item in
EditorLayoutView(layout: item, focus: $focus)
.transformEnvironment(\.isAtEdge) { belowToolbar in
calcIsAtEdge(current: &belowToolbar, index: index)
}
.environment(\.splitEditor) { [weak data] edge, newEditor in
data?.split(edge, at: index, new: newEditor)
}
}
}
func calcIsAtEdge(current: inout VerticalEdge.Set, index: Int) {
if case .vertical = data.axis {
guard data.editorLayouts.count != 1 else { return }
if index == data.editorLayouts.count - 1 {
current.remove(.top)
} else if index == 0 {
current.remove(.bottom)
} else {
current = []
}
}
}
}
}
private struct BelowToolbarEnvironmentKey: EnvironmentKey {
static var defaultValue: VerticalEdge.Set = .all
}
extension EnvironmentValues {
fileprivate var isAtEdge: BelowToolbarEnvironmentKey.Value {
get { self[BelowToolbarEnvironmentKey.self] }
set { self[BelowToolbarEnvironmentKey.self] = newValue }
}
}