KMToolbarController.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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, shareDocument item:NSMenuItem)
  38. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareFlatten item:NSMenuItem)
  39. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareOriginalPDF item:NSMenuItem)
  40. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, scanOCRModel selectedTag:Int)
  41. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, rightPanel toolbarItem: KMToolBoxItem)
  42. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, leftPanel toolbarItem: KMToolBoxItem)
  43. }
  44. class KMToolbarController: NSViewController {
  45. @IBOutlet weak var mainToolBarBox: NSBox!
  46. @IBOutlet weak var childToolBarBox: NSBox!
  47. @IBOutlet weak var secondaryToolBarBox: NSBox!
  48. @IBOutlet weak var mainToolBarHeight: NSLayoutConstraint!
  49. @IBOutlet weak var childToolBarHeight: NSLayoutConstraint!
  50. @IBOutlet weak var secondaryToolBarHeight: NSLayoutConstraint!
  51. @IBOutlet weak var bottomOffset: NSLayoutConstraint!
  52. var mainToolBarView: KMToolbarViewController?
  53. var childToolBarView: KMToolbarViewController?
  54. var _toolbarType : KMToolbarViewType = KMToolbarViewType.None
  55. var mainViewController: KMMainViewController?
  56. open weak var delegate: KMToolbarControllerDelegate?
  57. var toolbarItems : [NSToolbarItem.Identifier : Any] = [:]
  58. var listView : CPDFListView?
  59. var lastItemBox : KMToolBoxItem = KMToolBoxItem()
  60. var lastChildItemBox : KMToolBoxItem = KMToolBoxItem()
  61. var popover: NSPopover?
  62. // 是否显示所有注释
  63. private var _isShowAllAnnotations = true
  64. // 是否显示所有注释
  65. var isShowAllAnnotations: Bool {
  66. get {
  67. return _isShowAllAnnotations
  68. }
  69. set {
  70. _isShowAllAnnotations = newValue
  71. self.childToolBarView?.isShowAllAnnotations = newValue
  72. self.findChildItem(KMToolbarShowToolbarItemIdentifier)?.isSelected = !newValue
  73. }
  74. }
  75. private var _ignoreCurrentAnnotationTypeChange = false
  76. var ignoreCurrentAnnotationTypeChange: Bool {
  77. get {
  78. return self._ignoreCurrentAnnotationTypeChange
  79. }
  80. set {
  81. self._ignoreCurrentAnnotationTypeChange = newValue
  82. self.childToolBarView?.ignoreCurrentAnnotationTypeChange = self.ignoreCurrentAnnotationTypeChange
  83. // 忽略后重置 这个属性是基于单次添加,所以使用后会重置
  84. // self._ignoreCurrentAnnotationTypeChange = false
  85. }
  86. }
  87. override func viewDidLoad() {
  88. super.viewDidLoad()
  89. // Do view setup here.
  90. updateViewColor()
  91. mainToolBarView = KMToolbarViewController.init()
  92. mainToolBarView?.view.wantsLayer = true
  93. mainToolBarView?.view.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
  94. mainToolBarBox.contentView = mainToolBarView?.view
  95. mainToolBarView?.view.frame = mainToolBarBox.frame
  96. mainToolBarView?.delegate = self
  97. mainToolBarView?.view.autoresizingMask = [.width, .height]
  98. mainToolBarView?.pdfView = self.listView!
  99. childToolBarView = KMToolbarViewController.init()
  100. childToolBarView?.view.wantsLayer = true
  101. childToolBarView?.view.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
  102. childToolBarBox.contentView = childToolBarView?.view
  103. childToolBarView?.view.frame = childToolBarBox.frame
  104. childToolBarView?.delegate = self
  105. childToolBarView?.view.autoresizingMask = [.width, .height]
  106. childToolBarView?.pdfView = self.listView!
  107. toolbarType = .None
  108. mainToolBarView?.toolbarType = .Main
  109. mainToolBarView?.reloadateToolbar()
  110. addObserverForAppearanceChange()
  111. NotificationCenter.default.addObserver(self, selector: #selector(selectedShapAnnotationChangeNotification(_:)), name: NSNotification.Name.init(rawValue: "KMSelectedShapAnnotationChangeNotification"), object: nil)
  112. NotificationCenter.default.addObserver(self, selector: #selector(PDFChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "PDFChangedNotification"), object: nil)
  113. NotificationCenter.default.addObserver(self, selector: #selector(PDFTextEditPDFChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "KMPDFTextEditPDFChangedNotification"), object: nil)
  114. }
  115. // MARK:
  116. func updateViewColor() -> Void {
  117. }
  118. // MARK: Setter&Getter
  119. var toolbarType: KMToolbarViewType {
  120. set {
  121. if newValue == _toolbarType {
  122. if newValue == .Move || newValue == .Magnify || newValue == .Select || newValue == .SelectZoom {
  123. _toolbarType = .Annatiton
  124. } else {
  125. _toolbarType = .None
  126. }
  127. } else {
  128. _toolbarType = newValue
  129. }
  130. secondaryToolBarBox.isHidden = true
  131. secondaryToolBarHeight.constant = 51
  132. if _toolbarType == .None {
  133. bottomOffset.constant = 0
  134. childToolBarBox.isHidden = true
  135. self.delegate?.toolbarController?(self, heightOffsetChange: 40)
  136. } else if _toolbarType == .Page || _toolbarType == .LeftPanel {
  137. bottomOffset.constant = 0
  138. childToolBarBox.isHidden = true
  139. self.delegate?.toolbarController?(self, heightOffsetChange: 40)
  140. } else {
  141. bottomOffset.constant = 41
  142. childToolBarBox.isHidden = false
  143. self.delegate?.toolbarController?(self, heightOffsetChange: 81)
  144. }
  145. childToolBarView?.toolbarType = _toolbarType
  146. childToolBarView?.reloadateToolbar()
  147. }
  148. get {
  149. return _toolbarType
  150. }
  151. }
  152. // MARK: Dark&Light
  153. func addObserverForAppearanceChange() -> Void {
  154. }
  155. // MARK: -
  156. // MARK: Public Methods
  157. // 取消选中 [只是取消选中状态,不会执行具体的方法]
  158. public func cancelSelected(_ identifier: String) {
  159. if (isMainToolItem(identifier)) {
  160. self.findMainItem(identifier)?.isSelected = false
  161. return
  162. }
  163. self.findChildItem(identifier)?.isSelected = false
  164. }
  165. public func clickItem(_ identifier: String) {
  166. self.selectItem(identifier)
  167. }
  168. public func selectItem(_ identifier: String) {
  169. if (isMainToolItem(identifier)) {
  170. self.selectMainItem(identifier)
  171. return
  172. }
  173. if let parentItem = self.findItem(parentIdentifier(identifier)), parentItem.isSelected {
  174. if (self.trySelectChildItem(identifier)) { // 尝试去选子工具栏
  175. return
  176. }
  177. }
  178. // 当前主工具栏未开启
  179. let mainToolIdentifier = parentIdentifier(identifier)
  180. if (mainToolIdentifier.isEmpty) {
  181. return
  182. }
  183. // 找到主工具栏
  184. self.selectMainItem(mainToolIdentifier)
  185. DispatchQueue.main.async {
  186. // 再去选择子工具栏
  187. self.selectChildItem(identifier)
  188. }
  189. }
  190. public func findItem(_ identifier: String) -> KMToolBoxItem? {
  191. if (isMainToolItem(identifier)) {
  192. return self.findMainItem(identifier)
  193. }
  194. return self.findChildItem(identifier)
  195. }
  196. // MARK: -
  197. // MARK: private Methods
  198. private func selectMainItem(_ identifier: String) {
  199. if (self.mainToolBarView?.toolbarItems == nil) {
  200. return
  201. }
  202. var item: KMToolBoxItem?
  203. for (key, value) in self.mainToolBarView!.toolbarItems {
  204. if (key == identifier) {
  205. item = (value as! KMToolBoxItem)
  206. break
  207. }
  208. }
  209. // 主工具栏 item
  210. if (item != nil) {
  211. if (identifier == KMDocumentAnnotationToolbarItemIdentifier ||
  212. identifier == KMDocumentPageToolbarItemIdentifier ||
  213. identifier == KMDocumentConversonToolbarItemIdentifier ||
  214. identifier == KMDocumentScanOCRToolbarItemIdentifier ||
  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?.rightPanelControllButtonAction(item: item!.clickButton)
  222. } else if (identifier == KMLeftControlToolbarItemIdentifier) {
  223. item?.isSelected = !item!.isSelected
  224. self.delegate?.toolbarViewController?(self.mainToolBarView!, leftPanel: item!)
  225. }
  226. return
  227. }
  228. }
  229. private func trySelectChildItem(_ identifier: String) -> Bool {
  230. // 子工具栏 item [当前主工具栏已开启]
  231. var item: KMToolBoxItem?
  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! KMToolBoxItem)
  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: KMToolBoxItem?
  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! KMToolBoxItem)
  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) -> KMToolBoxItem? {
  271. if (self.mainToolBarView?.toolbarItems == nil) {
  272. return nil
  273. }
  274. var item: KMToolBoxItem?
  275. if (isMainToolItem(identifier)) {
  276. for (key, value) in self.mainToolBarView!.toolbarItems {
  277. if (key == identifier) {
  278. item = (value as! KMToolBoxItem)
  279. break
  280. }
  281. }
  282. return item
  283. }
  284. return nil
  285. }
  286. private func findChildItem(_ identifier: String) -> KMToolBoxItem? {
  287. if (self.childToolBarView?.toolbarItems == nil || self.childToolBarView!.toolbarItems.count == 0) {
  288. return nil
  289. }
  290. var item: KMToolBoxItem?
  291. for (key, value) in self.childToolBarView!.toolbarItems {
  292. if (key == identifier) {
  293. item = (value as! KMToolBoxItem)
  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 await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  434. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  435. let boxItem = item.clickObject as? KMToolBoxItem
  436. boxItem?.isSelected = false
  437. return
  438. }
  439. //文字编辑 图片编辑 选中按钮逻辑(只能同时选中其中一个)
  440. if type == .addText || type == .addImage {
  441. let boxItem = item.clickObject as? KMToolBoxItem
  442. if boxItem != nil {
  443. if self.lastChildItemBox != boxItem {
  444. self.lastChildItemBox.isSelected = false
  445. boxItem!.isSelected = true
  446. } else {
  447. boxItem!.isSelected = !boxItem!.isSelected
  448. }
  449. self.lastChildItemBox = boxItem!
  450. }
  451. }
  452. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  453. self.toolbarType = .Annatiton
  454. }
  455. self.delegate?.changeAnnotationModeAction?(item: item)
  456. }
  457. }
  458. func toolbarViewController(_ viewController: KMToolbarViewController, clickMode toolMode: KMToolbarViewType, toolbar toolbarItem: KMToolBoxItem, _ pages: [Int]) {
  459. let beforeModel = KMToolbarViewType(rawValue: self.lastItemBox.clickButton.tag) ?? .None
  460. if self.lastItemBox != nil {
  461. if (toolbarItem.isSelected && toolMode.isToolMode()) {
  462. // no nothings
  463. } else {
  464. self.lastItemBox.isSelected = false
  465. }
  466. }
  467. if self.lastChildItemBox != nil {
  468. if toolMode == .editPDF {
  469. if !self.lastItemBox.isSelected {
  470. self.lastChildItemBox.isSelected = false
  471. }
  472. }
  473. }
  474. if toolMode != .Magnify && toolMode != .Move && toolMode != .Select && toolMode != .SelectZoom && toolMode != .LeftPanel && toolMode != .RightPanel {
  475. if(toolMode == self.toolbarType) {
  476. toolbarItem.isSelected = false
  477. } else {
  478. toolbarItem.isSelected = true
  479. self.lastItemBox = toolbarItem
  480. }
  481. } else {
  482. if(toolMode != self.toolbarType && toolMode != .LeftPanel) {
  483. let item : KMToolBoxItem = (self.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentAnnotationToolbarItemIdentifier))!
  484. item.isSelected = true
  485. self.lastItemBox = item
  486. } else if (toolMode == .LeftPanel) {
  487. if(toolMode == self.toolbarType) {
  488. toolbarItem.isSelected = false
  489. } else {
  490. toolbarItem.isSelected = true
  491. }
  492. }
  493. }
  494. self.toolbarType = toolMode
  495. self.delegate?.mainToolDidClicked?(self, beforeModel, toolMode, toolbarItem, pages)
  496. }
  497. func toolbarViewController(_ viewController: KMToolbarViewController, rightPanel toolbarItem: KMToolBoxItem) {
  498. toolbarItem.isSelected = !toolbarItem.isSelected
  499. self.delegate?.toolbarViewController?(viewController, rightPanel: toolbarItem)
  500. }
  501. func toolbarViewController(_ viewController: KMToolbarViewController, leftPanel toolbarItem: KMToolBoxItem) {
  502. toolbarItem.isSelected = !toolbarItem.isSelected
  503. self.delegate?.toolbarViewController?(viewController, leftPanel: toolbarItem)
  504. }
  505. func showPDFLayoutModeAction(show: Bool) {
  506. self.delegate?.showPDFLayoutModeAction?(show: show)
  507. }
  508. func toolbarViewController(_ viewController: KMToolbarViewController, clickChaildToolType: KMToolbarType, toolbar toolbarItem: KMToolBoxItem) {
  509. if (clickChaildToolType == .crop) {
  510. let titles = [NSLocalizedString("Crop Current Page - White Margins", comment: ""), NSLocalizedString("Crop All Pages - Auto", comment: "")]
  511. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  512. let createFilePopover: NSPopover = NSPopover.init()
  513. createFilePopover.contentViewController = vc
  514. createFilePopover.animates = true
  515. createFilePopover.behavior = .transient
  516. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  517. createFilePopover.show(relativeTo: NSZeroRect, of: toolbarItem, preferredEdge: .maxY)
  518. self.popover = createFilePopover
  519. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  520. self?.popover?.close()
  521. self?.popover = nil
  522. if downEntered {
  523. if (count == titles.first) {
  524. self!.delegate?.clickChildTool?(type: .crop, index: 1)
  525. } else {
  526. self!.delegate?.clickChildTool?(type: .crop, index: 2)
  527. }
  528. }
  529. }
  530. } else if (clickChaildToolType == .bates || clickChaildToolType == .headerAndFooter || clickChaildToolType == .background || clickChaildToolType == .watermark) {
  531. toolbarItem.isSelected = false
  532. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  533. } else if (clickChaildToolType == .redact) {
  534. toolbarItem.isSelected = false
  535. self.enterRedact()
  536. } else if (clickChaildToolType == .compress) {
  537. self.delegate?.clickChildTool?(type: .compress, index: 0)
  538. } else if (clickChaildToolType == .secure) {
  539. // toolbarItem.isSelected = fals
  540. let titles = [NSLocalizedString("Set Passwords", comment: ""), NSLocalizedString("Remove Security", comment: "")]
  541. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  542. let createFilePopover: NSPopover = NSPopover.init()
  543. createFilePopover.contentViewController = vc
  544. createFilePopover.animates = true
  545. createFilePopover.behavior = .transient
  546. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  547. 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)
  548. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  549. if downEntered {
  550. if (count == titles.first) {
  551. self!.delegate?.clickChildTool?(type: .secure, index: 1)
  552. } else {
  553. self!.delegate?.clickChildTool?(type: .secure, index: 2)
  554. }
  555. }
  556. }
  557. } else if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(clickChaildToolType.rawValue)) { /// 转档
  558. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  559. } else if (clickChaildToolType == .merge) {
  560. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  561. }
  562. }
  563. func toolbarViewController(_ viewController: KMToolbarViewController, zoomModel selectedTag: Int) {
  564. self.delegate?.toolbarViewController?(viewController, zoomModel: selectedTag)
  565. }
  566. func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String) {
  567. self.delegate?.toolbarViewController?(viewController, zoomSting: zoomSting)
  568. }
  569. func changePDFViewZoomInAction() {
  570. self.delegate?.changePDFViewZoomInAction?()
  571. }
  572. func changePDFViewZoomOutAction() {
  573. self.delegate?.changePDFViewZoomOutAction?()
  574. }
  575. func changePDFViewGotoNextPageAction() {
  576. self.delegate?.changePDFViewGotoNextPageAction?()
  577. }
  578. func changePDFViewGoToPreviousPageAction() {
  579. self.delegate?.changePDFViewGoToPreviousPageAction?()
  580. }
  581. func showPDFViewPrintViewController() {
  582. self.delegate?.showPDFViewPrintViewController?()
  583. }
  584. func aiTranslationPDFFileAction() {
  585. self.delegate?.aiTranslationPDFFileAction?()
  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. }