KMToolbarController.swift 40 KB

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