Skip to content

Commit 78a328c

Browse files
committed
Finalize Custom Divider Style API
1 parent 59d3916 commit 78a328c

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed

CodeEdit/Features/Editor/Views/EditorLayoutView.swift

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,11 @@ struct EditorLayoutView: View {
5353
@ObservedObject var data: SplitViewData
5454
@FocusState.Binding var focus: Editor?
5555

56-
@State private var dividerColor: NSColor = .blue
57-
5856
var body: some View {
59-
SplitView(
60-
axis: data.axis,
61-
dividerStyle: .thick(color: dividerColor)
62-
) {
57+
SplitView(axis: data.axis, dividerStyle: .editorDivider) {
6358
splitView
6459
}
6560
.edgesIgnoringSafeArea([.top, .bottom])
66-
.onChange(of: colorScheme) { newValue in
67-
print("ColorScheme changed to: \(newValue == .dark ? "dark" : "light")")
68-
if newValue == .dark {
69-
dividerColor = .red
70-
} else {
71-
dividerColor = .blue
72-
}
73-
}
74-
.onAppear {
75-
print("SubEditorLayoutView appeared with colorScheme: \(colorScheme == .dark ? "dark" : "light")")
76-
if colorScheme == .dark {
77-
dividerColor = .red
78-
} else {
79-
dividerColor = .blue
80-
}
81-
}
8261
}
8362

8463
var splitView: some View {

CodeEdit/Features/SplitView/Model/CodeEditDividerStyle.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,36 @@
77

88
import AppKit
99

10+
/// The style of divider used by ``SplitView``.
11+
///
12+
/// To add a new style, add another case to this enum and fill in the ``customColor`` and ``customThickness``
13+
/// variables. When passed to ``SplitView``, the custom styles will be used instead of the default styles. Leave
14+
/// values as `nil` to use default styles.
1015
enum CodeEditDividerStyle: Equatable {
11-
case system(NSSplitView.DividerStyle, color: NSColor? = nil)
12-
case thick(color: NSColor? = nil)
16+
case system(NSSplitView.DividerStyle)
17+
case editorDivider
1318

14-
var color: NSColor? {
19+
var customColor: NSColor? {
1520
switch self {
16-
case .system(_, let color):
17-
return color
18-
case .thick(let color):
19-
return color
21+
case .system:
22+
return nil
23+
case .editorDivider:
24+
return NSColor(name: nil) { appearance in
25+
if appearance.name == .darkAqua {
26+
NSColor.black
27+
} else {
28+
NSColor(white: 203.0 / 255.0, alpha: 1.0)
29+
}
30+
}
31+
}
32+
}
33+
34+
var customThickness: CGFloat? {
35+
switch self {
36+
case .system:
37+
return nil
38+
case .editorDivider:
39+
return 3.0
2040
}
2141
}
2242
}

CodeEdit/Features/SplitView/Views/SplitViewControllerView.swift

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ struct SplitViewControllerView: NSViewControllerRepresentable {
1515
@Binding var viewController: () -> SplitViewController?
1616

1717
func makeNSViewController(context: Context) -> SplitViewController {
18-
let controller = SplitViewController(axis: axis)
19-
updateItems(controller: controller)
18+
let controller = SplitViewController(axis: axis) { controller in
19+
updateItems(controller: controller)
20+
}
2021
return controller
2122
}
2223

@@ -65,7 +66,7 @@ struct SplitViewControllerView: NSViewControllerRepresentable {
6566
}
6667

6768
func makeCoordinator() -> SplitViewController {
68-
SplitViewController(axis: axis)
69+
SplitViewController(axis: axis, setUpItems: nil)
6970
}
7071
}
7172

@@ -75,9 +76,9 @@ final class SplitViewController: NSSplitViewController {
7576
var customDividerStyle: CodeEditDividerStyle = .system(.thin) {
7677
didSet {
7778
switch customDividerStyle {
78-
case .system(let dividerStyle, _):
79+
case .system(let dividerStyle):
7980
self.dividerStyle = dividerStyle
80-
case .thick:
81+
case .editorDivider:
8182
return
8283
}
8384
}
@@ -93,34 +94,23 @@ final class SplitViewController: NSSplitViewController {
9394
}
9495

9596
override var dividerColor: NSColor {
96-
if let customColor = customDividerStyle.color {
97-
return customColor
98-
}
99-
100-
switch customDividerStyle {
101-
case .system:
102-
return super.dividerColor
103-
case .thick:
104-
return NSColor.separatorColor
105-
}
97+
customDividerStyle.customColor ?? super.dividerColor
10698
}
10799

108100
override var dividerThickness: CGFloat {
109-
switch customDividerStyle {
110-
case .system:
111-
return super.dividerThickness
112-
case .thick:
113-
return 3.0
114-
}
101+
customDividerStyle.customThickness ?? super.dividerThickness
115102
}
116103
}
117104

118105
var items: [SplitViewItem] = []
119106
var axis: Axis
120107
var parentView: SplitViewControllerView?
121108

122-
init(axis: Axis) {
109+
var setUpItems: ((SplitViewController) -> Void)?
110+
111+
init(axis: Axis, setUpItems: ((SplitViewController) -> Void)?) {
123112
self.axis = axis
113+
self.setUpItems = setUpItems
124114
super.init(nibName: nil, bundle: nil)
125115
}
126116

@@ -136,6 +126,7 @@ final class SplitViewController: NSSplitViewController {
136126
override func viewDidLoad() {
137127
super.viewDidLoad()
138128
splitView.isVertical = axis != .vertical
129+
setUpItems?(self)
139130
DispatchQueue.main.async { [weak self] in
140131
self?.parentView?.viewController = { [weak self] in
141132
self
@@ -147,6 +138,11 @@ final class SplitViewController: NSSplitViewController {
147138
false
148139
}
149140

141+
override func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
142+
guard items.count > 1 else { return false }
143+
return super.splitView(splitView, shouldHideDividerAt: dividerIndex)
144+
}
145+
150146
func setDividerStyle(_ dividerStyle: CodeEditDividerStyle) {
151147
guard let splitView = splitView as? CustomSplitView else {
152148
return

0 commit comments

Comments
 (0)