aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/View
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-01-02 01:07:46 +0530
committerRoopesh Chander <roop@roopc.net>2019-01-14 14:52:30 +0530
commit7934d6b0c798924c859a4b0d55f6561d48f6eaff (patch)
tree31ef34d4b653629eb6abfd79a8663fea3e6eda64 /WireGuard/WireGuard/UI/macOS/View
parentmacOS: Capitalize All Rights Reserved (diff)
downloadwireguard-apple-7934d6b0c798924c859a4b0d55f6561d48f6eaff.tar.xz
wireguard-apple-7934d6b0c798924c859a4b0d55f6561d48f6eaff.zip
macOS: Manage tunnels window: Tunnels list
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI/macOS/View')
-rw-r--r--WireGuard/WireGuard/UI/macOS/View/TunnelListCell.swift75
1 files changed, 75 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/View/TunnelListCell.swift b/WireGuard/WireGuard/UI/macOS/View/TunnelListCell.swift
new file mode 100644
index 0000000..11bf73c
--- /dev/null
+++ b/WireGuard/WireGuard/UI/macOS/View/TunnelListCell.swift
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import Cocoa
+
+class TunnelListCell: NSView {
+ var tunnel: TunnelContainer? {
+ didSet(value) {
+ // Bind to the tunnel's name
+ nameLabel.stringValue = tunnel?.name ?? ""
+ nameObservationToken = tunnel?.observe(\TunnelContainer.name) { [weak self] tunnel, _ in
+ self?.nameLabel.stringValue = tunnel.name
+ }
+ // Bind to the tunnel's status
+ statusImageView.image = TunnelListCell.image(for: tunnel?.status)
+ statusObservationToken = tunnel?.observe(\TunnelContainer.status) { [weak self] tunnel, _ in
+ self?.statusImageView.image = TunnelListCell.image(for: tunnel.status)
+ }
+ }
+ }
+
+ let nameLabel: NSTextField = {
+ let nameLabel = NSTextField()
+ nameLabel.isEditable = false
+ nameLabel.isSelectable = false
+ nameLabel.isBordered = false
+ nameLabel.maximumNumberOfLines = 1
+ nameLabel.lineBreakMode = .byTruncatingTail
+ return nameLabel
+ }()
+
+ let statusImageView = NSImageView()
+
+ private var statusObservationToken: AnyObject?
+ private var nameObservationToken: AnyObject?
+
+ init() {
+ super.init(frame: CGRect.zero)
+
+ addSubview(statusImageView)
+ addSubview(nameLabel)
+ statusImageView.translatesAutoresizingMaskIntoConstraints = false
+ nameLabel.translatesAutoresizingMaskIntoConstraints = false
+ nameLabel.backgroundColor = .clear
+ NSLayoutConstraint.activate([
+ self.leadingAnchor.constraint(equalTo: statusImageView.leadingAnchor),
+ statusImageView.trailingAnchor.constraint(equalTo: nameLabel.leadingAnchor),
+ statusImageView.widthAnchor.constraint(equalToConstant: 20),
+ nameLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor),
+ statusImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
+ nameLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor)
+ ])
+ }
+
+ required init?(coder decoder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ static func image(for status: TunnelStatus?) -> NSImage? {
+ guard let status = status else { return nil }
+ switch status {
+ case .active, .restarting, .reasserting:
+ return NSImage(named: NSImage.statusAvailableName)
+ case .activating, .waiting:
+ return NSImage(named: NSImage.statusPartiallyAvailableName)
+ case .deactivating, .inactive:
+ return nil
+ }
+ }
+
+ override func prepareForReuse() {
+ nameLabel.stringValue = ""
+ statusImageView.image = nil
+ }
+}