12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- //
- // 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()
-
- }
-
-
- }
- }
|