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.

109 lines
3.5 KiB

// Copyright © 2015 Abhishek Banthia
import Cocoa
class CenteredTabViewController: NSTabViewController {
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 {
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()
3 years ago
window?.identifier = NSUserInterfaceItemIdentifier("Preferences")
}
3 years ago
private func setupToolbarImages() {
guard let tabViewController = contentViewController as? CenteredTabViewController else {
return
}
5 years ago
let themer = Themer.shared()
var identifierTOImageMapping: [String: NSImage] = ["Appearance Tab": themer.appearanceTabImage(),
"Calendar Tab": themer.calendarTabImage(),
"Permissions Tab": themer.privacyTabImage()]
3 years ago
if let prefsTabImage = themer.generalTabImage() {
identifierTOImageMapping["Preferences Tab"] = prefsTabImage
}
3 years ago
if let aboutTabImage = themer.aboutTabImage() {
identifierTOImageMapping["About Tab"] = aboutTabImage
}
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]
}
}
}
3 years ago
// MARK: Public
3 years ago
3 years ago
func openPermissionsPane() {
openPreferenceTab(at: 3)
NSApp.activate(ignoringOtherApps: true)
}
3 years ago
3 years ago
// Action mapped to the + button in the PanelController. We should always open the General Pane when the + button is clicked.
func openGeneralPane() {
openPreferenceTab(at: 0)
NSApp.activate(ignoringOtherApps: true)
}
3 years ago
3 years ago
private func openPreferenceTab(at index: Int) {
guard let window = window else {
return
}
if !window.isMainWindow || !window.isVisible {
showWindow(nil)
}
guard let tabViewController = contentViewController as? CenteredTabViewController else {
return
}
tabViewController.selectedTabViewItemIndex = index
}
}