aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/View/PopupRow.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-01-09 04:14:08 +0530
committerRoopesh Chander <roop@roopc.net>2019-01-14 14:52:36 +0530
commit13e8c6b1780eab5bd7257ba79020651e421f099c (patch)
tree0478cdfc35591b0785532d70aeeb66689ebdfb6d /WireGuard/WireGuard/UI/macOS/View/PopupRow.swift
parentmacOS: Quit menu item (diff)
downloadwireguard-apple-13e8c6b1780eab5bd7257ba79020651e421f099c.tar.xz
wireguard-apple-13e8c6b1780eab5bd7257ba79020651e421f099c.zip
macOS: Support for on-demand activation
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI/macOS/View/PopupRow.swift')
-rw-r--r--WireGuard/WireGuard/UI/macOS/View/PopupRow.swift75
1 files changed, 75 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/View/PopupRow.swift b/WireGuard/WireGuard/UI/macOS/View/PopupRow.swift
new file mode 100644
index 0000000..2ef55f5
--- /dev/null
+++ b/WireGuard/WireGuard/UI/macOS/View/PopupRow.swift
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import Cocoa
+
+class PopupRow: NSView {
+ let keyLabel: NSTextField = {
+ let keyLabel = NSTextField()
+ keyLabel.isEditable = false
+ keyLabel.isSelectable = false
+ keyLabel.isBordered = false
+ keyLabel.alignment = .right
+ keyLabel.maximumNumberOfLines = 1
+ keyLabel.lineBreakMode = .byTruncatingTail
+ keyLabel.backgroundColor = .clear
+ return keyLabel
+ }()
+
+ let valuePopup = NSPopUpButton()
+
+ var key: String {
+ get { return keyLabel.stringValue }
+ set(value) { keyLabel.stringValue = value }
+ }
+
+ var valueOptions: [String] {
+ get { return valuePopup.itemTitles }
+ set(value) {
+ valuePopup.removeAllItems()
+ valuePopup.addItems(withTitles: value)
+ }
+ }
+
+ var selectedOptionIndex: Int {
+ get { return valuePopup.indexOfSelectedItem }
+ set(value) { valuePopup.selectItem(at: value) }
+ }
+
+ override var intrinsicContentSize: NSSize {
+ let height = max(keyLabel.intrinsicContentSize.height, valuePopup.intrinsicContentSize.height)
+ return NSSize(width: NSView.noIntrinsicMetric, height: height)
+ }
+
+ init() {
+ super.init(frame: CGRect.zero)
+
+ addSubview(keyLabel)
+ addSubview(valuePopup)
+ keyLabel.translatesAutoresizingMaskIntoConstraints = false
+ valuePopup.translatesAutoresizingMaskIntoConstraints = false
+
+ NSLayoutConstraint.activate([
+ keyLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor),
+ keyLabel.firstBaselineAnchor.constraint(equalTo: valuePopup.firstBaselineAnchor),
+ self.leadingAnchor.constraint(equalTo: keyLabel.leadingAnchor),
+ keyLabel.trailingAnchor.constraint(equalTo: valuePopup.leadingAnchor, constant: -5)
+ ])
+
+ keyLabel.setContentCompressionResistancePriority(.defaultHigh + 2, for: .horizontal)
+ keyLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
+
+ let widthConstraint = keyLabel.widthAnchor.constraint(equalToConstant: 150)
+ widthConstraint.priority = .defaultHigh + 1
+ widthConstraint.isActive = true
+ }
+
+ required init?(coder decoder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ override func prepareForReuse() {
+ key = ""
+ valueOptions = []
+ }
+}