KMToolbarController.swift 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. func isMarkup() -> Bool {
  22. if self == .highlight || self == .underline || self == .strikeOut || self == .ink {
  23. return true
  24. }
  25. return false
  26. }
  27. func isSquare() -> Bool {
  28. if self == .square || self == .circle || self == .line || self == .arrow {
  29. return true
  30. }
  31. return false
  32. }
  33. }
  34. @objc protocol KMToolbarControllerDelegate {
  35. @objc optional func toolbarController(_ viewController: KMToolbarController, heightOffsetChange heightOffset: Float, animated: Bool)
  36. @objc optional func changeAnnotationModeAction(item:KMToolbarClickButton)
  37. @objc optional func showPDFLayoutModeAction(show:Bool)
  38. @objc optional func clickChildTool(type: KMToolbarType, index: Int)
  39. @objc optional func changePDFViewZoomModelAction(selectedTag:Int)
  40. @objc optional func changePDFViewZoomInAction()
  41. @objc optional func changePDFViewZoomOutAction()
  42. @objc optional func changePDFViewGotoNextPageAction()
  43. @objc optional func changePDFViewGoToPreviousPageAction()
  44. @objc optional func changePDFViewGotoBackAction()
  45. @objc optional func changePDFViewGoToForwardAction()
  46. @objc optional func aiTranslationPDFFileAction()
  47. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, zoomModel selectedTag:Int)
  48. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String)
  49. @objc optional func mainToolDidClicked(_ toolController: KMToolbarController, _ beforeType: KMToolbarViewType, _ type: KMToolbarViewType, _ item: KMToolbarItemView, _ pages: [Int])
  50. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareAction toolbarItem: KMToolbarItemView)
  51. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareDocument item:NSMenuItem)
  52. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareFlatten item:NSMenuItem)
  53. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareOriginalPDF item:NSMenuItem)
  54. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, scanOCRModel selectedTag:Int)
  55. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, itemDidClick toolbarItem: KMToolbarItemView)
  56. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, searchAction searchString: String, forward: Bool)
  57. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, findSearchAction searchString: String, forward: Bool)
  58. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, findSearchAllAction searchString: String, forward: Bool)
  59. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, menuItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?)
  60. }
  61. class KMToolbarController: NSViewController {
  62. static let mainToolBarHeight: Float = 48
  63. static let childToolBarHeightValue: Float = 41
  64. static let findBarHeight: Float = 25
  65. @IBOutlet weak var mainToolBarBox: NSBox!
  66. @IBOutlet weak var childToolBarBox: NSBox!
  67. @IBOutlet weak var secondaryToolBarBox: NSBox!
  68. @IBOutlet weak var mainToolBarHeight: NSLayoutConstraint!
  69. @IBOutlet weak var childToolBarHeight: NSLayoutConstraint!
  70. @IBOutlet weak var secondaryToolBarHeight: NSLayoutConstraint!
  71. @IBOutlet weak var bottomOffset: NSLayoutConstraint!
  72. var mainToolBarView: KMToolbarViewController?
  73. var childToolBarView: KMToolbarViewController?
  74. var _toolbarType : KMToolbarViewType = KMToolbarViewType.None
  75. weak var mainViewController: KMMainViewController?
  76. open weak var delegate: KMToolbarControllerDelegate?
  77. var toolbarItems : [NSToolbarItem.Identifier : Any] = [:]
  78. var listView : CPDFListView?
  79. var lastItemBox : KMToolbarItemView = KMToolbarItemView()
  80. var lastChildItemBox : KMToolbarItemView = KMToolbarItemView()
  81. var popover: NSPopover?
  82. //find seaarch
  83. @IBOutlet weak var findSearchView: KMSearchFindView!
  84. // 是否显示所有注释
  85. private var _isShowAllAnnotations = true
  86. // 是否显示所有注释
  87. var isShowAllAnnotations: Bool {
  88. get {
  89. return _isShowAllAnnotations
  90. }
  91. set {
  92. _isShowAllAnnotations = newValue
  93. self.childToolBarView?.isShowAllAnnotations = newValue
  94. self.findChildItem(KMToolbarShowToolbarItemIdentifier)?.isSelected = !newValue
  95. }
  96. }
  97. private var _ignoreCurrentAnnotationTypeChange = false
  98. var ignoreCurrentAnnotationTypeChange: Bool {
  99. get {
  100. return self._ignoreCurrentAnnotationTypeChange
  101. }
  102. set {
  103. self._ignoreCurrentAnnotationTypeChange = newValue
  104. self.childToolBarView?.ignoreCurrentAnnotationTypeChange = self.ignoreCurrentAnnotationTypeChange
  105. // 忽略后重置 这个属性是基于单次添加,所以使用后会重置
  106. // self._ignoreCurrentAnnotationTypeChange = false
  107. }
  108. }
  109. var mainToolBarisVisable: Bool {
  110. get {
  111. return self.mainToolBarBox.isHidden == false
  112. }
  113. set {
  114. let needShow = newValue
  115. let currentIsShow = self.mainToolBarisVisable
  116. let toolBarH: Float = Self.mainToolBarHeight
  117. if needShow {
  118. if currentIsShow { // 当前已显示
  119. return
  120. }
  121. let height = self.view.frame.size.height
  122. self.mainToolBarBox.animator().isHidden = false
  123. self.mainToolBarHeight.animator().constant = toolBarH.cgFloat
  124. self.delegate?.toolbarController?(self, heightOffsetChange: Float(height)+toolBarH, animated: true)
  125. } else {
  126. if currentIsShow == false { // 当前已隐藏
  127. return
  128. }
  129. let height = self.view.frame.size.height
  130. self.mainToolBarBox.isHidden = true
  131. self.mainToolBarHeight.animator().constant = 0
  132. self.delegate?.toolbarController?(self, heightOffsetChange: Float(height)-toolBarH, animated: true)
  133. }
  134. }
  135. }
  136. override func viewDidLoad() {
  137. super.viewDidLoad()
  138. // Do view setup here.
  139. updateViewColor()
  140. mainToolBarView = KMToolbarViewController.init()
  141. mainToolBarView?.view.wantsLayer = true
  142. mainToolBarView?.view.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  143. mainToolBarBox.contentView = mainToolBarView?.view
  144. mainToolBarView?.delegate = self
  145. if let data = self.listView {
  146. mainToolBarView?.pdfView = data
  147. }
  148. childToolBarView = KMToolbarViewController.init()
  149. childToolBarView?.view.wantsLayer = true
  150. childToolBarView?.view.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  151. childToolBarBox.contentView = childToolBarView?.view
  152. childToolBarView?.view.frame = childToolBarBox.frame
  153. childToolBarView?.delegate = self
  154. childToolBarView?.view.autoresizingMask = [.width, .height]
  155. if let data = self.listView {
  156. childToolBarView?.pdfView = data
  157. }
  158. toolbarType = .None
  159. mainToolBarView?.toolbarType = .Main
  160. mainToolBarView?.reloadateToolbar()
  161. NotificationCenter.default.addObserver(self, selector: #selector(selectedShapAnnotationChangeNotification(_:)), name: NSNotification.Name.init(rawValue: "KMSelectedShapAnnotationChangeNotification"), object: nil)
  162. NotificationCenter.default.addObserver(self, selector: #selector(PDFChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "PDFChangedNotification"), object: nil)
  163. NotificationCenter.default.addObserver(self, selector: #selector(PDFTextEditPDFChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "KMPDFTextEditPDFChangedNotification"), object: nil)
  164. }
  165. // MARK:
  166. func updateViewColor() -> Void {
  167. }
  168. // MARK: Setter&Getter
  169. var toolbarType: KMToolbarViewType {
  170. set {
  171. if newValue == _toolbarType {
  172. if newValue == .Move || newValue == .Magnify || newValue == .Select || newValue == .SelectZoom {
  173. _toolbarType = .Annatiton
  174. } else {
  175. _toolbarType = .None
  176. }
  177. } else {
  178. _toolbarType = newValue
  179. }
  180. mainToolBarView?.toolbar?.isHidden = false
  181. secondaryToolBarBox.isHidden = true
  182. secondaryToolBarHeight.constant = 51
  183. let mainToolbarH = self.mainToolBarisVisable ? Self.mainToolBarHeight : 0
  184. if self.mainViewController!.isReadMode {
  185. if (_toolbarType == .Annatiton) {
  186. let childToolbarH = Self.childToolBarHeightValue
  187. bottomOffset.constant = childToolbarH.cgFloat
  188. mainToolBarHeight.constant = 0
  189. childToolBarBox.isHidden = false
  190. self.delegate?.toolbarController?(self, heightOffsetChange: 41, animated: false)
  191. } else {
  192. mainToolBarHeight.constant = 0
  193. bottomOffset.constant = 0
  194. childToolBarBox.isHidden = true
  195. mainToolBarView?.toolbar?.isHidden = true
  196. self.delegate?.toolbarController?(self, heightOffsetChange: 0, animated: false)
  197. }
  198. } else {
  199. if _toolbarType == .None {
  200. bottomOffset.constant = 0
  201. childToolBarBox.isHidden = true
  202. self.delegate?.toolbarController?(self, heightOffsetChange: mainToolbarH, animated: false)
  203. } else if _toolbarType == .Page || _toolbarType == .LeftPanel || _toolbarType == .redact {
  204. bottomOffset.constant = 0
  205. childToolBarBox.isHidden = true
  206. self.delegate?.toolbarController?(self, heightOffsetChange: mainToolbarH, animated: false)
  207. } else {
  208. let childToolbarH = Self.childToolBarHeightValue
  209. bottomOffset.constant = childToolbarH.cgFloat
  210. childToolBarBox.isHidden = false
  211. self.delegate?.toolbarController?(self, heightOffsetChange: mainToolbarH + childToolbarH, animated: false)
  212. }
  213. }
  214. childToolBarView?.toolbarType = _toolbarType
  215. childToolBarView?.reloadateToolbar()
  216. }
  217. get {
  218. return _toolbarType
  219. }
  220. }
  221. // MARK: -
  222. // MARK: Public Methods
  223. // 取消选中 [只是取消选中状态,不会执行具体的方法]
  224. public func cancelSelected(_ identifier: String) {
  225. if (isMainToolItem(identifier)) {
  226. self.findMainItem(identifier)?.isSelected = false
  227. return
  228. }
  229. self.findChildItem(identifier)?.isSelected = false
  230. }
  231. public func clickItem(_ identifier: String) {
  232. self.selectItem(identifier)
  233. }
  234. public func selectItem(_ identifier: String) {
  235. if (isMainToolItem(identifier)) {
  236. self.selectMainItem(identifier)
  237. return
  238. }
  239. if let parentItem = self.findItem(parentIdentifier(identifier)), parentItem.isSelected {
  240. if (self.trySelectChildItem(identifier)) { // 尝试去选子工具栏
  241. return
  242. }
  243. }
  244. // 当前主工具栏未开启
  245. let mainToolIdentifier = parentIdentifier(identifier)
  246. if (mainToolIdentifier.isEmpty) {
  247. return
  248. }
  249. // 找到主工具栏
  250. self.selectMainItem(mainToolIdentifier)
  251. DispatchQueue.main.async {
  252. // 再去选择子工具栏
  253. self.selectChildItem(identifier)
  254. }
  255. }
  256. public func findItem(_ identifier: String) -> KMToolbarItemView? {
  257. if (isMainToolItem(identifier)) {
  258. return self.findMainItem(identifier)
  259. }
  260. return self.findChildItem(identifier)
  261. }
  262. // MARK: -
  263. // MARK: private Methods
  264. private func selectMainItem(_ identifier: String) {
  265. if (self.mainToolBarView?.toolbarItems == nil) {
  266. return
  267. }
  268. var item: KMToolbarItemView?
  269. for (key, value) in self.mainToolBarView?.toolbarItems ?? [:] {
  270. if (key == identifier) {
  271. item = (value as? KMToolbarItemView)
  272. break
  273. }
  274. }
  275. // 主工具栏 item
  276. if let data = item {
  277. if (identifier == KMDocumentAnnotationToolbarItemIdentifier ||
  278. identifier == KMDocumentPageToolbarItemIdentifier ||
  279. identifier == KMDocumentConversonToolbarItemIdentifier ||
  280. identifier == KMDocumentEditToolbarItemIdentifier ||
  281. identifier == KMDocumentFormToolbarItemIdentifier ||
  282. identifier == KMDocumentFillSginToolbarItemIdentifier ||
  283. identifier == KMDocumentToolToolbarItemIdentifier) {
  284. self.mainToolBarView?.leftControllButtonAction(item: data.clickButton)
  285. } else if (identifier == KMRightControlToolbarItemIdentifier) {
  286. self.mainToolBarView?.itemAction(data)
  287. } else if (identifier == KMLeftControlToolbarItemIdentifier) {
  288. self.mainToolBarView?.itemAction(data)
  289. } else if identifier == KMDocumentRedactToolbarItemIdentifier {
  290. self.mainToolBarView?.itemAction(data)
  291. } else if identifier == KMDocumentScanOCRToolbarItemIdentifier {
  292. self.mainToolBarView?.itemAction(data)
  293. } else if identifier == KMDocumentAIToolsToolbarItemIdentifier {
  294. self.mainToolBarView?.itemAction(data)
  295. }
  296. return
  297. }
  298. }
  299. private func trySelectChildItem(_ identifier: String) -> Bool {
  300. // 子工具栏 item [当前主工具栏已开启]
  301. var item: KMToolbarItemView?
  302. if let items = self.childToolBarView?.toolbarItems, items.count > 0 {
  303. for (key, value) in items {
  304. if (key == identifier) {
  305. item = (value as? KMToolbarItemView)
  306. break
  307. }
  308. }
  309. // 子工具栏[找到]
  310. if (item != nil) {
  311. self.selectChildItem(identifier)
  312. return true
  313. }
  314. }
  315. return false
  316. }
  317. private func selectChildItem(_ identifier: String) {
  318. // 子工具栏 item [当前主工具栏已开启]
  319. var item: KMToolbarItemView?
  320. if let items = self.childToolBarView?.toolbarItems, items.count > 0 {
  321. for (key, value) in items {
  322. if (key == identifier) {
  323. item = (value as? KMToolbarItemView)
  324. break
  325. }
  326. }
  327. // 子工具栏[找到]
  328. if let data = item {
  329. if (identifier == KMToolbarMoveToolModeItemIdentifier ||
  330. identifier == KMToolbarMagnifyToolModeItemIdentifier ||
  331. identifier == KMToolbarSelectToolModeItemIdentifier ||
  332. identifier == KMToolbarZoomToSelectionItemIdentifier) {
  333. self.childToolBarView?.leftControllButtonAction(item: data.clickButton)
  334. } else if identifier == KMToolbarViewSettingIdentifier ||
  335. identifier == KMAnnotationImageToolbarItemIdentifier ||
  336. identifier == KMAnnotationTableToolbarItemIdentifier {
  337. self.childToolBarView?.itemAction(data)
  338. } else {
  339. self.childToolBarView?.changeAnnotationMode(item: data.clickButton)
  340. }
  341. }
  342. }
  343. }
  344. private func findMainItem(_ identifier: String) -> KMToolbarItemView? {
  345. if (self.mainToolBarView?.toolbarItems == nil) {
  346. return nil
  347. }
  348. var item: KMToolbarItemView?
  349. if (isMainToolItem(identifier)) {
  350. for (key, value) in self.mainToolBarView?.toolbarItems ?? [:] {
  351. if (key == identifier) {
  352. item = (value as? KMToolbarItemView)
  353. break
  354. }
  355. }
  356. return item
  357. }
  358. return nil
  359. }
  360. private func findChildItem(_ identifier: String) -> KMToolbarItemView? {
  361. if (self.childToolBarView?.toolbarItems == nil || self.childToolBarView!.toolbarItems.count == 0) {
  362. return nil
  363. }
  364. var item: KMToolbarItemView?
  365. for (key, value) in self.childToolBarView?.toolbarItems ?? [:] {
  366. if (key == identifier) {
  367. item = (value as? KMToolbarItemView)
  368. break
  369. }
  370. }
  371. return item
  372. }
  373. func exitPageEdit() -> Void {
  374. if let data = self.mainToolBarView {
  375. resetToolbarViewController(data)
  376. exitPageEditToolbarViewController(data)
  377. }
  378. }
  379. func exitTextEdit() -> Void {
  380. secondaryToolBarBox.isHidden = true
  381. secondaryToolBarHeight.constant = 50
  382. bottomOffset.constant = 51
  383. childToolBarBox.isHidden = false
  384. self.delegate?.toolbarController?(self, heightOffsetChange: 102, animated: false)
  385. }
  386. func enterWatermarkAdjective(_ type: KMToolbarType, _ itemTitles: Array<Array<String>>) {
  387. self.toolbarType = .None
  388. let topBarView = KMWatermarkAdjectiveTopBarView()
  389. topBarView.frame = (self.mainToolBarView?.toolbar?.bounds) ?? .zero
  390. self.mainToolBarView?.toolbar?.addSubview(topBarView)
  391. topBarView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  392. topBarView.wantsLayer = true
  393. topBarView.layer?.backgroundColor = NSColor.white.cgColor
  394. topBarView.isCanApply(can: false)
  395. var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
  396. for items in itemTitles {
  397. var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
  398. for title in items {
  399. let model = KMWatermarkAdjectiveTopBarItemModel()
  400. model.iconName = ""
  401. model.itemTitle = title
  402. array.append(model)
  403. }
  404. itemModels.append(array)
  405. }
  406. topBarView.initItemData(itemArrays: itemModels)
  407. topBarView.cancelClick = { [unowned self] in
  408. self.delegate?.clickChildTool?(type: type, index: 1)
  409. }
  410. topBarView.applyClick = { [unowned self] in
  411. self.delegate?.clickChildTool?(type: type, index: 2)
  412. }
  413. topBarView.itemClick = { [unowned self] section,item in
  414. self.delegate?.clickChildTool?(type: type, index: 3+item)
  415. }
  416. }
  417. func exitWatermarkAdjective() {
  418. let topBarView = self.mainToolBarView?.toolbar?.subviews.last
  419. if (topBarView == nil || topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self) == false) {
  420. return
  421. }
  422. topBarView?.removeFromSuperview()
  423. }
  424. func fetchTopBarView() -> NSView? {
  425. let topBarView = self.mainToolBarView?.toolbar?.subviews.last
  426. if (topBarView == nil) {
  427. return nil
  428. }
  429. if (topBarView?.isKind(of: KMWatermarkAdjectiveTopBarView.self) == true) {
  430. return topBarView
  431. }
  432. if (topBarView?.isKind(of: KMRedactTopToolBar.self) == true) {
  433. return topBarView
  434. }
  435. return nil
  436. }
  437. func enterRedact() {
  438. self.toolbarType = .None
  439. let topBarView = KMRedactTopToolBar()
  440. topBarView.frame = (self.mainToolBarView?.toolbar?.bounds) ?? .zero
  441. self.mainToolBarView?.toolbar?.addSubview(topBarView)
  442. topBarView.autoresizingMask = [.width, .height]
  443. topBarView.wantsLayer = true
  444. topBarView.layer?.backgroundColor = NSColor.white.cgColor
  445. topBarView.selectItem(0)
  446. self.delegate?.clickChildTool?(type: .redact, index: 4)
  447. topBarView.itemClick = { [weak self] index in
  448. self?.delegate?.clickChildTool?(type: .redact, index: index)
  449. }
  450. }
  451. func exitRedact() {
  452. let topBarView = self.mainToolBarView?.toolbar?.subviews.last
  453. if (topBarView == nil || topBarView?.isKind(of: KMRedactTopToolBar.self) == false) {
  454. return
  455. }
  456. topBarView?.removeFromSuperview()
  457. }
  458. // MARK: NSNotification
  459. @objc func selectedShapAnnotationChangeNotification(_ notification: Notification) -> Void {
  460. }
  461. @objc func PDFChangedNotification(_ notification: Notification) -> Void {
  462. }
  463. @objc func PDFTextEditPDFChangedNotification(_ notification: Notification) -> Void {
  464. }
  465. // MARK:
  466. func resetToolbarViewController(_ toolbarViewController: KMToolbarViewController) -> Void {
  467. self.toolbarType = .None
  468. self.mainToolBarView?.resetToolbar()
  469. self.mainViewController?.listView.annotationType = .unkown
  470. }
  471. func exitPageEditToolbarViewController(_ toolbarViewController: KMToolbarViewController) -> Void {
  472. }
  473. // MARK: item actions
  474. @objc func cropItemAction(sender: NSMenuItem) {
  475. if (sender.tag == 0) { /// 裁剪当前页面
  476. self.delegate?.clickChildTool?(type: .crop, index: 1)
  477. return
  478. }
  479. if (sender.tag == 1) { /// 裁剪所有页面
  480. self.delegate?.clickChildTool?(type: .crop, index: 2)
  481. return
  482. }
  483. /// 自定义裁剪区域
  484. self.delegate?.clickChildTool?(type: .crop, index: 3)
  485. }
  486. @objc func secureItemAction(sender: NSMenuItem) {
  487. var index: Int = 1
  488. if (sender.tag == 1) { /// 删除安全性设置
  489. index = 2
  490. }
  491. self.delegate?.clickChildTool?(type: .secure, index: index)
  492. }
  493. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  494. super.interfaceThemeDidChanged(appearance)
  495. self.mainToolBarView?.interfaceThemeDidChanged(appearance)
  496. self.childToolBarView?.interfaceThemeDidChanged(appearance)
  497. }
  498. }
  499. //MARK: Find Search
  500. extension KMToolbarController {
  501. func showFindBar() {
  502. let height: Float = self.fetchHeight(type: _toolbarType)
  503. self.delegate?.toolbarController?(self, heightOffsetChange: height, animated: false)
  504. self.findSearchView.findField.becomeFirstResponder()
  505. self.findSearchView.doneAction = { [unowned self] view in
  506. self.exitFindBar()
  507. }
  508. self.findSearchView.searchAction = { [weak self] view, searchString, forward in
  509. if let data = self?.mainToolBarView {
  510. self?.toolbarViewController(data, findSearchAction: searchString, forward: forward)
  511. }
  512. return true
  513. }
  514. self.findSearchView.showAllAction = { [unowned self] view, searchString, forward in
  515. if let data = self.mainToolBarView {
  516. self.toolbarViewController(data, findSearchAllAction: searchString, forward: forward)
  517. }
  518. }
  519. }
  520. func showFindString(forward: Bool) {
  521. if let data = self.mainToolBarView {
  522. self.toolbarViewController(data, findSearchAction: self.findSearchView.findString ?? "", forward: forward)
  523. }
  524. }
  525. func showAllAction() {
  526. if let data = self.mainToolBarView {
  527. self.toolbarViewController(data, findSearchAllAction: self.findSearchView.findString ?? "", forward: true)
  528. }
  529. }
  530. func exitFindBar() {
  531. let height: Float = self.fetchHeight(type: _toolbarType, isShow: false)
  532. self.delegate?.toolbarController?(self, heightOffsetChange: height, animated: false)
  533. }
  534. func fetchHeight(type: KMToolbarViewType, isShow: Bool = true) -> Float {
  535. var height: Float = 0.0
  536. let mainToolbarH = self.mainToolBarisVisable ? Self.mainToolBarHeight : 0
  537. let childToolbarH = Self.childToolBarHeightValue
  538. let findBarH = Self.findBarHeight
  539. if isShow {
  540. if _toolbarType == .None {
  541. bottomOffset.constant = findBarH.cgFloat
  542. height = mainToolbarH
  543. } else if _toolbarType == .Page || _toolbarType == .LeftPanel || _toolbarType == .redact {
  544. bottomOffset.constant = findBarH.cgFloat
  545. height = mainToolbarH
  546. } else {
  547. bottomOffset.constant = childToolbarH.cgFloat + findBarH.cgFloat
  548. height = mainToolbarH + childToolbarH
  549. }
  550. height = height + findBarH
  551. } else {
  552. if _toolbarType == .None {
  553. bottomOffset.constant = 0
  554. height = mainToolbarH
  555. } else if _toolbarType == .Page || _toolbarType == .LeftPanel || _toolbarType == .redact {
  556. bottomOffset.constant = 0
  557. height = mainToolbarH
  558. } else {
  559. bottomOffset.constant = childToolbarH.cgFloat
  560. height = mainToolbarH + childToolbarH
  561. }
  562. }
  563. return height
  564. }
  565. }
  566. extension KMToolbarController: KMToolbarViewControllerDelegate {
  567. func changeAnnotationModeAction(item: KMToolbarClickButton) {
  568. let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
  569. if (type.isAdvanced() == false) {
  570. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  571. self.toolbarType = .Annatiton
  572. }
  573. self.delegate?.changeAnnotationModeAction?(item: item)
  574. return
  575. }
  576. // 高级功能
  577. Task { @MainActor in
  578. //文字编辑 图片编辑 选中按钮逻辑(只能同时选中其中一个)
  579. if type == .addText || type == .addImage {
  580. let boxItem = item.clickObject as? KMToolbarItemView
  581. if let data = boxItem {
  582. if self.lastChildItemBox != data {
  583. self.lastChildItemBox.isSelected = false
  584. data.isSelected = true
  585. } else {
  586. data.isSelected = !data.isSelected
  587. }
  588. self.lastChildItemBox = data
  589. }
  590. }
  591. if self.toolbarType == .Magnify || self.toolbarType == .Move || self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  592. self.toolbarType = .Annatiton
  593. }
  594. self.trackEvent(type: type)
  595. self.delegate?.changeAnnotationModeAction?(item: item)
  596. }
  597. }
  598. func trackEvent(type: CAnnotationType) {
  599. var eventStr = "Btn_SubTbr_Fill&Sign_Check"
  600. switch type {
  601. case .signTure:
  602. eventStr = "Btn_SubTbr_Fill&Sign_Check"
  603. case .signFalse:
  604. eventStr = "Btn_SubTbr_Fill&Sign_X"
  605. case .signDate:
  606. eventStr = "Btn_SubTbr_Fill&Sign_Date"
  607. case .signDot:
  608. eventStr = "Btn_SubTbr_Fill&Sign_Dot"
  609. case .signCircle:
  610. eventStr = "Btn_SubTbr_Fill&Sign_Circle"
  611. case .signLine:
  612. eventStr = "Btn_SubTbr_Fill&Sign_Line"
  613. default:
  614. eventStr = ""
  615. break
  616. }
  617. if eventStr.count > 1{
  618. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_Fill&Sign", withProperties: ["SubTbr_Btn": eventStr])
  619. }
  620. }
  621. func toolbarViewController(_ viewController: KMToolbarViewController, clickMode toolMode: KMToolbarViewType, toolbar toolbarItem: KMToolbarItemView, _ pages: [Int]) {
  622. let beforeModel = KMToolbarViewType(rawValue: self.lastItemBox.clickButton.tag) ?? .None
  623. if self.lastItemBox != nil {
  624. if (toolbarItem.isSelected && toolMode.isToolMode()) {
  625. // no nothings
  626. } else {
  627. self.lastItemBox.isSelected = false
  628. }
  629. }
  630. if self.lastChildItemBox != nil {
  631. if toolMode == .editPDF {
  632. if !self.lastItemBox.isSelected {
  633. self.lastChildItemBox.isSelected = false
  634. }
  635. }
  636. }
  637. if toolMode != .Magnify && toolMode != .Move && toolMode != .Select && toolMode != .SelectZoom && toolMode != .LeftPanel && toolMode != .RightPanel {
  638. if(toolMode == self.toolbarType) {
  639. toolbarItem.isSelected = false
  640. } else {
  641. toolbarItem.isSelected = true
  642. self.lastItemBox = toolbarItem
  643. self.trackEvent(toolType: toolMode)
  644. }
  645. } else {
  646. if(toolMode != self.toolbarType && toolMode != .LeftPanel) {
  647. let item : KMToolbarItemView = (self.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentAnnotationToolbarItemIdentifier))!
  648. item.isSelected = true
  649. self.lastItemBox = item
  650. if toolMode == .Select {
  651. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_Tools", withProperties: ["SubTbr_Btn" : "Btn_SubTbr_Tools_ContentSelection"])
  652. }
  653. if toolMode == .SelectZoom {
  654. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_Tools", withProperties: ["SubTbr_Btn" : "Btn_SubTbr_Tools_Zoom"])
  655. }
  656. } else if (toolMode == .LeftPanel) {
  657. if(toolMode == self.toolbarType) {
  658. toolbarItem.isSelected = false
  659. } else {
  660. toolbarItem.isSelected = true
  661. }
  662. }
  663. }
  664. self.toolbarType = toolMode
  665. self.delegate?.mainToolDidClicked?(self, beforeModel, toolMode, toolbarItem, pages)
  666. }
  667. func trackEvent(toolType type: KMToolbarViewType) -> Void {
  668. if (type == .Annatiton) {
  669. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Tools"])
  670. } else if (type == .editPDF) {
  671. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_EditPDF"])
  672. } else if (type == .Page) {
  673. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_PageEdit"])
  674. } else if (type == .Conversion) {
  675. } else if (type == .Tool) {
  676. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Editor"])
  677. } else if (type == .Conversion) {
  678. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Converter"])
  679. } else if (type == .Form) {
  680. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Form"])
  681. } else if (type == .FillSign) {
  682. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Fill & Sign"])
  683. } else if (type == .redact) {
  684. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Redact"])
  685. }
  686. }
  687. func showPDFLayoutModeAction(show: Bool) {
  688. self.delegate?.showPDFLayoutModeAction?(show: show)
  689. }
  690. func toolbarViewController(_ viewController: KMToolbarViewController, clickChaildToolType: KMToolbarType, toolbar toolbarItem: KMToolbarItemView) {
  691. if (clickChaildToolType == .crop) {
  692. let titles = [NSLocalizedString("Crop Current Page - White Margins", comment: ""), NSLocalizedString("Crop All Pages - Auto", comment: "")]
  693. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  694. let createFilePopover: NSPopover = NSPopover.init()
  695. createFilePopover.contentViewController = vc
  696. createFilePopover.animates = true
  697. createFilePopover.behavior = .transient
  698. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  699. createFilePopover.show(relativeTo: NSZeroRect, of: toolbarItem, preferredEdge: .maxY)
  700. self.popover = createFilePopover
  701. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  702. self?.popover?.close()
  703. self?.popover = nil
  704. if downEntered {
  705. if (count == titles.first) {
  706. self?.delegate?.clickChildTool?(type: .crop, index: 1)
  707. } else {
  708. self?.delegate?.clickChildTool?(type: .crop, index: 2)
  709. }
  710. }
  711. }
  712. } else if (clickChaildToolType == .bates || clickChaildToolType == .headerAndFooter || clickChaildToolType == .background || clickChaildToolType == .watermark) {
  713. toolbarItem.isSelected = false
  714. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  715. } else if (clickChaildToolType == .redact) {
  716. toolbarItem.isSelected = false
  717. self.enterRedact()
  718. } else if (clickChaildToolType == .compress) {
  719. self.delegate?.clickChildTool?(type: .compress, index: 0)
  720. } else if (clickChaildToolType == .secure) {
  721. // toolbarItem.isSelected = fals
  722. let titles = [NSLocalizedString("Set Passwords", comment: ""), NSLocalizedString("Remove Security", comment: "")]
  723. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  724. let createFilePopover: NSPopover = NSPopover.init()
  725. createFilePopover.contentViewController = vc
  726. createFilePopover.animates = true
  727. createFilePopover.behavior = .transient
  728. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  729. 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)
  730. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  731. if downEntered {
  732. if (count == titles.first) {
  733. self?.delegate?.clickChildTool?(type: .secure, index: 1)
  734. } else {
  735. self?.delegate?.clickChildTool?(type: .secure, index: 2)
  736. }
  737. }
  738. }
  739. } else if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(clickChaildToolType.rawValue)) { /// 转档
  740. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  741. } else if (clickChaildToolType == .merge) {
  742. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  743. }
  744. }
  745. func toolbarViewController(_ viewController: KMToolbarViewController, zoomModel selectedTag: Int) {
  746. self.delegate?.toolbarViewController?(viewController, zoomModel: selectedTag)
  747. }
  748. func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String) {
  749. self.delegate?.toolbarViewController?(viewController, zoomSting: zoomSting)
  750. }
  751. func changePDFViewZoomInAction() {
  752. self.delegate?.changePDFViewZoomInAction?()
  753. }
  754. func changePDFViewZoomOutAction() {
  755. self.delegate?.changePDFViewZoomOutAction?()
  756. }
  757. func changePDFViewGotoNextPageAction() {
  758. self.delegate?.changePDFViewGotoNextPageAction?()
  759. }
  760. func changePDFViewGoToPreviousPageAction() {
  761. self.delegate?.changePDFViewGoToPreviousPageAction?()
  762. }
  763. func changePDFViewGotoBackAction() {
  764. self.delegate?.changePDFViewGotoBackAction?()
  765. }
  766. func changePDFViewGoToForwardAction() {
  767. self.delegate?.changePDFViewGoToForwardAction?()
  768. }
  769. func aiTranslationPDFFileAction() {
  770. self.delegate?.aiTranslationPDFFileAction?()
  771. }
  772. func toolbarViewController(_ viewController: KMToolbarViewController, shareAction toolbarItem: KMToolbarItemView) {
  773. self.delegate?.toolbarViewController?(viewController, shareAction: toolbarItem)
  774. }
  775. func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {
  776. self.delegate?.toolbarViewController?(viewController, shareDocument: item)
  777. }
  778. func toolbarViewController(_ viewController: KMToolbarViewController, shareFlatten item: NSMenuItem) {
  779. self.delegate?.toolbarViewController?(viewController, shareFlatten: item)
  780. }
  781. func toolbarViewController(_ viewController: KMToolbarViewController, shareOriginalPDF item: NSMenuItem) {
  782. self.delegate?.toolbarViewController?(viewController, shareOriginalPDF: item)
  783. }
  784. func toolbarViewController(_ viewController: KMToolbarViewController, scanOCRModel selectedTag: Int) {
  785. self.delegate?.toolbarViewController?(viewController, scanOCRModel: selectedTag)
  786. }
  787. func toolbarViewController(_ viewController: KMToolbarViewController, itemDidClick toolbarItem: KMToolbarItemView) {
  788. let itemIdentifier = toolbarItem.itemIdentifier
  789. if itemIdentifier != KMRightControlToolbarItemIdentifier {
  790. if self.lastItemBox != nil {
  791. if (toolbarItem.isSelected) {
  792. // no nothings
  793. } else {
  794. let p_itemIdentifier = parentIdentifier(itemIdentifier ?? "")
  795. if p_itemIdentifier != self.lastItemBox.itemIdentifier {
  796. self.lastItemBox.isSelected = false
  797. }
  798. }
  799. }
  800. }
  801. self.delegate?.toolbarViewController?(viewController, itemDidClick: toolbarItem)
  802. }
  803. func toolbarViewController(_ viewController: KMToolbarViewController, searchAction searchString: String, forward: Bool) {
  804. self.delegate?.toolbarViewController?(viewController, searchAction: searchString, forward: forward)
  805. }
  806. func toolbarViewController(_ viewController: KMToolbarViewController, findSearchAction searchString: String, forward: Bool) {
  807. self.delegate?.toolbarViewController?(viewController, findSearchAction: searchString, forward: forward)
  808. }
  809. func toolbarViewController(_ viewController: KMToolbarViewController, findSearchAllAction searchString: String, forward: Bool) {
  810. self.delegate?.toolbarViewController?(viewController, findSearchAllAction: searchString, forward: forward)
  811. }
  812. func toolbarViewController(_ viewController: KMToolbarViewController, menuItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?) {
  813. let itemIdentifier = toolbarItem.itemIdentifier
  814. if itemIdentifier != KMRightControlToolbarItemIdentifier {
  815. if self.lastItemBox != nil {
  816. if (toolbarItem.isSelected) {
  817. // no nothings
  818. } else {
  819. let p_itemIdentifier = parentIdentifier(itemIdentifier ?? "")
  820. if p_itemIdentifier != self.lastItemBox.itemIdentifier {
  821. self.lastItemBox.isSelected = false
  822. }
  823. }
  824. }
  825. }
  826. self.delegate?.toolbarViewController?(viewController, menuItemDidClick: toolbarItem, index: index, info: info)
  827. }
  828. }
  829. extension KMToolbarController {
  830. func updataItemVisible() {
  831. guard let isCompareModel = self.mainViewController?.isCompareModel else { return }
  832. if isCompareModel {
  833. self.toolbarType = .None
  834. self.mainToolBarView?.isEnable(isEnable: false)
  835. } else {
  836. self.mainToolBarView?.isEnable()
  837. }
  838. }
  839. }