KMToolbarController.swift 41 KB

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