KMToolbarController.swift 32 KB

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