KMPDFEditToolbar.swift 16 KB

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