KMToolbarController.swift 48 KB

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