This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathNSViewControllerExtension.swift
127 lines (108 loc) · 4.83 KB
/
NSViewControllerExtension.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
//
// NSViewControllerExtension.swift
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
// 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
//
// http://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 AppKit
extension NSViewController {
/// Applies a modal window style to a view controller if it is being presented by another view controller.
func applyModalWindowStyleIfNeeded() {
guard self.presentingViewController != nil else { return }
self.view.window?.titleVisibility = .hidden
self.view.window?.titlebarAppearsTransparent = true
self.view.window?.standardWindowButton(.zoomButton)?.isHidden = true
self.view.window?.standardWindowButton(.closeButton)?.isHidden = true
self.view.window?.standardWindowButton(.miniaturizeButton)?.isHidden = true
self.view.window?.styleMask = [.resizable, .titled, .fullSizeContentView]
}
func wrappedInWindowController() -> NSWindowController {
let window = NSWindow(contentViewController: self)
return NSWindowController(window: window)
}
func beginSheet(_ windowController: NSWindowController) {
if let windowSheet = windowController.window {
view.window?.beginSheet(windowSheet)
}
}
func beginSheet(_ viewController: NSViewController, completionHandler handler: ((NSApplication.ModalResponse) -> Void)? = nil) {
let windowController = viewController.wrappedInWindowController()
if let windowSheet = windowController.window {
view.window?.beginSheet(windowSheet, completionHandler: handler)
}
}
func beginSheetFromMainWindow(_ viewController: NSViewController) {
let newWindowController = viewController.wrappedInWindowController()
guard let newWindow = newWindowController.window,
let parentWindowController = WindowControllersManager.shared.lastKeyMainWindowController
else {
assertionFailure("Failed to present \(viewController)")
return
}
parentWindowController.window?.beginSheet(newWindow)
}
func dismiss() {
guard let window = view.window else {
assertionFailure("\(#file): Failed to get the view's window")
return
}
if let sheetParent = window.sheetParent {
sheetParent.endSheet(window, returnCode: .cancel)
} else if let presentingViewController = presentingViewController {
presentingViewController.dismiss(self)
} else if let popover = nextResponder as? NSPopover {
popover.close()
} else {
assertionFailure("dismiss() called not properly")
}
}
func addAndLayoutChild(_ vc: NSViewController, into containerView: NSView? = nil) {
assert(containerView == nil || sequence(first: containerView!, next: { $0.superview }).contains(self.view),
"\(containerView!) is not a part of \(self) view hierarchy")
self.addChild(vc)
(containerView ?? self.view).addAndLayout(vc.view)
}
func removeCompletely() {
guard parent != nil else { return }
removeFromParent()
view.removeFromSuperview()
}
/// #Preview helper to hide Window controls on View Controller appearance
func _preview_hidingWindowControlsOnAppear() -> Self { // swiftlint:disable:this identifier_name
Preview_ViewControllerWindowObserver().attach(to: self)
return self
}
}
func withoutAnimation(_ closure: () -> Void) {
CATransaction.begin()
CATransaction.setDisableActions(true)
closure()
CATransaction.commit()
}
/// #Preview helper to hide Window controls on View Controller appearance
final class Preview_ViewControllerWindowObserver: NSObject {
func attach(to viewController: NSViewController) {
// Start observing the view.window property
viewController.addObserver(self, forKeyPath: #keyPath(NSViewController.view.window), options: [.initial, .new], context: nil)
viewController.onDeinit {
withExtendedLifetime(self) {}
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
guard let window = change?[.newKey] as? NSWindow else { return }
window.titlebarAppearsTransparent = true
window.titleVisibility = .hidden
window.styleMask = []
}
}