KMToolbarController.swift 46 KB

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