From b028a9ed2ebcbc311d625a861df790590cb80d5c Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 24 Aug 2017 17:57:50 -0700 Subject: [PATCH] [kit] Show close button in prefs window if needed --- MetaballsKit/PreferencesViewController.swift | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/MetaballsKit/PreferencesViewController.swift b/MetaballsKit/PreferencesViewController.swift index 7f73c03..1d536f4 100644 --- a/MetaballsKit/PreferencesViewController.swift +++ b/MetaballsKit/PreferencesViewController.swift @@ -30,6 +30,11 @@ public class PreferencesViewController: NSViewController { } public var defaults = UserDefaults.standard + public var showsCloseButton: Bool = true { + didSet { + showCloseButtonIfNeeded() + } + } private var colorStackView = NSStackView() private var colorViews = [ColorView]() @@ -51,6 +56,24 @@ public class PreferencesViewController: NSViewController { return button }() + private lazy var closeView: NSView = { + let container = NSView() + container.translatesAutoresizingMaskIntoConstraints = false + + let buttonTitle = NSLocalizedString("Close", comment: "close button label") + let button = NSButton(title: buttonTitle, target: self, action: #selector(PreferencesViewController.closeWindow)) + button.translatesAutoresizingMaskIntoConstraints = false + + container.addSubview(button) + NSLayoutConstraint.activate([ + button.topAnchor.constraint(equalTo: container.topAnchor), + button.bottomAnchor.constraint(equalTo: container.bottomAnchor), + button.trailingAnchor.constraint(equalTo: container.trailingAnchor), + ]) + + return container + }() + override public func loadView() { let view = NSView() view.translatesAutoresizingMaskIntoConstraints = false @@ -84,6 +107,8 @@ public class PreferencesViewController: NSViewController { colorViews.append(colorView) } + showCloseButtonIfNeeded() + self.view = view } @@ -119,6 +144,14 @@ public class PreferencesViewController: NSViewController { colorPanel.setAction(#selector(PreferencesViewController.colorPanelDidUpdateValue)) } + private func showCloseButtonIfNeeded() { + if showsCloseButton { + colorStackView.addArrangedSubview(closeView) + } else { + colorStackView.removeArrangedSubview(closeView) + } + } + // MARK: - Actions func colorPanelDidUpdateValue(_ colorPanel: NSColorPanel) { @@ -130,6 +163,10 @@ public class PreferencesViewController: NSViewController { postColorNotification() } + func closeWindow() { + self.view.window?.close() + } + // MARK: - Private func updateColorViewVisibility() {