// // KMMainViewController+UI.swift // PDF Master // // Created by wanjun on 2022/12/15. // import Foundation extension KMMainViewController { func showChildToolbar(showToolbar:Bool) { if showToolbar == true && self.isReadMode == false { self.view.addSubview(self.childToolbarController.view) } else { self.childToolbarController.view.removeFromSuperview() } self.updateTopView() } private func updateTopView () { let frame = self.view.frame if self.childToolbarController.view.superview != nil { let toolbarFrame = self.childToolbarController.view.frame; self.childToolbarController.view.frame = CGRect(x: 0, y: (frame.size.height ?? 0)-toolbarFrame.size.height, width: frame.size.width ?? 0, height: toolbarFrame.size.height) self.childToolbarController.view.autoresizingMask = [.width,.minYMargin] self.toplayoutConstraint.constant = toolbarFrame.size.height } else { self.toplayoutConstraint.constant = 0 } } //通知 func preferenceDidChangeNotification(notification:Notification) { let info : [AnyHashable : Any] = notification.userInfo ?? [:] if info.keys.contains(KMPreference.viewZoomScaleTypeKey) { self.selectZoom(KMPreferenceManager.shared.viewZoomScaleType) } if notification.name.rawValue == "pdfViewDocumentDidLoaded" || info.keys.contains(KMPreference.viewPageDisplayTypeKey) { self.selectDisplay(display: KMPreferenceManager.shared.viewPageDisplayType) } if info.keys.contains(KMPreference.displayBackgroundNormalColorKey) || notification.name.rawValue == "pdfViewDocumentDidLoaded" { self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor self.listView.layoutDocumentView() } self.updatePageIndicatoreType() if (info.keys.contains(KMPreference.generalAuthorNameKey)) { // 作者名称 CPDFKitConfig.sharedInstance().setAnnotationAuthor((info[KMPreference.generalAuthorNameKey] as! String)) } if (info.keys.contains(KMPreference.highlightLinksKey)) { let hlLink = info[KMPreference.highlightLinksKey] as? Bool CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(hlLink == nil ? false : hlLink!) self.listView.setNeedsDisplayForVisiblePages() } if (info.keys.contains(KMPreference.autoSaveKey) || info.keys.contains(KMPreference.autoSavePerNumberMinuteKey)) { self.autoSaveTimeStartOrStopIfNeed() } } //刷新页面显示器 func updatePageIndicatoreType () { if self.isReadMode { self.pageNumberDisplayView.dismiss() } else { if self.listView.document != nil { self.pageNumberDisplayView.totalPagesCount = Int(self.listView.document.pageCount) } self.pageNumberDisplayView.currentPageIndex = self.listView.currentPageIndex self.pageNumberDisplayView.displayType = KMPreferenceManager.shared.pageIndicatorType self.readContentView.addSubview(self.tipCurrentPageBox, positioned: .above, relativeTo: self.readContentView) } } } extension KMMainViewController: KMToolbarControllerDelegate { func toolbarController(_ viewController: KMToolbarController, heightOffsetChange heightOffset: Float) { if self.isReadMode { self.heightOffset.constant = CGFloat(0) } else { self.heightOffset.constant = CGFloat(heightOffset) } self.refreshListViewToolMode() self.listView.annotationType = .unkown // self.rightSideViewController.view.isHidden = true self.rightSideViewController.isHidden = true if self.toolbarController.toolbarType != .LeftPanel { self.closeRightPane() } } func changeAnnotationModeAction(item: KMToolbarClickButton){ var currentType = self.listView.annotationType if (currentType == .eraser) { currentType = .ink } var isSelected = (currentType == CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown) && ((self.listView.toolMode == .noteToolMode) || (self.listView.toolMode == .selfSignMode)) var editSelectd = false if self.listView.annotationType == CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown && self.listView.toolMode == .editPDFToolMode { editSelectd = true } //多边形工具栏点击特殊处理 if (self.listView.annotationType == .square || self.listView.annotationType == .circle || self.listView.annotationType == .arrow || self.listView.annotationType == .line) { if CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown == .square || CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown == .circle || CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown == .arrow || CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown == .line { isSelected = true } } else if (item.tag > 3 && item.tag < 8) { let shape = UserDefaults.standard.object(forKey: KMToolBarToolPDFShapeStyle) item.tag = shape as? Int ?? item.tag } if isSelected { self.listView.toolMode = .textToolMode self.listView.annotationType = .unkown self.closeRightPane() } else { if self.listView.currentSelection != nil { let annotation = self.listView.addAnnotation(with: CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown, selection: self.listView.currentSelection, page: self.listView.currentSelection.page, bounds: self.listView.currentSelection.bounds) if((annotation != nil) && !self.listView.activeAnnotations.contains(annotation ?? self.listView.activeAnnotation)) { var newAnnonations : [CPDFAnnotation] = [] newAnnonations.append(annotation!) self.listView.updateActiveAnnotations(newAnnonations) self.listView.setNeedsDisplayAnnotationViewFor(annotation?.page) } } self.refreshListViewToolMode() let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown var annotations: [CPDFAnnotation] = [] if (type == .link) { for annotation in self.listView.activeAnnotations { if ((annotation is CPDFLinkAnnotation) == false) { continue } annotations.append(annotation as! CPDFAnnotation) } } self.listView.annotationType = type if (type == .link && annotations.count > 0) { self.listView.updateActiveAnnotations(annotations) } } if self.listView.toolMode != .editPDFToolMode && self.listView.annotationType != .unkown { // self.rightSideViewController.view.isHidden = false self.rightSideViewController.isHidden = false self.rightSideViewController.subViewType = .AnnotationProperts self.openRightPane() } else if self.listView.toolMode == .editPDFToolMode { if editSelectd { // self.rightSideViewController.view.isHidden = true self.rightSideViewController.isHidden = true self.listView.setShouAddEdit([]) self.listView.annotationType = .unkown self.closeRightPane() } else if self.listView.annotationType == .addText { // self.rightSideViewController.view.isHidden = false self.rightSideViewController.isHidden = false self.openRightPane() self.rightSideViewController.subViewType = .EditPDFAddText self.listView.setShouAddEdit(.text) } else if self.listView.annotationType == .addImage { self.rightSideViewController.subViewType = .EditPDFAddImage self.listView.setShouAddEdit(.image) } } else { // self.rightSideViewController.view.isHidden = true self.rightSideViewController.isHidden = true self.closeRightPane() } } func refreshListViewToolMode () { if self.toolbarController.toolbarType == .Annatiton { self.listView.toolMode = .noteToolMode } else if self.toolbarController.toolbarType == .Move { self.listView.toolMode = .moveToolMode } else if self.toolbarController.toolbarType == .Magnify { self.listView.toolMode = .magnifyToolMode } else if self.toolbarController.toolbarType == .Select { self.listView.toolMode = .selectToolMode } else if self.toolbarController.toolbarType == .SelectZoom { self.listView.toolMode = .selectZoomToolMode } else if self.toolbarController.toolbarType == .Page { } else if self.toolbarController.toolbarType == .Form { self.listView.toolMode = .formToolMode self.rightSideViewController.subViewType = .AnnotationProperts } else if self.toolbarController.toolbarType == .FillSign { self.listView.toolMode = .selfSignMode self.rightSideViewController.subViewType = .AnnotationProperts } else if self.toolbarController.toolbarType == .editPDF { self.listView.toolMode = .editPDFToolMode self.listView.editingConfig().isShowEditingAreaHover = true self.listView.editingConfig().editingBorderWidth = 1 self.listView.editingConfig().editingCenterRadius = 5 self.listView.editingConfig().editAreaMargin = 2 self.listView.editingConfig().editingHoverBorderColor = NSColor.init(red: 0, green: 136.0/255.0, blue: 1.0, alpha: 0.5) self.listView.editingConfig().editingBorderColor = NSColor.init(red: 0, green: 0, blue: 0, alpha: 0.4) self.listView.editingConfig().addEditAreaColor = NSColor.init(red: 23.0/255.0, green: 112.0/255.0, blue: 244.0/255.0, alpha: 0.5) self.listView.editingConfig().editAreaMoveFillColor = NSColor.init(red: 23.0/255.0, green: 112.0/255.0, blue: 244.0/255.0, alpha: 0.1) }else if self.toolbarController.toolbarType == .LeftPanel { self.toggleLeftPane() self.leftSideViewController.showPanelView(show: true) }else { // if self.leftSideViewController.isShowPanel { // self.leftSideViewController.showPanelView(show: false) // } self.listView.toolMode = .textToolMode } } func showPDFLayoutModeAction(show: Bool) { self.listView.annotationType = .unkown self.showOrHideNotes() } func toolbarViewController(_ viewController: KMToolbarViewController, rightPanel toolbarItem: KMToolBoxItem) { self.toggleRightPane() } func toolbarViewController(_ viewController: KMToolbarViewController, leftPanel toolbarItem: KMToolBoxItem) { self.toggleLeftPane() self.leftSideViewController.showPanelView(show: toolbarItem.isSelected) } } extension KMMainViewController : NSSplitViewDelegate { func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool { if splitView == self.mianSplitView { // return (subview == self.listView) == false } 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 == self.mianSplitView } func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat { if splitView == mianSplitView { let splitViewWidth = splitView.frame.width let dividerIndexX0 = panelWidth + functionWidth if dividerIndex == 0 { if leftPanelOpen { return CGFloat(dividerIndexX0) } else { return functionWidth } } else if dividerIndex == 1 { return splitViewWidth } } return proposedMaximumPosition } func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat { if splitView == mianSplitView { let splitViewWidth = splitView.frame.width let dividerIndexX1 = splitViewWidth - defaultRightWidth if dividerIndex == 0 { return functionWidth } else if dividerIndex == 1 { return dividerIndexX1 } } return proposedMinimumPosition } func splitView(_ splitView: NSSplitView, resizeSubviewsWithOldSize oldSize: NSSize) { if splitView.isEqual(to: mianSplitView) { let leftView = splitView.subviews[0] let centerView = splitView.subviews[1] let rightView = splitView.subviews[2] let leftCollapsed = splitView.isSubviewCollapsed(leftView) let rightCollapsed = splitView.isSubviewCollapsed(rightView) var leftSize = leftView.frame.size var mainSize = centerView.frame.size var rightSize = rightView.frame.size var contentWidth = splitView.frame.width if leftCollapsed { leftSize.width = 0.0 } else { contentWidth = contentWidth - splitView.dividerThickness } if rightCollapsed { rightSize.width = 0.0 } else { contentWidth = contentWidth - splitView.dividerThickness } if contentWidth < leftSize.width + rightSize.width { var oldContentWidth = oldSize.width if leftCollapsed == false { oldContentWidth = oldContentWidth - splitView.dividerThickness } if rightCollapsed == false { oldContentWidth = oldContentWidth - splitView.dividerThickness } let resizeFactor = contentWidth / oldContentWidth leftSize.width = floor(resizeFactor*leftSize.width) rightSize.width = floor(resizeFactor*rightSize.width) } mainSize.width = contentWidth - leftSize.width - rightSize.width leftSize.height = splitView.frame.height rightSize.height = splitView.frame.height mainSize.height = splitView.frame.height if leftCollapsed == false { leftView.setFrameSize(leftSize) } if rightCollapsed == false { rightView.setFrameSize(rightSize) } centerView.setFrameSize(mainSize) } splitView.adjustSubviews() } func splitViewWillResizeSubviews(_ notification: Notification) { let defaultView = KMSplitView() let sender : NSSplitView = notification.object as? NSSplitView ?? defaultView if (sender == self.mianSplitView || sender == defaultView) && self.view.window?.frameAutosaveName != nil{ let leftWidth = self.mianSplitView.isSubviewCollapsed(self.leftView) ? 0.0 : leftSideViewController.view.frame.width if leftPanelOpen { // if leftWidth < 100 && lastLeftPanWidth > functionWidth{ // self.closeLeftPane() // } } } } func splitViewDidResizeSubviews(_ notification: Notification) { self.locationPageView.frame = CGRect(x: 0, y: self.listView.frame.maxY-32, width: self.listView.frame.width, height: 32) let defaultView = KMSplitView() let sender : NSSplitView = notification.object as? NSSplitView ?? defaultView if (sender == self.mianSplitView || sender == defaultView) && self.view.window?.frameAutosaveName != nil{ let leftWidth = self.mianSplitView.isSubviewCollapsed(self.leftView) ? 0.0 : leftSideViewController.view.frame.width let rightWidth = self.mianSplitView.isSubviewCollapsed(self.rightView) ? 0.0 : rightSideViewController.view.frame.width UserDefaults.standard.set(leftWidth, forKey: CPDFOfficeLeftSidePaneWidthKey) UserDefaults.standard.set(rightWidth, forKey: CPDFOfficeRightSidePaneWidthKey) } } func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat { if dividerIndex == 0 { if leftPanelOpen { //当左边栏开启式,拖拽时不改变宽度,当宽度达到原宽度的一半时关闭窗口 //如果 isShowPanel 为 true 那么将不会收缩 if (proposedPosition < functionWidth + panelWidth * 0.5) && !self.leftSideViewController.isShowPanel { self.closeLeftPane() self.leftSideViewController.refreshMethodType(methodType: .None) return functionWidth } else { return panelWidth + functionWidth } } else { return functionWidth } } return proposedPosition } } extension KMMainViewController: KMLeftSideViewControllerDelegate { func controlStateChange(_ obj: KMLeftSideViewController, show: Bool) { if show { self.openLeftPane() } else { self.closeLeftPane() } } func enterEditMode(_ obj: KMLeftSideViewController, _ pages: [Int]) { let item : KMToolBoxItem = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentPageToolbarItemIdentifier))! self.toolbarController.mainToolBarView?.delegate?.toolbarViewController?(self.toolbarController.mainToolBarView!, clickMode: .Page, toolbar: item, pages) } func searchAction(searchString: String,isCase:Bool) { self.search(searchString: searchString,isCase: isCase) } func controller(_ controller: KMLeftSideViewController, dispayDidChange dispay: KMPDFDisplayType) { self.selectDisplay(display: dispay, viewSettingIsReload: false) } func controller(controller: KMLeftSideViewController, itemClick item: Any?, itemKey: KMItemKey, params: Any?) { if (itemKey == .print) { // 打印 var pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [], reversePrintOrder: false) if (params != nil) { pageRange.type = .custom pageRange.selectPages = params as! [Int] } self.showPrintWindow(pageRange: pageRange) } else if (itemKey == .cut || itemKey == .paste || itemKey == .delete || itemKey == .leftRotate || itemKey == .rightRotate) { self._isPDFDocumentEdited = true } } func controller(controller: KMLeftSideViewController, bookMarkDidChange bookMarks: [KMBookMarkItem]) { self.needSave = true self._isPDFDocumentEdited = true } } //MARK: ReadModel extension KMMainViewController { @objc func openReadModel() { //设置阅读模式 self.isReadMode = true self.readModelView.isHidden = false //顶部菜单栏状态清空 self.toolbarController.toolbarType = .None self.toolbarController.cancelSelected(KMLeftControlToolbarItemIdentifier) self.toolbarController.cancelSelected(KMRightControlToolbarItemIdentifier) self.toolbarController.lastItemBox.isSelected = false //左侧菜单栏情况 self.leftPanelOpen = false self.lastLeftPanWidth = 0 self.leftSideViewController.refreshMethodType(methodType: .None) //阅读界面初始化 self.readModelView.isHidden = false self.readModelView.currentPageIndex = self.listView.currentPageIndex self.readModelView.totalPagesCount = Int(self.listView.document.pageCount) self.readModelView.beginTimer() // self.listView.autoScales = true // self.readModelView.scaleType = .autoSize self.readModelView.zoomButton.stringValue = (NSString(format: "%.0f", self.listView.scaleFactor * 100) as String) + "%" //关闭左侧右侧菜单栏 self.closeLeftPane() self.closeRightPane() self.leftSideViewController.showPanelView(show: false) self.leftView.isHidden = true //弹出提示框 let alertView = CustomAlertView(message: NSLocalizedString("Read mode on, you can press ESC to exit.", comment: ""), from: self.view, with: .blue, backgroundColor: NSColor(hex: "#36383B")) } @objc func closeReadModel() { if self.isReadMode { self.isShowBOTA = false self.readModelView.isHidden = true self.isReadMode = false self.readModelView.isHidden = true self.toolbarController.toolbarType = self.toolbarController.toolbarType } else { self.toolbarController.toolbarType = .None } let alertView = CustomAlertView(message: NSLocalizedString("Read Mode Off", comment: ""), from: self.view, with: .blue, backgroundColor: NSColor(hex: "#36383B")) } @objc func highlightLinks() { let highlightLinks = KMPreferenceManager.shared.highlightLinks KMPreferenceManager.shared.highlightLinks = !highlightLinks } }