// // KMPDFToolbarController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/10/8. // import Cocoa import KMComponentLibrary @objc protocol KMPDFToolbarControllerDelegate: AnyObject { @objc optional func kmPDFToolbarControllerDidToolbarItemClicked(_ controller: KMPDFToolbarController, _ itemIdentifier: String) //结束页面编辑 @objc optional func kmPDFToolbarControllerDidExitPageEditMode(_ controller: KMPDFToolbarController) @objc optional func kmPDFToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) @objc optional func kmPDFToolbarControllerDidSelectTextDidChange(_ controller: KMPDFToolbarController, _ view: ComponentSelect) @objc optional func kmPDFToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) } class KMPDFToolbarController: NSViewController { @IBOutlet var contendBox: NSBox! @IBOutlet var mainToolbarContendView: NSView! @IBOutlet var leftToolsView: NSView! @IBOutlet var leftViewButton: ComponentButton! @IBOutlet var pageButton: ComponentButton! @IBOutlet var toolsView: ComponentDropdownTool! @IBOutlet var dividerView: ComponentDivider! @IBOutlet var selectZoom: ComponentSelectZoom! @IBOutlet var tabsContendView: NSView! @IBOutlet var tabsScrollView: NSScrollView! @IBOutlet var tabsMoreButton: ComponentButton! @IBOutlet var rightToolsView: NSView! @IBOutlet var secondContendView: NSView! @IBOutlet var tabsLeftConst: NSLayoutConstraint! @IBOutlet var tabsRightConst: NSLayoutConstraint! @IBOutlet var rightViewWidthConst: NSLayoutConstraint! var scrollDocumentView: NSView = NSView.init() var tabsView: ComponentTabs! //MARK: -自定义工具栏 private var customRightGroupView: ComponentGroup! private var customRightItems: [ComponentMenuitemProperty] = [] private var undoRedo_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Undo / Redo"), identifier: "undoRedo_Menuitem_PropertyKey") private var save_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Save"), identifier: "save_Menuitem_PropertyKey") private var aiTools_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("AI Tools"), identifier: "aiTools_Menuitem_PropertyKey") private var batch_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Batch"), identifier: "batch_Menuitem_PropertyKey") private var tts_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("TTS"), identifier: "tts_Menuitem_PropertyKey") private var ppt_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Slide Show"), identifier: "ppt_Menuitem_PropertyKey") private var print_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Print"), identifier: "print_Menuitem_PropertyKey") private var share_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Share"), identifier: "share_Menuitem_PropertyKey") //MARK: -更多Tabs private var moreTabsItems: [ComponentMenuitemProperty] = [] private var markup_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Markup"), identifier: "markup_Menuitem_PropertyKey") private var edit_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Edit"), identifier: "markup_Menuitem_PropertyKey") private var form_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Form"), identifier: "markup_Menuitem_PropertyKey") private var fill_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Fill"), identifier: "markup_Menuitem_PropertyKey") private var convert_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Convert"), identifier: "markup_Menuitem_PropertyKey") private var protect_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Protect"), identifier: "markup_Menuitem_PropertyKey") private var tools_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Tools"), identifier: "markup_Menuitem_PropertyKey") var pdfView: CPDFListView? var viewManager: KMPDFViewManager? var toolbarManager: KMPDFToolbarManager? //MARK: -View var secondToolBar: KMPDFSecToolbarController = KMPDFSecToolbarController.init() weak open var delegate: KMPDFToolbarControllerDelegate? override func viewWillLayout() { super.viewWillLayout() refreshTabsScrollViewUI() } override func viewDidLayout() { super.viewDidLayout() refreshTabsScrollViewUI() refreshTabsScrollViewVisiableRect() } override func viewDidAppear() { super.viewDidAppear() refreshTabsScrollViewUI() } override func viewDidLoad() { super.viewDidLoad() // Do view setup here. contendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle") leftToolsView.wantsLayer = true leftToolsView.layer?.backgroundColor = contendBox.fillColor.cgColor rightToolsView.wantsLayer = true rightToolsView.layer?.backgroundColor = contendBox.fillColor.cgColor tabsContendView.wantsLayer = true tabsContendView.layer?.backgroundColor = contendBox.fillColor.cgColor tabsMoreButton.superview?.wantsLayer = true tabsMoreButton.superview?.layer?.backgroundColor = contendBox.fillColor.cgColor tabsView = ComponentTabs.init() tabsView.frame = CGRectMake(0, 0, 446, 40) scrollDocumentView.wantsLayer = true tabsScrollView.documentView = scrollDocumentView setUpData() } func setUpData() { setUpProperty() setUpRightViews() setupSecondToolbar() reloadData() } func setUpProperty() { guard let toolbarManager = self.toolbarManager else { return } leftViewButton.properties = toolbarManager.viewProperty leftViewButton.setTarget(self, action: #selector(buttonClicked(_:))) pageButton.properties = toolbarManager.pageProperty pageButton.setTarget(self, action: #selector(buttonClicked(_:))) dividerView.properties = ComponentDividerProperty(type: .vertical, dash: false) toolsView.properties = toolbarManager.toolsProperty toolsView.delegate = self selectZoom.properties = toolbarManager.selectZoomProperty selectZoom.delegate = self tabsView.updateItemProperty([toolbarManager.markupMode_Property, toolbarManager.editMode_Property, toolbarManager.formMode_Property, toolbarManager.fillMode_Property, toolbarManager.convertMode_Property, toolbarManager.protectMode_Property, toolbarManager.toolsMode_Property]) tabsView.delegate = self scrollDocumentView.frame = CGRectMake(0, 0, tabsView.viewWidth, 28) tabsView.frame = scrollDocumentView.bounds tabsView.autoresizingMask = [.width, .height] scrollDocumentView.addSubview(tabsView) tabsMoreButton.properties = ComponentButtonProperty(type: .text_gray, size: .xs, onlyIcon: true, icon: NSImage(named: "toolbar_more")) tabsMoreButton.setTarget(self, action: #selector(tabsMoreButtonClicked(_:))) customRightItems = [undoRedo_Menuitem_Property, save_Menuitem_Property, aiTools_Menuitem_Property, batch_Menuitem_Property, tts_Menuitem_Property, ppt_Menuitem_Property, print_Menuitem_Property, share_Menuitem_Property] moreTabsItems = [markup_Menuitem_Property, edit_Menuitem_Property, form_Menuitem_Property, fill_Menuitem_Property, convert_Menuitem_Property, protect_Menuitem_Property, tools_Menuitem_Property] } //MARK: - 刷新一级工具栏 func reloadToolbarTabsView() { guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } toolbarManager.markupMode_Property.state = viewManager.toolMode == .Markup ? .pressed : .normal toolbarManager.editMode_Property.state = viewManager.toolMode == .Edit ? .pressed : .normal toolbarManager.formMode_Property.state = viewManager.toolMode == .Form ? .pressed : .normal toolbarManager.fillMode_Property.state = viewManager.toolMode == .Fill ? .pressed : .normal toolbarManager.convertMode_Property.state = viewManager.toolMode == .Convert ? .pressed : .normal toolbarManager.protectMode_Property.state = viewManager.toolMode == .Protect ? .pressed : .normal toolbarManager.toolsMode_Property.state = viewManager.toolMode == .Tools ? .pressed : .normal tabsView.refreshItems() } func reloadLeftIconView() { guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } toolbarManager.viewProperty.state = viewManager.showDisplayView ? .pressed : .normal toolbarManager.viewProperty.isDisabled = false if viewManager.isPageEditMode { toolbarManager.viewProperty.isDisabled = true } leftViewButton.reloadData() } func reloadPageEditView() { guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } toolbarManager.pageProperty.state = viewManager.isPageEditMode ? .pressed : .normal pageButton.reloadData() } func reloadSelectZoomView() { guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } toolbarManager.selectZoomProperty.isDisabled = false if viewManager.isPageEditMode { toolbarManager.selectZoomProperty.isDisabled = true } if let pdfview = self.pdfView { toolbarManager.selectZoomProperty.text = (NSString(format: "%.0f", pdfview.scaleFactor * 100) as String) + "%" } selectZoom.reloadData() } func refreshTabsScrollViewUI() { let pointX: CGFloat = (CGRectGetWidth(view.frame) - tabsView.viewWidth)/2 var xValue: CGFloat = 0 if pointX-12-264 > 10 { xValue = pointX - 12 - 264 tabsLeftConst.constant = xValue tabsMoreButton.superview?.isHidden = true } else { xValue = 10 let rightXValue = CGRectGetWidth(view.frame) - tabsView.viewWidth - 12 - 264 - rightViewWidthConst.constant - 12 - xValue if rightXValue < 10 { tabsMoreButton.superview?.isHidden = false tabsRightConst.constant = 44 } else { tabsMoreButton.superview?.isHidden = true tabsRightConst.constant = rightXValue } } } func refreshTabsScrollViewVisiableRect() { guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } if tabsView.viewWidth > CGRectGetWidth(tabsContendView.frame)-21 { if viewManager.toolMode != .None { var pointx: CGFloat = 0 print(tabsScrollView.documentVisibleRect) let _manager = toolbarManager if viewManager.toolMode == .Markup { pointx = 0 } else if viewManager.toolMode == .Edit { pointx = _manager.markupMode_Property.propertyInfo.viewWidth } else if viewManager.toolMode == .Form { pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth } else if viewManager.toolMode == .Fill { pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.formMode_Property.propertyInfo.viewWidth } else if viewManager.toolMode == .Convert { pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.formMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth } else if viewManager.toolMode == .Protect { pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.formMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.convertMode_Property.propertyInfo.viewWidth } else if viewManager.toolMode == .Tools { pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.formMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.convertMode_Property.propertyInfo.viewWidth + _manager.protectMode_Property.propertyInfo.viewWidth } if tabsScrollView.documentVisibleRect.origin.x > pointx { tabsScrollView.scroll(tabsScrollView.contentView, to: CGPoint(x: pointx, y: 0)) } } } } //MARK: -右侧工具模块 func setUpRightViews() { let subviews = rightToolsView.subviews for view in subviews { view.removeFromSuperview() } var itemXvalue: CGFloat = 4 guard let _manager = self.toolbarManager, let viewManager = self.viewManager else { return } var propertys: [Any] = [] var adddividerA: Bool = false var adddividerB: Bool = false for property in _manager.validRightPropertys { if property == _manager.undoProperty || property == _manager.redoProperty || property == _manager.saveProperty { adddividerA = true } else if property == _manager.aiToolsProperty || property == _manager.batchProperty || property == _manager.ttsProperty || property == _manager.pptProperty || property == _manager.printProperty || property == _manager.shareProperty { adddividerB = true } } for property in _manager.validRightPropertys { if adddividerA == true { if property == _manager.aiToolsProperty || property == _manager.batchProperty || property == _manager.ttsProperty || property == _manager.pptProperty || property == _manager.printProperty || property == _manager.shareProperty { propertys.append(ComponentDividerProperty(type: .vertical, dash: false)) adddividerA = false } } propertys.append(property) } if adddividerB == true { if propertys.last is ComponentDividerProperty { } else { propertys.append(ComponentDividerProperty(type: .vertical, dash: false)) } } propertys.append(_manager.rightViewProperty) for property in propertys { if property is ComponentButtonProperty { let buttonProperty = (property as! ComponentButtonProperty) let button = ComponentButton.init() button.properties = buttonProperty if buttonProperty.onlyIcon { button.frame = CGRectMake(itemXvalue, 0, 28, 28) } else { button.frame = CGRectMake(itemXvalue, 0, button.properties.propertyInfo.viewWidth, 28) } button.setTarget(self, action: #selector(buttonClicked(_:))) rightToolsView.addSubview(button) itemXvalue += CGRectGetWidth(button.frame) itemXvalue += 12 } else if property is ComponentDividerProperty { let divider = ComponentDivider.init() divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(rightToolsView.frame)/2-8, 1, 16) divider.properties = (property as! ComponentDividerProperty) rightToolsView.addSubview(divider) itemXvalue += 1 itemXvalue += 12 } else if property is ComponentDropdownToolProperty { let dropdownTool = ComponentDropdownTool.init() dropdownTool.properties = property as! ComponentDropdownToolProperty dropdownTool.frame = CGRectMake(itemXvalue, 0, 28, 28) dropdownTool.delegate = self rightToolsView.addSubview(dropdownTool) itemXvalue += CGRectGetWidth(dropdownTool.frame) itemXvalue += 12 } } itemXvalue -= 12 rightViewWidthConst.constant = itemXvalue } func reloadRightToolsView() { let subviews = rightToolsView.subviews for view in subviews { if view is ComponentButton { let button = view as! ComponentButton if button.properties.identifier == KMPDFToolbar_rightView_Identifier { if viewManager?.showRightSide == true { if button.properties.state != .pressed { button.properties.state = .pressed button.reloadData() } } else { if button.properties.state != .normal { button.properties.state = .normal button.reloadData() } } } } else if view is ComponentDropdownTool { } } } //MARK: - 二级工具栏 func setupSecondToolbar() { secondToolBar.view.frame = secondContendView.bounds secondToolBar.delegate = self secondToolBar.view.autoresizingMask = [.width, .height] secondToolBar.viewManager = viewManager secondContendView.addSubview(secondToolBar.view) } func reloadSecondToolbar() { guard let _manager = self.toolbarManager, let viewManager = self.viewManager else { return } if viewManager.isPageEditMode { secondToolBar.reloadMainPropertys(_manager.getSubToolbarItems(isPageEditMode: true)) secondToolBar.reloadRightPropertys([_manager.page_Reduce_Property, _manager.page_Increase_Property]) } else { secondToolBar.reloadMainPropertys(_manager.getSubToolbarItems(viewManager.toolMode)) secondToolBar.reloadRightPropertys([]) } } //只刷新Item状态 func refreshSecondToolbarItemsState() { secondToolBar.reloadMainview() secondToolBar.reloadRightview() } func clickWithIdentify(_ identify: String) { guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } if identify == KMPDFToolbar_PageEdit_Identifier { pageButton.properties.state = viewManager.isPageEditMode ? .pressed : .normal pageButton.reloadData() viewManager.toolMode = .None delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, pageButton.properties.identifier) reloadData() } } //MARK: - reloadData func reloadData() { reloadLeftIconView() reloadPageEditView() reloadToolsView() reloadSelectZoomView() reloadToolbarTabsView() reloadRightToolsView() reloadSecondToolbar() updateRightMenuItemSelectedState() } //MARK: - 刷新tools工具 func reloadToolsView() { toolbarManager?.reloadToolsData() toolbarManager?.tools_selectProperty.itemSelected = false toolbarManager?.tools_scrollProperty.itemSelected = false toolbarManager?.tools_contentProperty.itemSelected = false toolbarManager?.tools_magnifyProperty.itemSelected = false toolbarManager?.tools_areaProperty.itemSelected = false if viewManager?.viewToolsType == .Select { toolbarManager?.tools_selectProperty.itemSelected = true } else if viewManager?.viewToolsType == .Scroll { toolbarManager?.tools_scrollProperty.itemSelected = true } else if viewManager?.viewToolsType == .Content_Selection { toolbarManager?.tools_contentProperty.itemSelected = true } else if viewManager?.viewToolsType == .Magnify { toolbarManager?.tools_magnifyProperty.itemSelected = true } else if viewManager?.viewToolsType == .AreaZoom { toolbarManager?.tools_areaProperty.itemSelected = true } toolsView.reloadData() } //MARK: - Action @objc func buttonClicked(_ sender: ComponentButton) { guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } if sender.properties == toolbarManager.viewProperty { viewManager.showDisplayView = !viewManager.showDisplayView sender.properties.state = viewManager.showDisplayView ? .pressed : .normal sender.reloadData() } else if sender.properties == toolbarManager.pageProperty { viewManager.isPageEditMode = !viewManager.isPageEditMode sender.properties.state = viewManager.isPageEditMode ? .pressed : .normal sender.reloadData() viewManager.toolMode = .None } else if sender.properties == toolbarManager.rightViewProperty { viewManager.showRightSide = !viewManager.showRightSide } delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, sender.properties.identifier) reloadData() } @objc func tabsMoreButtonClicked(_ sender: ComponentButton) { guard let point = sender.superview?.convert(sender.frame.origin, to: view.window?.contentView) else { return } for item in moreTabsItems { item.state = .normal item.itemSelected = false } showGroupView(moreTabsItems, point) customRightGroupView.clickedAutoHide = true } //MARK: - 自定义工具栏 func updateRightMenuItemSelectedState() { undoRedo_Menuitem_Property.itemSelected = false save_Menuitem_Property.itemSelected = false aiTools_Menuitem_Property.itemSelected = false batch_Menuitem_Property.itemSelected = false tts_Menuitem_Property.itemSelected = false ppt_Menuitem_Property.itemSelected = false print_Menuitem_Property.itemSelected = false share_Menuitem_Property.itemSelected = false guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } for property in toolbarManager.validRightPropertys { if property == toolbarManager.undoProperty || property == toolbarManager.redoProperty { undoRedo_Menuitem_Property.itemSelected = true } else if property == toolbarManager.saveProperty { save_Menuitem_Property.itemSelected = true } else if property == toolbarManager.aiToolsProperty { aiTools_Menuitem_Property.itemSelected = true } else if property == toolbarManager.batchProperty { batch_Menuitem_Property.itemSelected = true } else if property == toolbarManager.ttsProperty { tts_Menuitem_Property.itemSelected = true } else if property == toolbarManager.pptProperty { ppt_Menuitem_Property.itemSelected = true } else if property == toolbarManager.printProperty { print_Menuitem_Property.itemSelected = true } else if property == toolbarManager.shareProperty { share_Menuitem_Property.itemSelected = true } } } func showPageTurnMenuWithPoint(_ point: CGPoint) { if (customRightGroupView?.superview) != nil { return } updateRightMenuItemSelectedState() let items: [ComponentMenuitemProperty] = [ComponentMenuitemProperty.header(KMLocalizedString("Show Right Tools")), undoRedo_Menuitem_Property, save_Menuitem_Property, ComponentMenuitemProperty.divider(), aiTools_Menuitem_Property, batch_Menuitem_Property, tts_Menuitem_Property, ppt_Menuitem_Property, print_Menuitem_Property, share_Menuitem_Property] showGroupView(items, point) customRightGroupView.clickedAutoHide = false refreshRightMenuState() } func showGroupView(_ items: [ComponentMenuitemProperty], _ point: NSPoint) { var viewHeight: CGFloat = 8.0 for item in items { item.state = .normal if item.type == .normal { viewHeight += 36 } else if item.type == .divider { viewHeight += 8 } else if item.type == .header { viewHeight += 36 } } if customRightGroupView == nil { customRightGroupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle()) } customRightGroupView.clickedAutoHide = false customRightGroupView?.frame = CGRectMake(0, 0, 160, viewHeight) customRightGroupView?.reloadData() customRightGroupView.groupDelegate = self customRightGroupView.showWithPoint(CGPoint(x: point.x, y: point.y-viewHeight), relativeTo: self.contendBox) customRightGroupView?.updateGroupInfo(items) } func hideGroupView() { customRightGroupView?.removeGroupView() } func refreshRightMenuState() { let totalItems = customRightItems var selectedCount = 0 for item in totalItems { if item.itemSelected == true { selectedCount += 1 } } var maxCount = 6 if undoRedo_Menuitem_Property.itemSelected == true { maxCount = 5 } if selectedCount == maxCount { for item in totalItems { if item.itemSelected == false { item.isDisabled = true } } } else { for item in totalItems { item.isDisabled = false } } if selectedCount == 5 && undoRedo_Menuitem_Property.itemSelected == false { //特殊情况,UndoRedo占两个空位 undoRedo_Menuitem_Property.isDisabled = true } for item in totalItems { NotificationCenter.default.post(name: Notification.Name(rawValue: ComponentMenuItemUpdatedKey), object: item.identifier) } } //MARK: - MouseEvent override func rightMouseUp(with event: NSEvent) { super.rightMouseUp(with: event) var clickEnabled = true let windowPoint = event.locationInWindow if tabsMoreButton.superview?.isHidden == false { if CGRectContainsPoint(tabsMoreButton.frame, mainToolbarContendView.convert(windowPoint, from: nil)) { clickEnabled = false } } if clickEnabled { showPageTurnMenuWithPoint(windowPoint) } } } //MARK: - ComponentDropdownToolDelegate:工具模式 extension KMPDFToolbarController: ComponentDropdownToolDelegate { func componentDropdownToolDidClicked(_ view: ComponentDropdownTool, menuItem: ComponentMenuitemProperty?) { guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } for item in toolbarManager.toolsProperty.menuItemArr ?? [] { item.itemSelected = false item.state = .normal } menuItem?.itemSelected = true if menuItem == toolbarManager.tools_selectProperty { viewManager.viewToolsType = .Select } else if menuItem == toolbarManager.tools_scrollProperty { viewManager.viewToolsType = .Scroll } else if menuItem == toolbarManager.tools_contentProperty { viewManager.viewToolsType = .Content_Selection } else if menuItem == toolbarManager.tools_magnifyProperty { viewManager.viewToolsType = .Magnify } else if menuItem == toolbarManager.tools_areaProperty { viewManager.viewToolsType = .AreaZoom } reloadToolsView() } } //MARK: - ComponentSelectZoomDelegate:缩放比例 extension KMPDFToolbarController: ComponentSelectZoomDelegate { func componentSelectZoomDidReduce(view: ComponentSelectZoom) { let zoomSting = view.properties.text var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0) switch scale { case 0...0.25: scale = 0 case 0.25...3: scale -= 0.25 case 3.1...10: scale -= 0.4 case 10.1...100: scale -= 1 default: scale -= 1 } pdfView?.scaleFactor = scale } func componentSelectZoomDidSelect(view: ComponentSelectZoom?, menuItemProperty: ComponentMenuitemProperty?) { if menuItemProperty?.identifier == KMPDFToolbar_Zoom_AdapWidth_Identifier { pdfView?.autoScales = true } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_AdapPage_Identifier { let pageHeight = pdfView?.currentPage()!.size.height let pdfviewHeight = pdfView?.bounds.size.height pdfView?.scaleFactor = pdfviewHeight!/pageHeight! pdfView?.autoScales = false } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_ActualSize_Identifier { if pdfView?.scaleFactor != 1.0 { pdfView?.scaleFactor = 1.0 pdfView?.autoScales = false } } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_10_Identifier { pdfView?.scaleFactor = 0.1 } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_25_Identifier { pdfView?.scaleFactor = 0.25 } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_50_Identifier { pdfView?.scaleFactor = 0.5 } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_75_Identifier { pdfView?.scaleFactor = 0.75 } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_100_Identifier { pdfView?.scaleFactor = 1.0 } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_150_Identifier { pdfView?.scaleFactor = 1.5 } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_200_Identifier { pdfView?.scaleFactor = 2.0 } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_400_Identifier { pdfView?.scaleFactor = 4.0 } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_800_Identifier { pdfView?.scaleFactor = 8.0 } } func componentSelectZoomDidAdd(view: ComponentSelectZoom) { let zoomSting = view.properties.text var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0) switch scale { case 0...0.25: scale += 0.25 case 0.25...3: scale += 0.25 case 3.1...10: scale += 0.4 case 10.1...100: scale += 1 default: scale += 1 } pdfView?.scaleFactor = scale } } //MARK: - ComponentTabsDelegate extension KMPDFToolbarController: ComponentTabsDelegate { func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) { guard let toolbarManager = self.toolbarManager, let _manager = self.viewManager else { return } _manager.viewToolsType = .Select if _manager.isPageEditMode == true { delegate?.kmPDFToolbarControllerDidExitPageEditMode?(self) _manager.isPageEditMode = false } if property == toolbarManager.markupMode_Property { if _manager.toolMode == .Markup { _manager.toolMode = .None property.state = .normal view.refreshItems() } else { _manager.toolMode = .Markup } _manager.subToolMode = .None } else if property == toolbarManager.editMode_Property { if _manager.toolMode == .Edit { _manager.toolMode = .None property.state = .normal view.refreshItems() } else { _manager.toolMode = .Edit } _manager.subToolMode = .None } else if property == toolbarManager.formMode_Property { if _manager.toolMode == .Form { _manager.toolMode = .None property.state = .normal view.refreshItems() } else { _manager.toolMode = .Form } _manager.subToolMode = .None } else if property == toolbarManager.fillMode_Property { if _manager.toolMode == .Fill { _manager.toolMode = .None property.state = .normal view.refreshItems() } else { _manager.toolMode = .Fill } _manager.subToolMode = .None } else if property == toolbarManager.convertMode_Property { if _manager.toolMode == .Convert { _manager.toolMode = .None property.state = .normal view.refreshItems() } else { _manager.toolMode = .Convert } _manager.subToolMode = .None } else if property == toolbarManager.protectMode_Property { if _manager.toolMode == .Protect { _manager.toolMode = .None property.state = .normal view.refreshItems() } else { _manager.toolMode = .Protect } _manager.subToolMode = .None } else if property == toolbarManager.toolsMode_Property { if _manager.toolMode == .Tools { _manager.toolMode = .None property.state = .normal view.refreshItems() } else { _manager.toolMode = .Tools } _manager.subToolMode = .None } refreshTabsScrollViewVisiableRect() delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, property.identifier) reloadData() } } //MARK: - KMPDFSecToolbarControllerDelegate:二级工具栏 extension KMPDFToolbarController: KMPDFSecToolbarControllerDelegate { func kmPDFSecToolbarControllerDidItemClicked(_ controller: KMPDFSecToolbarController, _ property: Any) { if let value = property as? ComponentButtonProperty { delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, value.identifier) } else if let value = property as? ComponentMenuitemProperty { delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, value.identifier) } } func kmPDFSecToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) { delegate?.kmPDFToolbarControllerDidSelectTextDidBeginEditing?(self, view) } func kmPDFSecToolbarControllerDidSelectTextDidChange(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) { delegate?.kmPDFToolbarControllerDidSelectTextDidChange?(self, view) } func kmPDFSecToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) { delegate?.kmPDFToolbarControllerDidSelectTextDidEndEditing?(self, view) } } //MARK: - ComponentGroupDelegate:右侧工具栏 extension KMPDFToolbarController: ComponentGroupDelegate { func componentGroupDidDismiss(group: ComponentGroup?) { tabsMoreButton.properties.state = .normal tabsMoreButton.reloadData() } func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) { guard let property = menuItemProperty else { return } guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else { return } if customRightItems.contains(property) { var validItems:[NSObject] = [] if undoRedo_Menuitem_Property.itemSelected { validItems.append(toolbarManager.undoProperty) validItems.append(toolbarManager.redoProperty) } if save_Menuitem_Property.itemSelected { validItems.append(toolbarManager.saveProperty) } if aiTools_Menuitem_Property.itemSelected { validItems.append(toolbarManager.aiToolsProperty) } if batch_Menuitem_Property.itemSelected { validItems.append(toolbarManager.batchProperty) } if tts_Menuitem_Property.itemSelected { validItems.append(toolbarManager.ttsProperty) } if ppt_Menuitem_Property.itemSelected { validItems.append(toolbarManager.pptProperty) } if print_Menuitem_Property.itemSelected { validItems.append(toolbarManager.printProperty) } if share_Menuitem_Property.itemSelected { validItems.append(toolbarManager.shareProperty) } let sortArray = toolbarManager.totalRightPropertys let sortedArray = validItems.sorted { guard let indexA = sortArray.firstIndex(of: $0 as NSObject), let indexB = sortArray.firstIndex(of: $1 as NSObject) else { return false } return indexA < indexB } toolbarManager.validRightPropertys = sortedArray setUpRightViews() refreshRightMenuState() } else if moreTabsItems.contains(property) { viewManager.viewToolsType = .Select if viewManager.isPageEditMode == true { delegate?.kmPDFToolbarControllerDidExitPageEditMode?(self) viewManager.isPageEditMode = false } if property == markup_Menuitem_Property { viewManager.toolMode = .Markup } else if property == edit_Menuitem_Property { viewManager.toolMode = .Edit } else if property == form_Menuitem_Property { viewManager.toolMode = .Form } else if property == fill_Menuitem_Property { viewManager.toolMode = .Fill } else if property == toolbarManager.convertMode_Property { viewManager.toolMode = .Convert } else if property == protect_Menuitem_Property { viewManager.toolMode = .Protect } else if property == tools_Menuitem_Property { viewManager.toolMode = .Tools } tabsView.refreshItems() delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, property.identifier) reloadData() } } }