KMPDFToolbarController.swift 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. @objc optional func kmPDFToolbarControllerDidToolbarItemClicked(_ controller: KMPDFToolbarController, _ itemIdentifier: String)
  11. //结束页面编辑
  12. @objc optional func kmPDFToolbarControllerDidExitPageEditMode(_ controller: KMPDFToolbarController)
  13. @objc optional func kmPDFToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect)
  14. @objc optional func kmPDFToolbarControllerDidSelectTextDidChange(_ controller: KMPDFToolbarController, _ view: ComponentSelect)
  15. @objc optional func kmPDFToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect)
  16. }
  17. class KMPDFToolbarController: NSViewController {
  18. @IBOutlet var contendBox: NSBox!
  19. @IBOutlet var mainToolbarContendView: NSView!
  20. @IBOutlet var leftToolsView: NSView!
  21. @IBOutlet var leftViewButton: ComponentButton!
  22. @IBOutlet var pageButton: ComponentButton!
  23. @IBOutlet var toolsView: ComponentDropdownTool!
  24. @IBOutlet var dividerView: ComponentDivider!
  25. @IBOutlet var selectZoom: ComponentSelectZoom!
  26. @IBOutlet var tabsContendView: NSView!
  27. @IBOutlet var tabsScrollView: NSScrollView!
  28. @IBOutlet var tabsMoreButton: ComponentButton!
  29. @IBOutlet var rightToolsView: NSView!
  30. @IBOutlet var secondContendView: NSView!
  31. @IBOutlet var tabsLeftConst: NSLayoutConstraint!
  32. @IBOutlet var tabsRightConst: NSLayoutConstraint!
  33. @IBOutlet var rightViewWidthConst: NSLayoutConstraint!
  34. var scrollDocumentView: NSView = NSView.init()
  35. var tabsView: ComponentTabs!
  36. //MARK: -自定义工具栏
  37. private var customRightGroupView: ComponentGroup!
  38. private var customRightItems: [ComponentMenuitemProperty] = []
  39. private var undoRedo_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Undo / Redo"), identifier: "undoRedo_Menuitem_PropertyKey")
  40. private var save_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Save"), identifier: "save_Menuitem_PropertyKey")
  41. private var aiTools_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("AI Tools"), identifier: "aiTools_Menuitem_PropertyKey")
  42. private var batch_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Batch"), identifier: "batch_Menuitem_PropertyKey")
  43. private var tts_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("TTS"), identifier: "tts_Menuitem_PropertyKey")
  44. private var ppt_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Slide Show"), identifier: "ppt_Menuitem_PropertyKey")
  45. private var print_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Print"), identifier: "print_Menuitem_PropertyKey")
  46. private var share_Menuitem_Property = ComponentMenuitemProperty(type: .normal, multipleSelect: true, itemSelected: false, text: KMLocalizedString("Share"), identifier: "share_Menuitem_PropertyKey")
  47. //MARK: -更多Tabs
  48. private var moreTabsItems: [ComponentMenuitemProperty] = []
  49. private var markup_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Markup"), identifier: "markup_Menuitem_PropertyKey")
  50. private var edit_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Edit"), identifier: "markup_Menuitem_PropertyKey")
  51. private var form_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Form"), identifier: "markup_Menuitem_PropertyKey")
  52. private var fill_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Fill"), identifier: "markup_Menuitem_PropertyKey")
  53. private var convert_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Convert"), identifier: "markup_Menuitem_PropertyKey")
  54. private var protect_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Protect"), identifier: "markup_Menuitem_PropertyKey")
  55. private var tools_Menuitem_Property = ComponentMenuitemProperty(type: .normal, itemSelected: false, text: KMLocalizedString("Tools"), identifier: "markup_Menuitem_PropertyKey")
  56. var pdfView: CPDFListView?
  57. var viewManager: KMPDFViewManager?
  58. var toolbarManager: KMPDFToolbarManager?
  59. //MARK: -View
  60. var secondToolBar: KMPDFSecToolbarController = KMPDFSecToolbarController.init()
  61. weak open var delegate: KMPDFToolbarControllerDelegate?
  62. override func viewWillLayout() {
  63. super.viewWillLayout()
  64. refreshTabsScrollViewUI()
  65. }
  66. override func viewDidLayout() {
  67. super.viewDidLayout()
  68. refreshTabsScrollViewUI()
  69. refreshTabsScrollViewVisiableRect()
  70. }
  71. override func viewDidAppear() {
  72. super.viewDidAppear()
  73. refreshTabsScrollViewUI()
  74. }
  75. override func viewDidLoad() {
  76. super.viewDidLoad()
  77. // Do view setup here.
  78. contendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  79. leftToolsView.wantsLayer = true
  80. leftToolsView.layer?.backgroundColor = contendBox.fillColor.cgColor
  81. rightToolsView.wantsLayer = true
  82. rightToolsView.layer?.backgroundColor = contendBox.fillColor.cgColor
  83. tabsContendView.wantsLayer = true
  84. tabsContendView.layer?.backgroundColor = contendBox.fillColor.cgColor
  85. tabsMoreButton.superview?.wantsLayer = true
  86. tabsMoreButton.superview?.layer?.backgroundColor = contendBox.fillColor.cgColor
  87. tabsView = ComponentTabs.init()
  88. tabsView.frame = CGRectMake(0, 0, 446, 40)
  89. scrollDocumentView.wantsLayer = true
  90. tabsScrollView.documentView = scrollDocumentView
  91. setUpData()
  92. }
  93. func setUpData() {
  94. setUpProperty()
  95. setUpRightViews()
  96. setupSecondToolbar()
  97. reloadData()
  98. }
  99. func setUpProperty() {
  100. guard let toolbarManager = self.toolbarManager else {
  101. return
  102. }
  103. leftViewButton.properties = toolbarManager.viewProperty
  104. leftViewButton.setTarget(self, action: #selector(buttonClicked(_:)))
  105. pageButton.properties = toolbarManager.pageProperty
  106. pageButton.setTarget(self, action: #selector(buttonClicked(_:)))
  107. dividerView.properties = ComponentDividerProperty(type: .vertical, dash: false)
  108. toolsView.properties = toolbarManager.toolsProperty
  109. toolsView.delegate = self
  110. selectZoom.properties = toolbarManager.selectZoomProperty
  111. selectZoom.delegate = self
  112. tabsView.updateItemProperty([toolbarManager.markupMode_Property,
  113. toolbarManager.editMode_Property,
  114. toolbarManager.formMode_Property,
  115. toolbarManager.fillMode_Property,
  116. toolbarManager.convertMode_Property,
  117. toolbarManager.protectMode_Property,
  118. toolbarManager.toolsMode_Property])
  119. tabsView.delegate = self
  120. scrollDocumentView.frame = CGRectMake(0, 0, tabsView.viewWidth, 28)
  121. tabsView.frame = scrollDocumentView.bounds
  122. tabsView.autoresizingMask = [.width, .height]
  123. scrollDocumentView.addSubview(tabsView)
  124. tabsMoreButton.properties = ComponentButtonProperty(type: .text_gray,
  125. size: .xs,
  126. onlyIcon: true,
  127. icon: NSImage(named: "toolbar_more"))
  128. tabsMoreButton.setTarget(self, action: #selector(tabsMoreButtonClicked(_:)))
  129. customRightItems = [undoRedo_Menuitem_Property,
  130. save_Menuitem_Property,
  131. aiTools_Menuitem_Property,
  132. batch_Menuitem_Property,
  133. tts_Menuitem_Property,
  134. ppt_Menuitem_Property,
  135. print_Menuitem_Property,
  136. share_Menuitem_Property]
  137. moreTabsItems = [markup_Menuitem_Property,
  138. edit_Menuitem_Property,
  139. form_Menuitem_Property,
  140. fill_Menuitem_Property,
  141. convert_Menuitem_Property,
  142. protect_Menuitem_Property,
  143. tools_Menuitem_Property]
  144. }
  145. //MARK: - 刷新一级工具栏
  146. func reloadToolbarTabsView() {
  147. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  148. return
  149. }
  150. toolbarManager.markupMode_Property.state = viewManager.toolMode == .Markup ? .pressed : .normal
  151. toolbarManager.editMode_Property.state = viewManager.toolMode == .Edit ? .pressed : .normal
  152. toolbarManager.formMode_Property.state = viewManager.toolMode == .Form ? .pressed : .normal
  153. toolbarManager.fillMode_Property.state = viewManager.toolMode == .Fill ? .pressed : .normal
  154. toolbarManager.convertMode_Property.state = viewManager.toolMode == .Convert ? .pressed : .normal
  155. toolbarManager.protectMode_Property.state = viewManager.toolMode == .Protect ? .pressed : .normal
  156. toolbarManager.toolsMode_Property.state = viewManager.toolMode == .Tools ? .pressed : .normal
  157. tabsView.refreshItems()
  158. }
  159. func reloadLeftIconView() {
  160. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  161. return
  162. }
  163. toolbarManager.viewProperty.state = viewManager.showDisplayView ? .pressed : .normal
  164. toolbarManager.viewProperty.isDisabled = false
  165. if viewManager.isPageEditMode {
  166. toolbarManager.viewProperty.isDisabled = true
  167. }
  168. leftViewButton.reloadData()
  169. }
  170. func reloadPageEditView() {
  171. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  172. return
  173. }
  174. toolbarManager.pageProperty.state = viewManager.isPageEditMode ? .pressed : .normal
  175. pageButton.reloadData()
  176. }
  177. func reloadSelectZoomView() {
  178. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  179. return
  180. }
  181. toolbarManager.selectZoomProperty.isDisabled = false
  182. if viewManager.isPageEditMode {
  183. toolbarManager.selectZoomProperty.isDisabled = true
  184. }
  185. if let pdfview = self.pdfView {
  186. toolbarManager.selectZoomProperty.text = (NSString(format: "%.0f", pdfview.scaleFactor * 100) as String) + "%"
  187. }
  188. selectZoom.reloadData()
  189. }
  190. func refreshTabsScrollViewUI() {
  191. let pointX: CGFloat = (CGRectGetWidth(view.frame) - tabsView.viewWidth)/2
  192. var xValue: CGFloat = 0
  193. if pointX-12-264 > 10 {
  194. xValue = pointX - 12 - 264
  195. tabsLeftConst.constant = xValue
  196. tabsMoreButton.superview?.isHidden = true
  197. } else {
  198. xValue = 10
  199. let rightXValue = CGRectGetWidth(view.frame) - tabsView.viewWidth - 12 - 264 - rightViewWidthConst.constant - 12 - xValue
  200. if rightXValue < 10 {
  201. tabsMoreButton.superview?.isHidden = false
  202. tabsRightConst.constant = 44
  203. } else {
  204. tabsMoreButton.superview?.isHidden = true
  205. tabsRightConst.constant = rightXValue
  206. }
  207. }
  208. }
  209. func refreshTabsScrollViewVisiableRect() {
  210. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  211. return
  212. }
  213. if tabsView.viewWidth > CGRectGetWidth(tabsContendView.frame)-21 {
  214. if viewManager.toolMode != .None {
  215. var pointx: CGFloat = 0
  216. print(tabsScrollView.documentVisibleRect)
  217. let _manager = toolbarManager
  218. if viewManager.toolMode == .Markup {
  219. pointx = 0
  220. } else if viewManager.toolMode == .Edit {
  221. pointx = _manager.markupMode_Property.propertyInfo.viewWidth
  222. } else if viewManager.toolMode == .Form {
  223. pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth
  224. } else if viewManager.toolMode == .Fill {
  225. pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.formMode_Property.propertyInfo.viewWidth
  226. } else if viewManager.toolMode == .Convert {
  227. pointx = _manager.markupMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth + _manager.formMode_Property.propertyInfo.viewWidth + _manager.editMode_Property.propertyInfo.viewWidth
  228. } else if viewManager.toolMode == .Protect {
  229. 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
  230. } else if viewManager.toolMode == .Tools {
  231. 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
  232. }
  233. if tabsScrollView.documentVisibleRect.origin.x > pointx {
  234. tabsScrollView.scroll(tabsScrollView.contentView, to: CGPoint(x: pointx, y: 0))
  235. }
  236. }
  237. }
  238. }
  239. //MARK: -右侧工具模块
  240. func setUpRightViews() {
  241. let subviews = rightToolsView.subviews
  242. for view in subviews {
  243. view.removeFromSuperview()
  244. }
  245. var itemXvalue: CGFloat = 4
  246. guard let _manager = self.toolbarManager, let viewManager = self.viewManager else {
  247. return
  248. }
  249. var propertys: [Any] = []
  250. var adddividerA: Bool = false
  251. var adddividerB: Bool = false
  252. for property in _manager.validRightPropertys {
  253. if property == _manager.undoProperty ||
  254. property == _manager.redoProperty ||
  255. property == _manager.saveProperty {
  256. adddividerA = true
  257. } else if property == _manager.aiToolsProperty ||
  258. property == _manager.batchProperty ||
  259. property == _manager.ttsProperty ||
  260. property == _manager.pptProperty ||
  261. property == _manager.printProperty ||
  262. property == _manager.shareProperty {
  263. adddividerB = true
  264. }
  265. }
  266. for property in _manager.validRightPropertys {
  267. if adddividerA == true {
  268. if property == _manager.aiToolsProperty ||
  269. property == _manager.batchProperty ||
  270. property == _manager.ttsProperty ||
  271. property == _manager.pptProperty ||
  272. property == _manager.printProperty ||
  273. property == _manager.shareProperty {
  274. propertys.append(ComponentDividerProperty(type: .vertical, dash: false))
  275. adddividerA = false
  276. }
  277. }
  278. propertys.append(property)
  279. }
  280. if adddividerB == true {
  281. if propertys.last is ComponentDividerProperty {
  282. } else {
  283. propertys.append(ComponentDividerProperty(type: .vertical, dash: false))
  284. }
  285. }
  286. propertys.append(_manager.rightViewProperty)
  287. for property in propertys {
  288. if property is ComponentButtonProperty {
  289. let buttonProperty = (property as! ComponentButtonProperty)
  290. let button = ComponentButton.init()
  291. button.properties = buttonProperty
  292. if buttonProperty.onlyIcon {
  293. button.frame = CGRectMake(itemXvalue, 0, 28, 28)
  294. } else {
  295. button.frame = CGRectMake(itemXvalue, 0, button.properties.propertyInfo.viewWidth, 28)
  296. }
  297. button.setTarget(self, action: #selector(buttonClicked(_:)))
  298. rightToolsView.addSubview(button)
  299. itemXvalue += CGRectGetWidth(button.frame)
  300. itemXvalue += 12
  301. } else if property is ComponentDividerProperty {
  302. let divider = ComponentDivider.init()
  303. divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(rightToolsView.frame)/2-8, 1, 16)
  304. divider.properties = (property as! ComponentDividerProperty)
  305. rightToolsView.addSubview(divider)
  306. itemXvalue += 1
  307. itemXvalue += 12
  308. } else if property is ComponentDropdownToolProperty {
  309. let dropdownTool = ComponentDropdownTool.init()
  310. dropdownTool.properties = property as! ComponentDropdownToolProperty
  311. dropdownTool.frame = CGRectMake(itemXvalue, 0, 28, 28)
  312. dropdownTool.delegate = self
  313. rightToolsView.addSubview(dropdownTool)
  314. itemXvalue += CGRectGetWidth(dropdownTool.frame)
  315. itemXvalue += 12
  316. }
  317. }
  318. itemXvalue -= 12
  319. rightViewWidthConst.constant = itemXvalue
  320. }
  321. func reloadRightToolsView() {
  322. let subviews = rightToolsView.subviews
  323. for view in subviews {
  324. if view is ComponentButton {
  325. let button = view as! ComponentButton
  326. if button.properties.identifier == KMPDFToolbar_rightView_Identifier {
  327. if viewManager?.showRightSide == true {
  328. if button.properties.state != .pressed {
  329. button.properties.state = .pressed
  330. button.reloadData()
  331. }
  332. } else {
  333. if button.properties.state != .normal {
  334. button.properties.state = .normal
  335. button.reloadData()
  336. }
  337. }
  338. }
  339. } else if view is ComponentDropdownTool {
  340. }
  341. }
  342. }
  343. //MARK: - 二级工具栏
  344. func setupSecondToolbar() {
  345. secondToolBar.view.frame = secondContendView.bounds
  346. secondToolBar.delegate = self
  347. secondToolBar.view.autoresizingMask = [.width, .height]
  348. secondToolBar.viewManager = viewManager
  349. secondContendView.addSubview(secondToolBar.view)
  350. }
  351. func reloadSecondToolbar() {
  352. guard let _manager = self.toolbarManager, let viewManager = self.viewManager else {
  353. return
  354. }
  355. if viewManager.isPageEditMode {
  356. secondToolBar.reloadMainPropertys(_manager.getSubToolbarItems(isPageEditMode: true))
  357. secondToolBar.reloadRightPropertys([_manager.page_Reduce_Property, _manager.page_Increase_Property])
  358. } else {
  359. secondToolBar.reloadMainPropertys(_manager.getSubToolbarItems(viewManager.toolMode))
  360. secondToolBar.reloadRightPropertys([])
  361. }
  362. }
  363. //只刷新Item状态
  364. func refreshSecondToolbarItemsState() {
  365. secondToolBar.reloadMainview()
  366. secondToolBar.reloadRightview()
  367. }
  368. func clickWithIdentify(_ identify: String) {
  369. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  370. return
  371. }
  372. if identify == KMPDFToolbar_PageEdit_Identifier {
  373. pageButton.properties.state = viewManager.isPageEditMode ? .pressed : .normal
  374. pageButton.reloadData()
  375. viewManager.toolMode = .None
  376. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, pageButton.properties.identifier)
  377. reloadData()
  378. }
  379. }
  380. //MARK: - reloadData
  381. func reloadData() {
  382. reloadLeftIconView()
  383. reloadPageEditView()
  384. reloadToolsView()
  385. reloadSelectZoomView()
  386. reloadToolbarTabsView()
  387. reloadRightToolsView()
  388. reloadSecondToolbar()
  389. updateRightMenuItemSelectedState()
  390. }
  391. //MARK: - 刷新tools工具
  392. func reloadToolsView() {
  393. toolbarManager?.reloadToolsData()
  394. toolbarManager?.tools_selectProperty.itemSelected = false
  395. toolbarManager?.tools_scrollProperty.itemSelected = false
  396. toolbarManager?.tools_contentProperty.itemSelected = false
  397. toolbarManager?.tools_magnifyProperty.itemSelected = false
  398. toolbarManager?.tools_areaProperty.itemSelected = false
  399. if viewManager?.viewToolsType == .Select {
  400. toolbarManager?.tools_selectProperty.itemSelected = true
  401. } else if viewManager?.viewToolsType == .Scroll {
  402. toolbarManager?.tools_scrollProperty.itemSelected = true
  403. } else if viewManager?.viewToolsType == .Content_Selection {
  404. toolbarManager?.tools_contentProperty.itemSelected = true
  405. } else if viewManager?.viewToolsType == .Magnify {
  406. toolbarManager?.tools_magnifyProperty.itemSelected = true
  407. } else if viewManager?.viewToolsType == .AreaZoom {
  408. toolbarManager?.tools_areaProperty.itemSelected = true
  409. }
  410. toolsView.reloadData()
  411. }
  412. //MARK: - Action
  413. @objc func buttonClicked(_ sender: ComponentButton) {
  414. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  415. return
  416. }
  417. if sender.properties == toolbarManager.viewProperty {
  418. viewManager.showDisplayView = !viewManager.showDisplayView
  419. sender.properties.state = viewManager.showDisplayView ? .pressed : .normal
  420. sender.reloadData()
  421. } else if sender.properties == toolbarManager.pageProperty {
  422. viewManager.isPageEditMode = !viewManager.isPageEditMode
  423. sender.properties.state = viewManager.isPageEditMode ? .pressed : .normal
  424. sender.reloadData()
  425. viewManager.toolMode = .None
  426. } else if sender.properties == toolbarManager.rightViewProperty {
  427. viewManager.showRightSide = !viewManager.showRightSide
  428. }
  429. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, sender.properties.identifier)
  430. reloadData()
  431. }
  432. @objc func tabsMoreButtonClicked(_ sender: ComponentButton) {
  433. guard let point = sender.superview?.convert(sender.frame.origin, to: view.window?.contentView) else { return }
  434. for item in moreTabsItems {
  435. item.state = .normal
  436. item.itemSelected = false
  437. }
  438. showGroupView(moreTabsItems, point)
  439. customRightGroupView.clickedAutoHide = true
  440. }
  441. //MARK: - 自定义工具栏
  442. func updateRightMenuItemSelectedState() {
  443. undoRedo_Menuitem_Property.itemSelected = false
  444. save_Menuitem_Property.itemSelected = false
  445. aiTools_Menuitem_Property.itemSelected = false
  446. batch_Menuitem_Property.itemSelected = false
  447. tts_Menuitem_Property.itemSelected = false
  448. ppt_Menuitem_Property.itemSelected = false
  449. print_Menuitem_Property.itemSelected = false
  450. share_Menuitem_Property.itemSelected = false
  451. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  452. return
  453. }
  454. for property in toolbarManager.validRightPropertys {
  455. if property == toolbarManager.undoProperty ||
  456. property == toolbarManager.redoProperty {
  457. undoRedo_Menuitem_Property.itemSelected = true
  458. } else if property == toolbarManager.saveProperty {
  459. save_Menuitem_Property.itemSelected = true
  460. } else if property == toolbarManager.aiToolsProperty {
  461. aiTools_Menuitem_Property.itemSelected = true
  462. } else if property == toolbarManager.batchProperty {
  463. batch_Menuitem_Property.itemSelected = true
  464. } else if property == toolbarManager.ttsProperty {
  465. tts_Menuitem_Property.itemSelected = true
  466. } else if property == toolbarManager.pptProperty {
  467. ppt_Menuitem_Property.itemSelected = true
  468. } else if property == toolbarManager.printProperty {
  469. print_Menuitem_Property.itemSelected = true
  470. } else if property == toolbarManager.shareProperty {
  471. share_Menuitem_Property.itemSelected = true
  472. }
  473. }
  474. }
  475. func showPageTurnMenuWithPoint(_ point: CGPoint) {
  476. if (customRightGroupView?.superview) != nil {
  477. return
  478. }
  479. updateRightMenuItemSelectedState()
  480. let items: [ComponentMenuitemProperty] = [ComponentMenuitemProperty.header(KMLocalizedString("Show Right Tools")), undoRedo_Menuitem_Property, save_Menuitem_Property, ComponentMenuitemProperty.divider(),
  481. aiTools_Menuitem_Property, batch_Menuitem_Property, tts_Menuitem_Property, ppt_Menuitem_Property, print_Menuitem_Property, share_Menuitem_Property]
  482. showGroupView(items, point)
  483. customRightGroupView.clickedAutoHide = false
  484. refreshRightMenuState()
  485. }
  486. func showGroupView(_ items: [ComponentMenuitemProperty], _ point: NSPoint) {
  487. var viewHeight: CGFloat = 8.0
  488. for item in items {
  489. item.state = .normal
  490. if item.type == .normal {
  491. viewHeight += 36
  492. } else if item.type == .divider {
  493. viewHeight += 8
  494. } else if item.type == .header {
  495. viewHeight += 36
  496. }
  497. }
  498. if customRightGroupView == nil {
  499. customRightGroupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  500. }
  501. customRightGroupView.clickedAutoHide = false
  502. customRightGroupView?.frame = CGRectMake(0, 0, 160, viewHeight)
  503. customRightGroupView?.reloadData()
  504. customRightGroupView.groupDelegate = self
  505. customRightGroupView.showWithPoint(CGPoint(x: point.x, y: point.y-viewHeight), relativeTo: self.contendBox)
  506. customRightGroupView?.updateGroupInfo(items)
  507. }
  508. func hideGroupView() {
  509. customRightGroupView?.removeGroupView()
  510. }
  511. func refreshRightMenuState() {
  512. let totalItems = customRightItems
  513. var selectedCount = 0
  514. for item in totalItems {
  515. if item.itemSelected == true {
  516. selectedCount += 1
  517. }
  518. }
  519. var maxCount = 6
  520. if undoRedo_Menuitem_Property.itemSelected == true {
  521. maxCount = 5
  522. }
  523. if selectedCount == maxCount {
  524. for item in totalItems {
  525. if item.itemSelected == false {
  526. item.isDisabled = true
  527. }
  528. }
  529. } else {
  530. for item in totalItems {
  531. item.isDisabled = false
  532. }
  533. }
  534. if selectedCount == 5 && undoRedo_Menuitem_Property.itemSelected == false {
  535. //特殊情况,UndoRedo占两个空位
  536. undoRedo_Menuitem_Property.isDisabled = true
  537. }
  538. for item in totalItems {
  539. NotificationCenter.default.post(name: Notification.Name(rawValue: ComponentMenuItemUpdatedKey), object: item.identifier)
  540. }
  541. }
  542. //MARK: - MouseEvent
  543. override func rightMouseUp(with event: NSEvent) {
  544. super.rightMouseUp(with: event)
  545. var clickEnabled = true
  546. let windowPoint = event.locationInWindow
  547. if tabsMoreButton.superview?.isHidden == false {
  548. if CGRectContainsPoint(tabsMoreButton.frame, mainToolbarContendView.convert(windowPoint, from: nil)) {
  549. clickEnabled = false
  550. }
  551. }
  552. if clickEnabled {
  553. showPageTurnMenuWithPoint(windowPoint)
  554. }
  555. }
  556. }
  557. //MARK: - ComponentDropdownToolDelegate:工具模式
  558. extension KMPDFToolbarController: ComponentDropdownToolDelegate {
  559. func componentDropdownToolDidClicked(_ view: ComponentDropdownTool, menuItem: ComponentMenuitemProperty?) {
  560. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  561. return
  562. }
  563. for item in toolbarManager.toolsProperty.menuItemArr ?? [] {
  564. item.itemSelected = false
  565. item.state = .normal
  566. }
  567. menuItem?.itemSelected = true
  568. if menuItem == toolbarManager.tools_selectProperty {
  569. viewManager.viewToolsType = .Select
  570. } else if menuItem == toolbarManager.tools_scrollProperty {
  571. viewManager.viewToolsType = .Scroll
  572. } else if menuItem == toolbarManager.tools_contentProperty {
  573. viewManager.viewToolsType = .Content_Selection
  574. } else if menuItem == toolbarManager.tools_magnifyProperty {
  575. viewManager.viewToolsType = .Magnify
  576. } else if menuItem == toolbarManager.tools_areaProperty {
  577. viewManager.viewToolsType = .AreaZoom
  578. }
  579. reloadToolsView()
  580. }
  581. }
  582. //MARK: - ComponentSelectZoomDelegate:缩放比例
  583. extension KMPDFToolbarController: ComponentSelectZoomDelegate {
  584. func componentSelectZoomDidReduce(view: ComponentSelectZoom) {
  585. let zoomSting = view.properties.text
  586. var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0)
  587. switch scale {
  588. case 0...0.25:
  589. scale = 0
  590. case 0.25...3:
  591. scale -= 0.25
  592. case 3.1...10:
  593. scale -= 0.4
  594. case 10.1...100:
  595. scale -= 1
  596. default:
  597. scale -= 1
  598. }
  599. pdfView?.scaleFactor = scale
  600. }
  601. func componentSelectZoomDidSelect(view: ComponentSelectZoom?, menuItemProperty: ComponentMenuitemProperty?) {
  602. if menuItemProperty?.identifier == KMPDFToolbar_Zoom_AdapWidth_Identifier {
  603. pdfView?.autoScales = true
  604. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_AdapPage_Identifier {
  605. let pageHeight = pdfView?.currentPage()!.size.height
  606. let pdfviewHeight = pdfView?.bounds.size.height
  607. pdfView?.scaleFactor = pdfviewHeight!/pageHeight!
  608. pdfView?.autoScales = false
  609. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_ActualSize_Identifier {
  610. if pdfView?.scaleFactor != 1.0 {
  611. pdfView?.scaleFactor = 1.0
  612. pdfView?.autoScales = false
  613. }
  614. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_10_Identifier {
  615. pdfView?.scaleFactor = 0.1
  616. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_25_Identifier {
  617. pdfView?.scaleFactor = 0.25
  618. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_50_Identifier {
  619. pdfView?.scaleFactor = 0.5
  620. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_75_Identifier {
  621. pdfView?.scaleFactor = 0.75
  622. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_100_Identifier {
  623. pdfView?.scaleFactor = 1.0
  624. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_150_Identifier {
  625. pdfView?.scaleFactor = 1.5
  626. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_200_Identifier {
  627. pdfView?.scaleFactor = 2.0
  628. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_400_Identifier {
  629. pdfView?.scaleFactor = 4.0
  630. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_800_Identifier {
  631. pdfView?.scaleFactor = 8.0
  632. }
  633. }
  634. func componentSelectZoomDidAdd(view: ComponentSelectZoom) {
  635. let zoomSting = view.properties.text
  636. var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0)
  637. switch scale {
  638. case 0...0.25:
  639. scale += 0.25
  640. case 0.25...3:
  641. scale += 0.25
  642. case 3.1...10:
  643. scale += 0.4
  644. case 10.1...100:
  645. scale += 1
  646. default:
  647. scale += 1
  648. }
  649. pdfView?.scaleFactor = scale
  650. }
  651. }
  652. //MARK: - ComponentTabsDelegate
  653. extension KMPDFToolbarController: ComponentTabsDelegate {
  654. func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) {
  655. guard let toolbarManager = self.toolbarManager, let _manager = self.viewManager else {
  656. return
  657. }
  658. _manager.viewToolsType = .Select
  659. if _manager.isPageEditMode == true {
  660. delegate?.kmPDFToolbarControllerDidExitPageEditMode?(self)
  661. _manager.isPageEditMode = false
  662. }
  663. if property == toolbarManager.markupMode_Property {
  664. if _manager.toolMode == .Markup {
  665. _manager.toolMode = .None
  666. property.state = .normal
  667. view.refreshItems()
  668. } else {
  669. _manager.toolMode = .Markup
  670. }
  671. _manager.subToolMode = .None
  672. } else if property == toolbarManager.editMode_Property {
  673. if _manager.toolMode == .Edit {
  674. _manager.toolMode = .None
  675. property.state = .normal
  676. view.refreshItems()
  677. } else {
  678. _manager.toolMode = .Edit
  679. }
  680. _manager.subToolMode = .None
  681. } else if property == toolbarManager.formMode_Property {
  682. if _manager.toolMode == .Form {
  683. _manager.toolMode = .None
  684. property.state = .normal
  685. view.refreshItems()
  686. } else {
  687. _manager.toolMode = .Form
  688. }
  689. _manager.subToolMode = .None
  690. } else if property == toolbarManager.fillMode_Property {
  691. if _manager.toolMode == .Fill {
  692. _manager.toolMode = .None
  693. property.state = .normal
  694. view.refreshItems()
  695. } else {
  696. _manager.toolMode = .Fill
  697. }
  698. _manager.subToolMode = .None
  699. } else if property == toolbarManager.convertMode_Property {
  700. if _manager.toolMode == .Convert {
  701. _manager.toolMode = .None
  702. property.state = .normal
  703. view.refreshItems()
  704. } else {
  705. _manager.toolMode = .Convert
  706. }
  707. _manager.subToolMode = .None
  708. } else if property == toolbarManager.protectMode_Property {
  709. if _manager.toolMode == .Protect {
  710. _manager.toolMode = .None
  711. property.state = .normal
  712. view.refreshItems()
  713. } else {
  714. _manager.toolMode = .Protect
  715. }
  716. _manager.subToolMode = .None
  717. } else if property == toolbarManager.toolsMode_Property {
  718. if _manager.toolMode == .Tools {
  719. _manager.toolMode = .None
  720. property.state = .normal
  721. view.refreshItems()
  722. } else {
  723. _manager.toolMode = .Tools
  724. }
  725. _manager.subToolMode = .None
  726. }
  727. refreshTabsScrollViewVisiableRect()
  728. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, property.identifier)
  729. reloadData()
  730. }
  731. }
  732. //MARK: - KMPDFSecToolbarControllerDelegate:二级工具栏
  733. extension KMPDFToolbarController: KMPDFSecToolbarControllerDelegate {
  734. func kmPDFSecToolbarControllerDidItemClicked(_ controller: KMPDFSecToolbarController, _ property: Any) {
  735. if let value = property as? ComponentButtonProperty {
  736. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, value.identifier)
  737. } else if let value = property as? ComponentMenuitemProperty {
  738. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, value.identifier)
  739. }
  740. }
  741. func kmPDFSecToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) {
  742. delegate?.kmPDFToolbarControllerDidSelectTextDidBeginEditing?(self, view)
  743. }
  744. func kmPDFSecToolbarControllerDidSelectTextDidChange(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) {
  745. delegate?.kmPDFToolbarControllerDidSelectTextDidChange?(self, view)
  746. }
  747. func kmPDFSecToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect) {
  748. delegate?.kmPDFToolbarControllerDidSelectTextDidEndEditing?(self, view)
  749. }
  750. }
  751. //MARK: - ComponentGroupDelegate:右侧工具栏
  752. extension KMPDFToolbarController: ComponentGroupDelegate {
  753. func componentGroupDidDismiss(group: ComponentGroup?) {
  754. tabsMoreButton.properties.state = .normal
  755. tabsMoreButton.reloadData()
  756. }
  757. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  758. guard let property = menuItemProperty else {
  759. return
  760. }
  761. guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
  762. return
  763. }
  764. if customRightItems.contains(property) {
  765. var validItems:[NSObject] = []
  766. if undoRedo_Menuitem_Property.itemSelected {
  767. validItems.append(toolbarManager.undoProperty)
  768. validItems.append(toolbarManager.redoProperty)
  769. }
  770. if save_Menuitem_Property.itemSelected {
  771. validItems.append(toolbarManager.saveProperty)
  772. }
  773. if aiTools_Menuitem_Property.itemSelected {
  774. validItems.append(toolbarManager.aiToolsProperty)
  775. }
  776. if batch_Menuitem_Property.itemSelected {
  777. validItems.append(toolbarManager.batchProperty)
  778. }
  779. if tts_Menuitem_Property.itemSelected {
  780. validItems.append(toolbarManager.ttsProperty)
  781. }
  782. if ppt_Menuitem_Property.itemSelected {
  783. validItems.append(toolbarManager.pptProperty)
  784. }
  785. if print_Menuitem_Property.itemSelected {
  786. validItems.append(toolbarManager.printProperty)
  787. }
  788. if share_Menuitem_Property.itemSelected {
  789. validItems.append(toolbarManager.shareProperty)
  790. }
  791. let sortArray = toolbarManager.totalRightPropertys
  792. let sortedArray = validItems.sorted {
  793. guard let indexA = sortArray.firstIndex(of: $0 as NSObject),
  794. let indexB = sortArray.firstIndex(of: $1 as NSObject) else {
  795. return false
  796. }
  797. return indexA < indexB
  798. }
  799. toolbarManager.validRightPropertys = sortedArray
  800. setUpRightViews()
  801. refreshRightMenuState()
  802. } else if moreTabsItems.contains(property) {
  803. viewManager.viewToolsType = .Select
  804. if viewManager.isPageEditMode == true {
  805. delegate?.kmPDFToolbarControllerDidExitPageEditMode?(self)
  806. viewManager.isPageEditMode = false
  807. }
  808. if property == markup_Menuitem_Property {
  809. viewManager.toolMode = .Markup
  810. } else if property == edit_Menuitem_Property {
  811. viewManager.toolMode = .Edit
  812. } else if property == form_Menuitem_Property {
  813. viewManager.toolMode = .Form
  814. } else if property == fill_Menuitem_Property {
  815. viewManager.toolMode = .Fill
  816. } else if property == toolbarManager.convertMode_Property {
  817. viewManager.toolMode = .Convert
  818. } else if property == protect_Menuitem_Property {
  819. viewManager.toolMode = .Protect
  820. } else if property == tools_Menuitem_Property {
  821. viewManager.toolMode = .Tools
  822. }
  823. tabsView.refreshItems()
  824. delegate?.kmPDFToolbarControllerDidToolbarItemClicked?(self, property.identifier)
  825. reloadData()
  826. }
  827. }
  828. }