KMToolbarController.swift 41 KB

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