KMToolbarController.swift 32 KB

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