KMPDFEditToolbar.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //
  2. // KMPDFEditToolbar.swift
  3. // PDF Master
  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 delete
  19. case zoomOut
  20. case zoomIn
  21. case paste = 100
  22. // static let none: KMPageEditType = KMPageEditType(rawValue: 0)!
  23. // static let paste: KMPageEditType = KMPageEditType(rawValue: 100)!
  24. static var insert_files: Int {
  25. get {
  26. return self.insert.rawValue + 1000
  27. }
  28. }
  29. static var insert_blank_page: Int {
  30. get {
  31. return self.insert.rawValue + 1001
  32. }
  33. }
  34. static var insert_custom_page: Int {
  35. get {
  36. return self.insert.rawValue + 1002
  37. }
  38. }
  39. }
  40. class KMPDFEditToolbar: NSView {
  41. private var toolBar_ = KMToolbar()
  42. var toolBar: KMToolbar {
  43. get {
  44. return self.toolBar_
  45. }
  46. }
  47. private var zoomOutButton_: NSButton?
  48. private var zoomInButton_: NSButton?
  49. open var zoomOutButton: NSButton? {
  50. get {
  51. return self.zoomOutButton_
  52. }
  53. }
  54. open var zoomInButton: NSButton? {
  55. get {
  56. return self.zoomInButton_
  57. }
  58. }
  59. private var pageRangeView_: KMDesignSelect?
  60. open var pageRangeView: KMDesignSelect? {
  61. get {
  62. return self.pageRangeView_
  63. }
  64. }
  65. private var pageRangeViewItems = [KMTools.pageRangeTypeString(pageRange: .all),
  66. KMTools.pageRangeTypeString(pageRange: .odd),
  67. KMTools.pageRangeTypeString(pageRange: .even),
  68. KMTools.pageRangeTypeString(pageRange: .horizontal),
  69. KMTools.pageRangeTypeString(pageRange: .vertical),
  70. KMTools.pageRangeTypeString(pageRange: .custom)]
  71. var itemClick: KMItemClickBlock<KMPDFEditToolbar>?
  72. var pageRangeValueDidChange: KMValueDidChangeBlock?
  73. lazy var insertItemStrings: [String] = {
  74. return [NSLocalizedString("Insert a Blank Page", comment: ""),
  75. NSLocalizedString("Insert Custom Page", comment: ""),
  76. NSLocalizedString("Insert File", comment: "")]
  77. }()
  78. private weak var popover: NSPopover?
  79. override init(frame frameRect: NSRect) {
  80. super.init(frame: frameRect)
  81. self.initDefaultValue()
  82. self.initSubViews()
  83. }
  84. required init?(coder: NSCoder) {
  85. super.init(coder: coder)
  86. self.initDefaultValue()
  87. self.initSubViews()
  88. }
  89. public func initDefaultValue() {
  90. }
  91. public func initSubViews() {
  92. self.addSubview(self.toolBar)
  93. self.toolBar.frame = self.bounds
  94. self.toolBar.autoresizingMask = [.width, .height]
  95. self.toolBar.delegate = self
  96. let rightItemView = self.rightItemView
  97. self.addSubview(rightItemView)
  98. rightItemView.frame = NSMakeRect(NSWidth(self.frame)-80, 0, 80, NSHeight(self.frame))
  99. rightItemView.autoresizingMask = [.minXMargin, .height]
  100. }
  101. // MARK: - Publick Methods
  102. public func setPageRangeString(_ string: String) {
  103. self.pageRangeView?.stringValue = string
  104. self.pageRangeView?.editable = true
  105. }
  106. public func getItem(for itemId: String) -> KMToolBoxItem? {
  107. return self.toolBar.item(forItemIdentifier: itemId)
  108. }
  109. public func selectItem(for itemId: String) {
  110. self.getItem(for: itemId)?.isSelected = true
  111. }
  112. public func unSelectItem(for itemId: String) {
  113. self.getItem(for: itemId)?.isSelected = false
  114. }
  115. // MARK: - Actions
  116. @objc private func zoomOutButtonAction() {
  117. guard let callback = self.itemClick else {
  118. return
  119. }
  120. callback(self, KMPageEditType.zoomOut.rawValue)
  121. }
  122. @objc private func zoomInButtonAction() {
  123. guard let callback = self.itemClick else {
  124. return
  125. }
  126. callback(self, KMPageEditType.zoomIn.rawValue)
  127. }
  128. // MARK: toolBar 菜单事件
  129. @objc func pageRangeMenuAction(sender: KMToolbarClickButton) {
  130. }
  131. @objc func leftRotateMenuAction(sender: KMToolbarClickButton?) {
  132. guard let callback = self.itemClick else {
  133. return
  134. }
  135. callback(self, KMPageEditType.leftRotate.rawValue)
  136. }
  137. @objc func rightRotateMenuAction(sender: KMToolbarClickButton?) {
  138. guard let callback = self.itemClick else {
  139. return
  140. }
  141. callback(self, KMPageEditType.rightRotate.rawValue)
  142. }
  143. @objc func insertMenuAction(sender: KMToolbarClickButton) {
  144. guard let callback = self.itemClick else {
  145. return
  146. }
  147. callback(self, KMPageEditType.insert.rawValue)
  148. if let _popover = self.popover, _popover.isShown {
  149. self.popover?.close()
  150. self.popover = nil
  151. return
  152. }
  153. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(self.insertItemStrings)
  154. let popover: NSPopover = NSPopover.init()
  155. self.popover = popover
  156. popover.contentViewController = vc
  157. popover.animates = true
  158. popover.behavior = .semitransient
  159. popover.setValue(true, forKey: "shouldHideAnchor")
  160. 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)
  161. vc.downCallback = { [weak self] downEntered, count in
  162. self?.popover?.close()
  163. self?.popover = nil
  164. if (!downEntered) {
  165. return
  166. }
  167. guard let index = self?.insertItemStrings.firstIndex(of: count) else {
  168. return
  169. }
  170. if (index == 0) {
  171. self?.item_insertBlankPage(sender: nil)
  172. } else if (index == 1) {
  173. self?.item_insertCustomPage(sender: nil)
  174. } else if (index == 2) {
  175. self?.item_insertFile(sender: nil)
  176. }
  177. }
  178. }
  179. @objc func extractMenuAction(sender: KMToolbarClickButton?) {
  180. guard let callback = self.itemClick else {
  181. return
  182. }
  183. callback(self, KMPageEditType.extract.rawValue)
  184. }
  185. @objc func replaceMenuAction(sender: KMToolbarClickButton?) {
  186. guard let callback = self.itemClick else {
  187. return
  188. }
  189. callback(self, KMPageEditType.replace.rawValue)
  190. }
  191. @objc func splitMenuAction(sender: KMToolbarClickButton) {
  192. guard let callback = self.itemClick else {
  193. return
  194. }
  195. callback(self, KMPageEditType.split.rawValue)
  196. }
  197. @objc func reverseMenuAction(sender: KMToolbarClickButton) {
  198. guard let callback = self.itemClick else {
  199. return
  200. }
  201. callback(self, KMPageEditType.reverse.rawValue)
  202. }
  203. @objc func deleteMenuAction(sender: KMToolbarClickButton) {
  204. guard let callback = self.itemClick else {
  205. return
  206. }
  207. callback(self, KMPageEditType.delete.rawValue)
  208. }
  209. @objc func item_insertFile(sender: NSMenuItem?) {
  210. guard let callback = self.itemClick else {
  211. return
  212. }
  213. callback(self, KMPageEditType.insert_files)
  214. }
  215. @objc func item_insertBlankPage(sender: NSMenuItem?) {
  216. guard let callback = self.itemClick else {
  217. return
  218. }
  219. callback(self, KMPageEditType.insert_blank_page)
  220. }
  221. @objc func item_insertCustomPage(sender: NSMenuItem?) {
  222. guard let callback = self.itemClick else {
  223. return
  224. }
  225. callback(self, KMPageEditType.insert_custom_page)
  226. }
  227. private lazy var rightItemView: NSView = {
  228. let view = NSView()
  229. let size: CGFloat = 28
  230. let y: CGFloat = 6
  231. let zoomInButton = NSButton()
  232. view.addSubview(zoomInButton)
  233. zoomInButton.frame = NSMakeRect(0, y, size, size)
  234. zoomInButton.isBordered = false
  235. zoomInButton.image = NSImage(named: "KMImageNamePageEditZoomIn")
  236. zoomInButton.target = self
  237. zoomInButton.action = #selector(zoomInButtonAction)
  238. self.zoomInButton_ = zoomInButton
  239. let zoomOutButton = NSButton()
  240. view.addSubview(zoomOutButton)
  241. zoomOutButton.frame = NSMakeRect(size+7, y, size, size)
  242. zoomOutButton.isBordered = false
  243. zoomOutButton.image = NSImage(named: "KMImageNamePageEditZoomOut")
  244. zoomOutButton.target = self
  245. zoomOutButton.action = #selector(zoomOutButtonAction)
  246. self.zoomOutButton_ = zoomOutButton
  247. return view
  248. }()
  249. }
  250. // MARK: - KMNewToolbarDelegate
  251. extension KMPDFEditToolbar: KMNewToolbarDelegate {
  252. func toolbarDefaultItemIdentifiers(_ toolbar: KMToolbar!) -> [String]! {
  253. return [KMToolbarPageEditPageRangeItemIdentifier,
  254. KMToolbarPageEditLeftRotateItemIdentifier,
  255. KMToolbarPageEditRightRotateItemIdentifier,
  256. KMToolbarDividerItemIdentifier,
  257. KMToolbarPageEditInsetItemIdentifier,
  258. KMToolbarPageEditExtractItemIdentifier,
  259. KMToolbarPageEditReplaceItemIdentifier,
  260. KMToolbarPageEditSplitItemIdentifier,
  261. KMToolbarPageEditReverseItemIdentifier,
  262. KMToolbarDividerItemIdentifier,
  263. KMToolbarPageEditDeleteItemIdentifier]
  264. }
  265. func toolbar(_ toolbar: KMToolbar!, itemForItemIdentifier identifier: String!) -> KMToolBoxItem! {
  266. let item = KMToolBoxItem(itemIdentifier: identifier)
  267. if (identifier == KMToolbarPageEditPageRangeItemIdentifier) {
  268. item?.image = NSImage(named: "KMImageNameMainToolbarrRight")
  269. item?.alternateImage = NSImage(named: "KMImageNameMainToolbarrRight")
  270. item?.target = self
  271. item?.btnTag = KMPageEditType.pageRange.rawValue
  272. // item?.toolTip = NSLocalizedString("pageRange", comment: "")
  273. item?.titleName = NSLocalizedString("pageRange ", comment: "")
  274. item?.boxImagePosition = KMToolBoxImagePosition.left
  275. item?.btnAction = #selector(pageRangeMenuAction)
  276. let view = NSView()
  277. item?.addSubview(view)
  278. view.frame = item!.bounds
  279. view.autoresizingMask = [.width,.height]
  280. view.wantsLayer = true
  281. view.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  282. let comBoxView = KMDesignSelect(withType: .Combox)
  283. view.addSubview(comBoxView.view)
  284. comBoxView.view.frame = NSMakeRect(0, 0.5, NSWidth(view.frame), 21)
  285. comBoxView.view.autoresizingMask = [.width]
  286. comBoxView.removeAllItems()
  287. comBoxView.addItems(withObjectValues: self.pageRangeViewItems)
  288. comBoxView.selectItem(at: 0)
  289. comBoxView.placeholderString = NSLocalizedString("eg .1,3-5,10", comment: "")
  290. comBoxView.editable = false
  291. comBoxView.delete = self
  292. self.pageRangeView_ = comBoxView
  293. let menuItem = NSMenuItem.init(title: "pageRange", action: #selector(pageRangeMenuAction), target: self)
  294. item?.menuFormRepresentation = menuItem
  295. } else if (identifier == KMToolbarPageEditLeftRotateItemIdentifier) {
  296. item?.image = NSImage(named: "KMImageNameMainToolPageLeftRotate")
  297. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageLeftRotate")
  298. item?.target = self
  299. item?.btnTag = KMPageEditType.leftRotate.rawValue
  300. item?.toolTip = NSLocalizedString("Rotate Left", comment: "")
  301. item?.titleName = NSLocalizedString("", comment: "")
  302. item?.boxImagePosition = KMToolBoxImagePosition.left
  303. item?.btnAction = #selector(leftRotateMenuAction)
  304. let menuItem = NSMenuItem.init(title: "", action: #selector(leftRotateMenuAction), target: self)
  305. item?.menuFormRepresentation = menuItem
  306. } else if (identifier == KMToolbarPageEditRightRotateItemIdentifier) {
  307. item?.image = NSImage(named: "KMImageNameMainToolPageRightRotate")
  308. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageRightRotate")
  309. item?.target = self
  310. item?.btnTag = KMPageEditType.rightRotate.rawValue
  311. item?.toolTip = NSLocalizedString("Rotate Right", comment: "")
  312. item?.titleName = NSLocalizedString("", comment: "")
  313. item?.boxImagePosition = KMToolBoxImagePosition.left
  314. item?.btnAction = #selector(rightRotateMenuAction)
  315. let menuItem = NSMenuItem.init(title: "", action: #selector(rightRotateMenuAction), target: self)
  316. item?.menuFormRepresentation = menuItem
  317. } else if (identifier == KMToolbarPageEditInsetItemIdentifier) {
  318. item?.image = NSImage(named: "KMImageNameMainToolPageInsert")
  319. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageInsert")
  320. item?.target = self
  321. item?.btnTag = KMPageEditType.insert.rawValue
  322. item?.toolTip = NSLocalizedString("Insert", comment: "")
  323. item?.titleName = NSLocalizedString("Insert", comment: "")
  324. item?.boxImagePosition = KMToolBoxImagePosition.expandLeft
  325. item?.btnAction = #selector(insertMenuAction)
  326. let menuItem = NSMenuItem.init(title: "Insert", action: #selector(insertMenuAction), target: self)
  327. item?.menuFormRepresentation = menuItem
  328. } else if (identifier == KMToolbarPageEditExtractItemIdentifier) {
  329. item?.image = NSImage(named: "KMImageNameMainToolPageExtract")
  330. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageExtract")
  331. item?.target = self
  332. item?.btnTag = KMPageEditType.extract.rawValue
  333. item?.toolTip = NSLocalizedString("Extract", comment: "")
  334. item?.titleName = NSLocalizedString("Extract", comment: "")
  335. item?.boxImagePosition = KMToolBoxImagePosition.left
  336. item?.btnAction = #selector(extractMenuAction)
  337. let menuItem = NSMenuItem.init(title: "Extract", action: #selector(extractMenuAction), target: self)
  338. item?.menuFormRepresentation = menuItem
  339. } else if (identifier == KMToolbarPageEditReplaceItemIdentifier) {
  340. item?.image = NSImage(named: "KMImageNameMainToolPageReplace")
  341. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageReplace")
  342. item?.target = self
  343. item?.btnTag = KMPageEditType.replace.rawValue
  344. item?.toolTip = NSLocalizedString("Replace", comment: "")
  345. item?.titleName = NSLocalizedString("Replace", comment: "")
  346. item?.boxImagePosition = KMToolBoxImagePosition.left
  347. item?.btnAction = #selector(replaceMenuAction)
  348. let menuItem = NSMenuItem.init(title: "Replace", action: #selector(replaceMenuAction), target: self)
  349. item?.menuFormRepresentation = menuItem
  350. } else if (identifier == KMToolbarPageEditSplitItemIdentifier) {
  351. item?.image = NSImage(named: "KMImageNameMainToolPageSplit")
  352. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageSplit")
  353. item?.target = self
  354. item?.btnTag = KMPageEditType.split.rawValue
  355. item?.toolTip = NSLocalizedString("Split the PDF into multiple files", comment: "")
  356. item?.titleName = NSLocalizedString("Split", comment: "")
  357. item?.boxImagePosition = KMToolBoxImagePosition.left
  358. item?.btnAction = #selector(splitMenuAction)
  359. let menuItem = NSMenuItem.init(title: "Split", action: #selector(splitMenuAction), target: self)
  360. item?.menuFormRepresentation = menuItem
  361. } else if (identifier == KMToolbarPageEditReverseItemIdentifier) {
  362. item?.image = NSImage(named: "KMImageNameMainToolPageReverse")
  363. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageReverse")
  364. item?.target = self
  365. item?.btnTag = KMPageEditType.reverse.rawValue
  366. item?.toolTip = NSLocalizedString("Reverse", comment: "")
  367. item?.titleName = NSLocalizedString("Reverse", comment: "")
  368. item?.boxImagePosition = KMToolBoxImagePosition.left
  369. item?.btnAction = #selector(reverseMenuAction)
  370. let menuItem = NSMenuItem.init(title: "Reverse", action: #selector(reverseMenuAction), target: self)
  371. item?.menuFormRepresentation = menuItem
  372. } else if (identifier == KMToolbarPageEditDeleteItemIdentifier) {
  373. item?.image = NSImage(named: "KMImageNameMainToolPageDelete")
  374. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageDelete")
  375. item?.target = self
  376. item?.btnTag = KMPageEditType.delete.rawValue
  377. item?.toolTip = NSLocalizedString("Delete", comment: "")
  378. item?.titleName = NSLocalizedString("", comment: "")
  379. item?.boxImagePosition = KMToolBoxImagePosition.left
  380. item?.btnAction = #selector(deleteMenuAction)
  381. let menuItem = NSMenuItem.init(title: "Delete", action: #selector(deleteMenuAction), target: self)
  382. item?.menuFormRepresentation = menuItem
  383. }
  384. return item
  385. }
  386. }
  387. // MARK: - KMSelectPopButtonDelegate
  388. extension KMPDFEditToolbar: KMSelectPopButtonDelegate {
  389. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  390. guard let callback = self.itemClick else {
  391. return
  392. }
  393. callback(self, KMPageEditType.pageRange.rawValue)
  394. }
  395. func km_controlTextDidChange(_ obj: KMDesignSelect) {
  396. }
  397. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  398. guard let callback = self.pageRangeValueDidChange else {
  399. return
  400. }
  401. callback(self.pageRangeView?.stringValue, nil)
  402. }
  403. }