KMToolbarController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. //
  2. // KMToolbarController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/12/29.
  6. //
  7. import Cocoa
  8. extension CAnnotationType {
  9. func isAdvanced() -> Bool {
  10. if (self == .link || self == .stamp || self == .signSignature) {
  11. return true
  12. }
  13. if (self == .signText || self == .signTure || self == .signFalse || self == .signCircle || self == .signLine || self == .signDot || self == .signDate) {
  14. return true
  15. }
  16. if (self == .addText || self == .addImage) {
  17. return true
  18. }
  19. return false
  20. }
  21. }
  22. @objc protocol KMToolbarControllerDelegate {
  23. @objc optional func toolbarController(_ viewController: KMToolbarController, heightOffsetChange heightOffset: Float)
  24. @objc optional func changeAnnotationModeAction(item:KMToolbarClickButton)
  25. @objc optional func showPDFLayoutModeAction(show:Bool)
  26. @objc optional func clickChildTool(type: KMToolbarType, index: Int)
  27. @objc optional func changePDFViewZoomModelAction(selectedTag:Int)
  28. @objc optional func changePDFViewZoomInAction()
  29. @objc optional func changePDFViewZoomOutAction()
  30. @objc optional func changePDFViewGotoNextPageAction()
  31. @objc optional func changePDFViewGoToPreviousPageAction()
  32. @objc optional func changePDFViewGotoBackAction()
  33. @objc optional func changePDFViewGoToForwardAction()
  34. @objc optional func showPDFViewPrintViewController()
  35. @objc optional func aiTranslationPDFFileAction()
  36. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, zoomModel selectedTag:Int)
  37. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String)
  38. @objc optional func mainToolDidClicked(_ toolController: KMToolbarController, _ beforeType: KMToolbarViewType, _ type: KMToolbarViewType, _ item: KMToolBoxItem, _ pages: [Int])
  39. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareAction toolbarItem: KMToolBoxItem)
  40. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareDocument item:NSMenuItem)
  41. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareFlatten item:NSMenuItem)
  42. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareOriginalPDF item:NSMenuItem)
  43. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, scanOCRModel selectedTag:Int)
  44. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, rightPanel toolbarItem: KMToolBoxItem)
  45. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, leftPanel toolbarItem: KMToolBoxItem)
  46. }
  47. class KMToolbarController: NSViewController {
  48. @IBOutlet weak var mainToolBarBox: NSBox!
  49. @IBOutlet weak var childToolBarBox: NSBox!
  50. @IBOutlet weak var secondaryToolBarBox: NSBox!
  51. @IBOutlet weak var mainToolBarHeight: NSLayoutConstraint!
  52. @IBOutlet weak var childToolBarHeight: NSLayoutConstraint!
  53. @IBOutlet weak var secondaryToolBarHeight: NSLayoutConstraint!
  54. @IBOutlet weak var bottomOffset: NSLayoutConstraint!
  55. var mainToolBarView: KMToolbarViewController?
  56. var childToolBarView: KMToolbarViewController?
  57. var _toolbarType : KMToolbarViewType = KMToolbarViewType.None
  58. var mainViewController: KMMainViewController?
  59. open weak var delegate: KMToolbarControllerDelegate?
  60. var toolbarItems : [NSToolbarItem.Identifier : Any] = [:]
  61. var listView : CPDFListView?
  62. var lastItemBox : KMToolBoxItem = KMToolBoxItem()
  63. var lastChildItemBox : KMToolBoxItem = KMToolBoxItem()
  64. var popover: NSPopover?
  65. // 是否显示所有注释
  66. private var _isShowAllAnnotations = true
  67. // 是否显示所有注释
  68. var isShowAllAnnotations: Bool {
  69. get {
  70. return _isShowAllAnnotations
  71. }
  72. set {
  73. _isShowAllAnnotations = newValue
  74. self.childToolBarView?.isShowAllAnnotations = newValue
  75. self.findChildItem(KMToolbarShowToolbarItemIdentifier)?.isSelected = !newValue
  76. }
  77. }
  78. private var _ignoreCurrentAnnotationTypeChange = false
  79. var ignoreCurrentAnnotationTypeChange: Bool {
  80. get {
  81. return self._ignoreCurrentAnnotationTypeChange
  82. }
  83. set {
  84. self._ignoreCurrentAnnotationTypeChange = newValue
  85. self.childToolBarView?.ignoreCurrentAnnotationTypeChange = self.ignoreCurrentAnnotationTypeChange
  86. // 忽略后重置 这个属性是基于单次添加,所以使用后会重置
  87. // self._ignoreCurrentAnnotationTypeChange = false
  88. }
  89. }
  90. override func viewDidLoad() {
  91. super.viewDidLoad()
  92. // Do view setup here.
  93. updateViewColor()
  94. mainToolBarView = KMToolbarViewController.init()
  95. mainToolBarView?.view.wantsLayer = true
  96. mainToolBarView?.view.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  97. mainToolBarBox.contentView = mainToolBarView?.view
  98. mainToolBarView?.view.frame = mainToolBarBox.frame
  99. mainToolBarView?.delegate = self
  100. mainToolBarView?.view.autoresizingMask = [.width, .height]
  101. mainToolBarView?.pdfView = self.listView!
  102. childToolBarView = KMToolbarViewController.init()
  103. childToolBarView?.view.wantsLayer = true
  104. childToolBarView?.view.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  105. childToolBarBox.contentView = childToolBarView?.view
  106. childToolBarView?.view.frame = childToolBarBox.frame
  107. childToolBarView?.delegate = self
  108. childToolBarView?.view.autoresizingMask = [.width, .height]
  109. childToolBarView?.pdfView = self.listView!
  110. toolbarType = .None
  111. mainToolBarView?.toolbarType = .Main
  112. mainToolBarView?.reloadateToolbar()
  113. addObserverForAppearanceChange()
  114. NotificationCenter.default.addObserver(self, selector: #selector(selectedShapAnnotationChangeNotification(_:)), name: NSNotification.Name.init(rawValue: "KMSelectedShapAnnotationChangeNotification"), object: nil)
  115. NotificationCenter.default.addObserver(self, selector: #selector(PDFChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "PDFChangedNotification"), object: nil)
  116. NotificationCenter.default.addObserver(self, selector: #selector(PDFTextEditPDFChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "KMPDFTextEditPDFChangedNotification"), object: nil)
  117. }
  118. // MARK:
  119. func updateViewColor() -> Void {
  120. }
  121. // MARK: Setter&Getter
  122. var toolbarType: KMToolbarViewType {
  123. set {
  124. if newValue == _toolbarType {
  125. if newValue == .Move || newValue == .Magnify || newValue == .Select || newValue == .SelectZoom {
  126. _toolbarType = .Annatiton
  127. } else {
  128. _toolbarType = .None
  129. }
  130. } else {
  131. _toolbarType = newValue
  132. }
  133. secondaryToolBarBox.isHidden = true
  134. secondaryToolBarHeight.constant = 51
  135. if _toolbarType == .None {
  136. bottomOffset.constant = 0
  137. childToolBarBox.isHidden = true
  138. self.delegate?.toolbarController?(self, heightOffsetChange: 40)
  139. } else if _toolbarType == .Page || _toolbarType == .LeftPanel {
  140. bottomOffset.constant = 0
  141. childToolBarBox.isHidden = true
  142. self.delegate?.toolbarController?(self, heightOffsetChange: 40)
  143. } else {
  144. bottomOffset.constant = 41
  145. childToolBarBox.isHidden = false
  146. self.delegate?.toolbarController?(self, heightOffsetChange: 81)
  147. }
  148. childToolBarView?.toolbarType = _toolbarType
  149. childToolBarView?.reloadateToolbar()
  150. }
  151. get {
  152. return _toolbarType
  153. }
  154. }
  155. // MARK: Dark&Light
  156. func addObserverForAppearanceChange() -> Void {
  157. }
  158. // MARK: -
  159. // MARK: Public Methods
  160. // 取消选中 [只是取消选中状态,不会执行具体的方法]
  161. public func cancelSelected(_ identifier: String) {
  162. if (isMainToolItem(identifier)) {
  163. self.findMainItem(identifier)?.isSelected = false
  164. return
  165. }
  166. self.findChildItem(identifier)?.isSelected = false
  167. }
  168. public func clickItem(_ identifier: String) {
  169. self.selectItem(identifier)
  170. }
  171. public func selectItem(_ identifier: String) {
  172. if (isMainToolItem(identifier)) {
  173. self.selectMainItem(identifier)
  174. return
  175. }
  176. if let parentItem = self.findItem(parentIdentifier(identifier)), parentItem.isSelected {
  177. if (self.trySelectChildItem(identifier)) { // 尝试去选子工具栏
  178. return
  179. }
  180. }
  181. // 当前主工具栏未开启
  182. let mainToolIdentifier = parentIdentifier(identifier)
  183. if (mainToolIdentifier.isEmpty) {
  184. return
  185. }
  186. // 找到主工具栏
  187. self.selectMainItem(mainToolIdentifier)
  188. DispatchQueue.main.async {
  189. // 再去选择子工具栏
  190. self.selectChildItem(identifier)
  191. }
  192. }
  193. public func findItem(_ identifier: String) -> KMToolBoxItem? {
  194. if (isMainToolItem(identifier)) {
  195. return self.findMainItem(identifier)
  196. }
  197. return self.findChildItem(identifier)
  198. }
  199. // MARK: -
  200. // MARK: private Methods
  201. private func selectMainItem(_ identifier: String) {
  202. if (self.mainToolBarView?.toolbarItems == nil) {
  203. return
  204. }
  205. var item: KMToolBoxItem?
  206. for (key, value) in self.mainToolBarView!.toolbarItems {
  207. if (key == identifier) {
  208. item = (value as! KMToolBoxItem)
  209. break
  210. }
  211. }
  212. // 主工具栏 item
  213. if (item != nil) {
  214. if (identifier == KMDocumentAnnotationToolbarItemIdentifier ||
  215. identifier == KMDocumentPageToolbarItemIdentifier ||
  216. identifier == KMDocumentConversonToolbarItemIdentifier ||
  217. identifier == KMDocumentScanOCRToolbarItemIdentifier ||
  218. identifier == KMDocumentEditToolbarItemIdentifier ||
  219. identifier == KMDocumentFormToolbarItemIdentifier ||
  220. identifier == KMDocumentFillSginToolbarItemIdentifier ||
  221. identifier == KMDocumentToolToolbarItemIdentifier) {
  222. self.mainToolBarView?.leftControllButtonAction(item: item!.clickButton)
  223. } else if (identifier == KMRightControlToolbarItemIdentifier) {
  224. self.mainToolBarView?.rightPanelControllButtonAction(item: item!.clickButton)
  225. } else if (identifier == KMLeftControlToolbarItemIdentifier) {
  226. item?.isSelected = !item!.isSelected
  227. self.delegate?.toolbarViewController?(self.mainToolBarView!, leftPanel: item!)
  228. }
  229. return
  230. }
  231. }
  232. private func trySelectChildItem(_ identifier: String) -> Bool {
  233. // 子工具栏 item [当前主工具栏已开启]
  234. var item: KMToolBoxItem?
  235. if (self.childToolBarView?.toolbarItems != nil && self.childToolBarView!.toolbarItems.count > 0) {
  236. for (key, value) in self.childToolBarView!.toolbarItems {
  237. if (key == identifier) {
  238. item = (value as! KMToolBoxItem)
  239. break
  240. }
  241. }
  242. // 子工具栏[找到]
  243. if (item != nil) {
  244. self.selectChildItem(identifier)
  245. return true
  246. }
  247. }
  248. return false
  249. }
  250. private func selectChildItem(_ identifier: String) {
  251. // 子工具栏 item [当前主工具栏已开启]
  252. var item: KMToolBoxItem?
  253. if (self.childToolBarView?.toolbarItems != nil && self.childToolBarView!.toolbarItems.count > 0) {
  254. for (key, value) in self.childToolBarView!.toolbarItems {
  255. if (key == identifier) {
  256. item = (value as! KMToolBoxItem)
  257. break
  258. }
  259. }
  260. // 子工具栏[找到]
  261. if (item != nil) {
  262. if (identifier == KMToolbarMoveToolModeItemIdentifier ||
  263. identifier == KMToolbarMagnifyToolModeItemIdentifier ||
  264. identifier == KMToolbarSelectToolModeItemIdentifier ||
  265. identifier == KMToolbarZoomToSelectionItemIdentifier) {
  266. self.childToolBarView?.leftControllButtonAction(item: item!.clickButton)
  267. } else {
  268. self.childToolBarView?.changeAnnotationMode(item: item!.clickButton)
  269. }
  270. }
  271. }
  272. }
  273. private func findMainItem(_ identifier: String) -> KMToolBoxItem? {
  274. if (self.mainToolBarView?.toolbarItems == nil) {
  275. return nil
  276. }
  277. var item: KMToolBoxItem?
  278. if (isMainToolItem(identifier)) {
  279. for (key, value) in self.mainToolBarView!.toolbarItems {
  280. if (key == identifier) {
  281. item = (value as! KMToolBoxItem)
  282. break
  283. }
  284. }
  285. return item
  286. }
  287. return nil
  288. }
  289. private func findChildItem(_ identifier: String) -> KMToolBoxItem? {
  290. if (self.childToolBarView?.toolbarItems == nil || self.childToolBarView!.toolbarItems.count == 0) {
  291. return nil
  292. }
  293. var item: KMToolBoxItem?
  294. for (key, value) in self.childToolBarView!.toolbarItems {
  295. if (key == identifier) {
  296. item = (value as! KMToolBoxItem)
  297. break
  298. }
  299. }
  300. return item
  301. }
  302. func exitPageEdit() -> Void {
  303. resetToolbarViewController(mainToolBarView!)
  304. exitPageEditToolbarViewController(mainToolBarView!)
  305. }
  306. func exitTextEdit() -> Void {
  307. secondaryToolBarBox.isHidden = true
  308. secondaryToolBarHeight.constant = 50
  309. bottomOffset.constant = 51
  310. childToolBarBox.isHidden = false
  311. self.delegate?.toolbarController?(self, heightOffsetChange: 102)
  312. }
  313. func enterWatermarkAdjective(_ type: KMToolbarType, _ itemTitles: Array<Array<String>>) {
  314. self.toolbarType = .None
  315. let topBarView = KMWatermarkAdjectiveTopBarView()
  316. topBarView.frame = (self.mainToolBarView?.toolbar.bounds)!
  317. self.mainToolBarView?.toolbar.addSubview(topBarView)
  318. topBarView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  319. topBarView.wantsLayer = true
  320. topBarView.layer?.backgroundColor = NSColor.white.cgColor
  321. topBarView.isCanApply(can: false)
  322. var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
  323. for items in itemTitles {
  324. var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
  325. for title in items {
  326. let model = KMWatermarkAdjectiveTopBarItemModel()
  327. model.iconName = ""
  328. model.itemTitle = title
  329. array.append(model)
  330. }
  331. itemModels.append(array)
  332. }
  333. topBarView.initItemData(itemArrays: itemModels)
  334. topBarView.cancelClick = {
  335. [unowned self] in
  336. self.delegate?.clickChildTool!(type: type, index: 1)
  337. }
  338. topBarView.applyClick = {
  339. [unowned self] in
  340. self.delegate?.clickChildTool!(type: type, index: 2)
  341. }
  342. topBarView.itemClick = {
  343. [unowned self] (section: Int, item: Int) -> () in
  344. self.delegate?.clickChildTool!(type: type, index: 3+item)
  345. }
  346. }
  347. func exitWatermarkAdjective() {
  348. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  349. if (topBarView == nil || topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self) == false) {
  350. return
  351. }
  352. topBarView?.removeFromSuperview()
  353. }
  354. func fetchTopBarView() -> NSView? {
  355. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  356. if (topBarView == nil) {
  357. return nil
  358. }
  359. if (topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self) == true) {
  360. return topBarView
  361. }
  362. if (topBarView?.isKind(of: KMRedactTopToolBar.self) == true) {
  363. return topBarView
  364. }
  365. return nil
  366. }
  367. func enterRedact() {
  368. self.toolbarType = .None
  369. let topBarView = KMRedactTopToolBar()
  370. topBarView.frame = (self.mainToolBarView?.toolbar.bounds)!
  371. self.mainToolBarView?.toolbar.addSubview(topBarView)
  372. topBarView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  373. topBarView.wantsLayer = true
  374. topBarView.layer?.backgroundColor = NSColor.white.cgColor
  375. topBarView.selectItem(0)
  376. self.delegate?.clickChildTool!(type: .redact, index: 4)
  377. topBarView.itemClick = { [weak self] index in
  378. self!.delegate?.clickChildTool!(type: .redact, index: index)
  379. }
  380. }
  381. func exitRedact() {
  382. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  383. if (topBarView == nil || topBarView?.isKind(of: KMRedactTopToolBar.self) == false) {
  384. return
  385. }
  386. topBarView?.removeFromSuperview()
  387. }
  388. // MARK: NSNotification
  389. @objc func selectedShapAnnotationChangeNotification(_ notification: Notification) -> Void {
  390. }
  391. @objc func PDFChangedNotification(_ notification: Notification) -> Void {
  392. }
  393. @objc func PDFTextEditPDFChangedNotification(_ notification: Notification) -> Void {
  394. }
  395. // MARK:
  396. func resetToolbarViewController(_ toolbarViewController: KMToolbarViewController) -> Void {
  397. self.toolbarType = .None
  398. self.mainToolBarView?.resetToolbar()
  399. self.mainViewController?.listView.annotationType = .unkown
  400. }
  401. func exitPageEditToolbarViewController(_ toolbarViewController: KMToolbarViewController) -> Void {
  402. }
  403. // MARK: item actions
  404. @objc func cropItemAction(sender: NSMenuItem) {
  405. if (sender.tag == 0) { /// 裁剪当前页面
  406. self.delegate?.clickChildTool!(type: .crop, index: 1)
  407. return
  408. }
  409. if (sender.tag == 1) { /// 裁剪所有页面
  410. self.delegate?.clickChildTool!(type: .crop, index: 2)
  411. return
  412. }
  413. /// 自定义裁剪区域
  414. self.delegate?.clickChildTool!(type: .crop, index: 3)
  415. }
  416. @objc func secureItemAction(sender: NSMenuItem) {
  417. var index: Int = 1
  418. if (sender.tag == 1) { /// 删除安全性设置
  419. index = 2
  420. }
  421. self.delegate?.clickChildTool?(type: .secure, index: index)
  422. }
  423. }
  424. extension KMToolbarController: KMToolbarViewControllerDelegate {
  425. func changeAnnotationModeAction(item: KMToolbarClickButton) {
  426. let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
  427. if (type.isAdvanced() == false) {
  428. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  429. self.toolbarType = .Annatiton
  430. }
  431. self.delegate?.changeAnnotationModeAction?(item: item)
  432. return
  433. }
  434. // 高级功能
  435. Task { @MainActor in
  436. #if VERSION_DMG
  437. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  438. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  439. let boxItem = item.clickObject as? KMToolBoxItem
  440. boxItem?.isSelected = false
  441. return
  442. }
  443. #endif
  444. //文字编辑 图片编辑 选中按钮逻辑(只能同时选中其中一个)
  445. if type == .addText || type == .addImage {
  446. let boxItem = item.clickObject as? KMToolBoxItem
  447. if boxItem != nil {
  448. if self.lastChildItemBox != boxItem {
  449. self.lastChildItemBox.isSelected = false
  450. boxItem!.isSelected = true
  451. } else {
  452. boxItem!.isSelected = !boxItem!.isSelected
  453. }
  454. self.lastChildItemBox = boxItem!
  455. }
  456. }
  457. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  458. self.toolbarType = .Annatiton
  459. }
  460. self.delegate?.changeAnnotationModeAction?(item: item)
  461. }
  462. }
  463. func toolbarViewController(_ viewController: KMToolbarViewController, clickMode toolMode: KMToolbarViewType, toolbar toolbarItem: KMToolBoxItem, _ pages: [Int]) {
  464. let beforeModel = KMToolbarViewType(rawValue: self.lastItemBox.clickButton.tag) ?? .None
  465. if self.lastItemBox != nil {
  466. if (toolbarItem.isSelected && toolMode.isToolMode()) {
  467. // no nothings
  468. } else {
  469. self.lastItemBox.isSelected = false
  470. }
  471. }
  472. if self.lastChildItemBox != nil {
  473. if toolMode == .editPDF {
  474. if !self.lastItemBox.isSelected {
  475. self.lastChildItemBox.isSelected = false
  476. }
  477. }
  478. }
  479. if toolMode != .Magnify && toolMode != .Move && toolMode != .Select && toolMode != .SelectZoom && toolMode != .LeftPanel && toolMode != .RightPanel {
  480. if(toolMode == self.toolbarType) {
  481. toolbarItem.isSelected = false
  482. } else {
  483. toolbarItem.isSelected = true
  484. self.lastItemBox = toolbarItem
  485. }
  486. } else {
  487. if(toolMode != self.toolbarType && toolMode != .LeftPanel) {
  488. let item : KMToolBoxItem = (self.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentAnnotationToolbarItemIdentifier))!
  489. item.isSelected = true
  490. self.lastItemBox = item
  491. } else if (toolMode == .LeftPanel) {
  492. if(toolMode == self.toolbarType) {
  493. toolbarItem.isSelected = false
  494. } else {
  495. toolbarItem.isSelected = true
  496. }
  497. }
  498. }
  499. self.toolbarType = toolMode
  500. self.delegate?.mainToolDidClicked?(self, beforeModel, toolMode, toolbarItem, pages)
  501. }
  502. func toolbarViewController(_ viewController: KMToolbarViewController, rightPanel toolbarItem: KMToolBoxItem) {
  503. toolbarItem.isSelected = !toolbarItem.isSelected
  504. self.delegate?.toolbarViewController?(viewController, rightPanel: toolbarItem)
  505. }
  506. func toolbarViewController(_ viewController: KMToolbarViewController, leftPanel toolbarItem: KMToolBoxItem) {
  507. toolbarItem.isSelected = !toolbarItem.isSelected
  508. self.delegate?.toolbarViewController?(viewController, leftPanel: toolbarItem)
  509. }
  510. func showPDFLayoutModeAction(show: Bool) {
  511. self.delegate?.showPDFLayoutModeAction?(show: show)
  512. }
  513. func toolbarViewController(_ viewController: KMToolbarViewController, clickChaildToolType: KMToolbarType, toolbar toolbarItem: KMToolBoxItem) {
  514. if (clickChaildToolType == .crop) {
  515. let titles = [NSLocalizedString("Crop Current Page - White Margins", comment: ""), NSLocalizedString("Crop All Pages - Auto", comment: "")]
  516. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  517. let createFilePopover: NSPopover = NSPopover.init()
  518. createFilePopover.contentViewController = vc
  519. createFilePopover.animates = true
  520. createFilePopover.behavior = .transient
  521. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  522. createFilePopover.show(relativeTo: NSZeroRect, of: toolbarItem, preferredEdge: .maxY)
  523. self.popover = createFilePopover
  524. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  525. self?.popover?.close()
  526. self?.popover = nil
  527. if downEntered {
  528. if (count == titles.first) {
  529. self!.delegate?.clickChildTool?(type: .crop, index: 1)
  530. } else {
  531. self!.delegate?.clickChildTool?(type: .crop, index: 2)
  532. }
  533. }
  534. }
  535. } else if (clickChaildToolType == .bates || clickChaildToolType == .headerAndFooter || clickChaildToolType == .background || clickChaildToolType == .watermark) {
  536. toolbarItem.isSelected = false
  537. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  538. } else if (clickChaildToolType == .redact) {
  539. toolbarItem.isSelected = false
  540. self.enterRedact()
  541. } else if (clickChaildToolType == .compress) {
  542. self.delegate?.clickChildTool?(type: .compress, index: 0)
  543. } else if (clickChaildToolType == .secure) {
  544. // toolbarItem.isSelected = fals
  545. let titles = [NSLocalizedString("Set Passwords", comment: ""), NSLocalizedString("Remove Security", comment: "")]
  546. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  547. let createFilePopover: NSPopover = NSPopover.init()
  548. createFilePopover.contentViewController = vc
  549. createFilePopover.animates = true
  550. createFilePopover.behavior = .transient
  551. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  552. createFilePopover.show(relativeTo: CGRect(x: toolbarItem.bounds.origin.x, y: 10, width: toolbarItem.bounds.size.width, height: toolbarItem.bounds.size.height), of: toolbarItem, preferredEdge: .minY)
  553. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  554. if downEntered {
  555. if (count == titles.first) {
  556. self!.delegate?.clickChildTool?(type: .secure, index: 1)
  557. } else {
  558. self!.delegate?.clickChildTool?(type: .secure, index: 2)
  559. }
  560. }
  561. }
  562. } else if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(clickChaildToolType.rawValue)) { /// 转档
  563. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  564. } else if (clickChaildToolType == .merge) {
  565. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  566. }
  567. }
  568. func toolbarViewController(_ viewController: KMToolbarViewController, zoomModel selectedTag: Int) {
  569. self.delegate?.toolbarViewController?(viewController, zoomModel: selectedTag)
  570. }
  571. func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String) {
  572. self.delegate?.toolbarViewController?(viewController, zoomSting: zoomSting)
  573. }
  574. func changePDFViewZoomInAction() {
  575. self.delegate?.changePDFViewZoomInAction?()
  576. }
  577. func changePDFViewZoomOutAction() {
  578. self.delegate?.changePDFViewZoomOutAction?()
  579. }
  580. func changePDFViewGotoNextPageAction() {
  581. self.delegate?.changePDFViewGotoNextPageAction?()
  582. }
  583. func changePDFViewGoToPreviousPageAction() {
  584. self.delegate?.changePDFViewGoToPreviousPageAction?()
  585. }
  586. func changePDFViewGotoBackAction() {
  587. self.delegate?.changePDFViewGotoBackAction?()
  588. }
  589. func changePDFViewGoToForwardAction() {
  590. self.delegate?.changePDFViewGoToForwardAction?()
  591. }
  592. func showPDFViewPrintViewController() {
  593. self.delegate?.showPDFViewPrintViewController?()
  594. }
  595. func aiTranslationPDFFileAction() {
  596. self.delegate?.aiTranslationPDFFileAction?()
  597. }
  598. func toolbarViewController(_ viewController: KMToolbarViewController, shareAction toolbarItem: KMToolBoxItem) {
  599. self.delegate?.toolbarViewController?(viewController, shareAction: toolbarItem)
  600. }
  601. func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {
  602. self.delegate?.toolbarViewController?(viewController, shareDocument: item)
  603. }
  604. func toolbarViewController(_ viewController: KMToolbarViewController, shareFlatten item: NSMenuItem) {
  605. self.delegate?.toolbarViewController?(viewController, shareFlatten: item)
  606. }
  607. func toolbarViewController(_ viewController: KMToolbarViewController, shareOriginalPDF item: NSMenuItem) {
  608. self.delegate?.toolbarViewController?(viewController, shareOriginalPDF: item)
  609. }
  610. func toolbarViewController(_ viewController: KMToolbarViewController, scanOCRModel selectedTag: Int) {
  611. self.delegate?.toolbarViewController?(viewController, scanOCRModel: selectedTag)
  612. }
  613. }