KMToolbarController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. identifier == KMAnnotationImageToolbarItemIdentifier ||
  272. identifier == KMAnnotationTableToolbarItemIdentifier {
  273. self.childToolBarView?.itemAction(item!)
  274. } else {
  275. self.childToolBarView?.changeAnnotationMode(item: item!.clickButton)
  276. }
  277. }
  278. }
  279. }
  280. private func findMainItem(_ identifier: String) -> KMToolbarItemView? {
  281. if (self.mainToolBarView?.toolbarItems == nil) {
  282. return nil
  283. }
  284. var item: KMToolbarItemView?
  285. if (isMainToolItem(identifier)) {
  286. for (key, value) in self.mainToolBarView!.toolbarItems {
  287. if (key == identifier) {
  288. item = (value as! KMToolbarItemView)
  289. break
  290. }
  291. }
  292. return item
  293. }
  294. return nil
  295. }
  296. private func findChildItem(_ identifier: String) -> KMToolbarItemView? {
  297. if (self.childToolBarView?.toolbarItems == nil || self.childToolBarView!.toolbarItems.count == 0) {
  298. return nil
  299. }
  300. var item: KMToolbarItemView?
  301. for (key, value) in self.childToolBarView!.toolbarItems {
  302. if (key == identifier) {
  303. item = (value as! KMToolbarItemView)
  304. break
  305. }
  306. }
  307. return item
  308. }
  309. func exitPageEdit() -> Void {
  310. resetToolbarViewController(mainToolBarView!)
  311. exitPageEditToolbarViewController(mainToolBarView!)
  312. }
  313. func exitTextEdit() -> Void {
  314. secondaryToolBarBox.isHidden = true
  315. secondaryToolBarHeight.constant = 50
  316. bottomOffset.constant = 51
  317. childToolBarBox.isHidden = false
  318. self.delegate?.toolbarController?(self, heightOffsetChange: 102)
  319. }
  320. func enterWatermarkAdjective(_ type: KMToolbarType, _ itemTitles: Array<Array<String>>) {
  321. self.toolbarType = .None
  322. let topBarView = KMWatermarkAdjectiveTopBarView()
  323. topBarView.frame = (self.mainToolBarView?.toolbar.bounds)!
  324. self.mainToolBarView?.toolbar.addSubview(topBarView)
  325. topBarView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  326. topBarView.wantsLayer = true
  327. topBarView.layer?.backgroundColor = NSColor.white.cgColor
  328. topBarView.isCanApply(can: false)
  329. var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
  330. for items in itemTitles {
  331. var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
  332. for title in items {
  333. let model = KMWatermarkAdjectiveTopBarItemModel()
  334. model.iconName = ""
  335. model.itemTitle = title
  336. array.append(model)
  337. }
  338. itemModels.append(array)
  339. }
  340. topBarView.initItemData(itemArrays: itemModels)
  341. topBarView.cancelClick = {
  342. [unowned self] in
  343. self.delegate?.clickChildTool!(type: type, index: 1)
  344. }
  345. topBarView.applyClick = {
  346. [unowned self] in
  347. self.delegate?.clickChildTool!(type: type, index: 2)
  348. }
  349. topBarView.itemClick = {
  350. [unowned self] (section: Int, item: Int) -> () in
  351. self.delegate?.clickChildTool!(type: type, index: 3+item)
  352. }
  353. }
  354. func exitWatermarkAdjective() {
  355. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  356. if (topBarView == nil || topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self) == false) {
  357. return
  358. }
  359. topBarView?.removeFromSuperview()
  360. }
  361. func fetchTopBarView() -> NSView? {
  362. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  363. if (topBarView == nil) {
  364. return nil
  365. }
  366. if (topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self) == true) {
  367. return topBarView
  368. }
  369. if (topBarView?.isKind(of: KMRedactTopToolBar.self) == true) {
  370. return topBarView
  371. }
  372. return nil
  373. }
  374. func enterRedact() {
  375. self.toolbarType = .None
  376. let topBarView = KMRedactTopToolBar()
  377. topBarView.frame = (self.mainToolBarView?.toolbar.bounds)!
  378. self.mainToolBarView?.toolbar.addSubview(topBarView)
  379. topBarView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  380. topBarView.wantsLayer = true
  381. topBarView.layer?.backgroundColor = NSColor.white.cgColor
  382. topBarView.selectItem(0)
  383. self.delegate?.clickChildTool!(type: .redact, index: 4)
  384. topBarView.itemClick = { [weak self] index in
  385. self!.delegate?.clickChildTool!(type: .redact, index: index)
  386. }
  387. }
  388. func exitRedact() {
  389. let topBarView = self.mainToolBarView?.toolbar.subviews.last
  390. if (topBarView == nil || topBarView?.isKind(of: KMRedactTopToolBar.self) == false) {
  391. return
  392. }
  393. topBarView?.removeFromSuperview()
  394. }
  395. // MARK: NSNotification
  396. @objc func selectedShapAnnotationChangeNotification(_ notification: Notification) -> Void {
  397. }
  398. @objc func PDFChangedNotification(_ notification: Notification) -> Void {
  399. }
  400. @objc func PDFTextEditPDFChangedNotification(_ notification: Notification) -> Void {
  401. }
  402. // MARK:
  403. func resetToolbarViewController(_ toolbarViewController: KMToolbarViewController) -> Void {
  404. self.toolbarType = .None
  405. self.mainToolBarView?.resetToolbar()
  406. self.mainViewController?.listView.annotationType = .unkown
  407. }
  408. func exitPageEditToolbarViewController(_ toolbarViewController: KMToolbarViewController) -> Void {
  409. }
  410. // MARK: item actions
  411. @objc func cropItemAction(sender: NSMenuItem) {
  412. if (sender.tag == 0) { /// 裁剪当前页面
  413. self.delegate?.clickChildTool!(type: .crop, index: 1)
  414. return
  415. }
  416. if (sender.tag == 1) { /// 裁剪所有页面
  417. self.delegate?.clickChildTool!(type: .crop, index: 2)
  418. return
  419. }
  420. /// 自定义裁剪区域
  421. self.delegate?.clickChildTool!(type: .crop, index: 3)
  422. }
  423. @objc func secureItemAction(sender: NSMenuItem) {
  424. var index: Int = 1
  425. if (sender.tag == 1) { /// 删除安全性设置
  426. index = 2
  427. }
  428. self.delegate?.clickChildTool?(type: .secure, index: index)
  429. }
  430. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  431. super.interfaceThemeDidChanged(appearance)
  432. self.mainToolBarView?.interfaceThemeDidChanged(appearance)
  433. }
  434. }
  435. //MARK: Find Search
  436. extension KMToolbarController {
  437. func showFindBar() {
  438. print("showFindBar")
  439. var height: Float = self.fetchHeight(type: _toolbarType)
  440. self.delegate?.toolbarController?(self, heightOffsetChange: height)
  441. self.findSearchView.findField.becomeFirstResponder()
  442. self.findSearchView.doneAction = { [unowned self] view in
  443. print("doneAction")
  444. self.exitFindBar()
  445. }
  446. self.findSearchView.searchAction = { view, searchString, forward in
  447. print("searchAction")
  448. self.toolbarViewController(self.mainToolBarView!, searchAction: searchString, forward: forward)
  449. return true
  450. }
  451. self.findSearchView.showAllAction = { [unowned self] view, searchString, forward in
  452. self.toolbarViewController(self.mainToolBarView!, searchAllAction: searchString, forward: forward)
  453. print("showAllAction: \(searchString)")
  454. }
  455. }
  456. func showFindString(forward: Bool) {
  457. self.toolbarViewController(self.mainToolBarView!, searchAction: self.findSearchView.findString ?? "", forward: forward)
  458. }
  459. func showAllAction() {
  460. self.toolbarViewController(self.mainToolBarView!, searchAllAction: self.findSearchView.findString ?? "", forward: true)
  461. }
  462. func exitFindBar() {
  463. print("exitFindBar")
  464. var height: Float = self.fetchHeight(type: _toolbarType, isShow: false)
  465. self.delegate?.toolbarController?(self, heightOffsetChange: height)
  466. }
  467. func fetchHeight(type: KMToolbarViewType, isShow: Bool = true) -> Float {
  468. var height: Float = 0.0
  469. if isShow {
  470. if _toolbarType == .None {
  471. bottomOffset.constant = 0 + 25
  472. height = 40 + 8
  473. } else if _toolbarType == .Page || _toolbarType == .LeftPanel || _toolbarType == .redact {
  474. bottomOffset.constant = 0 + 25
  475. height = 40 + 8
  476. } else {
  477. bottomOffset.constant = 41 + 25
  478. height = 81 + 8
  479. }
  480. height = height + 25
  481. } else {
  482. if _toolbarType == .None {
  483. bottomOffset.constant = 0
  484. height = 40 + 8
  485. } else if _toolbarType == .Page || _toolbarType == .LeftPanel || _toolbarType == .redact {
  486. bottomOffset.constant = 0
  487. height = 40 + 8
  488. } else {
  489. bottomOffset.constant = 41
  490. height = 81 + 8
  491. }
  492. }
  493. return height
  494. }
  495. }
  496. extension KMToolbarController: KMToolbarViewControllerDelegate {
  497. func changeAnnotationModeAction(item: KMToolbarClickButton) {
  498. let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
  499. if (type.isAdvanced() == false) {
  500. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  501. self.toolbarType = .Annatiton
  502. }
  503. self.delegate?.changeAnnotationModeAction?(item: item)
  504. return
  505. }
  506. // 高级功能
  507. Task { @MainActor in
  508. //文字编辑 图片编辑 选中按钮逻辑(只能同时选中其中一个)
  509. if type == .addText || type == .addImage {
  510. let boxItem = item.clickObject as? KMToolbarItemView
  511. if boxItem != nil {
  512. if self.lastChildItemBox != boxItem {
  513. self.lastChildItemBox.isSelected = false
  514. boxItem!.isSelected = true
  515. } else {
  516. boxItem!.isSelected = !boxItem!.isSelected
  517. }
  518. self.lastChildItemBox = boxItem!
  519. }
  520. }
  521. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  522. self.toolbarType = .Annatiton
  523. }
  524. self.delegate?.changeAnnotationModeAction?(item: item)
  525. }
  526. }
  527. func toolbarViewController(_ viewController: KMToolbarViewController, clickMode toolMode: KMToolbarViewType, toolbar toolbarItem: KMToolbarItemView, _ pages: [Int]) {
  528. let beforeModel = KMToolbarViewType(rawValue: self.lastItemBox.clickButton.tag) ?? .None
  529. if self.lastItemBox != nil {
  530. if (toolbarItem.isSelected && toolMode.isToolMode()) {
  531. // no nothings
  532. } else {
  533. self.lastItemBox.isSelected = false
  534. }
  535. }
  536. if self.lastChildItemBox != nil {
  537. if toolMode == .editPDF {
  538. if !self.lastItemBox.isSelected {
  539. self.lastChildItemBox.isSelected = false
  540. }
  541. }
  542. }
  543. if toolMode != .Magnify && toolMode != .Move && toolMode != .Select && toolMode != .SelectZoom && toolMode != .LeftPanel && toolMode != .RightPanel {
  544. if(toolMode == self.toolbarType) {
  545. toolbarItem.isSelected = false
  546. } else {
  547. toolbarItem.isSelected = true
  548. self.lastItemBox = toolbarItem
  549. if toolMode == .editPDF {
  550. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_EditPDF"])
  551. }
  552. }
  553. } else {
  554. if(toolMode != self.toolbarType && toolMode != .LeftPanel) {
  555. let item : KMToolbarItemView = (self.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentAnnotationToolbarItemIdentifier))!
  556. item.isSelected = true
  557. self.lastItemBox = item
  558. } else if (toolMode == .LeftPanel) {
  559. if(toolMode == self.toolbarType) {
  560. toolbarItem.isSelected = false
  561. } else {
  562. toolbarItem.isSelected = true
  563. }
  564. }
  565. }
  566. self.toolbarType = toolMode
  567. self.delegate?.mainToolDidClicked?(self, beforeModel, toolMode, toolbarItem, pages)
  568. }
  569. func showPDFLayoutModeAction(show: Bool) {
  570. self.delegate?.showPDFLayoutModeAction?(show: show)
  571. }
  572. func toolbarViewController(_ viewController: KMToolbarViewController, clickChaildToolType: KMToolbarType, toolbar toolbarItem: KMToolbarItemView) {
  573. if (clickChaildToolType == .crop) {
  574. let titles = [NSLocalizedString("Crop Current Page - White Margins", comment: ""), NSLocalizedString("Crop All Pages - Auto", comment: "")]
  575. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  576. let createFilePopover: NSPopover = NSPopover.init()
  577. createFilePopover.contentViewController = vc
  578. createFilePopover.animates = true
  579. createFilePopover.behavior = .transient
  580. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  581. createFilePopover.show(relativeTo: NSZeroRect, of: toolbarItem, preferredEdge: .maxY)
  582. self.popover = createFilePopover
  583. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  584. self?.popover?.close()
  585. self?.popover = nil
  586. if downEntered {
  587. if (count == titles.first) {
  588. self!.delegate?.clickChildTool?(type: .crop, index: 1)
  589. } else {
  590. self!.delegate?.clickChildTool?(type: .crop, index: 2)
  591. }
  592. }
  593. }
  594. } else if (clickChaildToolType == .bates || clickChaildToolType == .headerAndFooter || clickChaildToolType == .background || clickChaildToolType == .watermark) {
  595. toolbarItem.isSelected = false
  596. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  597. } else if (clickChaildToolType == .redact) {
  598. toolbarItem.isSelected = false
  599. self.enterRedact()
  600. } else if (clickChaildToolType == .compress) {
  601. self.delegate?.clickChildTool?(type: .compress, index: 0)
  602. } else if (clickChaildToolType == .secure) {
  603. // toolbarItem.isSelected = fals
  604. let titles = [NSLocalizedString("Set Passwords", comment: ""), NSLocalizedString("Remove Security", comment: "")]
  605. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  606. let createFilePopover: NSPopover = NSPopover.init()
  607. createFilePopover.contentViewController = vc
  608. createFilePopover.animates = true
  609. createFilePopover.behavior = .transient
  610. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  611. 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)
  612. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  613. if downEntered {
  614. if (count == titles.first) {
  615. self!.delegate?.clickChildTool?(type: .secure, index: 1)
  616. } else {
  617. self!.delegate?.clickChildTool?(type: .secure, index: 2)
  618. }
  619. }
  620. }
  621. } else if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(clickChaildToolType.rawValue)) { /// 转档
  622. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  623. } else if (clickChaildToolType == .merge) {
  624. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  625. }
  626. }
  627. func toolbarViewController(_ viewController: KMToolbarViewController, zoomModel selectedTag: Int) {
  628. self.delegate?.toolbarViewController?(viewController, zoomModel: selectedTag)
  629. }
  630. func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String) {
  631. self.delegate?.toolbarViewController?(viewController, zoomSting: zoomSting)
  632. }
  633. func changePDFViewZoomInAction() {
  634. self.delegate?.changePDFViewZoomInAction?()
  635. }
  636. func changePDFViewZoomOutAction() {
  637. self.delegate?.changePDFViewZoomOutAction?()
  638. }
  639. func changePDFViewGotoNextPageAction() {
  640. self.delegate?.changePDFViewGotoNextPageAction?()
  641. }
  642. func changePDFViewGoToPreviousPageAction() {
  643. self.delegate?.changePDFViewGoToPreviousPageAction?()
  644. }
  645. func changePDFViewGotoBackAction() {
  646. self.delegate?.changePDFViewGotoBackAction?()
  647. }
  648. func changePDFViewGoToForwardAction() {
  649. self.delegate?.changePDFViewGoToForwardAction?()
  650. }
  651. func aiTranslationPDFFileAction() {
  652. self.delegate?.aiTranslationPDFFileAction?()
  653. }
  654. func toolbarViewController(_ viewController: KMToolbarViewController, shareAction toolbarItem: KMToolbarItemView) {
  655. self.delegate?.toolbarViewController?(viewController, shareAction: toolbarItem)
  656. }
  657. func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {
  658. self.delegate?.toolbarViewController?(viewController, shareDocument: item)
  659. }
  660. func toolbarViewController(_ viewController: KMToolbarViewController, shareFlatten item: NSMenuItem) {
  661. self.delegate?.toolbarViewController?(viewController, shareFlatten: item)
  662. }
  663. func toolbarViewController(_ viewController: KMToolbarViewController, shareOriginalPDF item: NSMenuItem) {
  664. self.delegate?.toolbarViewController?(viewController, shareOriginalPDF: item)
  665. }
  666. func toolbarViewController(_ viewController: KMToolbarViewController, scanOCRModel selectedTag: Int) {
  667. self.delegate?.toolbarViewController?(viewController, scanOCRModel: selectedTag)
  668. }
  669. func toolbarViewController(_ viewController: KMToolbarViewController, itemDidClick toolbarItem: KMToolbarItemView) {
  670. let itemIdentifier = toolbarItem.itemIdentifier
  671. if itemIdentifier != KMRightControlToolbarItemIdentifier {
  672. if self.lastItemBox != nil {
  673. if (toolbarItem.isSelected) {
  674. // no nothings
  675. } else {
  676. let p_itemIdentifier = parentIdentifier(itemIdentifier ?? "")
  677. if p_itemIdentifier != self.lastItemBox.itemIdentifier {
  678. self.lastItemBox.isSelected = false
  679. }
  680. }
  681. }
  682. }
  683. self.delegate?.toolbarViewController?(viewController, itemDidClick: toolbarItem)
  684. }
  685. func toolbarViewController(_ viewController: KMToolbarViewController, searchAction searchString: String, forward: Bool) {
  686. self.delegate?.toolbarViewController?(viewController, searchAction: searchString, forward: forward)
  687. }
  688. func toolbarViewController(_ viewController: KMToolbarViewController, searchAllAction searchString: String, forward: Bool) {
  689. self.delegate?.toolbarViewController?(viewController, searchAllAction: searchString, forward: forward)
  690. }
  691. func toolbarViewController(_ viewController: KMToolbarViewController, menuItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?) {
  692. let itemIdentifier = toolbarItem.itemIdentifier
  693. if itemIdentifier != KMRightControlToolbarItemIdentifier {
  694. if self.lastItemBox != nil {
  695. if (toolbarItem.isSelected) {
  696. // no nothings
  697. } else {
  698. let p_itemIdentifier = parentIdentifier(itemIdentifier ?? "")
  699. if p_itemIdentifier != self.lastItemBox.itemIdentifier {
  700. self.lastItemBox.isSelected = false
  701. }
  702. }
  703. }
  704. }
  705. self.delegate?.toolbarViewController?(viewController, menuItemDidClick: toolbarItem, index: index, info: info)
  706. }
  707. }
  708. extension KMToolbarController {
  709. func updataItemVisible() {
  710. guard let isCompareModel = self.mainViewController?.isCompareModel else { return }
  711. if isCompareModel {
  712. self.toolbarType = .None
  713. self.mainToolBarView?.isEnable(isEnable: false)
  714. } else {
  715. self.mainToolBarView?.isEnable()
  716. }
  717. }
  718. }