100 lines
3.2 KiB
Swift
100 lines
3.2 KiB
Swift
//
|
|
// WindowController.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Murad Shabanov on 20.07.2021.
|
|
// Copyright © 2021 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension NSWindow.FrameAutosaveName {
|
|
static let preferences: NSWindow.FrameAutosaveName = "io.privado.preferences.FrameAutosaveName"
|
|
}
|
|
|
|
final class WindowController: NSWindowController {
|
|
|
|
private var viewController: PreferencesViewController?
|
|
|
|
init(panes: [PreferencePane], output: PreferenceOutputProtocol, controller: PreferencesViewController) {
|
|
let window = UserInteractionPausableWindow(
|
|
// temp contentRect
|
|
contentRect: NSRect(x: 0, y: 0, width: 660, height: 560), styleMask: [ .titled, .closable ],
|
|
backing: .buffered,
|
|
defer: true
|
|
)
|
|
|
|
let applicationView = PreferencesView(frame: NSRect(origin: .zero, size: NSSize(width: 660, height: 560)))
|
|
|
|
let navigationController = JSNavigationController(rootViewController: controller, viewDelegate: applicationView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
applicationView.widthAnchor.constraint(equalToConstant: 660),
|
|
applicationView.heightAnchor.constraint(equalToConstant: 560)
|
|
])
|
|
|
|
navigationController.preferredContentSize = NSSize(width: 660, height: 560)
|
|
navigationController.view.addSubview(applicationView)
|
|
|
|
super.init(window: window)
|
|
|
|
self.viewController = controller
|
|
self.navigationController = navigationController
|
|
|
|
window.delegate = self
|
|
window.titleVisibility = .hidden
|
|
window.isOpaque = true
|
|
window.backgroundColor = .clear
|
|
window.contentViewController = navigationController
|
|
|
|
|
|
window.titlebarAppearsTransparent = true
|
|
window.styleMask.insert(.fullSizeContentView)
|
|
|
|
self.viewController?.children = panes
|
|
self.viewController?.preferencePanes = panes
|
|
}
|
|
|
|
var navigationController: JSNavigationController?
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func show(preferencePane preferenceIdentifier: PreferencePane.Identifier? = nil) {
|
|
|
|
// if let preferenceIdentifier = preferenceIdentifier {
|
|
// self.viewController?.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false)
|
|
// } else {
|
|
// self.viewController?.restoreInitialTab()
|
|
// }
|
|
|
|
self.viewController?.restoreInitialTab()
|
|
|
|
self.showWindow(self)
|
|
self.restoreWindowPosition()
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
}
|
|
|
|
private func restoreWindowPosition() {
|
|
guard let window = self.window
|
|
, let screen = NSScreen.main else {
|
|
return
|
|
}
|
|
|
|
window.setFrameOrigin(CGPoint(
|
|
x: screen.visibleFrame.midX - window.frame.width / 2,
|
|
y: screen.visibleFrame.midY - window.frame.height / 2
|
|
))
|
|
window.setFrameUsingName(.preferences)
|
|
window.setFrameAutosaveName(.preferences)
|
|
}
|
|
}
|
|
|
|
extension WindowController: NSWindowDelegate {
|
|
|
|
func windowShouldClose(_ sender: NSWindow) -> Bool {
|
|
return true
|
|
}
|
|
}
|