KMToolbarController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 aiTranslationPDFFileAction()
  35. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, zoomModel selectedTag:Int)
  36. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String)
  37. @objc optional func mainToolDidClicked(_ toolController: KMToolbarController, _ beforeType: KMToolbarViewType, _ type: KMToolbarViewType, _ item: KMToolbarItemView, _ pages: [Int])
  38. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareAction toolbarItem: KMToolbarItemView)
  39. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareDocument item:NSMenuItem)
  40. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareFlatten item:NSMenuItem)
  41. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareOriginalPDF item:NSMenuItem)
  42. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, scanOCRModel selectedTag:Int)
  43. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, itemDidClick toolbarItem: KMToolbarItemView)
  44. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, searchAction searchString: String)
  45. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, menuItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?)
  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 : KMToolbarItemView = KMToolbarItemView()
  63. var lastChildItemBox : KMToolbarItemView = KMToolbarItemView()
  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?.delegate = self
  99. mainToolBarView?.pdfView = self.listView!
  100. childToolBarView = KMToolbarViewController.init()
  101. childToolBarView?.view.wantsLayer = true
  102. childToolBarView?.view.layer?.backgroundColor = NSColor.km_init(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+8)
  137. } else if _toolbarType == .Page || _toolbarType == .LeftPanel {
  138. bottomOffset.constant = 0
  139. childToolBarBox.isHidden = true
  140. self.delegate?.toolbarController?(self, heightOffsetChange: 40+8)
  141. } else {
  142. bottomOffset.constant = 41
  143. childToolBarBox.isHidden = false
  144. self.delegate?.toolbarController?(self, heightOffsetChange: 81+8)
  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) -> KMToolbarItemView? {
  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: KMToolbarItemView?
  204. for (key, value) in self.mainToolBarView!.toolbarItems {
  205. if (key == identifier) {
  206. item = (value as! KMToolbarItemView)
  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?.itemAction(item!)
  223. } else if (identifier == KMLeftControlToolbarItemIdentifier) {
  224. self.mainToolBarView?.itemAction(item!)
  225. }
  226. return
  227. }
  228. }
  229. private func trySelectChildItem(_ identifier: String) -> Bool {
  230. // 子工具栏 item [当前主工具栏已开启]
  231. var item: KMToolbarItemView?
  232. if (self.childToolBarView?.toolbarItems != nil && self.childToolBarView!.toolbarItems.count > 0) {
  233. for (key, value) in self.childToolBarView!.toolbarItems {
  234. if (key == identifier) {
  235. item = (value as! KMToolbarItemView)
  236. break
  237. }
  238. }
  239. // 子工具栏[找到]
  240. if (item != nil) {
  241. self.selectChildItem(identifier)
  242. return true
  243. }
  244. }
  245. return false
  246. }
  247. private func selectChildItem(_ identifier: String) {
  248. // 子工具栏 item [当前主工具栏已开启]
  249. var item: KMToolbarItemView?
  250. if (self.childToolBarView?.toolbarItems != nil && self.childToolBarView!.toolbarItems.count > 0) {
  251. for (key, value) in self.childToolBarView!.toolbarItems {
  252. if (key == identifier) {
  253. item = (value as! KMToolbarItemView)
  254. break
  255. }
  256. }
  257. // 子工具栏[找到]
  258. if (item != nil) {
  259. if (identifier == KMToolbarMoveToolModeItemIdentifier ||
  260. identifier == KMToolbarMagnifyToolModeItemIdentifier ||
  261. identifier == KMToolbarSelectToolModeItemIdentifier ||
  262. identifier == KMToolbarZoomToSelectionItemIdentifier) {
  263. self.childToolBarView?.leftControllButtonAction(item: item!.clickButton)
  264. } else {
  265. self.childToolBarView?.changeAnnotationMode(item: item!.clickButton)
  266. }
  267. }
  268. }
  269. }
  270. private func findMainItem(_ identifier: String) -> KMToolbarItemView? {
  271. if (self.mainToolBarView?.toolbarItems == nil) {
  272. return nil
  273. }
  274. var item: KMToolbarItemView?
  275. if (isMainToolItem(identifier)) {
  276. for (key, value) in self.mainToolBarView!.toolbarItems {
  277. if (key == identifier) {
  278. item = (value as! KMToolbarItemView)
  279. break
  280. }
  281. }
  282. return item
  283. }
  284. return nil
  285. }
  286. private func findChildItem(_ identifier: String) -> KMToolbarItemView? {
  287. if (self.childToolBarView?.toolbarItems == nil || self.childToolBarView!.toolbarItems.count == 0) {
  288. return nil
  289. }
  290. var item: KMToolbarItemView?
  291. for (key, value) in self.childToolBarView!.toolbarItems {
  292. if (key == identifier) {
  293. item = (value as! KMToolbarItemView)
  294. break
  295. }
  296. }
  297. return item
  298. }
  299. func exitPageEdit() -> Void {
  300. resetToolbarViewController(mainToolBarView!)
  301. exitPageEditToolbarViewController(mainToolBarView!)
  302. }
  303. func exitTextEdit() -> Void {
  304. secondaryToolBarBox.isHidden = true
  305. secondaryToolBarHeight.constant = 50
  306. bottomOffset.constant = 51
  307. childToolBarBox.isHidden = false
  308. self.delegate?.toolbarController?(self, heightOffsetChange: 102)
  309. }
  310. func enterWatermarkAdjective(_ type: KMToolbarType, _ itemTitles: Array<Array<String>>) {
  311. self.toolbarType = .None
  312. let topBarView = KMWatermarkAdjectiveTopBarView()
  313. topBarView.frame = (self.mainToolBarView?.toolbar.bounds)!
  314. self.mainToolBarView?.toolbar.addSubview(topBarView)
  315. topBarView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  316. topBarView.wantsLayer = true
  317. topBarView.layer?.backgroundColor = NSColor.white.cgColor
  318. topBarView.isCanApply(can: false)
  319. var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
  320. for items in itemTitles {
  321. var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
  322. for title in items {
  323. let model = KMWatermarkAdjectiveTopBarItemModel()
  324. model.iconName = ""
  325. model.itemTitle = title
  326. array.append(model)
  327. }
  328. itemModels.append(array)
  329. }
  330. topBarView.initItemData(itemArrays: itemModels)
  331. topBarView.cancelClick = {
  332. [unowned self] in
  333. self.delegate?.clickChildTool!(type: type, index: 1)
  334. }
  335. topBarView.applyClick = {
  336. [unowned self] in
  337. self.delegate?.clickChildTool!(type: type, index: 2)
  338. }
  339. topBarView.itemClick = {
  340. [unowned self] (section: Int, item: Int) -> () in
  341. self.delegate?.clickChildTool!(type: type, index: 3+item)
  342. }
  343. }
  344. func exitWatermarkAdjective() {
  345. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  346. if (topBarView == nil || topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self) == false) {
  347. return
  348. }
  349. topBarView?.removeFromSuperview()
  350. }
  351. func fetchTopBarView() -> NSView? {
  352. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  353. if (topBarView == nil) {
  354. return nil
  355. }
  356. if (topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self) == true) {
  357. return topBarView
  358. }
  359. if (topBarView?.isKind(of: KMRedactTopToolBar.self) == true) {
  360. return topBarView
  361. }
  362. return nil
  363. }
  364. func enterRedact() {
  365. self.toolbarType = .None
  366. let topBarView = KMRedactTopToolBar()
  367. topBarView.frame = (self.mainToolBarView?.toolbar.bounds)!
  368. self.mainToolBarView?.toolbar.addSubview(topBarView)
  369. topBarView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  370. topBarView.wantsLayer = true
  371. topBarView.layer?.backgroundColor = NSColor.white.cgColor
  372. topBarView.selectItem(0)
  373. self.delegate?.clickChildTool!(type: .redact, index: 4)
  374. topBarView.itemClick = { [weak self] index in
  375. self!.delegate?.clickChildTool!(type: .redact, index: index)
  376. }
  377. }
  378. func exitRedact() {
  379. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  380. if (topBarView == nil || topBarView?.isKind(of: KMRedactTopToolBar.self) == false) {
  381. return
  382. }
  383. topBarView?.removeFromSuperview()
  384. }
  385. // MARK: NSNotification
  386. @objc func selectedShapAnnotationChangeNotification(_ notification: Notification) -> Void {
  387. }
  388. @objc func PDFChangedNotification(_ notification: Notification) -> Void {
  389. }
  390. @objc func PDFTextEditPDFChangedNotification(_ notification: Notification) -> Void {
  391. }
  392. // MARK:
  393. func resetToolbarViewController(_ toolbarViewController: KMToolbarViewController) -> Void {
  394. self.toolbarType = .None
  395. self.mainToolBarView?.resetToolbar()
  396. self.mainViewController?.listView.annotationType = .unkown
  397. }
  398. func exitPageEditToolbarViewController(_ toolbarViewController: KMToolbarViewController) -> Void {
  399. }
  400. // MARK: item actions
  401. @objc func cropItemAction(sender: NSMenuItem) {
  402. if (sender.tag == 0) { /// 裁剪当前页面
  403. self.delegate?.clickChildTool!(type: .crop, index: 1)
  404. return
  405. }
  406. if (sender.tag == 1) { /// 裁剪所有页面
  407. self.delegate?.clickChildTool!(type: .crop, index: 2)
  408. return
  409. }
  410. /// 自定义裁剪区域
  411. self.delegate?.clickChildTool!(type: .crop, index: 3)
  412. }
  413. @objc func secureItemAction(sender: NSMenuItem) {
  414. var index: Int = 1
  415. if (sender.tag == 1) { /// 删除安全性设置
  416. index = 2
  417. }
  418. self.delegate?.clickChildTool?(type: .secure, index: index)
  419. }
  420. }
  421. extension KMToolbarController: KMToolbarViewControllerDelegate {
  422. func changeAnnotationModeAction(item: KMToolbarClickButton) {
  423. let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
  424. if (type.isAdvanced() == false) {
  425. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  426. self.toolbarType = .Annatiton
  427. }
  428. self.delegate?.changeAnnotationModeAction?(item: item)
  429. return
  430. }
  431. // 高级功能
  432. Task { @MainActor in
  433. #if VERSION_DMG
  434. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  435. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  436. let boxItem = item.clickObject as? KMToolbarItemView
  437. boxItem?.isSelected = false
  438. return
  439. }
  440. #endif
  441. //文字编辑 图片编辑 选中按钮逻辑(只能同时选中其中一个)
  442. if type == .addText || type == .addImage {
  443. let boxItem = item.clickObject as? KMToolbarItemView
  444. if boxItem != nil {
  445. if self.lastChildItemBox != boxItem {
  446. self.lastChildItemBox.isSelected = false
  447. boxItem!.isSelected = true
  448. } else {
  449. boxItem!.isSelected = !boxItem!.isSelected
  450. }
  451. self.lastChildItemBox = boxItem!
  452. }
  453. }
  454. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  455. self.toolbarType = .Annatiton
  456. }
  457. self.delegate?.changeAnnotationModeAction?(item: item)
  458. }
  459. }
  460. func toolbarViewController(_ viewController: KMToolbarViewController, clickMode toolMode: KMToolbarViewType, toolbar toolbarItem: KMToolbarItemView, _ pages: [Int]) {
  461. let beforeModel = KMToolbarViewType(rawValue: self.lastItemBox.clickButton.tag) ?? .None
  462. if self.lastItemBox != nil {
  463. if (toolbarItem.isSelected && toolMode.isToolMode()) {
  464. // no nothings
  465. } else {
  466. self.lastItemBox.isSelected = false
  467. }
  468. }
  469. if self.lastChildItemBox != nil {
  470. if toolMode == .editPDF {
  471. if !self.lastItemBox.isSelected {
  472. self.lastChildItemBox.isSelected = false
  473. }
  474. }
  475. }
  476. if toolMode != .Magnify && toolMode != .Move && toolMode != .Select && toolMode != .SelectZoom && toolMode != .LeftPanel && toolMode != .RightPanel {
  477. if(toolMode == self.toolbarType) {
  478. toolbarItem.isSelected = false
  479. } else {
  480. toolbarItem.isSelected = true
  481. self.lastItemBox = toolbarItem
  482. }
  483. } else {
  484. if(toolMode != self.toolbarType && toolMode != .LeftPanel) {
  485. let item : KMToolbarItemView = (self.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentAnnotationToolbarItemIdentifier))!
  486. item.isSelected = true
  487. self.lastItemBox = item
  488. } else if (toolMode == .LeftPanel) {
  489. if(toolMode == self.toolbarType) {
  490. toolbarItem.isSelected = false
  491. } else {
  492. toolbarItem.isSelected = true
  493. }
  494. }
  495. }
  496. self.toolbarType = toolMode
  497. self.delegate?.mainToolDidClicked?(self, beforeModel, toolMode, toolbarItem, pages)
  498. }
  499. func showPDFLayoutModeAction(show: Bool) {
  500. self.delegate?.showPDFLayoutModeAction?(show: show)
  501. }
  502. func toolbarViewController(_ viewController: KMToolbarViewController, clickChaildToolType: KMToolbarType, toolbar toolbarItem: KMToolbarItemView) {
  503. if (clickChaildToolType == .crop) {
  504. let titles = [NSLocalizedString("Crop Current Page - White Margins", comment: ""), NSLocalizedString("Crop All Pages - Auto", comment: "")]
  505. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  506. let createFilePopover: NSPopover = NSPopover.init()
  507. createFilePopover.contentViewController = vc
  508. createFilePopover.animates = true
  509. createFilePopover.behavior = .transient
  510. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  511. createFilePopover.show(relativeTo: NSZeroRect, of: toolbarItem, preferredEdge: .maxY)
  512. self.popover = createFilePopover
  513. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  514. self?.popover?.close()
  515. self?.popover = nil
  516. if downEntered {
  517. if (count == titles.first) {
  518. self!.delegate?.clickChildTool?(type: .crop, index: 1)
  519. } else {
  520. self!.delegate?.clickChildTool?(type: .crop, index: 2)
  521. }
  522. }
  523. }
  524. } else if (clickChaildToolType == .bates || clickChaildToolType == .headerAndFooter || clickChaildToolType == .background || clickChaildToolType == .watermark) {
  525. toolbarItem.isSelected = false
  526. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  527. } else if (clickChaildToolType == .redact) {
  528. toolbarItem.isSelected = false
  529. self.enterRedact()
  530. } else if (clickChaildToolType == .compress) {
  531. self.delegate?.clickChildTool?(type: .compress, index: 0)
  532. } else if (clickChaildToolType == .secure) {
  533. // toolbarItem.isSelected = fals
  534. let titles = [NSLocalizedString("Set Passwords", comment: ""), NSLocalizedString("Remove Security", comment: "")]
  535. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  536. let createFilePopover: NSPopover = NSPopover.init()
  537. createFilePopover.contentViewController = vc
  538. createFilePopover.animates = true
  539. createFilePopover.behavior = .transient
  540. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  541. 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)
  542. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  543. if downEntered {
  544. if (count == titles.first) {
  545. self!.delegate?.clickChildTool?(type: .secure, index: 1)
  546. } else {
  547. self!.delegate?.clickChildTool?(type: .secure, index: 2)
  548. }
  549. }
  550. }
  551. } else if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(clickChaildToolType.rawValue)) { /// 转档
  552. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  553. } else if (clickChaildToolType == .merge) {
  554. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  555. }
  556. }
  557. func toolbarViewController(_ viewController: KMToolbarViewController, zoomModel selectedTag: Int) {
  558. self.delegate?.toolbarViewController?(viewController, zoomModel: selectedTag)
  559. }
  560. func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String) {
  561. self.delegate?.toolbarViewController?(viewController, zoomSting: zoomSting)
  562. }
  563. func changePDFViewZoomInAction() {
  564. self.delegate?.changePDFViewZoomInAction?()
  565. }
  566. func changePDFViewZoomOutAction() {
  567. self.delegate?.changePDFViewZoomOutAction?()
  568. }
  569. func changePDFViewGotoNextPageAction() {
  570. self.delegate?.changePDFViewGotoNextPageAction?()
  571. }
  572. func changePDFViewGoToPreviousPageAction() {
  573. self.delegate?.changePDFViewGoToPreviousPageAction?()
  574. }
  575. func changePDFViewGotoBackAction() {
  576. self.delegate?.changePDFViewGotoBackAction?()
  577. }
  578. func changePDFViewGoToForwardAction() {
  579. self.delegate?.changePDFViewGoToForwardAction?()
  580. }
  581. func aiTranslationPDFFileAction() {
  582. self.delegate?.aiTranslationPDFFileAction?()
  583. }
  584. func toolbarViewController(_ viewController: KMToolbarViewController, shareAction toolbarItem: KMToolbarItemView) {
  585. self.delegate?.toolbarViewController?(viewController, shareAction: toolbarItem)
  586. }
  587. func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {
  588. self.delegate?.toolbarViewController?(viewController, shareDocument: item)
  589. }
  590. func toolbarViewController(_ viewController: KMToolbarViewController, shareFlatten item: NSMenuItem) {
  591. self.delegate?.toolbarViewController?(viewController, shareFlatten: item)
  592. }
  593. func toolbarViewController(_ viewController: KMToolbarViewController, shareOriginalPDF item: NSMenuItem) {
  594. self.delegate?.toolbarViewController?(viewController, shareOriginalPDF: item)
  595. }
  596. func toolbarViewController(_ viewController: KMToolbarViewController, scanOCRModel selectedTag: Int) {
  597. self.delegate?.toolbarViewController?(viewController, scanOCRModel: selectedTag)
  598. }
  599. func toolbarViewController(_ viewController: KMToolbarViewController, itemDidClick toolbarItem: KMToolbarItemView) {
  600. if toolbarItem.itemIdentifier == KMDocumentViewDisplayToolbarItemIdentifier {
  601. self.toolbarType = .viewSetting
  602. }
  603. self.delegate?.toolbarViewController?(viewController, itemDidClick: toolbarItem)
  604. }
  605. func toolbarViewController(_ viewController: KMToolbarViewController, searchAction searchString: String) {
  606. self.delegate?.toolbarViewController?(viewController, searchAction: searchString)
  607. }
  608. func toolbarViewController(_ viewController: KMToolbarViewController, menuItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?) {
  609. self.delegate?.toolbarViewController?(viewController, menuItemDidClick: toolbarItem, index: index, info: info)
  610. }
  611. }