KMPDFEditToolbar.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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?.target = self
  375. item?.btnTag = KMPageEditType.pageRange.rawValue
  376. // item?.toolTip = NSLocalizedString("pageRange", comment: "")
  377. item?.titleName = NSLocalizedString("Page Range", comment: "")
  378. item?.boxImagePosition = .imageExpandLeft
  379. // item?.btnAction = #selector(pageRangeMenuAction)
  380. item?.needExpandAction = true
  381. // let view = NSView()
  382. // item?.addSubview(view)
  383. // view.frame = item!.bounds
  384. // view.autoresizingMask = [.width,.height]
  385. // view.wantsLayer = true
  386. // view.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  387. // let comBoxView = KMDesignSelect(withType: .Combox)
  388. // view.addSubview(comBoxView.view)
  389. // comBoxView.view.frame = NSMakeRect(0, 0.5, NSWidth(view.frame), 21)
  390. // comBoxView.view.autoresizingMask = [.width]
  391. // comBoxView.removeAllItems()
  392. // comBoxView.addItems(withObjectValues: self.pageRangeViewItems)
  393. // comBoxView.selectItem(at: 0)
  394. // comBoxView.placeholderString = NSLocalizedString("eg .1,3-5,10", comment: "")
  395. // comBoxView.editable = false
  396. // comBoxView.delete = self
  397. // self.pageRangeView_ = comBoxView
  398. let menuItem = NSMenuItem.init(title: NSLocalizedString("Page Range", comment: ""), action: #selector(pageRangeMenuAction), target: self)
  399. let subMenu = NSMenu()
  400. subMenu.addItem(title: NSLocalizedString("Odd Pages Only", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.odd.rawValue)
  401. subMenu.addItem(title: NSLocalizedString("Even Pages Only", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.even.rawValue)
  402. subMenu.addItem(title: NSLocalizedString("Portrait pages", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.vertical.rawValue)
  403. subMenu.addItem(title: NSLocalizedString("Landscape pages", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.horizontal.rawValue)
  404. subMenu.addItem(title: NSLocalizedString("All Pages", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.all.rawValue)
  405. subMenu.addItem(.separator())
  406. subMenu.addItem(title: NSLocalizedString("Customize", comment: ""), action: #selector(_pageRangeAction), target: self, tag: KMPageRange.custom.rawValue)
  407. menuItem.submenu = subMenu
  408. item?.menuFormRepresentation = menuItem
  409. } else if (identifier == KMToolbarPageEditRotateItemIdentifier) {
  410. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageRotate)
  411. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageRotate)
  412. item?.target = self
  413. item?.btnTag = KMPageEditType.rightRotate.rawValue
  414. item?.toolTip = NSLocalizedString("Rotate", comment: "")
  415. item?.titleName = NSLocalizedString("Rotate", comment: "")
  416. item?.boxImagePosition = .imageLeft
  417. item?.btnAction = #selector(rightRotateMenuAction)
  418. let menuItem = NSMenuItem.init(title: NSLocalizedString("Rotate", comment: ""), action: #selector(rightRotateMenuAction), target: self)
  419. item?.menuFormRepresentation = menuItem
  420. } else if (identifier == KMToolbarPageEditLeftRotateItemIdentifier) {
  421. item?.image = NSImage(named: "KMImageNameMainToolPageLeftRotate")
  422. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageLeftRotate")
  423. item?.target = self
  424. item?.btnTag = KMPageEditType.leftRotate.rawValue
  425. item?.toolTip = NSLocalizedString("Rotate Left", comment: "")
  426. item?.titleName = NSLocalizedString("", comment: "")
  427. item?.boxImagePosition = .imageLeft
  428. item?.btnAction = #selector(leftRotateMenuAction)
  429. let menuItem = NSMenuItem.init(title: NSLocalizedString("Rotate Left", comment: ""), action: #selector(leftRotateMenuAction), target: self)
  430. item?.menuFormRepresentation = menuItem
  431. } else if (identifier == KMToolbarPageEditRightRotateItemIdentifier) {
  432. item?.image = NSImage(named: "KMImageNameMainToolPageRightRotate")
  433. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageRightRotate")
  434. item?.target = self
  435. item?.btnTag = KMPageEditType.rightRotate.rawValue
  436. item?.toolTip = NSLocalizedString("Rotate Right", comment: "")
  437. item?.titleName = NSLocalizedString("", comment: "")
  438. item?.boxImagePosition = .imageLeft
  439. item?.btnAction = #selector(rightRotateMenuAction)
  440. let menuItem = NSMenuItem.init(title: NSLocalizedString("Rotate Right", comment: ""), action: #selector(rightRotateMenuAction), target: self)
  441. item?.menuFormRepresentation = menuItem
  442. } else if (identifier == KMToolbarPageEditInsetItemIdentifier) {
  443. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageInsert)
  444. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageInsert)
  445. item?.target = self
  446. item?.btnTag = KMPageEditType.insert.rawValue
  447. // item?.toolTip = NSLocalizedString("Inserting pages into an existing PDF", comment: "")
  448. item?.titleName = NSLocalizedString("Insert", comment: "")
  449. item?.boxImagePosition = .imageExpandLeft
  450. // item?.btnAction = #selector(insertMenuAction)
  451. item?.needExpandAction = true
  452. let menuItem = NSMenuItem.init(title: NSLocalizedString("Insert", comment: ""), action: nil, target: self)
  453. let subMenu = NSMenu()
  454. menuItem.submenu = subMenu
  455. _ = subMenu.addItem(title: self.insertItemStrings[0], action: #selector(item_insertBlankPage) , target: self)
  456. _ = subMenu.addItem(title: self.insertItemStrings[1], action: #selector(item_insertCustomPage) , target: self)
  457. _ = subMenu.addItem(title: self.insertItemStrings[2], action: #selector(item_insertFile) , target: self)
  458. item?.menuFormRepresentation = menuItem
  459. } else if (identifier == KMToolbarPageEditExtractItemIdentifier) {
  460. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageExtract)
  461. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageExtract)
  462. item?.target = self
  463. item?.btnTag = KMPageEditType.extract.rawValue
  464. // item?.toolTip = NSLocalizedString("Exctract selected pages", comment: "")
  465. item?.titleName = NSLocalizedString("Extract", comment: "")
  466. item?.boxImagePosition = .imageExpandLeft
  467. // item?.btnAction = #selector(extractMenuAction)
  468. item?.needExpandAction = true
  469. let menuItem = NSMenuItem.init(title: NSLocalizedString("Extract", comment: ""), action: nil, target: self)
  470. let subMenu = NSMenu()
  471. menuItem.submenu = subMenu
  472. subMenu.addItem(title: NSLocalizedString("Extract selected pages", comment: ""), action: #selector(_extractMenuItemAction) , target: self, tag: 1)
  473. subMenu.addItem(title: NSLocalizedString("Extract", comment: ""), action: #selector(_extractMenuItemAction) , target: self, tag: 2)
  474. item?.menuFormRepresentation = menuItem
  475. } else if (identifier == KMToolbarPageEditReplaceItemIdentifier) {
  476. item?.image = NSImage(named: KMImageNameToolbarReplace)
  477. item?.alternateImage = NSImage(named: KMImageNameToolbarReplace)
  478. item?.target = self
  479. item?.btnTag = KMPageEditType.replace.rawValue
  480. item?.toolTip = NSLocalizedString("Replace", comment: "")
  481. item?.titleName = NSLocalizedString("Replace", comment: "")
  482. item?.boxImagePosition = .imageLeft
  483. item?.btnAction = #selector(replaceMenuAction)
  484. let menuItem = NSMenuItem.init(title: NSLocalizedString("Replace", comment: ""), action: #selector(replaceMenuAction), target: self)
  485. item?.menuFormRepresentation = menuItem
  486. } else if (identifier == KMToolbarPageEditSplitItemIdentifier) {
  487. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageSplit)
  488. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageSplit)
  489. item?.target = self
  490. item?.btnTag = KMPageEditType.split.rawValue
  491. // item?.toolTip = NSLocalizedString("Split and save selected PDF pages as a new separate document", comment: "")
  492. item?.titleName = NSLocalizedString("Split", comment: "")
  493. item?.boxImagePosition = .imageExpandLeft
  494. item?.needExpandAction = true
  495. // item?.btnAction = #selector(splitMenuAction)
  496. let menuItem = NSMenuItem.init(title: NSLocalizedString("Split", comment: ""), action: nil, target: self)
  497. let subMenu = NSMenu()
  498. subMenu.addItem(title: NSLocalizedString("Split selected pages", comment: ""), action: #selector(_splitAction), target: self, tag: 0)
  499. subMenu.addItem(title: NSLocalizedString("Split", comment: ""), action: #selector(_splitAction), target: self, tag: 1)
  500. menuItem.submenu = subMenu
  501. item?.menuFormRepresentation = menuItem
  502. } else if (identifier == KMToolbarPageEditReverseItemIdentifier) {
  503. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageReverse)
  504. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageReverse)
  505. item?.target = self
  506. item?.btnTag = KMPageEditType.reverse.rawValue
  507. item?.toolTip = NSLocalizedString("Reverse", comment: "")
  508. item?.titleName = NSLocalizedString("Reverse", comment: "")
  509. item?.boxImagePosition = .imageLeft
  510. item?.btnAction = #selector(reverseMenuAction)
  511. let menuItem = NSMenuItem.init(title: NSLocalizedString("Reverse", comment: ""), action: #selector(reverseMenuAction), target: self)
  512. item?.menuFormRepresentation = menuItem
  513. } else if (identifier == KMToolbarPageEditCopyItemIdentifier) {
  514. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageCopy)
  515. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageCopy)
  516. item?.target = self
  517. item?.btnTag = KMPageEditType.delete.rawValue
  518. item?.toolTip = NSLocalizedString("Copy", comment: "")
  519. item?.titleName = NSLocalizedString("Copy", comment: "")
  520. item?.boxImagePosition = .imageLeft
  521. item?.btnAction = #selector(copyMenuAction)
  522. let menuItem = NSMenuItem.init(title: NSLocalizedString("Copy", comment: ""), action: #selector(copyMenuAction), target: self)
  523. item?.menuFormRepresentation = menuItem
  524. } else if (identifier == KMToolbarPageEditPasteItemIdentifier) {
  525. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPagePaste)
  526. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPagePaste)
  527. item?.target = self
  528. item?.btnTag = KMPageEditType.delete.rawValue
  529. item?.toolTip = NSLocalizedString("Paste", comment: "")
  530. item?.titleName = NSLocalizedString("Paste", comment: "")
  531. item?.boxImagePosition = .imageLeft
  532. item?.btnAction = #selector(pasteMenuAction)
  533. let menuItem = NSMenuItem.init(title: NSLocalizedString("Paste", comment: ""), action: #selector(pasteMenuAction), target: self)
  534. item?.menuFormRepresentation = menuItem
  535. } else if (identifier == KMToolbarPageEditDeleteItemIdentifier) {
  536. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageDelete)
  537. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageDelete)
  538. item?.target = self
  539. item?.btnTag = KMPageEditType.delete.rawValue
  540. item?.toolTip = NSLocalizedString("Delete", comment: "")
  541. item?.titleName = NSLocalizedString("Delete", comment: "")
  542. item?.boxImagePosition = .imageLeft
  543. item?.btnAction = #selector(deleteMenuAction)
  544. let menuItem = NSMenuItem.init(title: NSLocalizedString("Delete", comment: ""), action: #selector(deleteMenuAction), target: self)
  545. item?.menuFormRepresentation = menuItem
  546. }
  547. return item!
  548. }
  549. }
  550. // MARK: - KMSelectPopButtonDelegate
  551. extension KMPDFEditToolbar: KMSelectPopButtonDelegate {
  552. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  553. guard let callback = self.itemClick else {
  554. return
  555. }
  556. callback(self, KMPageEditType.pageRange.rawValue)
  557. }
  558. func km_controlTextDidChange(_ obj: KMDesignSelect) {
  559. }
  560. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  561. guard let callback = self.pageRangeValueDidChange else {
  562. return
  563. }
  564. callback(self.pageRangeView?.stringValue, nil)
  565. }
  566. }