KMToolbarController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. //
  2. // KMToolbarController.swift
  3. // PDF Reader Pro
  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 || _toolbarType == .redact {
  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 == KMDocumentEditToolbarItemIdentifier ||
  216. identifier == KMDocumentFormToolbarItemIdentifier ||
  217. identifier == KMDocumentFillSginToolbarItemIdentifier ||
  218. identifier == KMDocumentToolToolbarItemIdentifier) {
  219. self.mainToolBarView?.leftControllButtonAction(item: item!.clickButton)
  220. } else if (identifier == KMRightControlToolbarItemIdentifier) {
  221. self.mainToolBarView?.itemAction(item!)
  222. } else if (identifier == KMLeftControlToolbarItemIdentifier) {
  223. self.mainToolBarView?.itemAction(item!)
  224. } else if identifier == KMDocumentRedactToolbarItemIdentifier {
  225. self.mainToolBarView?.itemAction(item!)
  226. } else if identifier == KMDocumentScanOCRToolbarItemIdentifier {
  227. self.mainToolBarView?.itemAction(item!)
  228. }
  229. return
  230. }
  231. }
  232. private func trySelectChildItem(_ identifier: String) -> Bool {
  233. // 子工具栏 item [当前主工具栏已开启]
  234. var item: KMToolbarItemView?
  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! KMToolbarItemView)
  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: KMToolbarItemView?
  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! KMToolbarItemView)
  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) -> KMToolbarItemView? {
  274. if (self.mainToolBarView?.toolbarItems == nil) {
  275. return nil
  276. }
  277. var item: KMToolbarItemView?
  278. if (isMainToolItem(identifier)) {
  279. for (key, value) in self.mainToolBarView!.toolbarItems {
  280. if (key == identifier) {
  281. item = (value as! KMToolbarItemView)
  282. break
  283. }
  284. }
  285. return item
  286. }
  287. return nil
  288. }
  289. private func findChildItem(_ identifier: String) -> KMToolbarItemView? {
  290. if (self.childToolBarView?.toolbarItems == nil || self.childToolBarView!.toolbarItems.count == 0) {
  291. return nil
  292. }
  293. var item: KMToolbarItemView?
  294. for (key, value) in self.childToolBarView!.toolbarItems {
  295. if (key == identifier) {
  296. item = (value as! KMToolbarItemView)
  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? KMToolbarItemView
  440. boxItem?.isSelected = false
  441. return
  442. }
  443. #endif
  444. //文字编辑 图片编辑 选中按钮逻辑(只能同时选中其中一个)
  445. if type == .addText || type == .addImage {
  446. let boxItem = item.clickObject as? KMToolbarItemView
  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: KMToolbarItemView, _ 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 : KMToolbarItemView = (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 showPDFLayoutModeAction(show: Bool) {
  503. self.delegate?.showPDFLayoutModeAction?(show: show)
  504. }
  505. func toolbarViewController(_ viewController: KMToolbarViewController, clickChaildToolType: KMToolbarType, toolbar toolbarItem: KMToolbarItemView) {
  506. if (clickChaildToolType == .crop) {
  507. let titles = [NSLocalizedString("Crop Current Page - White Margins", comment: ""), NSLocalizedString("Crop All Pages - Auto", comment: "")]
  508. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  509. let createFilePopover: NSPopover = NSPopover.init()
  510. createFilePopover.contentViewController = vc
  511. createFilePopover.animates = true
  512. createFilePopover.behavior = .transient
  513. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  514. createFilePopover.show(relativeTo: NSZeroRect, of: toolbarItem, preferredEdge: .maxY)
  515. self.popover = createFilePopover
  516. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  517. self?.popover?.close()
  518. self?.popover = nil
  519. if downEntered {
  520. if (count == titles.first) {
  521. self!.delegate?.clickChildTool?(type: .crop, index: 1)
  522. } else {
  523. self!.delegate?.clickChildTool?(type: .crop, index: 2)
  524. }
  525. }
  526. }
  527. } else if (clickChaildToolType == .bates || clickChaildToolType == .headerAndFooter || clickChaildToolType == .background || clickChaildToolType == .watermark) {
  528. toolbarItem.isSelected = false
  529. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  530. } else if (clickChaildToolType == .redact) {
  531. toolbarItem.isSelected = false
  532. self.enterRedact()
  533. } else if (clickChaildToolType == .compress) {
  534. self.delegate?.clickChildTool?(type: .compress, index: 0)
  535. } else if (clickChaildToolType == .secure) {
  536. // toolbarItem.isSelected = fals
  537. let titles = [NSLocalizedString("Set Passwords", comment: ""), NSLocalizedString("Remove Security", comment: "")]
  538. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  539. let createFilePopover: NSPopover = NSPopover.init()
  540. createFilePopover.contentViewController = vc
  541. createFilePopover.animates = true
  542. createFilePopover.behavior = .transient
  543. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  544. 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)
  545. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  546. if downEntered {
  547. if (count == titles.first) {
  548. self!.delegate?.clickChildTool?(type: .secure, index: 1)
  549. } else {
  550. self!.delegate?.clickChildTool?(type: .secure, index: 2)
  551. }
  552. }
  553. }
  554. } else if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(clickChaildToolType.rawValue)) { /// 转档
  555. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  556. } else if (clickChaildToolType == .merge) {
  557. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  558. }
  559. }
  560. func toolbarViewController(_ viewController: KMToolbarViewController, zoomModel selectedTag: Int) {
  561. self.delegate?.toolbarViewController?(viewController, zoomModel: selectedTag)
  562. }
  563. func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String) {
  564. self.delegate?.toolbarViewController?(viewController, zoomSting: zoomSting)
  565. }
  566. func changePDFViewZoomInAction() {
  567. self.delegate?.changePDFViewZoomInAction?()
  568. }
  569. func changePDFViewZoomOutAction() {
  570. self.delegate?.changePDFViewZoomOutAction?()
  571. }
  572. func changePDFViewGotoNextPageAction() {
  573. self.delegate?.changePDFViewGotoNextPageAction?()
  574. }
  575. func changePDFViewGoToPreviousPageAction() {
  576. self.delegate?.changePDFViewGoToPreviousPageAction?()
  577. }
  578. func changePDFViewGotoBackAction() {
  579. self.delegate?.changePDFViewGotoBackAction?()
  580. }
  581. func changePDFViewGoToForwardAction() {
  582. self.delegate?.changePDFViewGoToForwardAction?()
  583. }
  584. func aiTranslationPDFFileAction() {
  585. self.delegate?.aiTranslationPDFFileAction?()
  586. }
  587. func toolbarViewController(_ viewController: KMToolbarViewController, shareAction toolbarItem: KMToolbarItemView) {
  588. self.delegate?.toolbarViewController?(viewController, shareAction: toolbarItem)
  589. }
  590. func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {
  591. self.delegate?.toolbarViewController?(viewController, shareDocument: item)
  592. }
  593. func toolbarViewController(_ viewController: KMToolbarViewController, shareFlatten item: NSMenuItem) {
  594. self.delegate?.toolbarViewController?(viewController, shareFlatten: item)
  595. }
  596. func toolbarViewController(_ viewController: KMToolbarViewController, shareOriginalPDF item: NSMenuItem) {
  597. self.delegate?.toolbarViewController?(viewController, shareOriginalPDF: item)
  598. }
  599. func toolbarViewController(_ viewController: KMToolbarViewController, scanOCRModel selectedTag: Int) {
  600. self.delegate?.toolbarViewController?(viewController, scanOCRModel: selectedTag)
  601. }
  602. func toolbarViewController(_ viewController: KMToolbarViewController, itemDidClick toolbarItem: KMToolbarItemView) {
  603. if toolbarItem.itemIdentifier == KMDocumentViewDisplayToolbarItemIdentifier {
  604. self.toolbarType = .viewSetting
  605. }
  606. self.delegate?.toolbarViewController?(viewController, itemDidClick: toolbarItem)
  607. }
  608. func toolbarViewController(_ viewController: KMToolbarViewController, searchAction searchString: String) {
  609. self.delegate?.toolbarViewController?(viewController, searchAction: searchString)
  610. }
  611. func toolbarViewController(_ viewController: KMToolbarViewController, menuItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?) {
  612. self.delegate?.toolbarViewController?(viewController, menuItemDidClick: toolbarItem, index: index, info: info)
  613. }
  614. }