[kit] Show close button in prefs window if needed
This commit is contained in:
parent
5d6cf1a575
commit
b028a9ed2e
1 changed files with 37 additions and 0 deletions
|
@ -30,6 +30,11 @@ public class PreferencesViewController: NSViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
public var defaults = UserDefaults.standard
|
public var defaults = UserDefaults.standard
|
||||||
|
public var showsCloseButton: Bool = true {
|
||||||
|
didSet {
|
||||||
|
showCloseButtonIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private var colorStackView = NSStackView()
|
private var colorStackView = NSStackView()
|
||||||
private var colorViews = [ColorView]()
|
private var colorViews = [ColorView]()
|
||||||
|
@ -51,6 +56,24 @@ public class PreferencesViewController: NSViewController {
|
||||||
return button
|
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() {
|
override public func loadView() {
|
||||||
let view = NSView()
|
let view = NSView()
|
||||||
view.translatesAutoresizingMaskIntoConstraints = false
|
view.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
@ -84,6 +107,8 @@ public class PreferencesViewController: NSViewController {
|
||||||
colorViews.append(colorView)
|
colorViews.append(colorView)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showCloseButtonIfNeeded()
|
||||||
|
|
||||||
self.view = view
|
self.view = view
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,6 +144,14 @@ public class PreferencesViewController: NSViewController {
|
||||||
colorPanel.setAction(#selector(PreferencesViewController.colorPanelDidUpdateValue))
|
colorPanel.setAction(#selector(PreferencesViewController.colorPanelDidUpdateValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func showCloseButtonIfNeeded() {
|
||||||
|
if showsCloseButton {
|
||||||
|
colorStackView.addArrangedSubview(closeView)
|
||||||
|
} else {
|
||||||
|
colorStackView.removeArrangedSubview(closeView)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Actions
|
// MARK: - Actions
|
||||||
|
|
||||||
func colorPanelDidUpdateValue(_ colorPanel: NSColorPanel) {
|
func colorPanelDidUpdateValue(_ colorPanel: NSColorPanel) {
|
||||||
|
@ -130,6 +163,10 @@ public class PreferencesViewController: NSViewController {
|
||||||
postColorNotification()
|
postColorNotification()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func closeWindow() {
|
||||||
|
self.view.window?.close()
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Private
|
// MARK: - Private
|
||||||
|
|
||||||
func updateColorViewVisibility() {
|
func updateColorViewVisibility() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue