123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824 |
- //
- // KMCloudDocumentsViewController.swift
- // PDF Master
- //
- // Created by wanjun on 2022/11/23.
- //
- import Cocoa
- enum KMCloudDocumentState : Int {
- case Box = 0
- case DropBox
- case GoogleDrive
- case OneDrive
-
- public func string() -> String {
- switch self {
- case .Box:
- return "Box"
- case .DropBox:
- return "DropBox"
- case .GoogleDrive:
- return "GoogleDrive"
- case .OneDrive:
- return "OneDrive"
- }
- }
- }
- class KMCloudAccountListTableviewCell: NSTableCellView {
- @IBOutlet weak var accountLabel: NSTextField!
- }
- class KMCloudFileNameListTableviewCell: NSTableCellView {
- @IBOutlet weak var fileImageView: NSImageView!
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var subtitleLabel: NSTextField!
- }
- class KMCloudFileModifyTimeListTableviewCell: NSTableCellView {
- @IBOutlet weak var modifyTimeLabel: NSTextField!
- }
- class KMCloudFileSizeListTableviewCell: NSTableCellView {
- @IBOutlet weak var sizeLabel: NSTextField!
- }
- class KMCloudDocumentsViewController: NSViewController {
-
- @IBOutlet weak var topBox: NSBox!
- @IBOutlet weak var cloudDocumentsLabel: NSTextField!
- @IBOutlet weak var addCloudsBox: KMBox!
- @IBOutlet weak var addCloudsLabel: NSTextField!
- @IBOutlet weak var addCloudsImageView: NSImageView!
- @IBOutlet weak var mainBox: NSBox!
- @IBOutlet weak var emptyScrollView: NSScrollView!
- @IBOutlet weak var emptyCollectionView: KMCollectionView!
- @IBOutlet weak var cloudSplitView: NSSplitView!
- @IBOutlet weak var cloudLeftBox: NSBox!
- @IBOutlet weak var cloudRightBox: NSBox!
- @IBOutlet weak var accountListTableView: NSTableView!
- @IBOutlet weak var fileListTableView: KMMenuTableView!
- @IBOutlet weak var fileNameTableColumn: NSTableColumn!
- @IBOutlet weak var fileModifyTimeTableColumn: NSTableColumn!
- @IBOutlet weak var fileSizeTableColumn: NSTableColumn!
-
- @IBOutlet weak var pathControl: NSPathControl!
-
- @IBOutlet weak var pathBox: NSBox!
- var myPathControl = KMCloudPathControl()
- var pathString: String = ""
-
- var cloudPlates: [KMCloudDocumentState] = [.DropBox, .GoogleDrive]
- var accounts: [Any] = []
- var cloudFiles: [Any] = []
- var currentAccount: KMCloudServer?
- var filepaths: [KMServicesCloudFile] = []
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- let serverArr = KMCloudServer.getAllLoginServer()
- for server in serverArr! {
- self.accounts.append(server)
- }
- emptyCollectionView.register(KMCloudEmptyCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMCloudEmptyCollectionViewItem"))
- emptyCollectionView.isSelectable = true
- self.fileListTableView.menuDelegate = self
-
- initializeUI()
- initLocalization()
-
- self.pathControl.url = URL(fileURLWithPath: "Cloud Documents\\Test\\Test2")
- self.pathControl.pathStyle = .popUp
-
- addCloudsBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
- if self != nil {
- if downEntered {
- self!.addCloudsAction()
- }
- }
- }
- }
-
- override func viewDidAppear() {
- super.viewDidAppear()
-
- reloadData()
- }
-
- override func viewWillLayout() {
- super.viewWillLayout()
-
- if (self.emptyCollectionView.isHidden == false) {
- self.emptyCollectionView.reloadData()
- }
- }
-
- // MARK: Init
- func initializeUI() -> Void {
- let menu = NSMenu()
- let item = NSMenuItem(title: NSLocalizedString("Delete", comment: ""), action: #selector(accountListTableView_itemClick), target: self)
- menu.addItem(item!)
- self.accountListTableView.menu = menu
- self.accountListTableView.allowsEmptySelection = false
-
- self.fileListTableView.doubleAction = #selector(fileListTableViewDoubleAction)
-
- let pathControl = self.myPathControl
- self.pathBox.contentView?.addSubview(pathControl)
- pathControl.frame = self.pathBox.contentView!.bounds
- pathControl.autoresizingMask = [.width, .height]
- self.pathString = NSLocalizedString("Cloud Documents", comment: "")
- pathControl.urlString = self.pathString
- pathControl.itemClick = { [weak self] index in
- if (index == 0) {
- self!.updateFilesList((self?.currentAccount)!, nil)
- } else {
- self!.updateFilesList((self?.currentAccount)!, self?.filepaths[index-1])
- }
- }
- }
-
- func initLocalization() -> Void {
- cloudDocumentsLabel.stringValue = NSLocalizedString("Cloud Documents", comment: "")
- addCloudsLabel.stringValue = NSLocalizedString("Add Account", comment: "")
- addCloudsBox.toolTip = NSLocalizedString("Add Account", comment: "")
-
- fileNameTableColumn.title = NSLocalizedString("File Name", comment: "")
- fileModifyTimeTableColumn.title = NSLocalizedString("Change time", comment: "")
- fileSizeTableColumn.title = NSLocalizedString("Size", comment: "")
- }
-
- // MARK: private
-
- func reloadData() -> Void {
- DispatchQueue.main.async {
- if (KMCloudServer.isConnectionAvailable()) {
- self.hiddenNotNetworkView()
-
- self.emptyCollectionView.isHidden = true
- self.cloudSplitView.isHidden = true
- if self.accounts.count > 0 {
- self.cloudSplitView.isHidden = false
-
- self.accountListTableView.reloadData()
- self.fileListTableView.reloadData()
- } else {
- self.emptyCollectionView.isHidden = false
- self.emptyCollectionView.reloadData()
- }
- } else {
- self.showNotNetworkView()
- }
- }
- }
- // MARK: Action
-
- func loginCloud(state: KMCloudDocumentState) -> Void {
- switch state {
- case .Box:
- break
- case .DropBox:
- let serverArray = KMCloudServer.getAllLoginServer()
- var dropboxIsLogin: Bool = false
- for server in serverArray! {
- if (server.serverType == KMDropbox) {
- dropboxIsLogin = true
- break
- }
- }
- if (dropboxIsLogin) {
- self.logoutAccount(self.currentAccount!)
- } else {
- let server = KMCloudServer(cloudWith: KMDropbox)
- server?.authorizedLoginCompletion({ cloudFiles, finished in
- if (!finished) { /// 登录失败
- return
- }
- let serverArr = KMCloudServer.getAllLoginServer()
- var currentServer: KMCloudServer?
- for server in serverArr! {
- if ((server as! KMCloudServer).serverType == KMDropbox) {
- currentServer = server
- break
- }
- }
- // let userData: KMDropboxUserMetadata = cloudFiles as! KMDropboxUserMetadata
- if (currentServer != nil) {
- self.accounts.append(currentServer)
- self.reloadData()
- }
- })
- }
- break
- case .GoogleDrive:
- let serverArray = KMCloudServer.getAllLoginServer()
- var isLogin: Bool = false
- for server in serverArray! {
- if (server.serverType == KMGoogleDrive) {
- isLogin = true
- break
- }
- }
- if (isLogin) {
- self.logoutAccount(self.currentAccount!)
- } else {
- let server = KMCloudServer(cloudWith: KMGoogleDrive)
- server?.authorizedLoginCompletion({ cloudFiles, finished in
- if (!finished) { /// 登录失败
- return
- }
- let serverArr = KMCloudServer.getAllLoginServer()
- var currentServer: KMCloudServer?
- for server in serverArr! {
- if ((server as! KMCloudServer).serverType == KMGoogleDrive) {
- currentServer = server
- break
- }
- }
- // let userData: KMDropboxUserMetadata = cloudFiles as! KMDropboxUserMetadata
- if (currentServer != nil) {
- self.accounts.append(currentServer)
- self.reloadData()
- }
- })
- }
- break
- case .OneDrive:
- break
- }
- }
-
- func addCloudsAction() -> Void {
- var popViewDataArr: [String] = []
- for plate in self.cloudPlates {
- popViewDataArr.append(plate.string())
- }
- let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(popViewDataArr)
- let createFilePopover: NSPopover = NSPopover.init()
- createFilePopover.contentViewController = vc
- createFilePopover.animates = true
- createFilePopover.behavior = .transient
- createFilePopover.setValue(true, forKey: "shouldHideAnchor")
- createFilePopover.show(relativeTo: CGRect(x: addCloudsBox.bounds.origin.x, y: 10, width: addCloudsBox.bounds.size.width, height: addCloudsBox.bounds.size.height), of: addCloudsBox, preferredEdge: .minY)
-
- vc.downCallback = { [weak self](downEntered: Bool, count: String) -> Void in
- if self != nil {
- if downEntered {
- var state: KMCloudDocumentState = .Box
- if count == NSLocalizedString("Box", comment: "") {
- state = .Box
- } else if count == NSLocalizedString("DropBox", comment: "") {
- state = .DropBox
- } else if count == NSLocalizedString("GoogleDrive", comment: "") {
- state = .GoogleDrive
- } else if count == NSLocalizedString("OneDrive", comment: "") {
- state = .OneDrive
- }
- self!.loginCloud(state: state)
- }
- }
- }
- }
-
- private func showNotNetworkView() {
- self.hiddenNotNetworkView()
-
- self.emptyCollectionView.isHidden = true
- self.cloudSplitView.isHidden = true
-
- let superview = self.emptyCollectionView.superview
-
- let view = KMCloudNoNetworkView()
- let viewH: CGFloat = 220
- view.frame = NSMakeRect(0, (NSHeight(superview!.bounds)-viewH)*0.5, (superview?.bounds.size.width)!, viewH)
- view.autoresizingMask = [.width, .minYMargin]
- superview?.addSubview(view)
-
- view.itemClick = { [weak self] index in
- /// 刷新
- self?.hiddenNotNetworkView()
- self?.reloadData()
- }
- }
-
- private func hiddenNotNetworkView() {
- let superview = self.emptyCollectionView.superview
- var flagView: KMCloudNoNetworkView?
- for view in superview!.subviews {
- if (view is KMCloudNoNetworkView) {
- flagView = (view as! KMCloudNoNetworkView)
- break
- }
- }
-
- if (flagView == nil) {
- return
- }
-
- flagView?.removeFromSuperview()
- }
-
- private func showCloudFileEmptyView() {
- self.hiddenCloudFileEmptyView()
-
- let superview = self.fileListTableView.enclosingScrollView?.superview
- self.fileListTableView.enclosingScrollView!.isHidden = true
-
- let view = KMCloudFileEmptyView()
- let viewH: CGFloat = 220
- view.frame = NSMakeRect(0, (NSHeight(superview!.bounds)-viewH)*0.5, (superview?.bounds.size.width)!, viewH)
- view.autoresizingMask = [.width, .minYMargin]
- superview?.addSubview(view)
- }
-
- private func hiddenCloudFileEmptyView() {
- let superview = self.fileListTableView.enclosingScrollView?.superview
- self.fileListTableView.enclosingScrollView!.isHidden = false
- var flagView: KMCloudFileEmptyView?
- for view in superview!.subviews {
- if (view is KMCloudFileEmptyView) {
- flagView = (view as! KMCloudFileEmptyView)
- break
- }
- }
-
- if (flagView == nil) {
- return
- }
-
- flagView?.removeFromSuperview()
- }
-
- @objc func accountListTableView_itemClick(sender: NSMenuItem) {
- if (self.accountListTableView.selectedRow < self.accounts.count) {
- self.logoutAccount(self.accounts[self.accountListTableView.selectedRow] as! KMCloudServer)
- }
- }
-
- @objc func openFileMenuClick(sender: NSMenuItem) {
- if (self.currentAccount == nil) {
- return
- }
-
- let file: KMServicesCloudFile = self.cloudFiles[sender.tag] as! KMServicesCloudFile
- self.fileListBeginLoading()
- KMCloudDocumentTools.downloadDocument(self.currentAccount!, file) { finished, fileUrl in
- self.fileListEndLoading()
- if (finished) {
- if !fileUrl.path.isPDFValid() {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
- alert.runModal()
- return
- }
- NSDocumentController.shared.openDocument(withContentsOf: fileUrl, display: true) { document, result, error in
- if error != nil {
- NSApp.presentError(error!)
- return
- }
- (document as! KMMainDocument).cloud = true
- (document as! KMMainDocument).cloudUploadHanddler = { callback in
- KMCloudDocumentTools.uploadDocument(self.currentAccount!, file) { finished, fileUrl in
- callback(finished, "")
- }
- }
- }
- }
- }
- }
-
- @objc func openFolderMenuClick(sender: NSMenuItem) {
- if (self.currentAccount == nil) {
- return
- }
-
- let file: KMServicesCloudFile = self.cloudFiles[sender.tag] as! KMServicesCloudFile
- self.updateFilesList(self.currentAccount!, file)
- }
-
- @objc func fileListTableViewDoubleAction(sender: Any) {
- let indexs = self.fileListTableView.selectedRowIndexes
- for index in indexs {
- let file: KMServicesCloudFile = self.cloudFiles[index] as! KMServicesCloudFile
- if (file.filetype == .file) {
- if (self.currentAccount == nil) {
- continue
- }
- if (!KMCloudDocumentTools.canOpenDocument(file)) {
- continue
- }
- self.fileListBeginLoading()
- KMCloudDocumentTools.downloadDocument(self.currentAccount!, file) { finished, fileUrl in
- self.fileListEndLoading()
- if (finished) {
- NSDocumentController.shared.openDocument(withContentsOf: fileUrl, display: true) { document, result, error in
- if error != nil {
- NSApp.presentError(error!)
- return
- }
- (document as! KMMainDocument).cloud = true
- (document as! KMMainDocument).cloudUploadHanddler = { callback in
- KMCloudDocumentTools.uploadDocument(self.currentAccount!, file) { finished, fileUrl in
- callback(finished, "")
- }
- }
- }
- }
- }
- } else {
- if (self.currentAccount == nil) {
- continue
- }
- self.updateFilesList(self.currentAccount!, file)
- }
- }
- }
-
- private func logoutAccount(_ account: KMCloudServer) {
- DispatchQueue.main.async {
- let alert = NSAlert()
- alert.messageText = NSLocalizedString("Are you Sure Delete“\(account.userEmail!)”?", comment: "")
- alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
- alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
- let response = alert.runModal()
- if (response == .alertSecondButtonReturn) { /// 取消
- return
- }
- account.authorizedLogoutCompletion({ [weak self] finished in
- if (!finished) {
- return
- }
- var i: Int = 0
- for account_ in self!.accounts {
- if ((account_ as! KMCloudServer).serverType == account.serverType) {
- break
- }
- i += 1
- }
- self!.accounts.remove(at: i)
- self!.cloudFiles.removeAll()
- self!.reloadData()
- })
- }
- }
-
- private func updateFilesList(_ server: KMCloudServer, _ file: KMServicesCloudFile?) {
- self.myPathControl.enabled = false
- self.fileListBeginLoading()
- server.getListWithFilePath(file) { [weak self] fileList, serviceType, error in
- if (error != nil) {
- self!.myPathControl.enabled = true
- return
- }
- if (fileList == nil) {
- self!.myPathControl.enabled = true
- return
- }
- if (file == nil) { /// 切换账号
- self?.filepaths.removeAll()
- self?.currentAccount = server
- } else {
- let index = self?.filepaths.firstIndex(of: file!)
- if (index == nil) {
- self?.filepaths.append(file!)
- } else {
- let count = self?.filepaths.count
- for i in 0 ..< count! {
- if (i <= index!) {
- continue
- }
- self?.filepaths.remove(at: i)
- }
- }
- }
-
- self!.pathString = NSLocalizedString("Cloud Documents", comment: "")
- for filepath in self!.filepaths {
- self!.pathString = self!.pathString.appending("\\\(filepath.fileName!)")
- }
- self!.myPathControl.urlString = self?.pathString
-
- self?.cloudFiles.removeAll()
- for file in fileList! {
- self?.cloudFiles.append(file)
- }
- /// 排序
- self?.cloudFiles.sort(by: { file1, file2 in
- if ((file1 as! KMServicesCloudFile).filetype == .file) {
- if ((file2 as! KMServicesCloudFile).filetype == .file) {
- return (file1 as! KMServicesCloudFile).fileName.caseInsensitiveCompare((file2 as! KMServicesCloudFile).fileName) == .orderedAscending
- } else {
- return false
- }
- } else {
- if ((file2 as! KMServicesCloudFile).filetype == .file) {
- return true
- } else {
- return (file1 as! KMServicesCloudFile).fileName.caseInsensitiveCompare((file2 as! KMServicesCloudFile).fileName) == .orderedAscending
- }
- }
- })
- DispatchQueue.main.async {
- self?.fileListTableView.reloadData()
- if (self?.cloudFiles.count == 0) {
- self?.showCloudFileEmptyView()
- } else {
- self?.hiddenCloudFileEmptyView()
- }
- self!.myPathControl.enabled = true
- self?.fileListEndLoading()
- }
- }
- }
-
- private func fileListBeginLoading() {
- self.fileListTableView.enclosingScrollView?.superview!.beginLoading()
- }
-
- private func fileListEndLoading() {
- self.fileListTableView.enclosingScrollView?.superview?.endLoading()
- }
- }
- // MARK: - NSSplitViewDelegate
- extension KMCloudDocumentsViewController: NSSplitViewDelegate {
- func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
- return 170.0
- }
-
- func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
- return 170.0
- }
-
- func splitView(_ splitView: NSSplitView, shouldAdjustSizeOfSubview view: NSView) -> Bool {
- return false
- }
-
- func splitViewDidResizeSubviews(_ notification: Notification) {
- let rect = self.cloudSplitView.frame
- self.cloudLeftBox.setFrameSize(CGSize(width: 170, height: rect.size.height))
-
- self.cloudRightBox.frame = CGRect(origin: CGPoint(x: 171.0, y: 0), size: CGSize(width: rect.width - 171.0, height: rect.height))
- }
-
- func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
- return proposedPosition
- }
-
- func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
- return false
- }
-
- func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
- return false
- }
-
- func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
- return splitView.isEqual(to: self.cloudSplitView)
- }
-
- func splitView(_ splitView: NSSplitView, effectiveRect proposedEffectiveRect: NSRect, forDrawnRect drawnRect: NSRect, ofDividerAt dividerIndex: Int) -> NSRect {
- return proposedEffectiveRect
- }
-
- func splitView(_ splitView: NSSplitView, additionalEffectiveRectOfDividerAt dividerIndex: Int) -> NSRect {
- return NSMakeRect(0, 0, 0, 0)
- }
- }
- // MARK: - NSCollectionViewDataSource
- extension KMCloudDocumentsViewController: NSCollectionViewDataSource {
- func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
- if collectionView.isEqual(to: emptyCollectionView) {
- return self.cloudPlates.count
- }
- return 0
- }
-
- func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
- if collectionView.isEqual(to: emptyCollectionView) {
- let item: KMCloudEmptyCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMCloudEmptyCollectionViewItem"), for: indexPath) as! KMCloudEmptyCollectionViewItem
- let state = self.cloudPlates[indexPath.item]
- item.refreshUI(item: state.rawValue)
- return item
- }
- return NSCollectionViewItem.init()
- }
- }
- // MARK: - NSCollectionViewDelegate
- extension KMCloudDocumentsViewController: NSCollectionViewDelegate {
- func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
- if collectionView.isEqual(to: emptyCollectionView) {
- let state = self.cloudPlates[indexPaths.first!.item]
- loginCloud(state: state)
- }
- }
- }
- // MARK: - NSCollectionViewDelegateFlowLayout
- extension KMCloudDocumentsViewController: NSCollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
- if collectionView.isEqual(to: emptyCollectionView) {
- return self.emptyCollectionView.itemSize
- }
- return CGSize(width: 218, height: 224)
- }
-
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- if collectionView.isEqual(to: emptyCollectionView) {
- return self.emptyCollectionView.minimumLineSpacing
- }
- return 14
- }
-
-
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
- if collectionView.isEqual(to: emptyCollectionView) {
- return self.emptyCollectionView.minimumInteritemSpacing
- }
- return 14
- }
-
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
- if (collectionView.isEqual(to: self.emptyCollectionView)) {
- let width: CGFloat = NSWidth(collectionView.superview!.frame)
- let height: CGFloat = NSHeight(collectionView.superview!.frame)
-
- let itemSize = self.emptyCollectionView.itemSize
- let itemCount: Int = self.emptyCollectionView.numberOfItems(inSection: 0)
- let itemHSpace = self.emptyCollectionView.minimumLineSpacing
- let itemVSpace = self.emptyCollectionView.minimumInteritemSpacing
- var left: CGFloat = (width - itemSize.width * CGFloat(itemCount) - itemHSpace * CGFloat(itemCount-1)) * 0.5
- if (left < 0) {
- left = 0
- }
- var top: CGFloat = (height - itemSize.height * CGFloat(itemCount) - itemVSpace * CGFloat(itemCount-1)) * 0.5
- if (top < 0) {
- top = 0
- }
-
- return NSEdgeInsetsMake(top, left, top, left)
- }
- return NSEdgeInsetsZero
- }
- }
- // MARK: NSTableViewDelegate
- extension KMCloudDocumentsViewController: NSTableViewDelegate {
- func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
- if tableView.isEqual(to: accountListTableView) {
- return 57
- } else if tableView.isEqual(to: fileListTableView) {
- return 75
- }
- return 0;
- }
-
- func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
- let identifier = tableColumn?.identifier
- if tableView.isEqual(to: accountListTableView) {
- let server: KMCloudServer = self.accounts[row] as! KMCloudServer
- let cellView: KMCloudAccountListTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMCloudAccountListTableviewCell
- cellView.accountLabel.stringValue = server.userEmail ?? ""
- cellView.toolTip = server.userEmail
- return cellView
- } else if tableView.isEqual(to: fileListTableView) {
- let file: KMServicesCloudFile = self.cloudFiles[row] as! KMServicesCloudFile
- if identifier == NSUserInterfaceItemIdentifier("name") {
- let cellView: KMCloudFileNameListTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMCloudFileNameListTableviewCell
- if (file.filetype == .file) {
- let fileType = KMCloudDocumentTools.getFileType(self.currentAccount!, file)
-
- let image = NSWorkspace.shared.icon(forFileType: fileType)
- cellView.fileImageView.image = image
- cellView.subtitleLabel.stringValue = fileType
- } else {
- cellView.fileImageView.image = NSImage(named: NSImage.folderName)
- cellView.subtitleLabel.stringValue = "Folders"
- }
- cellView.titleLabel.stringValue = file.fileName
-
- return cellView
- } else if identifier == NSUserInterfaceItemIdentifier("modifyTime") {
- let cellView: KMCloudFileModifyTimeListTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMCloudFileModifyTimeListTableviewCell
- if (file.fileModiDateString != nil) {
- cellView.modifyTimeLabel.stringValue = KMCloudDocumentTools.parseDate(file.fileModiDate)
- } else {
- cellView.modifyTimeLabel.stringValue = ""
- }
-
- return cellView
- } else if identifier == NSUserInterfaceItemIdentifier("size") {
- let cellView: KMCloudFileSizeListTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMCloudFileSizeListTableviewCell
- if (file.filetype == .file) {
- cellView.sizeLabel.stringValue = file.fileSizeString
- } else {
- cellView.sizeLabel.stringValue = ""
- }
-
- return cellView
- } else {
- let cellView: KMCloudFileNameListTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMCloudFileNameListTableviewCell
- return cellView
- }
- } else {
- let cellView: KMCloudAccountListTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMCloudAccountListTableviewCell
- return cellView
- }
- }
-
- func tableViewSelectionDidChange(_ notification: Notification) {
- let tableView = notification.object as! NSTableView
- if tableView.isEqual(to: accountListTableView) {
- let server: KMCloudServer = self.accounts[self.accountListTableView.selectedRow] as! KMCloudServer
- self.updateFilesList(server, nil)
- self.pathString = NSLocalizedString("Cloud Documents", comment: "")
- self.myPathControl.urlString = self.pathString
- } else if tableView.isEqual(to: fileListTableView) {}
- }
- }
- // MARK: NSTableViewDataSource
- extension KMCloudDocumentsViewController: NSTableViewDataSource {
- func numberOfRows(in tableView: NSTableView) -> Int {
- if tableView.isEqual(to: accountListTableView) {
- return self.accounts.count
- } else if tableView.isEqual(to: fileListTableView) {
- return self.cloudFiles.count
- }
- return 0
- }
- }
- extension KMCloudDocumentsViewController: KMLoadingProtocol {
- func beginLoading() {
- self.view.beginLoading()
- }
-
- func endLoading() {
- self.view.endLoading()
- }
- }
- extension KMCloudDocumentsViewController: KMMenuTableViewDelegate {
- func km_menu(for event: NSEvent) -> NSMenu? {
- let point = self.fileListTableView.convert(event.locationInWindow, from: nil)
- let index = self.fileListTableView.row(at: point)
- if (index < 0) { /// 空白处
- return nil
- }
-
-
- let file: KMServicesCloudFile = self.cloudFiles[index] as! KMServicesCloudFile
- if (file.filetype == .folder) {
- let menu = NSMenu(title: "")
- let editItem = NSMenuItem(title: NSLocalizedString("Open Folder", comment: ""), action: #selector(openFolderMenuClick), keyEquivalent: "")
- editItem.target = self
- editItem.tag = index
- menu.addItem(editItem)
- return menu
- }
- if (file.filetype == .file) {
- let fileExtension: String = file.fileName.components(separatedBy: ".").last!
- if (fileExtension.lowercased() != "pdf") {
- return nil
- }
-
- let menu = NSMenu(title: "")
- let editItem = NSMenuItem(title: NSLocalizedString("Open File", comment: ""), action: #selector(openFileMenuClick), keyEquivalent: "")
- editItem.target = self
- editItem.tag = index
- menu.addItem(editItem)
- return menu
- }
- return nil
- }
- }
|