-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplitViewController.swift
220 lines (146 loc) · 6.8 KB
/
SplitViewController.swift
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// SplitViewController.swift
//
// CotEditor
// https://coteditor.com
//
// Created by nakamuxu on 2006-03-26.
//
// ---------------------------------------------------------------------------
//
// © 2004-2007 nakamuxu
// © 2014-2018 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Cocoa
final class SplitViewController: NSSplitViewController {
// MARK: Public Properties
private(set) weak var focusedSubviewController: TextEditorSplitViewController?
// MARK: -
// MARK: Split View Controller Methods
/// setup view
override func viewDidLoad() {
super.viewDidLoad()
self.splitView.isVertical = UserDefaults.standard[.splitViewVertical]
self.invalidateOpenSplitEditorButtons()
// observe focus change
NotificationCenter.default.addObserver(self, selector: #selector(textViewDidBecomeFirstResponder), name: EditorTextView.didBecomeFirstResponderNotification, object: nil)
}
/// update close split view button state after remove
override func removeSplitViewItem(_ splitViewItem: NSSplitViewItem) {
super.removeSplitViewItem(splitViewItem)
self.invalidateCloseSplitEditorButtons()
}
/// workaround for crash on macOS 10.12 (2016-09) and on macOS 10.13 (2018-05)
override func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
return false
}
/// apply current state to related menu items
override func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
guard let action = item.action else { return false }
switch action {
case #selector(toggleSplitOrientation):
if let item = item as? NSMenuItem {
let title = self.splitView.isVertical ? "Stack Editors Horizontally" : "Stack Editors Vertically"
item.title = title.localized
}
return self.splitViewItems.count > 1
case #selector(focusNextSplitTextView), #selector(focusPrevSplitTextView):
return self.splitViewItems.count > 1
default: break
}
return super.validateUserInterfaceItem(item)
}
// MARK: Notifications
/// editor's focus did change
@objc private func textViewDidBecomeFirstResponder(_ notification: Notification) {
guard let textView = notification.object as? EditorTextView else { return }
guard let viewController = {
self.children.lazy
.compactMap { $0 as? TextEditorSplitViewController }
.first { $0.textView == textView }
}() else { return }
self.focusedSubviewController = viewController
}
// MARK: Public Methods
/// add subview for given viewController at desired position
func addSubview(for editorViewController: TextEditorSplitViewController, relativeTo otherEditorViewController: TextEditorSplitViewController?) {
let splitViewItem = NSSplitViewItem(viewController: editorViewController)
splitViewItem.holdingPriority = NSLayoutConstraint.Priority(251)
if let otherEditorViewController = otherEditorViewController {
guard let baseIndex = self.children.index(of: otherEditorViewController) else {
assertionFailure("The base editor view is not belong to the same window.")
return
}
self.insertSplitViewItem(splitViewItem, at: baseIndex + 1)
} else {
self.addSplitViewItem(splitViewItem)
}
self.invalidateOpenSplitEditorButtons()
self.invalidateCloseSplitEditorButtons()
}
/// find viewController for given subview
func viewController(for subview: NSView) -> TextEditorSplitViewController? {
return self.children.lazy
.compactMap { $0 as? TextEditorSplitViewController }
.first { $0.splitView == subview }
}
// MARK: Action Messages
/// toggle divider orientation
@IBAction func toggleSplitOrientation(_ sender: Any?) {
self.splitView.isVertical.toggle()
self.invalidateOpenSplitEditorButtons()
}
/// move focus to next text view
@IBAction func focusNextSplitTextView(_ sender: Any?) {
self.focusSplitTextView(onNext: true)
}
/// move focus to previous text view
@IBAction func focusPrevSplitTextView(_ sender: Any?) {
self.focusSplitTextView(onNext: false)
}
// MARK: Private Methods
/// move focus to next/previous text view
private func focusSplitTextView(onNext: Bool) {
let count = self.splitViewItems.count
guard count > 1 else { return }
let focusIndex = self.children.index(of: self.focusedSubviewController!) ?? 0
let index: Int = {
switch focusIndex {
case 0 where !onNext:
return count - 1
case count - 1 where onNext:
return 0
default:
return focusIndex + (onNext ? 1 : -1)
}
}()
guard let nextEditorViewController = self.children[index] as? TextEditorSplitViewController else { return }
self.view.window?.makeFirstResponder(nextEditorViewController.textView)
}
/// update "Split Editor" button state
private func invalidateOpenSplitEditorButtons() {
let isVertical = self.splitView.isVertical
for case let viewController as TextEditorSplitViewController in self.children {
viewController.navigationBarController?.isSplitOrientationVertical = isVertical
}
}
/// update "Close Split Editor" button state
private func invalidateCloseSplitEditorButtons() {
let isEnabled = self.splitViewItems.count > 1
for case let viewController as TextEditorSplitViewController in self.children {
viewController.navigationBarController?.isCloseSplitButtonEnabled = isEnabled
}
}
}