You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.6 KiB

// Copyright © 2015 Abhishek Banthia
import Cocoa
class CenteredTabViewController: NSTabViewController {
override func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
super.toolbarDefaultItemIdentifiers(toolbar)
6 years ago
var toolbarItems: [NSToolbarItem.Identifier] = [NSToolbarItem.Identifier.flexibleSpace]
6 years ago
6 years ago
tabViewItems.forEach { item in
if let identifier = item.identifier as? String {
6 years ago
toolbarItems.append(NSToolbarItem.Identifier(identifier))
}
}
6 years ago
toolbarItems.append(NSToolbarItem.Identifier.flexibleSpace)
return toolbarItems
}
override func viewDidLoad() {
super.viewDidLoad()
// Setup localized tab labels
tabViewItems.forEach { item in
if let identifier = item.identifier as? String {
item.label = NSLocalizedString(identifier, comment: "Tab View Item Label for \(identifier)")
}
}
}
}
class OneWindowController: NSWindowController {
private static var sharedWindow: OneWindowController!
6 years ago
private var themeDidChangeNotification: NSObjectProtocol?
6 years ago
override func windowDidLoad() {
super.windowDidLoad()
setup()
6 years ago
themeDidChangeNotification = NotificationCenter.default.addObserver(forName: .themeDidChangeNotification, object: nil, queue: OperationQueue.main) { _ in
6 years ago
6 years ago
NSAnimationContext.runAnimationGroup { context in
context.duration = 1
6 years ago
context.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
self.window?.animator().backgroundColor = Themer.shared().mainBackgroundColor()
6 years ago
}
self.setupToolbarImages()
}
}
6 years ago
6 years ago
deinit {
if let themeDidChangeNotif = themeDidChangeNotification {
NotificationCenter.default.removeObserver(themeDidChangeNotif)
}
}
private func setup() {
setupWindow()
setupToolbarImages()
}
6 years ago
private func setupWindow() {
window?.titlebarAppearsTransparent = true
window?.backgroundColor = Themer.shared().mainBackgroundColor()
}
6 years ago
class func shared() -> OneWindowController {
6 years ago
if sharedWindow == nil {
6 years ago
let prefStoryboard = NSStoryboard(name: "Preferences", bundle: nil)
sharedWindow = prefStoryboard.instantiateInitialController() as? OneWindowController
}
return sharedWindow
}
6 years ago
func openPermissions() {
guard let window = window else {
return
}
6 years ago
6 years ago
if !window.isMainWindow || !window.isVisible {
showWindow(nil)
}
6 years ago
guard let tabViewController = contentViewController as? CenteredTabViewController else {
return
}
6 years ago
tabViewController.selectedTabViewItemIndex = 3
}
6 years ago
private func setupToolbarImages() {
guard let tabViewController = contentViewController as? CenteredTabViewController else {
return
}
6 years ago
let themer = Themer.shared()
let identifierTOImageMapping: [String: NSImage] = ["Appearance": themer.appearanceTabImage(),
"Calendar": themer.calendarTabImage(),
"Permissions": themer.privacyTabImage()]
6 years ago
6 years ago
tabViewController.tabViewItems.forEach { tabViewItem in
let identity = (tabViewItem.identifier as? String) ?? ""
6 years ago
if identifierTOImageMapping[identity] != nil {
tabViewItem.image = identifierTOImageMapping[identity]
}
}
}
}