KMToolbarController.swift 45 KB

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