KMPDFEditToolbar.swift 29 KB

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