KMPDFEditToolbar.swift 29 KB

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