2019-01-01 19:37:46 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-17 14:56:46 +00:00
|
|
|
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
2019-01-01 19:37:46 +00:00
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class ManageTunnelsRootViewController: NSViewController {
|
|
|
|
|
|
|
|
let tunnelsManager: TunnelsManager
|
2019-01-04 13:03:46 +00:00
|
|
|
var tunnelsListVC: TunnelsListTableViewController?
|
2019-01-16 20:14:40 +00:00
|
|
|
var tunnelDetailVC: TunnelDetailTableViewController?
|
2019-01-02 19:46:27 +00:00
|
|
|
let tunnelDetailContainerView = NSView()
|
|
|
|
var tunnelDetailContentVC: NSViewController?
|
2019-01-01 19:37:46 +00:00
|
|
|
|
|
|
|
init(tunnelsManager: TunnelsManager) {
|
|
|
|
self.tunnelsManager = tunnelsManager
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func loadView() {
|
|
|
|
view = NSView()
|
|
|
|
|
2019-01-03 14:04:50 +00:00
|
|
|
let horizontalSpacing: CGFloat = 20
|
2019-01-01 19:37:46 +00:00
|
|
|
let verticalSpacing: CGFloat = 20
|
2019-01-03 14:04:50 +00:00
|
|
|
let centralSpacing: CGFloat = 10
|
2019-01-01 19:37:46 +00:00
|
|
|
|
|
|
|
let container = NSLayoutGuide()
|
|
|
|
view.addLayoutGuide(container)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
container.topAnchor.constraint(equalTo: view.topAnchor, constant: verticalSpacing),
|
|
|
|
view.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: verticalSpacing),
|
|
|
|
container.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: horizontalSpacing),
|
|
|
|
view.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: horizontalSpacing)
|
|
|
|
])
|
|
|
|
|
2019-01-04 13:03:46 +00:00
|
|
|
tunnelsListVC = TunnelsListTableViewController(tunnelsManager: tunnelsManager)
|
|
|
|
tunnelsListVC!.delegate = self
|
|
|
|
let tunnelsListView = tunnelsListVC!.view
|
2019-01-01 19:37:46 +00:00
|
|
|
|
2019-01-04 13:03:46 +00:00
|
|
|
addChild(tunnelsListVC!)
|
2019-01-01 19:37:46 +00:00
|
|
|
view.addSubview(tunnelsListView)
|
2019-01-02 19:46:27 +00:00
|
|
|
view.addSubview(tunnelDetailContainerView)
|
2019-01-01 19:37:46 +00:00
|
|
|
|
|
|
|
tunnelsListView.translatesAutoresizingMaskIntoConstraints = false
|
2019-01-02 19:46:27 +00:00
|
|
|
tunnelDetailContainerView.translatesAutoresizingMaskIntoConstraints = false
|
2019-01-01 19:37:46 +00:00
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
tunnelsListView.topAnchor.constraint(equalTo: container.topAnchor),
|
|
|
|
tunnelsListView.bottomAnchor.constraint(equalTo: container.bottomAnchor),
|
|
|
|
tunnelsListView.leadingAnchor.constraint(equalTo: container.leadingAnchor),
|
2019-01-02 19:46:27 +00:00
|
|
|
tunnelDetailContainerView.topAnchor.constraint(equalTo: container.topAnchor),
|
|
|
|
tunnelDetailContainerView.bottomAnchor.constraint(equalTo: container.bottomAnchor),
|
2019-01-03 14:04:50 +00:00
|
|
|
tunnelDetailContainerView.leadingAnchor.constraint(equalTo: tunnelsListView.trailingAnchor, constant: centralSpacing),
|
2019-02-15 10:10:08 +00:00
|
|
|
tunnelDetailContainerView.trailingAnchor.constraint(equalTo: container.trailingAnchor)
|
2019-01-01 19:37:46 +00:00
|
|
|
])
|
|
|
|
}
|
2019-01-02 19:46:27 +00:00
|
|
|
|
|
|
|
private func setTunnelDetailContentVC(_ contentVC: NSViewController) {
|
|
|
|
if let currentContentVC = tunnelDetailContentVC {
|
|
|
|
currentContentVC.view.removeFromSuperview()
|
|
|
|
currentContentVC.removeFromParent()
|
|
|
|
}
|
|
|
|
addChild(contentVC)
|
|
|
|
tunnelDetailContainerView.addSubview(contentVC.view)
|
|
|
|
contentVC.view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
tunnelDetailContainerView.topAnchor.constraint(equalTo: contentVC.view.topAnchor),
|
|
|
|
tunnelDetailContainerView.bottomAnchor.constraint(equalTo: contentVC.view.bottomAnchor),
|
|
|
|
tunnelDetailContainerView.leadingAnchor.constraint(equalTo: contentVC.view.leadingAnchor),
|
|
|
|
tunnelDetailContainerView.trailingAnchor.constraint(equalTo: contentVC.view.trailingAnchor)
|
|
|
|
])
|
|
|
|
tunnelDetailContentVC = contentVC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ManageTunnelsRootViewController: TunnelsListTableViewControllerDelegate {
|
2019-03-10 14:32:19 +00:00
|
|
|
func tunnelsSelected(tunnelIndices: [Int]) {
|
|
|
|
assert(!tunnelIndices.isEmpty)
|
|
|
|
if tunnelIndices.count == 1 {
|
|
|
|
let tunnel = tunnelsManager.tunnel(at: tunnelIndices.first!)
|
2019-06-11 00:03:11 +00:00
|
|
|
if tunnel.isTunnelAvailableToUser {
|
2019-04-03 11:02:12 +00:00
|
|
|
let tunnelDetailVC = TunnelDetailTableViewController(tunnelsManager: tunnelsManager, tunnel: tunnel)
|
|
|
|
setTunnelDetailContentVC(tunnelDetailVC)
|
|
|
|
self.tunnelDetailVC = tunnelDetailVC
|
|
|
|
} else {
|
2019-04-04 06:54:25 +00:00
|
|
|
let unusableTunnelDetailVC = tunnelDetailContentVC as? UnusableTunnelDetailViewController ?? UnusableTunnelDetailViewController()
|
2019-04-03 11:02:12 +00:00
|
|
|
unusableTunnelDetailVC.onButtonClicked = { [weak tunnelsListVC] in
|
|
|
|
tunnelsListVC?.handleRemoveTunnelAction()
|
|
|
|
}
|
|
|
|
setTunnelDetailContentVC(unusableTunnelDetailVC)
|
|
|
|
self.tunnelDetailVC = nil
|
|
|
|
}
|
2019-03-10 14:32:19 +00:00
|
|
|
} else if tunnelIndices.count > 1 {
|
2019-04-04 06:54:25 +00:00
|
|
|
let multiSelectionVC = tunnelDetailContentVC as? ButtonedDetailViewController ?? ButtonedDetailViewController()
|
2019-03-10 14:32:19 +00:00
|
|
|
multiSelectionVC.setButtonTitle(tr(format: "macButtonDeleteTunnels (%d)", tunnelIndices.count))
|
|
|
|
multiSelectionVC.onButtonClicked = { [weak tunnelsListVC] in
|
|
|
|
tunnelsListVC?.handleRemoveTunnelAction()
|
|
|
|
}
|
|
|
|
setTunnelDetailContentVC(multiSelectionVC)
|
|
|
|
self.tunnelDetailVC = nil
|
|
|
|
}
|
2019-01-02 19:46:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 12:54:45 +00:00
|
|
|
func tunnelsListEmpty() {
|
2019-03-10 11:02:26 +00:00
|
|
|
let noTunnelsVC = ButtonedDetailViewController()
|
|
|
|
noTunnelsVC.setButtonTitle(tr("macButtonImportTunnels"))
|
|
|
|
noTunnelsVC.onButtonClicked = { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
ImportPanelPresenter.presentImportPanel(tunnelsManager: self.tunnelsManager, sourceVC: self)
|
|
|
|
}
|
2019-01-05 12:54:45 +00:00
|
|
|
setTunnelDetailContentVC(noTunnelsVC)
|
2019-01-16 20:14:40 +00:00
|
|
|
self.tunnelDetailVC = nil
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 13:22:58 +00:00
|
|
|
|
|
|
|
extension ManageTunnelsRootViewController {
|
|
|
|
override func supplementalTarget(forAction action: Selector, sender: Any?) -> Any? {
|
|
|
|
switch action {
|
|
|
|
case #selector(TunnelsListTableViewController.handleViewLogAction),
|
|
|
|
#selector(TunnelsListTableViewController.handleAddEmptyTunnelAction),
|
|
|
|
#selector(TunnelsListTableViewController.handleImportTunnelAction),
|
|
|
|
#selector(TunnelsListTableViewController.handleExportTunnelsAction),
|
|
|
|
#selector(TunnelsListTableViewController.handleRemoveTunnelAction):
|
|
|
|
return tunnelsListVC
|
|
|
|
case #selector(TunnelDetailTableViewController.handleToggleActiveStatusAction),
|
|
|
|
#selector(TunnelDetailTableViewController.handleEditTunnelAction):
|
|
|
|
return tunnelDetailVC
|
|
|
|
default:
|
|
|
|
return super.supplementalTarget(forAction: action, sender: sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|