KMToolbarController.swift 28 KB

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