KMToolbarController.swift 28 KB

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