KMToolbarController.swift 41 KB

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