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.

101 lines
3.3 KiB

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