// // DocHubControllers.swift // DocHub // // Created by Tristan Juricek on 6/10/14. // Copyright (c) 2014 Perforce. All rights reserved. // import Foundation import AppKit // The controller deals with nearly all "application" events from the different // views, well, since there's really only supposed to be one window. class DocHubController : NSObject, NSApplicationDelegate, NSSplitViewDelegate, LibraryViewDelegate { var window: NSWindow var mainSplitView: NSSplitView var librarySourceListController: LibrarySourceListController var libraryView: LibraryView var navigatorController: NavigatorController var toolbar: NSToolbar var toolbarDelegate: DocHubToolbarDelegate? var addPerforceController: AddPerforceController? = nil let docHubLocation = DocHubLocation() init() { let winRect = NSRect(x: 50, y: 150, width: 1024, height: 800) window = NSWindow(contentRect: winRect, styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing: NSBackingStoreType.Buffered, defer: false) mainSplitView = NSSplitView(frame: NSRect(x:0, y:0, width: 1024, height:800)) librarySourceListController = LibrarySourceListController(nibName: "LibrarySourceListView", bundle: nil, docHubLocation: docHubLocation) libraryView = LibraryView(frame: NSRect(x: 0, y: 0, width: 200, height:800), sourceList: librarySourceListController.view) navigatorController = NavigatorController(nibName: "NavigatorView", bundle: nil) toolbar = NSToolbar(identifier: "DocHubToolbar") super.init() libraryView.delegate = self } func applicationDidFinishLaunching(aNotification: NSNotification?) { docHubLocation.libraryObserver.listeners.append({ self.libraryChanged($0) }) docHubLocation.areaObserver.listeners.append({ self.areaChanged($0) }) toolbarDelegate = DocHubToolbarDelegate(mainController:self) toolbar.delegate = toolbarDelegate toolbar.sizeMode = NSToolbarSizeMode.Regular toolbar.displayMode = NSToolbarDisplayMode.IconOnly window.toolbar = toolbar let libraryItem = toolbar.items .findFirst({ $0.itemIdentifier == DocHubLibraryToolbarItemIdentifier }) as DocHubToolbarDelegate.LibraryItem libraryItem.selected = true mainSplitView.dividerStyle = NSSplitViewDividerStyle.Thin mainSplitView.vertical = true mainSplitView.delegate = self mainSplitView.autoresizingMask = NSAutoresizingMaskOptions.ViewHeightSizable | NSAutoresizingMaskOptions.ViewWidthSizable mainSplitView.addSubview(libraryView) mainSplitView.addSubview(navigatorController.view) window.contentView.addSubview(mainSplitView) window.title = NSLocalizedString("Document Hub", comment:"window.title") window.makeKeyAndOrderFront(self) mainSplitView.setPosition(200, ofDividerAtIndex: 0) } func libraryChanged(newLib:Library) { window.title = newLib.name } func areaChanged(area:Area) { window.title = area.name } func applicationWillTerminate(aNotification: NSNotification?) { // Insert code here to tear down your application } func toggleLibrary() { let isCollapsed = mainSplitView.isSubviewCollapsed(libraryView) if isCollapsed { uncollapseLibrary() } else { collapseLibrary() } } func uncollapseLibrary() { mainSplitView.setPosition(200, ofDividerAtIndex: 0) libraryView.hidden = false let libraryItem = toolbar.items .findFirst({ $0.itemIdentifier == DocHubLibraryToolbarItemIdentifier }) as DocHubToolbarDelegate.LibraryItem libraryItem.selected = true } func collapseLibrary() { mainSplitView.setPosition(0, ofDividerAtIndex: 0) libraryView.hidden = true let libraryItem = toolbar.items .findFirst({ $0.itemIdentifier == DocHubLibraryToolbarItemIdentifier }) as DocHubToolbarDelegate.LibraryItem libraryItem.selected = false } func splitView(splitView: NSSplitView!, canCollapseSubview subview: NSView!) -> Bool { return subview === libraryView } func splitView(splitView: NSSplitView!, shouldCollapseSubview subview: NSView!, forDoubleClickOnDividerAtIndex dividerIndex: Int) -> Bool { return dividerIndex == 0 } func addPerforceServer() { addPerforceController = AddPerforceController(windowNibName:"AddPerforceSheet") NSApp.beginSheet(addPerforceController!.window, modalForWindow: window, modalDelegate: self, didEndSelector: "addPerforceServerDone", contextInfo: nil) } func addPerforceServerDone() { addPerforceController!.window.orderOut(addPerforceController!) addPerforceController = nil librarySourceListController.reload() } }