KMToolbarController.swift 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. }
  666. }
  667. if self.lastItemBox != nil {
  668. if (toolbarItem.isSelected && toolMode.isToolMode()) {
  669. // no nothings
  670. } else {
  671. self.lastItemBox.isSelected = false
  672. }
  673. }
  674. if self.lastChildItemBox != nil {
  675. if toolMode == .editPDF {
  676. if !self.lastItemBox.isSelected {
  677. self.lastChildItemBox.isSelected = false
  678. }
  679. }
  680. }
  681. if toolMode != .Magnify && toolMode != .Move && toolMode != .Select && toolMode != .SelectZoom && toolMode != .LeftPanel && toolMode != .RightPanel {
  682. if(toolMode == self.toolbarType) {
  683. toolbarItem.isSelected = false
  684. } else {
  685. toolbarItem.isSelected = true
  686. self.lastItemBox = toolbarItem
  687. self.trackEvent(toolType: toolMode)
  688. }
  689. } else {
  690. if(toolMode != self.toolbarType && toolMode != .LeftPanel) {
  691. let item : KMToolbarItemView = (self.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentAnnotationToolbarItemIdentifier))!
  692. item.isSelected = true
  693. self.lastItemBox = item
  694. if toolMode == .Select {
  695. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_Tools", withProperties: ["SubTbr_Btn" : "Btn_SubTbr_Tools_ContentSelection"])
  696. }
  697. if toolMode == .SelectZoom {
  698. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_Tools", withProperties: ["SubTbr_Btn" : "Btn_SubTbr_Tools_Zoom"])
  699. }
  700. } else if (toolMode == .LeftPanel) {
  701. if(toolMode == self.toolbarType) {
  702. toolbarItem.isSelected = false
  703. } else {
  704. toolbarItem.isSelected = true
  705. }
  706. }
  707. }
  708. self.toolbarType = toolMode
  709. self.delegate?.mainToolDidClicked?(self, beforeModel, toolMode, toolbarItem, pages)
  710. }
  711. func trackEvent(toolType type: KMToolbarViewType) -> Void {
  712. if (type == .Annatiton) {
  713. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Tools"])
  714. } else if (type == .editPDF) {
  715. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_EditPDF"])
  716. } else if (type == .Page) {
  717. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_PageEdit"])
  718. } else if (type == .Conversion) {
  719. } else if (type == .Tool) {
  720. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Editor"])
  721. } else if (type == .Conversion) {
  722. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Converter"])
  723. } else if (type == .Form) {
  724. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Form"])
  725. } else if (type == .FillSign) {
  726. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Fill & Sign"])
  727. } else if (type == .redact) {
  728. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn" : "Btn_Tbr_Redact"])
  729. }
  730. }
  731. func showPDFLayoutModeAction(show: Bool) {
  732. self.delegate?.showPDFLayoutModeAction?(show: show)
  733. }
  734. func toolbarViewController(_ viewController: KMToolbarViewController, clickChaildToolType: KMToolbarType, toolbar toolbarItem: KMToolbarItemView) {
  735. if (clickChaildToolType == .crop) {
  736. let titles = [NSLocalizedString("Crop Current Page - White Margins", comment: ""), NSLocalizedString("Crop All Pages - Auto", comment: "")]
  737. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  738. let createFilePopover: NSPopover = NSPopover.init()
  739. createFilePopover.contentViewController = vc
  740. createFilePopover.animates = true
  741. createFilePopover.behavior = .transient
  742. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  743. createFilePopover.show(relativeTo: NSZeroRect, of: toolbarItem, preferredEdge: .maxY)
  744. self.popover = createFilePopover
  745. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  746. self?.popover?.close()
  747. self?.popover = nil
  748. if downEntered {
  749. if (count == titles.first) {
  750. self?.delegate?.clickChildTool?(type: .crop, index: 1)
  751. } else {
  752. self?.delegate?.clickChildTool?(type: .crop, index: 2)
  753. }
  754. }
  755. }
  756. } else if (clickChaildToolType == .bates || clickChaildToolType == .headerAndFooter || clickChaildToolType == .background || clickChaildToolType == .watermark) {
  757. toolbarItem.isSelected = false
  758. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  759. } else if (clickChaildToolType == .redact) {
  760. toolbarItem.isSelected = false
  761. self.enterRedact()
  762. } else if (clickChaildToolType == .compress) {
  763. self.delegate?.clickChildTool?(type: .compress, index: 0)
  764. } else if (clickChaildToolType == .secure) {
  765. // toolbarItem.isSelected = fals
  766. let titles = [NSLocalizedString("Set Passwords", comment: ""), NSLocalizedString("Remove Security", comment: "")]
  767. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(titles)
  768. let createFilePopover: NSPopover = NSPopover.init()
  769. createFilePopover.contentViewController = vc
  770. createFilePopover.animates = true
  771. createFilePopover.behavior = .transient
  772. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  773. 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)
  774. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  775. if downEntered {
  776. if (count == titles.first) {
  777. self?.delegate?.clickChildTool?(type: .secure, index: 1)
  778. } else {
  779. self?.delegate?.clickChildTool?(type: .secure, index: 2)
  780. }
  781. }
  782. }
  783. } else if ((KMToolbarType.word.rawValue ... KMToolbarType.conversion_image.rawValue).contains(clickChaildToolType.rawValue)) { /// 转档
  784. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  785. } else if (clickChaildToolType == .merge) {
  786. self.delegate?.clickChildTool?(type: clickChaildToolType, index: 0)
  787. }
  788. }
  789. func toolbarViewController(_ viewController: KMToolbarViewController, zoomModel selectedTag: Int) {
  790. self.delegate?.toolbarViewController?(viewController, zoomModel: selectedTag)
  791. }
  792. func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String) {
  793. self.delegate?.toolbarViewController?(viewController, zoomSting: zoomSting)
  794. }
  795. func changePDFViewZoomInAction() {
  796. self.delegate?.changePDFViewZoomInAction?()
  797. }
  798. func changePDFViewZoomOutAction() {
  799. self.delegate?.changePDFViewZoomOutAction?()
  800. }
  801. func changePDFViewGotoNextPageAction() {
  802. self.delegate?.changePDFViewGotoNextPageAction?()
  803. }
  804. func changePDFViewGoToPreviousPageAction() {
  805. self.delegate?.changePDFViewGoToPreviousPageAction?()
  806. }
  807. func changePDFViewGotoBackAction() {
  808. self.delegate?.changePDFViewGotoBackAction?()
  809. }
  810. func changePDFViewGoToForwardAction() {
  811. self.delegate?.changePDFViewGoToForwardAction?()
  812. }
  813. func aiTranslationPDFFileAction() {
  814. self.delegate?.aiTranslationPDFFileAction?()
  815. }
  816. func toolbarViewController(_ viewController: KMToolbarViewController, shareAction toolbarItem: KMToolbarItemView) {
  817. self.delegate?.toolbarViewController?(viewController, shareAction: toolbarItem)
  818. }
  819. func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {
  820. self.delegate?.toolbarViewController?(viewController, shareDocument: item)
  821. }
  822. func toolbarViewController(_ viewController: KMToolbarViewController, shareFlatten item: NSMenuItem) {
  823. self.delegate?.toolbarViewController?(viewController, shareFlatten: item)
  824. }
  825. func toolbarViewController(_ viewController: KMToolbarViewController, shareOriginalPDF item: NSMenuItem) {
  826. self.delegate?.toolbarViewController?(viewController, shareOriginalPDF: item)
  827. }
  828. func toolbarViewController(_ viewController: KMToolbarViewController, scanOCRModel selectedTag: Int) {
  829. self.delegate?.toolbarViewController?(viewController, scanOCRModel: selectedTag)
  830. }
  831. func toolbarViewController(_ viewController: KMToolbarViewController, itemDidClick toolbarItem: KMToolbarItemView) {
  832. let itemIdentifier = toolbarItem.itemIdentifier
  833. if itemIdentifier != KMRightControlToolbarItemIdentifier {
  834. if self.lastItemBox != nil {
  835. if (toolbarItem.isSelected) {
  836. // no nothings
  837. } else {
  838. let p_itemIdentifier = parentIdentifier(itemIdentifier ?? "")
  839. if p_itemIdentifier != self.lastItemBox.itemIdentifier {
  840. self.lastItemBox.isSelected = false
  841. }
  842. }
  843. }
  844. }
  845. if toolbarItem.itemIdentifier != KMDocumentRedactToolbarItemIdentifier { // 标记秘文
  846. let mainC = self.delegate as? KMMainViewController
  847. var control: KMPDFRedactViewController?
  848. for childC in mainC?.children ?? [] {
  849. if (childC.isKind(of: KMPDFRedactViewController.self)) {
  850. control = (childC as! KMPDFRedactViewController)
  851. break
  852. }
  853. }
  854. if let annos = control?.redactPdfView.newAddAnnotation, annos.count > 0 {
  855. let alert = NSAlert()
  856. alert.informativeText = KMLocalizedString("There are unapplied redactions in this file. Exit will not save redaction.", nil)
  857. alert.addButton(withTitle: KMLocalizedString("Exit", nil))
  858. alert.addButton(withTitle: KMLocalizedString("Cancel", nil))
  859. let resp = alert.runModal()
  860. if resp != .alertFirstButtonReturn { // 取消
  861. return
  862. }
  863. }
  864. }
  865. self.delegate?.toolbarViewController?(viewController, itemDidClick: toolbarItem)
  866. }
  867. func toolbarViewController(_ viewController: KMToolbarViewController, searchAction searchString: String, forward: Bool) {
  868. self.delegate?.toolbarViewController?(viewController, searchAction: searchString, forward: forward)
  869. }
  870. func toolbarViewController(_ viewController: KMToolbarViewController, findSearchAction searchString: String, forward: Bool) {
  871. self.delegate?.toolbarViewController?(viewController, findSearchAction: searchString, forward: forward)
  872. }
  873. func toolbarViewController(_ viewController: KMToolbarViewController, findSearchAllAction searchString: String, forward: Bool) {
  874. self.delegate?.toolbarViewController?(viewController, findSearchAllAction: searchString, forward: forward)
  875. }
  876. func toolbarViewController(_ viewController: KMToolbarViewController, menuItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?) {
  877. let itemIdentifier = toolbarItem.itemIdentifier
  878. if itemIdentifier != KMRightControlToolbarItemIdentifier {
  879. if self.lastItemBox != nil {
  880. if (toolbarItem.isSelected) {
  881. // no nothings
  882. } else {
  883. let p_itemIdentifier = parentIdentifier(itemIdentifier ?? "")
  884. if p_itemIdentifier != self.lastItemBox.itemIdentifier {
  885. self.lastItemBox.isSelected = false
  886. }
  887. }
  888. }
  889. }
  890. if toolbarItem.itemIdentifier != KMDocumentRedactToolbarItemIdentifier { // 标记秘文
  891. let mainC = self.delegate as? KMMainViewController
  892. var control: KMPDFRedactViewController?
  893. for childC in mainC?.children ?? [] {
  894. if (childC.isKind(of: KMPDFRedactViewController.self)) {
  895. control = (childC as! KMPDFRedactViewController)
  896. break
  897. }
  898. }
  899. if let annos = control?.redactPdfView.newAddAnnotation, annos.count > 0 {
  900. let alert = NSAlert()
  901. alert.informativeText = KMLocalizedString("There are unapplied redactions in this file. Exit will not save redaction.", nil)
  902. alert.addButton(withTitle: KMLocalizedString("Exit", nil))
  903. alert.addButton(withTitle: KMLocalizedString("Cancel", nil))
  904. let resp = alert.runModal()
  905. if resp != .alertFirstButtonReturn { // 取消
  906. return
  907. }
  908. }
  909. }
  910. self.delegate?.toolbarViewController?(viewController, menuItemDidClick: toolbarItem, index: index, info: info)
  911. }
  912. func toolbarViewController(_ viewController: KMToolbarViewController, viewItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?) {
  913. self.delegate?.toolbarViewController?(viewController, viewItemDidClick: toolbarItem, index: index, info: info)
  914. }
  915. }
  916. extension KMToolbarController {
  917. func updataItemVisible() {
  918. guard let isCompareModel = self.mainViewController?.isCompareModel else { return }
  919. if isCompareModel {
  920. self.toolbarType = .None
  921. self.mainToolBarView?.isEnable(isEnable: false)
  922. } else {
  923. self.mainToolBarView?.isEnable()
  924. }
  925. }
  926. }