KMPDFEditToolbar.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. //
  2. // KMPDFEditToolbar.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/5/4.
  6. //
  7. import Cocoa
  8. enum KMPageEditType: Int {
  9. case none = 0
  10. case pageRange = 1
  11. case leftRotate
  12. case rightRotate
  13. case insert
  14. case extract
  15. case replace
  16. case split
  17. case reverse
  18. case copy
  19. case delete
  20. case zoomOut
  21. case zoomIn
  22. case paste = 100
  23. // static let none: KMPageEditType = KMPageEditType(rawValue: 0)!
  24. // static let paste: KMPageEditType = KMPageEditType(rawValue: 100)!
  25. static var insert_files: Int {
  26. get {
  27. return self.insert.rawValue + 1000
  28. }
  29. }
  30. static var insert_blank_page: Int {
  31. get {
  32. return self.insert.rawValue + 1001
  33. }
  34. }
  35. static var insert_custom_page: Int {
  36. get {
  37. return self.insert.rawValue + 1002
  38. }
  39. }
  40. static var extract_selected_pages: Int {
  41. get {
  42. return self.extract.rawValue + 2000
  43. }
  44. }
  45. static var split_selected_pages: Int {
  46. get {
  47. return self.split.rawValue + 3000
  48. }
  49. }
  50. }
  51. class KMPDFEditToolbar: NSView {
  52. private var toolBar_ = KMToolbarView.init(identifier: "KMPageEditIdentifier")
  53. var toolBar: KMToolbarView {
  54. get {
  55. return self.toolBar_
  56. }
  57. }
  58. private var zoomOutButton_: NSButton?
  59. private var zoomInButton_: NSButton?
  60. open var zoomOutButton: NSButton? {
  61. get {
  62. return self.zoomOutButton_
  63. }
  64. }
  65. open var zoomInButton: NSButton? {
  66. get {
  67. return self.zoomInButton_
  68. }
  69. }
  70. private var pageRangeView_: KMDesignSelect?
  71. open var pageRangeView: KMDesignSelect? {
  72. get {
  73. return self.pageRangeView_
  74. }
  75. }
  76. private var pageRangeViewItems = [KMTools.pageRangeTypeString(pageRange: .all),
  77. KMTools.pageRangeTypeString(pageRange: .odd),
  78. KMTools.pageRangeTypeString(pageRange: .even),
  79. KMTools.pageRangeTypeString(pageRange: .horizontal),
  80. KMTools.pageRangeTypeString(pageRange: .vertical),
  81. KMTools.pageRangeTypeString(pageRange: .custom)]
  82. var itemClick: KMItemClickBlock<KMPDFEditToolbar>?
  83. var pageRangeitemClick: ((KMPageRange)->Void)?
  84. var pageRangeValueDidChange: KMValueDidChangeBlock?
  85. lazy var insertItemStrings: [String] = {
  86. return [NSLocalizedString("Blank Page", comment: ""),
  87. NSLocalizedString("Blank Page - Custom...", comment: ""),
  88. NSLocalizedString("From PDF", comment: "")]
  89. }()
  90. private weak var popover: NSPopover?
  91. override init(frame frameRect: NSRect) {
  92. super.init(frame: frameRect)
  93. self.initDefaultValue()
  94. self.initSubViews()
  95. }
  96. required init?(coder: NSCoder) {
  97. super.init(coder: coder)
  98. self.initDefaultValue()
  99. self.initSubViews()
  100. }
  101. public func initDefaultValue() {
  102. }
  103. public func initSubViews() {
  104. self.addSubview(self.toolBar)
  105. self.toolBar.frame = self.bounds
  106. self.toolBar.autoresizingMask = [.width, .height]
  107. self.toolBar.delegate = self
  108. // let rightItemView = self.rightItemView
  109. // self.addSubview(rightItemView)
  110. // rightItemView.frame = NSMakeRect(NSWidth(self.frame)-80, 0, 80, NSHeight(self.frame))
  111. // rightItemView.autoresizingMask = [.minXMargin, .height]
  112. }
  113. // MARK: - Publick Methods
  114. public func setPageRangeString(_ string: String) {
  115. self.pageRangeView?.stringValue = string
  116. self.pageRangeView?.editable = true
  117. }
  118. public func getItem(for itemId: String) -> KMToolbarItemView? {
  119. return self.toolBar.itemForItemIdentifier(itemId)
  120. }
  121. public func selectItem(for itemId: String) {
  122. self.getItem(for: itemId)?.isSelected = true
  123. }
  124. public func unSelectItem(for itemId: String) {
  125. self.getItem(for: itemId)?.isSelected = false
  126. }
  127. // MARK: - Actions
  128. @objc private func zoomOutButtonAction() {
  129. guard let callback = self.itemClick else {
  130. return
  131. }
  132. callback(self, KMPageEditType.zoomOut.rawValue)
  133. }
  134. @objc private func zoomInButtonAction() {
  135. guard let callback = self.itemClick else {
  136. return
  137. }
  138. callback(self, KMPageEditType.zoomIn.rawValue)
  139. }
  140. // MARK: toolBar 菜单事件
  141. @objc func pageRangeMenuAction(sender: KMToolbarClickButton) {
  142. if let subMenu = (sender.clickObject as? KMToolbarItemView)?.menuFormRepresentation?.submenu {
  143. subMenu.popUp(positioning: nil, at: sender.frame.origin, in: sender.superview)
  144. }
  145. }
  146. @objc private func _pageRangeAction(sender: NSMenuItem) {
  147. guard let type = KMPageRange(rawValue: sender.tag) else {
  148. return
  149. }
  150. guard let callback = self.pageRangeitemClick else {
  151. return
  152. }
  153. callback(type)
  154. }
  155. @objc func leftRotateMenuAction(sender: KMToolbarClickButton?) {
  156. guard let callback = self.itemClick else {
  157. return
  158. }
  159. callback(self, KMPageEditType.leftRotate.rawValue)
  160. }
  161. @objc func rightRotateMenuAction(sender: KMToolbarClickButton?) {
  162. guard let callback = self.itemClick else {
  163. return
  164. }
  165. callback(self, KMPageEditType.rightRotate.rawValue)
  166. }
  167. @objc func insertMenuAction(sender: KMToolbarClickButton) {
  168. guard let callback = self.itemClick else {
  169. return
  170. }
  171. callback(self, KMPageEditType.insert.rawValue)
  172. if let _popover = self.popover, _popover.isShown {
  173. self.popover?.close()
  174. self.popover = nil
  175. return
  176. }
  177. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(self.insertItemStrings)
  178. let popover: NSPopover = NSPopover.init()
  179. self.popover = popover
  180. popover.contentViewController = vc
  181. popover.animates = true
  182. popover.behavior = .semitransient
  183. popover.setValue(true, forKey: "shouldHideAnchor")
  184. popover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
  185. vc.downCallback = { [weak self] downEntered, count in
  186. self?.popover?.close()
  187. self?.popover = nil
  188. if (!downEntered) {
  189. return
  190. }
  191. guard let index = self?.insertItemStrings.firstIndex(of: count) else {
  192. return
  193. }
  194. if (index == 0) {
  195. self?.item_insertBlankPage(sender: nil)
  196. } else if (index == 1) {
  197. self?.item_insertCustomPage(sender: nil)
  198. } else if (index == 2) {
  199. self?.item_insertFile(sender: nil)
  200. }
  201. }
  202. }
  203. @objc func extractMenuAction(sender: KMToolbarClickButton?) {
  204. // guard let callback = self.itemClick else {
  205. // return
  206. // }
  207. // callback(self, KMPageEditType.insert.rawValue)
  208. if let _popover = self.popover, _popover.isShown {
  209. self.popover?.close()
  210. self.popover = nil
  211. return
  212. }
  213. let array = [NSLocalizedString("Extract selected pages", comment: ""), NSLocalizedString("Extract", comment: "")]
  214. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(array)
  215. let popover: NSPopover = NSPopover.init()
  216. self.popover = popover
  217. popover.contentViewController = vc
  218. popover.animates = true
  219. popover.behavior = .semitransient
  220. popover.setValue(true, forKey: "shouldHideAnchor")
  221. if let data = sender {
  222. popover.show(relativeTo: CGRect(x: sender?.bounds.origin.x ?? 0, y: 10, width: sender?.bounds.size.width ?? 0, height: sender?.bounds.size.height ?? 0), of: data, preferredEdge: .maxY)
  223. }
  224. vc.downCallback = { [weak self] downEntered, count in
  225. self?.popover?.close()
  226. self?.popover = nil
  227. if (!downEntered) {
  228. return
  229. }
  230. guard let index = array.firstIndex(of: count) else {
  231. return
  232. }
  233. if (index == 0) {
  234. guard let callback = self?.itemClick else {
  235. return
  236. }
  237. callback(self, KMPageEditType.extract_selected_pages)
  238. } else if (index == 1) {
  239. guard let callback = self?.itemClick else {
  240. return
  241. }
  242. callback(self, KMPageEditType.extract.rawValue)
  243. }
  244. }
  245. }
  246. @objc private func _extractMenuItemAction(_ sender: NSMenuItem) {
  247. if (sender.tag == 1) {
  248. guard let callback = self.itemClick else {
  249. return
  250. }
  251. callback(self, KMPageEditType.extract_selected_pages)
  252. } else if (sender.tag == 2) {
  253. guard let callback = self.itemClick else {
  254. return
  255. }
  256. callback(self, KMPageEditType.extract.rawValue)
  257. }
  258. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Extract"])
  259. }
  260. @objc func replaceMenuAction(sender: KMToolbarClickButton?) {
  261. guard let callback = self.itemClick else {
  262. return
  263. }
  264. callback(self, KMPageEditType.replace.rawValue)
  265. }
  266. @objc func splitMenuAction(sender: KMToolbarClickButton) {
  267. if let subMenu = (sender.clickObject as? KMToolbarItemView)?.menuFormRepresentation?.submenu {
  268. subMenu.popUp(positioning: nil, at: sender.frame.origin, in: sender.superview)
  269. }
  270. }
  271. @objc func reverseMenuAction(sender: KMToolbarClickButton) {
  272. guard let callback = self.itemClick else {
  273. return
  274. }
  275. callback(self, KMPageEditType.reverse.rawValue)
  276. }
  277. @objc func copyMenuAction(sender: KMToolbarClickButton) {
  278. guard let callback = self.itemClick else {
  279. return
  280. }
  281. callback(self, KMPageEditType.copy.rawValue)
  282. }
  283. @objc func pasteMenuAction(sender: KMToolbarClickButton) {
  284. guard let callback = self.itemClick else {
  285. return
  286. }
  287. callback(self, KMPageEditType.paste.rawValue)
  288. }
  289. @objc func deleteMenuAction(sender: KMToolbarClickButton) {
  290. guard let callback = self.itemClick else {
  291. return
  292. }
  293. callback(self, KMPageEditType.delete.rawValue)
  294. }
  295. @objc func item_insertFile(sender: NSMenuItem?) {
  296. guard let callback = self.itemClick else {
  297. return
  298. }
  299. callback(self, KMPageEditType.insert_files)
  300. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Insert"])
  301. }
  302. @objc func item_insertBlankPage(sender: NSMenuItem?) {
  303. guard let callback = self.itemClick else {
  304. return
  305. }
  306. callback(self, KMPageEditType.insert_blank_page)
  307. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Insert"])
  308. }
  309. @objc func item_insertCustomPage(sender: NSMenuItem?) {
  310. guard let callback = self.itemClick else {
  311. return
  312. }
  313. callback(self, KMPageEditType.insert_custom_page)
  314. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Insert"])
  315. }
  316. @objc private func _splitAction(sender: NSMenuItem) {
  317. if (sender.tag == 1) {
  318. guard let callback = self.itemClick else {
  319. return
  320. }
  321. callback(self, KMPageEditType.split.rawValue)
  322. return
  323. }
  324. guard let callback = self.itemClick else {
  325. return
  326. }
  327. callback(self, KMPageEditType.split_selected_pages)
  328. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Split"])
  329. }
  330. private lazy var rightItemView: NSView = {
  331. let view = NSView()
  332. let size: CGFloat = 28
  333. let y: CGFloat = 6
  334. let zoomInButton = NSButton()
  335. view.addSubview(zoomInButton)
  336. zoomInButton.frame = NSMakeRect(0, y, size, size)
  337. zoomInButton.isBordered = false
  338. zoomInButton.image = NSImage(named: "KMImageNamePageEditZoomIn")
  339. zoomInButton.target = self
  340. zoomInButton.action = #selector(zoomInButtonAction)
  341. self.zoomInButton_ = zoomInButton
  342. let zoomOutButton = NSButton()
  343. view.addSubview(zoomOutButton)
  344. zoomOutButton.frame = NSMakeRect(size+7, y, size, size)
  345. zoomOutButton.isBordered = false
  346. zoomOutButton.image = NSImage(named: "KMImageNamePageEditZoomOut")
  347. zoomOutButton.target = self
  348. zoomOutButton.action = #selector(zoomOutButtonAction)
  349. self.zoomOutButton_ = zoomOutButton
  350. return view
  351. }()
  352. }
  353. // MARK: - KMNewToolbarDelegate
  354. extension KMPDFEditToolbar: KMToolbarViewDelegate {
  355. func toolbarDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String] {
  356. return [KMToolbarPageEditPageRangeItemIdentifier,
  357. // KMToolbarPageEditLeftRotateItemIdentifier,
  358. // KMToolbarPageEditRightRotateItemIdentifier,
  359. KMToolbarDividerItemIdentifier,
  360. KMToolbarPageEditInsetItemIdentifier,
  361. KMToolbarPageEditReplaceItemIdentifier,
  362. KMToolbarPageEditExtractItemIdentifier,
  363. KMToolbarPageEditSplitItemIdentifier,
  364. KMToolbarPageEditRotateItemIdentifier,
  365. KMToolbarPageEditReverseItemIdentifier,
  366. KMToolbarPageEditCopyItemIdentifier,
  367. KMToolbarPageEditPasteItemIdentifier,
  368. KMToolbarPageEditDeleteItemIdentifier]
  369. }
  370. func toolbar(_ toolbar: KMToolbarView, itemFor identifier: String) -> KMToolbarItemView {
  371. let item: KMToolbarItemView? = KMToolbarCustomItemView(itemIdentifier: identifier)
  372. if (identifier == KMToolbarPageEditPageRangeItemIdentifier) {
  373. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageRange)
  374. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageRangeHighlight")
  375. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageRangeHighlight")
  376. item?.target = self
  377. item?.btnTag = KMPageEditType.pageRange.rawValue
  378. item?.titleName = NSLocalizedString("Page Range", comment: "")
  379. item?.boxImagePosition = .imageExpandLeft
  380. item?.needExpandAction = true
  381. let menuItem = NSMenuItem.init(title: NSLocalizedString("Page Range", comment: ""), action: #selector(pageRangeMenuAction), target: self)
  382. let subMenu = NSMenu()
  383. subMenu.addItem(title: NSLocalizedString("Odd Pages Only", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.odd.rawValue)
  384. subMenu.addItem(title: NSLocalizedString("Even Pages Only", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.even.rawValue)
  385. subMenu.addItem(title: NSLocalizedString("Portrait pages", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.vertical.rawValue)
  386. subMenu.addItem(title: NSLocalizedString("Landscape pages", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.horizontal.rawValue)
  387. subMenu.addItem(title: NSLocalizedString("All Pages", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.all.rawValue)
  388. subMenu.addItem(.separator())
  389. subMenu.addItem(title: NSLocalizedString("Customize", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.custom.rawValue)
  390. menuItem.submenu = subMenu
  391. item?.menuFormRepresentation = menuItem
  392. } else if (identifier == KMToolbarPageEditRotateItemIdentifier) {
  393. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageRotate)
  394. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageRotateHighlight")
  395. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageRotateHighlight")
  396. item?.target = self
  397. item?.btnTag = KMPageEditType.rightRotate.rawValue
  398. item?.toolTip = NSLocalizedString("Rotate", comment: "")
  399. item?.titleName = NSLocalizedString("Rotate", comment: "")
  400. item?.boxImagePosition = .imageLeft
  401. item?.btnAction = #selector(rightRotateMenuAction)
  402. let menuItem = NSMenuItem.init(title: NSLocalizedString("Rotate", comment: ""), action: #selector(rightRotateMenuAction), target: self)
  403. item?.menuFormRepresentation = menuItem
  404. } else if (identifier == KMToolbarPageEditLeftRotateItemIdentifier) {
  405. item?.image = NSImage(named: "KMImageNameMainToolPageLeftRotate")
  406. item?.selectedImage = NSImage(named: "KMImageNameMainToolPageLeftRotateHighlight")
  407. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageLeftRotateHighlight")
  408. item?.target = self
  409. item?.btnTag = KMPageEditType.leftRotate.rawValue
  410. item?.toolTip = NSLocalizedString("Rotate Left", comment: "")
  411. item?.titleName = NSLocalizedString("", comment: "")
  412. item?.boxImagePosition = .imageLeft
  413. item?.btnAction = #selector(leftRotateMenuAction)
  414. let menuItem = NSMenuItem.init(title: NSLocalizedString("Rotate Left", comment: ""), action: #selector(leftRotateMenuAction), target: self)
  415. item?.menuFormRepresentation = menuItem
  416. } else if (identifier == KMToolbarPageEditRightRotateItemIdentifier) {
  417. item?.image = NSImage(named: "KMImageNameMainToolPageRightRotate")
  418. item?.selectedImage = NSImage(named: "KMImageNameMainToolPageRightRotateHighlight")
  419. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageRightRotateHighlight")
  420. item?.target = self
  421. item?.btnTag = KMPageEditType.rightRotate.rawValue
  422. item?.toolTip = NSLocalizedString("Rotate Right", comment: "")
  423. item?.titleName = NSLocalizedString("", comment: "")
  424. item?.boxImagePosition = .imageLeft
  425. item?.btnAction = #selector(rightRotateMenuAction)
  426. let menuItem = NSMenuItem.init(title: NSLocalizedString("Rotate Right", comment: ""), action: #selector(rightRotateMenuAction), target: self)
  427. item?.menuFormRepresentation = menuItem
  428. } else if (identifier == KMToolbarPageEditInsetItemIdentifier) {
  429. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageInsert)
  430. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageInsertHighlight")
  431. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageInsertHighlight")
  432. item?.target = self
  433. item?.btnTag = KMPageEditType.insert.rawValue
  434. // item?.toolTip = NSLocalizedString("Inserting pages into an existing PDF", comment: "")
  435. item?.titleName = NSLocalizedString("Insert", comment: "")
  436. item?.boxImagePosition = .imageExpandLeft
  437. // item?.btnAction = #selector(insertMenuAction)
  438. item?.needExpandAction = true
  439. let menuItem = NSMenuItem.init(title: NSLocalizedString("Insert", comment: ""), action: nil, target: self)
  440. let subMenu = NSMenu()
  441. menuItem.submenu = subMenu
  442. _ = subMenu.addItem(title: self.insertItemStrings[0], action: #selector(item_insertBlankPage) , target: self)
  443. _ = subMenu.addItem(title: self.insertItemStrings[1], action: #selector(item_insertCustomPage) , target: self)
  444. _ = subMenu.addItem(title: self.insertItemStrings[2], action: #selector(item_insertFile) , target: self)
  445. item?.menuFormRepresentation = menuItem
  446. } else if (identifier == KMToolbarPageEditExtractItemIdentifier) {
  447. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageExtract)
  448. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageExtractHighlight")
  449. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageExtractHighlight")
  450. item?.target = self
  451. item?.btnTag = KMPageEditType.extract.rawValue
  452. // item?.toolTip = NSLocalizedString("Exctract selected pages", comment: "")
  453. item?.titleName = NSLocalizedString("Extract", comment: "")
  454. item?.boxImagePosition = .imageExpandLeft
  455. // item?.btnAction = #selector(extractMenuAction)
  456. item?.needExpandAction = true
  457. let menuItem = NSMenuItem.init(title: NSLocalizedString("Extract", comment: ""), action: nil, target: self)
  458. let subMenu = NSMenu()
  459. menuItem.submenu = subMenu
  460. subMenu.addItem(title: NSLocalizedString("Extract selected pages", comment: ""), action: #selector(_extractMenuItemAction) , target: self, tag: 1)
  461. subMenu.addItem(title: NSLocalizedString("Extract", comment: ""), action: #selector(_extractMenuItemAction) , target: self, tag: 2)
  462. item?.menuFormRepresentation = menuItem
  463. } else if (identifier == KMToolbarPageEditReplaceItemIdentifier) {
  464. item?.image = NSImage(named: KMImageNameToolbarReplace)
  465. item?.selectedImage = NSImage(named: "KMImageNameToolbarReplaceHighlight")
  466. item?.alternateImage = NSImage(named: "KMImageNameToolbarReplaceHighlight")
  467. item?.target = self
  468. item?.btnTag = KMPageEditType.replace.rawValue
  469. item?.toolTip = NSLocalizedString("Replace", comment: "")
  470. item?.titleName = NSLocalizedString("Replace", comment: "")
  471. item?.boxImagePosition = .imageLeft
  472. item?.btnAction = #selector(replaceMenuAction)
  473. let menuItem = NSMenuItem.init(title: NSLocalizedString("Replace", comment: ""), action: #selector(replaceMenuAction), target: self)
  474. item?.menuFormRepresentation = menuItem
  475. } else if (identifier == KMToolbarPageEditSplitItemIdentifier) {
  476. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageSplit)
  477. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageSplitHighlight")
  478. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageSplitHighlight")
  479. item?.target = self
  480. item?.btnTag = KMPageEditType.split.rawValue
  481. // item?.toolTip = NSLocalizedString("Split and save selected PDF pages as a new separate document", comment: "")
  482. item?.titleName = NSLocalizedString("Split", comment: "")
  483. item?.boxImagePosition = .imageExpandLeft
  484. item?.needExpandAction = true
  485. // item?.btnAction = #selector(splitMenuAction)
  486. let menuItem = NSMenuItem.init(title: NSLocalizedString("Split", comment: ""), action: nil, target: self)
  487. let subMenu = NSMenu()
  488. subMenu.addItem(title: NSLocalizedString("Split selected pages", comment: ""), action: #selector(_splitAction), target: self, tag: 0)
  489. subMenu.addItem(title: NSLocalizedString("Split", comment: ""), action: #selector(_splitAction), target: self, tag: 1)
  490. menuItem.submenu = subMenu
  491. item?.menuFormRepresentation = menuItem
  492. } else if (identifier == KMToolbarPageEditReverseItemIdentifier) {
  493. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageReverse)
  494. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageReverseHighlight")
  495. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageReverseHighlight")
  496. item?.target = self
  497. item?.btnTag = KMPageEditType.reverse.rawValue
  498. item?.toolTip = NSLocalizedString("Reverse", comment: "")
  499. item?.titleName = NSLocalizedString("Reverse", comment: "")
  500. item?.boxImagePosition = .imageLeft
  501. item?.btnAction = #selector(reverseMenuAction)
  502. let menuItem = NSMenuItem.init(title: NSLocalizedString("Reverse", comment: ""), action: #selector(reverseMenuAction), target: self)
  503. item?.menuFormRepresentation = menuItem
  504. } else if (identifier == KMToolbarPageEditCopyItemIdentifier) {
  505. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageCopy)
  506. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageCopyHighlight")
  507. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageCopyHighlight")
  508. item?.target = self
  509. item?.btnTag = KMPageEditType.delete.rawValue
  510. item?.toolTip = NSLocalizedString("Copy", comment: "")
  511. item?.titleName = NSLocalizedString("Copy", comment: "")
  512. item?.boxImagePosition = .imageLeft
  513. item?.btnAction = #selector(copyMenuAction)
  514. let menuItem = NSMenuItem.init(title: NSLocalizedString("Copy", comment: ""), action: #selector(copyMenuAction), target: self)
  515. item?.menuFormRepresentation = menuItem
  516. } else if (identifier == KMToolbarPageEditPasteItemIdentifier) {
  517. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPagePaste)
  518. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPagePasteHighlight")
  519. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPagePasteHighlight")
  520. item?.target = self
  521. item?.btnTag = KMPageEditType.delete.rawValue
  522. item?.toolTip = NSLocalizedString("Paste", comment: "")
  523. item?.titleName = NSLocalizedString("Paste", comment: "")
  524. item?.boxImagePosition = .imageLeft
  525. item?.btnAction = #selector(pasteMenuAction)
  526. let menuItem = NSMenuItem.init(title: NSLocalizedString("Paste", comment: ""), action: #selector(pasteMenuAction), target: self)
  527. item?.menuFormRepresentation = menuItem
  528. } else if (identifier == KMToolbarPageEditDeleteItemIdentifier) {
  529. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageDelete)
  530. item?.selectedImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageDeleteHighlight")
  531. item?.alternateImage = NSImage(named: "KMImageNameUXIconSubtoolbarPageDeleteHighlight")
  532. item?.target = self
  533. item?.btnTag = KMPageEditType.delete.rawValue
  534. item?.toolTip = NSLocalizedString("Delete", comment: "")
  535. item?.titleName = NSLocalizedString("Delete", comment: "")
  536. item?.boxImagePosition = .imageLeft
  537. item?.btnAction = #selector(deleteMenuAction)
  538. let menuItem = NSMenuItem.init(title: NSLocalizedString("Delete", comment: ""), action: #selector(deleteMenuAction), target: self)
  539. item?.menuFormRepresentation = menuItem
  540. }
  541. return item!
  542. }
  543. }
  544. // MARK: - KMSelectPopButtonDelegate
  545. extension KMPDFEditToolbar: KMSelectPopButtonDelegate {
  546. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  547. guard let callback = self.itemClick else {
  548. return
  549. }
  550. callback(self, KMPageEditType.pageRange.rawValue)
  551. }
  552. func km_controlTextDidChange(_ obj: KMDesignSelect) {
  553. }
  554. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  555. guard let callback = self.pageRangeValueDidChange else {
  556. return
  557. }
  558. callback(self.pageRangeView?.stringValue, nil)
  559. }
  560. }