KMPDFToolbarController.swift 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. //
  2. // KMPDFToolbarController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMPDFToolbarControllerDelegate: AnyObject {
  10. //ViewTools视图工具发生变化时调用
  11. @objc optional func kmPDFToolbarControllerDidViewToolsChanged(_ controller: KMPDFToolbarController)
  12. //一级工具栏状态发生变化时调用
  13. @objc optional func kmPDFToolbarControllerDidToolModeChanged(_ controller: KMPDFToolbarController)
  14. //非一级工具栏按钮点击调用
  15. @objc optional func kmPDFToolbarControllerDidToolbarItemClicked(_ controller: KMPDFToolbarController, _ itemIdentifier: String)
  16. //结束页面编辑
  17. @objc optional func kmPDFToolbarControllerDidExitPageEditMode(_ controller: KMPDFToolbarController)
  18. //ComponentSelect中文字编辑回调
  19. @objc optional func kmPDFToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect)
  20. @objc optional func kmPDFToolbarControllerDidSelectTextDidChange(_ controller: KMPDFToolbarController, _ view: ComponentSelect)
  21. @objc optional func kmPDFToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect)
  22. }
  23. class KMPDFToolbarController: NSViewController {
  24. @IBOutlet var contendBox: NSBox!
  25. @IBOutlet var mainToolbarContendView: NSView!
  26. @IBOutlet var leftToolsView: NSView!
  27. @IBOutlet var leftViewButton: ComponentButton!
  28. @IBOutlet var pageButton: ComponentButton!
  29. @IBOutlet var toolsView: ComponentDropdownTool!
  30. @IBOutlet var dividerView: ComponentDivider!
  31. @IBOutlet var selectZoom: ComponentSelectZoom!
  32. @IBOutlet var tabsContendView: NSView!
  33. @IBOutlet var tabsScrollView: NSScrollView!
  34. @IBOutlet var tabsMoreButton: ComponentButton!
  35. @IBOutlet var rightToolsView: NSView!
  36. @IBOutlet var secondContendView: NSView!
  37. @IBOutlet var tabsLeftConst: NSLayoutConstraint!
  38. @IBOutlet var tabsRightConst: NSLayoutConstraint!
  39. @IBOutlet var rightViewWidthConst: NSLayoutConstraint!
  40. var scrollDocumentView: NSView = NSView.init()
  41. var tabsView: ComponentTabs!
  42. //MARK: -自定义工具栏
  43. private var customRightGroupView: ComponentGroup!
  44. private var customRightItems: [ComponentMenuitemProperty] = []
  45. private var undoRedo_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Undo / Redo"), identifier: "undoRedo_Menuitem_PropertyKey")
  46. private var save_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Save"), identifier: "save_Menuitem_PropertyKey")
  47. private var fileInfo_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("File Info"), identifier: "fileInfo_Menuitem_PropertyKey")
  48. private var batch_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Batch"), identifier: "batch_Menuitem_PropertyKey")
  49. private var tts_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("TTS"), identifier: "tts_Menuitem_PropertyKey")
  50. private var ppt_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Slide Show"), identifier: "ppt_Menuitem_PropertyKey")
  51. private var print_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Print"), identifier: "print_Menuitem_PropertyKey")
  52. private var share_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Share"), identifier: "share_Menuitem_PropertyKey")
  53. //MARK: -更多Tabs
  54. private var moreTabsItems: [ComponentMenuitemProperty] = []
  55. private var markup_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Markup"), identifier: "markup_Menuitem_PropertyKey")
  56. private var edit_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Edit"), identifier: "markup_Menuitem_PropertyKey")
  57. private var form_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Form"), identifier: "markup_Menuitem_PropertyKey")
  58. private var fill_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Fill"), identifier: "markup_Menuitem_PropertyKey")
  59. private var convert_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Convert"), identifier: "markup_Menuitem_PropertyKey")
  60. private var protect_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Protect"), identifier: "markup_Menuitem_PropertyKey")
  61. private var tools_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Tools"), identifier: "markup_Menuitem_PropertyKey")
  62. var pdfView: CPDFListView?
  63. var viewManager: KMPDFViewManager?
  64. var toolbarManager: KMPDFToolbarManager?
  65. //MARK: -View
  66. var secondToolBar: KMPDFSecToolbarController = KMPDFSecToolbarController.init()
  67. var redactToolbar: KMRedactToolbarController = KMRedactToolbarController.init()
  68. weak open var delegate: KMPDFToolbarControllerDelegate?
  69. override func viewWillLayout() {
  70. super.viewWillLayout()
  71. refreshTabsScrollViewUI()
  72. }
  73. override func viewDidLayout() {
  74. super.viewDidLayout()
  75. refreshTabsScrollViewUI()
  76. refreshTabsScrollViewVisiableRect()
  77. }
  78. override func viewDidAppear() {
  79. super.viewDidAppear()
  80. refreshTabsScrollViewUI()
  81. }
  82. override func viewDidLoad() {
  83. super.viewDidLoad()
  84. // Do view setup here.
  85. contendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  86. leftToolsView.wantsLayer = true
  87. leftToolsView.layer?.backgroundColor = contendBox.fillColor.cgColor
  88. rightToolsView.wantsLayer = true
  89. rightToolsView.layer?.backgroundColor = contendBox.fillColor.cgColor
  90. tabsContendView.wantsLayer = true
  91. tabsContendView.layer?.backgroundColor = contendBox.fillColor.cgColor
  92. tabsMoreButton.superview?.wantsLayer = true
  93. tabsMoreButton.superview?.layer?.backgroundColor = contendBox.fillColor.cgColor
  94. tabsView = ComponentTabs.init()
  95. tabsView.frame = CGRectMake(0, 0, 446, 40)
  96. scrollDocumentView.wantsLayer = true
  97. tabsScrollView.documentView = scrollDocumentView
  98. setUpData()
  99. NotificationCenter.default.addObserver(self, selector: #selector(imageColorChangedNoti), name: toolbarImageColorChangedNotificationName, object: nil)
  100. }
  101. func setUpData() {
  102. setUpProperty()
  103. setUpRightViews()
  104. setupSecondToolbar()
  105. reloadData()
  106. }
  107. func setUpProperty() {
  108. guard let toolbarManager = self.toolbarManager else {
  109. return
  110. }
  111. leftViewButton.properties = toolbarManager.viewProperty
  112. leftViewButton.setTarget(self, action: #selector(buttonClicked(_:)))
  113. pageButton.properties = toolbarManager.pageProperty
  114. pageButton.setTarget(self, action: #selector(buttonClicked(_:)))
  115. dividerView.properties = ComponentDividerProperty(type: .vertical, dash: false)
  116. toolsView.properties = toolbarManager.toolsProperty
  117. toolsView.delegate = self
  118. selectZoom.properties = toolbarManager.selectZoomProperty
  119. selectZoom.delegate = self
  120. tabsView.updateItemProperty([toolbarManager.markupMode_Property,
  121. toolbarManager.editMode_Property,
  122. toolbarManager.formMode_Property,
  123. toolbarManager.fillMode_Property,
  124. toolbarManager.convertMode_Property,
  125. toolbarManager.protectMode_Property,
  126. toolbarManager.toolsMode_Property])
  127. tabsView.delegate = self
  128. scrollDocumentView.frame = CGRectMake(0, 0, tabsView.viewWidth, 28)
  129. tabsView.frame = scrollDocumentView.bounds
  130. tabsView.autoresizingMask = [.width, .height]
  131. scrollDocumentView.addSubview(tabsView)
  132. tabsMoreButton.properties = ComponentButtonProperty(type: .text_gray,
  133. size: .xs,
  134. onlyIcon: true,
  135. icon: NSImage(named: "toolbar_more"))
  136. tabsMoreButton.setTarget(self, action: #selector(tabsMoreButtonClicked(_:)))
  137. customRightItems = [undoRedo_Menuitem_Property,
  138. save_Menuitem_Property,
  139. fileInfo_Menuitem_Property,
  140. batch_Menuitem_Property,
  141. tts_Menuitem_Property,
  142. ppt_Menuitem_Property,
  143. print_Menuitem_Property,
  144. share_Menuitem_Property]
  145. moreTabsItems = [markup_Menuitem_Property,
  146. edit_Menuitem_Property,
  147. form_Menuitem_Property,
  148. fill_Menuitem_Property,
  149. convert_Menuitem_Property,
  150. protect_Menuitem_Property,
  151. tools_Menuitem_Property]
  152. }
  153. //MARK: - 刷新一级工具栏
  154. func reloadToolbarTabsView() {
  155. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  156. return
  157. }
  158. toolbarManager.markupMode_Property.state = viewManager.toolMode == .Markup ? .pressed : .normal
  159. toolbarManager.editMode_Property.state = viewManager.toolMode == .Edit ? .pressed : .normal
  160. toolbarManager.formMode_Property.state = viewManager.toolMode == .Form ? .pressed : .normal
  161. toolbarManager.fillMode_Property.state = viewManager.toolMode == .Fill ? .pressed : .normal
  162. toolbarManager.convertMode_Property.state = viewManager.toolMode == .Convert ? .pressed : .normal
  163. toolbarManager.protectMode_Property.state = viewManager.toolMode == .Protect ? .pressed : .normal
  164. toolbarManager.toolsMode_Property.state = viewManager.toolMode == .Tools ? .pressed : .normal
  165. tabsView.refreshItems()
  166. }
  167. func reloadLeftIconView() {
  168. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  169. return
  170. }
  171. toolbarManager.viewProperty.state = viewManager.showDisplayView ? .pressed : .normal
  172. toolbarManager.viewProperty.isDisabled = false
  173. if viewManager.isPageEditMode {
  174. toolbarManager.viewProperty.isDisabled = true
  175. }
  176. leftViewButton.reloadData()
  177. }
  178. func reloadPageEditView() {
  179. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  180. return
  181. }
  182. toolbarManager.pageProperty.state = viewManager.isPageEditMode ? .pressed : .normal
  183. pageButton.reloadData()
  184. }
  185. func reloadSelectZoomView() {
  186. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  187. return
  188. }
  189. toolbarManager.selectZoomProperty.isDisabled = false
  190. if viewManager.isPageEditMode {
  191. toolbarManager.selectZoomProperty.isDisabled = true
  192. }
  193. if let pdfview = self.pdfView {
  194. toolbarManager.selectZoomProperty.text = (NSString(format: "%.0f", pdfview.scaleFactor * 100) as String) + "%"
  195. }
  196. selectZoom.reloadData()
  197. }
  198. func refreshTabsScrollViewUI() {
  199. let pointX: CGFloat = (CGRectGetWidth(view.frame) - tabsView.viewWidth)/2
  200. var xValue: CGFloat = 0
  201. if pointX-12-264 > 10 {
  202. xValue = pointX - 12 - 264
  203. tabsLeftConst.constant = xValue
  204. tabsMoreButton.superview?.isHidden = true
  205. } else {
  206. xValue = 10
  207. let rightXValue = CGRectGetWidth(view.frame) - tabsView.viewWidth - 12 - 264 - rightViewWidthConst.constant - 12 - xValue
  208. if rightXValue < 10 {
  209. tabsMoreButton.superview?.isHidden = false
  210. tabsRightConst.constant = 44
  211. } else {
  212. tabsMoreButton.superview?.isHidden = true
  213. tabsRightConst.constant = rightXValue
  214. }
  215. }
  216. }
  217. func refreshTabsScrollViewVisiableRect() {
  218. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  219. return
  220. }
  221. if tabsView.viewWidth > CGRectGetWidth(tabsContendView.frame)-21 {
  222. if viewManager.toolMode != .None {
  223. var pointx: CGFloat = 0
  224. print(tabsScrollView.documentVisibleRect)
  225. let _manager = toolbarManager
  226. if viewManager.toolMode == .Markup {
  227. pointx = 0
  228. } else if viewManager.toolMode == .Edit {
  229. pointx = _manager.markupMode_Property.propertyInfo.viewWidth
  230. } else if viewManager.toolMode == .Form {
  231. pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth
  232. } else if viewManager.toolMode == .Fill {
  233. pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.formMode_Property.propertyInfo.viewWidth
  234. } else if viewManager.toolMode == .Convert {
  235. pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.formMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth
  236. } else if viewManager.toolMode == .Protect {
  237. 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
  238. } else if viewManager.toolMode == .Tools {
  239. 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
  240. }
  241. if tabsScrollView.documentVisibleRect.origin.x > pointx {
  242. tabsScrollView.scroll(tabsScrollView.contentView, to: CGPoint(x: pointx, y: 0))
  243. }
  244. }
  245. }
  246. }
  247. //MARK: -右侧工具模块
  248. func setUpRightViews() {
  249. let subviews = rightToolsView.subviews
  250. for view in subviews {
  251. view.removeFromSuperview()
  252. }
  253. var itemXvalue: CGFloat = 4
  254. guard let _manager = self.toolbarManager, let _ = self.viewManager else {
  255. return
  256. }
  257. var propertys: [Any] = []
  258. var adddividerA: Bool = false
  259. var adddividerB: Bool = false
  260. for property in _manager.validRightPropertys {
  261. if property == _manager.undoProperty ||
  262. property == _manager.redoProperty ||
  263. property == _manager.saveProperty {
  264. adddividerA = true
  265. } else if property == _manager.fileInfo_Property ||
  266. property == _manager.batchProperty ||
  267. property == _manager.ttsProperty ||
  268. property == _manager.pptProperty ||
  269. property == _manager.printProperty ||
  270. property == _manager.shareProperty {
  271. adddividerB = true
  272. }
  273. }
  274. for property in _manager.validRightPropertys {
  275. if adddividerA == true {
  276. if property == _manager.fileInfo_Property ||
  277. property == _manager.batchProperty ||
  278. property == _manager.ttsProperty ||
  279. property == _manager.pptProperty ||
  280. property == _manager.printProperty ||
  281. property == _manager.shareProperty {
  282. propertys.append(ComponentDividerProperty(type: .vertical, dash: false))
  283. adddividerA = false
  284. }
  285. }
  286. propertys.append(property)
  287. }
  288. if adddividerB == true {
  289. if propertys.last is ComponentDividerProperty {
  290. } else {
  291. propertys.append(ComponentDividerProperty(type: .vertical, dash: false))
  292. }
  293. }
  294. propertys.append(_manager.rightViewProperty)
  295. for property in propertys {
  296. if property is ComponentButtonProperty {
  297. let buttonProperty = (property as! ComponentButtonProperty)
  298. let button = ComponentButton.init()
  299. button.properties = buttonProperty
  300. if buttonProperty.onlyIcon {
  301. button.frame = CGRectMake(itemXvalue, 0, 28, 28)
  302. } else {
  303. button.frame = CGRectMake(itemXvalue, 0, button.properties.propertyInfo.viewWidth, 28)
  304. }
  305. button.setTarget(self, action: #selector(buttonClicked(_:)))
  306. rightToolsView.addSubview(button)
  307. itemXvalue += CGRectGetWidth(button.frame)
  308. itemXvalue += 12
  309. } else if property is ComponentDividerProperty {
  310. let divider = ComponentDivider.init()
  311. divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(rightToolsView.frame)/2-8, 1, 16)
  312. divider.properties = (property as! ComponentDividerProperty)
  313. rightToolsView.addSubview(divider)
  314. itemXvalue += 1
  315. itemXvalue += 12
  316. } else if property is ComponentDropdownToolProperty {
  317. let dropdownTool = ComponentDropdownTool.init()
  318. dropdownTool.properties = property as! ComponentDropdownToolProperty
  319. dropdownTool.frame = CGRectMake(itemXvalue, 0, 28, 28)
  320. dropdownTool.delegate = self
  321. rightToolsView.addSubview(dropdownTool)
  322. itemXvalue += CGRectGetWidth(dropdownTool.frame)
  323. itemXvalue += 12
  324. }
  325. }
  326. itemXvalue -= 12
  327. rightViewWidthConst.constant = itemXvalue
  328. }
  329. func reloadRightToolsView() {
  330. let subviews = rightToolsView.subviews
  331. for view in subviews {
  332. if view is ComponentButton {
  333. let button = view as! ComponentButton
  334. if button.properties.identifier == KMPDFToolbar_rightView_Identifier {
  335. if viewManager?.showRightSide == true {
  336. if button.properties.state != .pressed {
  337. button.properties.state = .pressed
  338. button.reloadData()
  339. }
  340. } else {
  341. if button.properties.state != .normal {
  342. button.properties.state = .normal
  343. button.reloadData()
  344. }
  345. }
  346. button.reloadData()
  347. }
  348. } else if view is ComponentDropdownTool {
  349. }
  350. }
  351. }
  352. //MARK: - 二级工具栏
  353. func setupSecondToolbar() {
  354. secondToolBar.view.frame = secondContendView.bounds
  355. secondToolBar.delegate = self
  356. secondToolBar.view.autoresizingMask = [.width, .height]
  357. secondToolBar.viewManager = viewManager
  358. secondContendView.addSubview(secondToolBar.view)
  359. }
  360. func reloadSecondToolbar(forceRefresh refresh: Bool = false) {
  361. guard let _manager = self.toolbarManager, let viewManager = self.viewManager else {
  362. return
  363. }
  364. if viewManager.isPageEditMode {
  365. secondToolBar.reloadMainPropertys(_manager.getSubToolbarItems(isPageEditMode: true))
  366. secondToolBar.reloadRightPropertys([_manager.page_Reduce_Property, _manager.page_Increase_Property])
  367. } else {
  368. let propertys = _manager.getSubToolbarItems(viewManager.toolMode)
  369. if let firstProperty = propertys.first {
  370. if secondToolBar.propertys.contains(firstProperty) == false {
  371. secondToolBar.reloadMainPropertys(propertys)
  372. } else {
  373. //同一种类型
  374. if refresh {
  375. //特殊情况下同一级点击刷新所有item
  376. secondToolBar.reloadMainPropertys(propertys)
  377. }
  378. }
  379. } else {
  380. secondToolBar.reloadMainPropertys(propertys)
  381. }
  382. secondToolBar.reloadRightPropertys([])
  383. }
  384. }
  385. //只刷新Item状态
  386. func refreshSecondToolbarItemsState() {
  387. toolbarManager?.eyeProperty.state = pdfView?.hideNotes == true ? .pressed : .normal
  388. secondToolBar.reloadMainview()
  389. secondToolBar.reloadRightview()
  390. }
  391. //MARK: - 取消二级工具栏按钮选中状态
  392. func cancelSelectedSecondToolbarItems(_ toolMode: KMPDFToolsMode) {
  393. guard let _manager = self.toolbarManager, let _ = self.viewManager else {
  394. return
  395. }
  396. let propertys = _manager.getSubToolbarItems(toolMode)
  397. for property in propertys {
  398. if property is ComponentButtonProperty {
  399. (property as! ComponentButtonProperty).state = .normal
  400. } else if property is ComponentDropdownToolProperty {
  401. (property as! ComponentDropdownToolProperty).state = .normal
  402. } else if property is ComponentSelectProperties {
  403. (property as! ComponentSelectProperties).state = .normal
  404. }
  405. }
  406. }
  407. //MARK: - PDF界面选中注释发生变化时调用
  408. func pdfViewActiveAnnotationsChanged() {
  409. if self.viewManager?.toolMode == .Form {
  410. let annotations = self.pdfView?.activeAnnotations ?? []
  411. if annotations.count < 2 {
  412. toolbarManager?.form_alignment_left_Property.isDisabled = true
  413. toolbarManager?.form_alignment_Hori_Property.isDisabled = true
  414. toolbarManager?.form_alignment_Right_Property.isDisabled = true
  415. toolbarManager?.form_alignment_Top_Property.isDisabled = true
  416. toolbarManager?.form_alignment_Vert_Property.isDisabled = true
  417. toolbarManager?.form_alignment_Bottom_Property.isDisabled = true
  418. toolbarManager?.form_alignment_Distribute_Vert_Property.isDisabled = true
  419. toolbarManager?.form_alignment_Distribute_Hori_Property.isDisabled = true
  420. } else if annotations.count < 3 {
  421. toolbarManager?.form_alignment_left_Property.isDisabled = false
  422. toolbarManager?.form_alignment_Hori_Property.isDisabled = false
  423. toolbarManager?.form_alignment_Right_Property.isDisabled = false
  424. toolbarManager?.form_alignment_Top_Property.isDisabled = false
  425. toolbarManager?.form_alignment_Vert_Property.isDisabled = false
  426. toolbarManager?.form_alignment_Bottom_Property.isDisabled = false
  427. toolbarManager?.form_alignment_Distribute_Vert_Property.isDisabled = true
  428. toolbarManager?.form_alignment_Distribute_Hori_Property.isDisabled = true
  429. } else {
  430. toolbarManager?.form_alignment_left_Property.isDisabled = false
  431. toolbarManager?.form_alignment_Hori_Property.isDisabled = false
  432. toolbarManager?.form_alignment_Right_Property.isDisabled = false
  433. toolbarManager?.form_alignment_Top_Property.isDisabled = false
  434. toolbarManager?.form_alignment_Vert_Property.isDisabled = false
  435. toolbarManager?.form_alignment_Bottom_Property.isDisabled = false
  436. toolbarManager?.form_alignment_Distribute_Vert_Property.isDisabled = false
  437. toolbarManager?.form_alignment_Distribute_Hori_Property.isDisabled = false
  438. }
  439. }
  440. }
  441. //MARK: - Action点击
  442. public func clickWithIdentify(_ identify: String) {
  443. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  444. return
  445. }
  446. if identify == KMPDFToolbar_PageEdit_Identifier {
  447. //MARK: -页面编辑
  448. pageButton.properties.state = viewManager.isPageEditMode ? .pressed : .normal
  449. pageButton.reloadData()
  450. viewManager.toolMode = .None
  451. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, pageButton.properties.identifier)
  452. reloadData()
  453. } else if identify == KMPDFToolbar_Markup_Identifier {
  454. componentTabsDidSelected(tabsView, toolbarManager.markupMode_Property)
  455. } else if identify == KMPDFToolbar_edit_link_Identifier {
  456. viewManager.toolMode = .Edit
  457. viewManager.subToolMode = .Edit_Link
  458. toolbarManager.edit_text_Property.state = .normal
  459. toolbarManager.edit_image_Property.state = .normal
  460. toolbarManager.edit_link_Property.state = .pressed
  461. refreshSecondToolbarItemsState()
  462. kmPDFSecToolbarControllerDidItemClicked(secondToolBar, toolbarManager.edit_link_Property)
  463. } else if identify == KMPDFToolbar_edit_text_Identifier {
  464. viewManager.toolMode = .Edit
  465. viewManager.subToolMode = .Edit_text
  466. toolbarManager.edit_text_Property.state = .pressed
  467. toolbarManager.edit_image_Property.state = .normal
  468. toolbarManager.edit_link_Property.state = .normal
  469. refreshSecondToolbarItemsState()
  470. kmPDFSecToolbarControllerDidItemClicked(secondToolBar, toolbarManager.edit_text_Property)
  471. } else if identify == KMPDFToolbar_edit_image_Identifier {
  472. viewManager.toolMode = .Edit
  473. viewManager.subToolMode = .Edit_Image
  474. toolbarManager.edit_text_Property.state = .normal
  475. toolbarManager.edit_image_Property.state = .pressed
  476. toolbarManager.edit_link_Property.state = .normal
  477. refreshSecondToolbarItemsState()
  478. kmPDFSecToolbarControllerDidItemClicked(secondToolBar, toolbarManager.edit_image_Property)
  479. }
  480. }
  481. //MARK: - 查找
  482. func findViewWith(_ identify: String) -> NSView? {
  483. //左侧
  484. //中间
  485. //右侧
  486. if true {
  487. let views = rightToolsView.subviews
  488. for view in views {
  489. if view is ComponentButton {
  490. if (view as! ComponentButton).properties.identifier == identify {
  491. return view
  492. }
  493. } else if view is ComponentDropdownTool {
  494. if (view as! ComponentDropdownTool).properties.identifier == identify {
  495. return view
  496. }
  497. for item in (view as! ComponentDropdownTool).properties.menuItemArr ?? [] {
  498. if item.identifier == identify {
  499. return view
  500. }
  501. }
  502. }
  503. }
  504. }
  505. return nil
  506. }
  507. //MARK: - reloadData
  508. func reloadData() {
  509. reloadLeftIconView()
  510. reloadPageEditView()
  511. reloadToolsView()
  512. reloadSelectZoomView()
  513. reloadToolbarTabsView()
  514. reloadRightToolsView()
  515. reloadSecondToolbar()
  516. refreshSecondToolbarItemsState()
  517. updateRightMenuItemSelectedState()
  518. reloadRedactToolbar()
  519. }
  520. //MARK: - 刷新tools工具
  521. func reloadToolsView() {
  522. toolbarManager?.reloadToolsData()
  523. toolbarManager?.tools_selectProperty.itemSelected = false
  524. toolbarManager?.tools_scrollProperty.itemSelected = false
  525. toolbarManager?.tools_contentProperty.itemSelected = false
  526. toolbarManager?.tools_magnifyProperty.itemSelected = false
  527. toolbarManager?.tools_areaProperty.itemSelected = false
  528. if viewManager?.viewToolsType == .Select {
  529. toolbarManager?.tools_selectProperty.itemSelected = true
  530. } else if viewManager?.viewToolsType == .Scroll {
  531. toolbarManager?.tools_scrollProperty.itemSelected = true
  532. } else if viewManager?.viewToolsType == .Content_Selection {
  533. toolbarManager?.tools_contentProperty.itemSelected = true
  534. } else if viewManager?.viewToolsType == .Magnify {
  535. toolbarManager?.tools_magnifyProperty.itemSelected = true
  536. } else if viewManager?.viewToolsType == .AreaZoom {
  537. toolbarManager?.tools_areaProperty.itemSelected = true
  538. }
  539. toolsView.reloadData()
  540. }
  541. //MARK: - Action
  542. @objc func buttonClicked(_ sender: ComponentButton) {
  543. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  544. return
  545. }
  546. if sender.properties == toolbarManager.viewProperty {
  547. //MARK: -View
  548. viewManager.showDisplayView = !viewManager.showDisplayView
  549. sender.properties.state = viewManager.showDisplayView ? .pressed : .normal
  550. sender.reloadData()
  551. } else if sender.properties == toolbarManager.pageProperty {
  552. //MARK: -页面编辑
  553. viewManager.isPageEditMode = !viewManager.isPageEditMode
  554. sender.properties.state = viewManager.isPageEditMode ? .pressed : .normal
  555. sender.reloadData()
  556. if viewManager.toolMode != .None {
  557. cancelSelectedSecondToolbarItems(viewManager.toolMode)
  558. viewManager.toolMode = .None
  559. }
  560. } else if sender.properties == toolbarManager.rightViewProperty {
  561. //MARK: -属性栏
  562. viewManager.showRightSide = !viewManager.showRightSide
  563. }
  564. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, sender.properties.identifier)
  565. reloadData()
  566. }
  567. @objc func tabsMoreButtonClicked(_ sender: ComponentButton) {
  568. guard let point = sender.superview?.convert(sender.frame.origin, to: view.window?.contentView) else { return }
  569. for item in moreTabsItems {
  570. item.state = .normal
  571. item.itemSelected = false
  572. }
  573. showGroupView(moreTabsItems, point)
  574. customRightGroupView.clickedAutoHide = true
  575. }
  576. //MARK: - 自定义工具栏
  577. func updateRightMenuItemSelectedState() {
  578. undoRedo_Menuitem_Property.itemSelected = false
  579. save_Menuitem_Property.itemSelected = false
  580. fileInfo_Menuitem_Property.itemSelected = false
  581. batch_Menuitem_Property.itemSelected = false
  582. tts_Menuitem_Property.itemSelected = false
  583. ppt_Menuitem_Property.itemSelected = false
  584. print_Menuitem_Property.itemSelected = false
  585. share_Menuitem_Property.itemSelected = false
  586. guard let toolbarManager = self.toolbarManager, let _ = self.viewManager else {
  587. return
  588. }
  589. for property in toolbarManager.validRightPropertys {
  590. if property == toolbarManager.undoProperty ||
  591. property == toolbarManager.redoProperty {
  592. undoRedo_Menuitem_Property.itemSelected = true
  593. } else if property == toolbarManager.saveProperty {
  594. save_Menuitem_Property.itemSelected = true
  595. } else if property == toolbarManager.fileInfo_Property {
  596. fileInfo_Menuitem_Property.itemSelected = true
  597. } else if property == toolbarManager.batchProperty {
  598. batch_Menuitem_Property.itemSelected = true
  599. } else if property == toolbarManager.ttsProperty {
  600. tts_Menuitem_Property.itemSelected = true
  601. } else if property == toolbarManager.pptProperty {
  602. ppt_Menuitem_Property.itemSelected = true
  603. } else if property == toolbarManager.printProperty {
  604. print_Menuitem_Property.itemSelected = true
  605. } else if property == toolbarManager.shareProperty {
  606. share_Menuitem_Property.itemSelected = true
  607. }
  608. }
  609. }
  610. func showPageTurnMenuWithPoint(_ point: CGPoint) {
  611. if (customRightGroupView?.superview) != nil {
  612. return
  613. }
  614. updateRightMenuItemSelectedState()
  615. let items: [ComponentMenuitemProperty] = [ComponentMenuitemProperty.header(KMLocalizedString("Show Right Tools")), undoRedo_Menuitem_Property, save_Menuitem_Property, ComponentMenuitemProperty.divider(),
  616. fileInfo_Menuitem_Property, batch_Menuitem_Property, tts_Menuitem_Property, ppt_Menuitem_Property, print_Menuitem_Property, share_Menuitem_Property]
  617. showGroupView(items, point)
  618. customRightGroupView.clickedAutoHide = false
  619. refreshRightMenuState()
  620. }
  621. func showGroupView(_ items: [ComponentMenuitemProperty], _ point: NSPoint) {
  622. var viewHeight: CGFloat = 8.0
  623. for item in items {
  624. item.state = .normal
  625. if item.type == .normal {
  626. viewHeight += 36
  627. } else if item.type == .divider {
  628. viewHeight += 8
  629. } else if item.type == .header {
  630. viewHeight += 36
  631. }
  632. }
  633. if customRightGroupView == nil {
  634. customRightGroupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  635. }
  636. customRightGroupView.clickedAutoHide = false
  637. customRightGroupView?.frame = CGRectMake(0, 0, 160, viewHeight)
  638. customRightGroupView?.reloadData()
  639. customRightGroupView.groupDelegate = self
  640. customRightGroupView.showWithPoint(CGPoint(x: point.x, y: point.y-viewHeight), relativeTo: self.contendBox)
  641. customRightGroupView?.updateGroupInfo(items)
  642. }
  643. func hideGroupView() {
  644. customRightGroupView?.removeGroupView()
  645. }
  646. func refreshRightMenuState() {
  647. let totalItems = customRightItems
  648. var selectedCount = 0
  649. for item in totalItems {
  650. if item.itemSelected == true {
  651. selectedCount += 1
  652. }
  653. }
  654. var maxCount = 6
  655. if undoRedo_Menuitem_Property.itemSelected == true {
  656. maxCount = 5
  657. }
  658. if selectedCount == maxCount {
  659. for item in totalItems {
  660. if item.itemSelected == false {
  661. item.isDisabled = true
  662. }
  663. }
  664. } else {
  665. for item in totalItems {
  666. item.isDisabled = false
  667. }
  668. }
  669. if selectedCount == 5 && undoRedo_Menuitem_Property.itemSelected == false {
  670. //特殊情况,UndoRedo占两个空位
  671. undoRedo_Menuitem_Property.isDisabled = true
  672. }
  673. for item in totalItems {
  674. NotificationCenter.default.post(name: Notification.Name(rawValue: ComponentMenuItemUpdatedKey), object: item.identifier)
  675. }
  676. }
  677. //MARK: - Redact
  678. func reloadRedactToolbar() {
  679. if viewManager?.subToolMode == .Redact {
  680. redactToolbar.view.frame = contendBox.bounds
  681. redactToolbar.view.autoresizingMask = [.width, .height]
  682. contendBox.addSubview(redactToolbar.view)
  683. redactToolbar.delegate = self
  684. redactToolbar.reloadData()
  685. } else {
  686. redactToolbar.view.removeFromSuperview()
  687. }
  688. }
  689. //MARK: - Noti
  690. @objc func imageColorChangedNoti() {
  691. toolbarManager?.refreshMarkupColorImages()
  692. secondToolBar.reloadMainview()
  693. }
  694. //MARK: - MouseEvent
  695. override func rightMouseUp(with event: NSEvent) {
  696. super.rightMouseUp(with: event)
  697. var clickEnabled = true
  698. let windowPoint = event.locationInWindow
  699. if tabsMoreButton.superview?.isHidden == false {
  700. if CGRectContainsPoint(tabsMoreButton.frame, mainToolbarContendView.convert(windowPoint, from: nil)) {
  701. clickEnabled = false
  702. }
  703. }
  704. if clickEnabled {
  705. showPageTurnMenuWithPoint(windowPoint)
  706. }
  707. }
  708. }
  709. //MARK: - ComponentDropdownToolDelegate:工具模式
  710. extension KMPDFToolbarController: ComponentDropdownToolDelegate {
  711. func componentDropdownToolDidClicked(_ view: ComponentDropdownTool, menuItem: ComponentMenuitemProperty?) {
  712. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  713. return
  714. }
  715. for item in toolbarManager.toolsProperty.menuItemArr ?? [] {
  716. item.itemSelected = false
  717. item.state = .normal
  718. }
  719. menuItem?.itemSelected = true
  720. var itemIdentifier = menuItem?.identifier ?? ""
  721. if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_View_Identifier).contains(itemIdentifier) {
  722. //MARK: 工具模式
  723. if menuItem == toolbarManager.tools_selectProperty {
  724. viewManager.viewToolsType = .Select
  725. } else if menuItem == toolbarManager.tools_scrollProperty {
  726. viewManager.viewToolsType = .Scroll
  727. } else if menuItem == toolbarManager.tools_contentProperty {
  728. viewManager.viewToolsType = .Content_Selection
  729. } else if menuItem == toolbarManager.tools_magnifyProperty {
  730. viewManager.viewToolsType = .Magnify
  731. } else if menuItem == toolbarManager.tools_areaProperty {
  732. viewManager.viewToolsType = .AreaZoom
  733. }
  734. if viewManager.viewToolsType != .Select {
  735. viewManager.toolMode = .None
  736. reloadToolbarTabsView()
  737. }
  738. reloadToolsView()
  739. delegate?.kmPDFToolbarControllerDidViewToolsChanged?(self)
  740. } else if menuItem == toolbarManager.share_PDF_Property ||
  741. menuItem == toolbarManager.share_Flatted_Property ||
  742. menuItem == toolbarManager.share_Original_Property {
  743. //MARK: -分享
  744. if let id = menuItem?.identifier {
  745. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, id)
  746. }
  747. } else {
  748. }
  749. }
  750. }
  751. //MARK: - ComponentSelectZoomDelegate:缩放比例
  752. extension KMPDFToolbarController: ComponentSelectZoomDelegate {
  753. func componentSelectZoomDidReduce(view: ComponentSelectZoom) {
  754. let zoomSting = view.properties.text
  755. var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0)
  756. switch scale {
  757. case 0...0.25:
  758. scale = 0
  759. case 0.25...3:
  760. scale -= 0.25
  761. case 3.1...10:
  762. scale -= 0.4
  763. case 10.1...100:
  764. scale -= 1
  765. default:
  766. scale -= 1
  767. }
  768. pdfView?.scaleFactor = scale
  769. }
  770. func componentSelectZoomDidSelect(view: ComponentSelectZoom?, menuItemProperty: ComponentMenuitemProperty?) {
  771. if menuItemProperty?.identifier == KMPDFToolbar_Zoom_AdapWidth_Identifier {
  772. pdfView?.autoScales = true
  773. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_AdapPage_Identifier {
  774. let pageHeight = pdfView?.currentPage()!.size.height
  775. let pdfviewHeight = pdfView?.bounds.size.height
  776. pdfView?.scaleFactor = pdfviewHeight!/pageHeight!
  777. pdfView?.autoScales = false
  778. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_ActualSize_Identifier {
  779. if pdfView?.scaleFactor != 1.0 {
  780. pdfView?.scaleFactor = 1.0
  781. pdfView?.autoScales = false
  782. }
  783. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_10_Identifier {
  784. pdfView?.scaleFactor = 0.1
  785. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_25_Identifier {
  786. pdfView?.scaleFactor = 0.25
  787. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_50_Identifier {
  788. pdfView?.scaleFactor = 0.5
  789. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_75_Identifier {
  790. pdfView?.scaleFactor = 0.75
  791. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_100_Identifier {
  792. pdfView?.scaleFactor = 1.0
  793. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_150_Identifier {
  794. pdfView?.scaleFactor = 1.5
  795. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_200_Identifier {
  796. pdfView?.scaleFactor = 2.0
  797. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_400_Identifier {
  798. pdfView?.scaleFactor = 4.0
  799. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_800_Identifier {
  800. pdfView?.scaleFactor = 8.0
  801. }
  802. }
  803. func componentSelectZoomDidAdd(view: ComponentSelectZoom) {
  804. let zoomSting = view.properties.text
  805. var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0)
  806. switch scale {
  807. case 0...0.25:
  808. scale += 0.25
  809. case 0.25...3:
  810. scale += 0.25
  811. case 3.1...10:
  812. scale += 0.4
  813. case 10.1...100:
  814. scale += 1
  815. default:
  816. scale += 1
  817. }
  818. pdfView?.scaleFactor = scale
  819. }
  820. }
  821. //MARK: - ComponentTabsDelegate:一级工具栏
  822. extension KMPDFToolbarController: ComponentTabsDelegate {
  823. func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) {
  824. guard let toolbarManager = self.toolbarManager, let _manager = self.viewManager else {
  825. return
  826. }
  827. if _manager.viewToolsType != .Select {
  828. _manager.viewToolsType = .Select
  829. delegate?.kmPDFToolbarControllerDidViewToolsChanged?(self)
  830. }
  831. if _manager.isPageEditMode == true {
  832. delegate?.kmPDFToolbarControllerDidExitPageEditMode?(self)
  833. _manager.isPageEditMode = false
  834. }
  835. cancelSelectedSecondToolbarItems(_manager.toolMode)
  836. if property == toolbarManager.markupMode_Property {
  837. if _manager.toolMode == .Markup {
  838. _manager.toolMode = .None
  839. property.state = .normal
  840. view.refreshItems()
  841. } else {
  842. _manager.toolMode = .Markup
  843. }
  844. _manager.subToolMode = .None
  845. } else if property == toolbarManager.editMode_Property {
  846. if _manager.toolMode == .Edit {
  847. _manager.toolMode = .None
  848. property.state = .normal
  849. view.refreshItems()
  850. } else {
  851. _manager.toolMode = .Edit
  852. }
  853. _manager.subToolMode = .None
  854. } else if property == toolbarManager.formMode_Property {
  855. if _manager.toolMode == .Form {
  856. _manager.toolMode = .None
  857. property.state = .normal
  858. view.refreshItems()
  859. } else {
  860. _manager.toolMode = .Form
  861. }
  862. _manager.subToolMode = .None
  863. pdfViewActiveAnnotationsChanged()
  864. } else if property == toolbarManager.fillMode_Property {
  865. if _manager.toolMode == .Fill {
  866. _manager.toolMode = .None
  867. property.state = .normal
  868. view.refreshItems()
  869. } else {
  870. _manager.toolMode = .Fill
  871. }
  872. _manager.subToolMode = .None
  873. } else if property == toolbarManager.convertMode_Property {
  874. if _manager.toolMode == .Convert {
  875. _manager.toolMode = .None
  876. property.state = .normal
  877. view.refreshItems()
  878. } else {
  879. _manager.toolMode = .Convert
  880. }
  881. _manager.subToolMode = .None
  882. } else if property == toolbarManager.protectMode_Property {
  883. if _manager.toolMode == .Protect {
  884. _manager.toolMode = .None
  885. property.state = .normal
  886. view.refreshItems()
  887. } else {
  888. _manager.toolMode = .Protect
  889. }
  890. _manager.subToolMode = .None
  891. } else if property == toolbarManager.toolsMode_Property {
  892. if _manager.toolMode == .Tools {
  893. _manager.toolMode = .None
  894. property.state = .normal
  895. view.refreshItems()
  896. } else {
  897. _manager.toolMode = .Tools
  898. }
  899. _manager.subToolMode = .None
  900. }
  901. refreshTabsScrollViewVisiableRect()
  902. delegate?.kmPDFToolbarControllerDidToolModeChanged?(self)
  903. reloadData()
  904. }
  905. }
  906. //MARK: - KMPDFSecToolbarControllerDelegate:二级工具栏
  907. extension KMPDFToolbarController: KMPDFSecToolbarControllerDelegate {
  908. func kmPDFSecToolbarControllerDidItemClicked(_ controller: KMPDFSecToolbarController, _ property: Any) {
  909. if let value = property as? ComponentButtonProperty {
  910. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, value.identifier)
  911. reloadData()
  912. } else if let value = property as? ComponentMenuitemProperty {
  913. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, value.identifier)
  914. reloadData()
  915. } else if let value = property as? ComponentDropdownToolProperty {
  916. if value.identifier == KMPDFToolbar_edit_Watermark_Identifier ||
  917. value.identifier == KMPDFToolbar_edit_BG_Identifier ||
  918. value.identifier == KMPDFToolbar_edit_HF_Identifier ||
  919. value.identifier == KMPDFToolbar_edit_Bates_Identifier {
  920. if viewManager?.subToolMode != .None {
  921. viewManager?.subToolMode = .None
  922. }
  923. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, value.identifier)
  924. }
  925. }
  926. }
  927. func kmPDFSecToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) {
  928. delegate?.kmPDFToolbarControllerDidSelectTextDidBeginEditing?(self, view)
  929. }
  930. func kmPDFSecToolbarControllerDidSelectTextDidChange(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) {
  931. delegate?.kmPDFToolbarControllerDidSelectTextDidChange?(self, view)
  932. }
  933. func kmPDFSecToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) {
  934. delegate?.kmPDFToolbarControllerDidSelectTextDidEndEditing?(self, view)
  935. }
  936. }
  937. //MARK: - ComponentGroupDelegate:右侧工具栏
  938. extension KMPDFToolbarController: ComponentGroupDelegate {
  939. func componentGroupDidDismiss(group: ComponentGroup?) {
  940. tabsMoreButton.properties.state = .normal
  941. tabsMoreButton.reloadData()
  942. }
  943. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  944. guard let property = menuItemProperty else {
  945. return
  946. }
  947. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  948. return
  949. }
  950. if customRightItems.contains(property) {
  951. var validItems:[NSObject] = []
  952. if undoRedo_Menuitem_Property.itemSelected {
  953. validItems.append(toolbarManager.undoProperty)
  954. validItems.append(toolbarManager.redoProperty)
  955. }
  956. if save_Menuitem_Property.itemSelected {
  957. validItems.append(toolbarManager.saveProperty)
  958. }
  959. if fileInfo_Menuitem_Property.itemSelected {
  960. validItems.append(toolbarManager.fileInfo_Property)
  961. }
  962. if batch_Menuitem_Property.itemSelected {
  963. validItems.append(toolbarManager.batchProperty)
  964. }
  965. if tts_Menuitem_Property.itemSelected {
  966. validItems.append(toolbarManager.ttsProperty)
  967. }
  968. if ppt_Menuitem_Property.itemSelected {
  969. validItems.append(toolbarManager.pptProperty)
  970. }
  971. if print_Menuitem_Property.itemSelected {
  972. validItems.append(toolbarManager.printProperty)
  973. }
  974. if share_Menuitem_Property.itemSelected {
  975. validItems.append(toolbarManager.shareProperty)
  976. }
  977. let sortArray = toolbarManager.totalRightPropertys
  978. let sortedArray = validItems.sorted {
  979. guard let indexA = sortArray.firstIndex(of: $0 as NSObject),
  980. let indexB = sortArray.firstIndex(of: $1 as NSObject) else {
  981. return false
  982. }
  983. return indexA < indexB
  984. }
  985. toolbarManager.validRightPropertys = sortedArray
  986. setUpRightViews()
  987. refreshRightMenuState()
  988. } else if moreTabsItems.contains(property) {
  989. viewManager.viewToolsType = .Select
  990. if viewManager.isPageEditMode == true {
  991. delegate?.kmPDFToolbarControllerDidExitPageEditMode?(self)
  992. viewManager.isPageEditMode = false
  993. }
  994. if property == markup_Menuitem_Property {
  995. viewManager.toolMode = .Markup
  996. } else if property == edit_Menuitem_Property {
  997. viewManager.toolMode = .Edit
  998. } else if property == form_Menuitem_Property {
  999. viewManager.toolMode = .Form
  1000. } else if property == fill_Menuitem_Property {
  1001. viewManager.toolMode = .Fill
  1002. } else if property == toolbarManager.convertMode_Property {
  1003. viewManager.toolMode = .Convert
  1004. } else if property == protect_Menuitem_Property {
  1005. viewManager.toolMode = .Protect
  1006. } else if property == tools_Menuitem_Property {
  1007. viewManager.toolMode = .Tools
  1008. }
  1009. tabsView.refreshItems()
  1010. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, property.identifier)
  1011. reloadData()
  1012. }
  1013. }
  1014. }
  1015. //MARK: - KMRedactToolbarControllerDelegate标记密文
  1016. extension KMPDFToolbarController: KMRedactToolbarControllerDelegate {
  1017. func redactToolbarControllerDidButtonClick(controller: KMRedactToolbarController, buttonIndex: Int) {
  1018. if buttonIndex == 1 {
  1019. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, KMPDFToolbar_protect_redact_Property_Identifier)
  1020. } else if buttonIndex == 2 {
  1021. viewManager?.subToolMode = .None
  1022. cancelSelectedSecondToolbarItems(.Protect)
  1023. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, KMPDFToolbar_protect_redact_Apply_Identifier)
  1024. } else if buttonIndex == 3 {
  1025. viewManager?.subToolMode = .None
  1026. cancelSelectedSecondToolbarItems(.Protect)
  1027. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, KMPDFToolbar_protect_redact_Exit_Identifier)
  1028. }
  1029. reloadData()
  1030. }
  1031. }