KMMainToolbarController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // KMMainToolbarController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/12/1.
  6. //
  7. import Cocoa
  8. class SKToolbar : NSToolbar {
  9. func allowsSizeMode(sizeMode:NSToolbar.SizeMode) -> Bool {
  10. return true
  11. }
  12. }
  13. @objc protocol KMMainToolbarControllerDelegate {
  14. @objc optional func changeModelAction(mode : CToolMode)
  15. }
  16. class KMMainToolbarController: NSViewController{
  17. @IBOutlet weak var leftControlButton: NSButton!
  18. @IBOutlet weak var pagebox: NSBox!
  19. @IBOutlet weak var beforePageButton: NSButton!
  20. @IBOutlet weak var nextPageButton: NSButton!
  21. @IBOutlet weak var annotationModelButton: NSButton!
  22. @IBOutlet weak var pageModelButton: NSButton!
  23. @IBOutlet weak var conversionModelButton: NSButton!
  24. @IBOutlet weak var scanORCModelButton: NSButton!
  25. @IBOutlet weak var editModelButton: NSButton!
  26. @IBOutlet weak var formModelButton: NSButton!
  27. @IBOutlet weak var signatureModelButton: NSButton!
  28. @IBOutlet weak var toolModelButton: NSButton!
  29. @IBOutlet weak var printModelButton: NSButton!
  30. @IBOutlet weak var shareModelButton: NSButton!
  31. @IBOutlet weak var rightControlButton: NSButton!
  32. @IBOutlet weak var zoomView: NSView!
  33. @IBOutlet weak var mainWindowController : KMBrowserWindowController!
  34. open weak var delegate: KMMainToolbarControllerDelegate?
  35. var toolbarItems : [NSToolbarItem.Identifier : Any] = [:]
  36. private func nibName() -> String {
  37. return "KMMainToolbarController"
  38. }
  39. func setupToolbar() {
  40. _ = self.view
  41. let toolbar : SKToolbar = SKToolbar.init(identifier: "KMDocumentToolbar")
  42. // toolbar.allowsUserCustomization = true
  43. toolbar.autosavesConfiguration = true
  44. toolbar.displayMode = .default
  45. toolbar.delegate = self
  46. self.mainWindowController.window?.toolbar = toolbar
  47. self.mainWindowController.window?.titlebarAppearsTransparent = true
  48. // self.mainWindowController.window?.backgroundColor = NSColor.red
  49. }
  50. //MARK: Action
  51. @IBAction func annotationButtonAction(item:NSMenuItem) {
  52. }
  53. @IBAction func leftControllButtonAction(item:NSMenuItem) {
  54. }
  55. @IBAction func editPDFButtonAction(item:NSMenuItem) {
  56. self.delegate?.changeModelAction?(mode: .editPDFToolMode)
  57. }
  58. }
  59. //MARK: NSToolbarDelegate
  60. extension KMMainToolbarController : NSToolbarDelegate ,NSToolbarItemValidation,NSMenuItemValidation{
  61. func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
  62. var item = self.toolbarItems[itemIdentifier] as? KMToolbarItem
  63. if item == nil {
  64. item = KMToolbarItem.init(itemIdentifier: itemIdentifier)
  65. self.toolbarItems[itemIdentifier] = item
  66. if itemIdentifier.rawValue == KMLeftControlToolbarItemIdentifier {
  67. let menuItem = NSMenuItem.init(title: "left", action: #selector(leftControllButtonAction), target: self)
  68. item?.toolTip = NSLocalizedString("left", comment: "")
  69. item?.setViewWithSizes(self.leftControlButton)
  70. item?.menuFormRepresentation = menuItem
  71. } else if itemIdentifier.rawValue == KMDocumentZoomToolbarItemIdentifier {
  72. let menuItem = NSMenuItem.init(title: "zoom", action: #selector(annotationButtonAction), target: self)
  73. // item?.toolTip = NSLocalizedString("Annotate PDF with highlight, text box and markup tools", comment: "")
  74. item?.setViewWithSizes(self.zoomView)
  75. item?.menuFormRepresentation = menuItem
  76. }else if itemIdentifier.rawValue == KMDocumentAnnotationToolbarItemIdentifier {
  77. let menuItem = NSMenuItem.init(title: "Annotation", action: #selector(annotationButtonAction), target: self)
  78. item?.toolTip = NSLocalizedString("Annotate PDF with highlight, text box and markup tools", comment: "")
  79. item?.setViewWithSizes(self.annotationModelButton)
  80. item?.menuFormRepresentation = menuItem
  81. } else if itemIdentifier.rawValue == KMDocumentNextPageToolbarItemIdentifier {
  82. let menuItem = NSMenuItem.init(title: "next", action: #selector(leftControllButtonAction), target: self)
  83. item?.toolTip = NSLocalizedString("nexts", comment: "")
  84. item?.setViewWithSizes(self.pagebox)
  85. item?.menuFormRepresentation = menuItem
  86. } else if itemIdentifier.rawValue == KMDocumentPageToolbarItemIdentifier {
  87. let menuItem = NSMenuItem.init(title: "page", action: #selector(leftControllButtonAction), target: self)
  88. item?.setViewWithSizes(self.pageModelButton)
  89. item?.menuFormRepresentation = menuItem
  90. } else if itemIdentifier.rawValue == KMDocumentConversonToolbarItemIdentifier {
  91. let menuItem = NSMenuItem.init(title: "Converson", action: #selector(leftControllButtonAction), target: self)
  92. item?.setViewWithSizes(self.conversionModelButton)
  93. item?.menuFormRepresentation = menuItem
  94. } else if itemIdentifier.rawValue == KMDocumentScanOCRToolbarItemIdentifier {
  95. let menuItem = NSMenuItem.init(title: "scan", action: #selector(leftControllButtonAction), target: self)
  96. item?.setViewWithSizes(self.scanORCModelButton)
  97. item?.menuFormRepresentation = menuItem
  98. } else if itemIdentifier.rawValue == KMDocumentAIToolsToolbarItemIdentifier {
  99. let menuItem = NSMenuItem.init(title: "AI Tools", action: #selector(leftControllButtonAction), target: self)
  100. item?.setViewWithSizes(self.scanORCModelButton)
  101. item?.menuFormRepresentation = menuItem
  102. } else if itemIdentifier.rawValue == KMDocumentEditToolbarItemIdentifier {
  103. let menuItem = NSMenuItem.init(title: "edit", action: #selector(editPDFButtonAction), target: self)
  104. item?.setViewWithSizes(self.editModelButton)
  105. item?.menuFormRepresentation = menuItem
  106. } else if itemIdentifier.rawValue == KMDocumentFillSginToolbarItemIdentifier {
  107. let menuItem = NSMenuItem.init(title: "fillsign", action: #selector(leftControllButtonAction), target: self)
  108. item?.setViewWithSizes(self.signatureModelButton)
  109. item?.menuFormRepresentation = menuItem
  110. }else if itemIdentifier.rawValue == KMDocumentFormToolbarItemIdentifier {
  111. let menuItem = NSMenuItem.init(title: "form", action: #selector(leftControllButtonAction), target: self)
  112. item?.setViewWithSizes(self.formModelButton)
  113. item?.menuFormRepresentation = menuItem
  114. } else if itemIdentifier.rawValue == KMDocumentToolToolbarItemIdentifier {
  115. let menuItem = NSMenuItem.init(title: "tool", action: #selector(leftControllButtonAction), target: self)
  116. item?.setViewWithSizes(self.toolModelButton)
  117. item?.menuFormRepresentation = menuItem
  118. } else if itemIdentifier.rawValue == KMDocumentPrintToolbarItemIdentifier {
  119. let menuItem = NSMenuItem.init(title: "print", action: #selector(leftControllButtonAction), target: self)
  120. item?.setViewWithSizes(self.printModelButton)
  121. item?.menuFormRepresentation = menuItem
  122. } else if itemIdentifier.rawValue == KMDocumentShareToolbarItemIdentifier {
  123. let menuItem = NSMenuItem.init(title: "share", action: #selector(leftControllButtonAction), target: self)
  124. item?.setViewWithSizes(self.shareModelButton)
  125. item?.menuFormRepresentation = menuItem
  126. } else if itemIdentifier.rawValue == KMRightControlToolbarItemIdentifier {
  127. let menuItem = NSMenuItem.init(title: "right", action: #selector(leftControllButtonAction), target: self)
  128. item?.setViewWithSizes(self.rightControlButton)
  129. item?.menuFormRepresentation = menuItem
  130. }
  131. }
  132. if flag == false {
  133. item?.isEnabled = true
  134. }
  135. return item
  136. }
  137. func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  138. return [NSToolbarItem.Identifier.space,
  139. NSToolbarItem.Identifier(KMLeftControlToolbarItemIdentifier),
  140. NSToolbarItem.Identifier(KMDocumentZoomToolbarItemIdentifier),
  141. NSToolbarItem.Identifier(KMDocumentNextPageToolbarItemIdentifier),
  142. NSToolbarItem.Identifier.flexibleSpace,
  143. NSToolbarItem.Identifier(KMDocumentAnnotationToolbarItemIdentifier),
  144. NSToolbarItem.Identifier(KMDocumentPageToolbarItemIdentifier),
  145. NSToolbarItem.Identifier(KMDocumentConversonToolbarItemIdentifier),
  146. NSToolbarItem.Identifier(KMDocumentScanOCRToolbarItemIdentifier),
  147. NSToolbarItem.Identifier(KMDocumentAIToolsToolbarItemIdentifier),
  148. NSToolbarItem.Identifier(KMDocumentEditToolbarItemIdentifier),
  149. NSToolbarItem.Identifier(KMDocumentFormToolbarItemIdentifier),
  150. NSToolbarItem.Identifier(KMDocumentFillSginToolbarItemIdentifier),
  151. NSToolbarItem.Identifier(KMDocumentToolToolbarItemIdentifier),
  152. NSToolbarItem.Identifier.space,
  153. NSToolbarItem.Identifier.flexibleSpace,
  154. NSToolbarItem.Identifier(KMDocumentPrintToolbarItemIdentifier),
  155. NSToolbarItem.Identifier(KMDocumentShareToolbarItemIdentifier),
  156. NSToolbarItem.Identifier(KMRightControlToolbarItemIdentifier),
  157. NSToolbarItem.Identifier.space]
  158. }
  159. func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  160. return [NSToolbarItem.Identifier(KMLeftControlToolbarItemIdentifier),
  161. NSToolbarItem.Identifier(KMDocumentZoomToolbarItemIdentifier),
  162. NSToolbarItem.Identifier(KMDocumentNextPageToolbarItemIdentifier),
  163. NSToolbarItem.Identifier(KMDocumentAnnotationToolbarItemIdentifier),
  164. NSToolbarItem.Identifier(KMDocumentPageToolbarItemIdentifier),
  165. NSToolbarItem.Identifier(KMDocumentConversonToolbarItemIdentifier),
  166. NSToolbarItem.Identifier(KMDocumentScanOCRToolbarItemIdentifier),
  167. NSToolbarItem.Identifier(KMDocumentAIToolsToolbarItemIdentifier),
  168. NSToolbarItem.Identifier(KMDocumentEditToolbarItemIdentifier),
  169. NSToolbarItem.Identifier(KMDocumentFormToolbarItemIdentifier),
  170. NSToolbarItem.Identifier(KMDocumentFillSginToolbarItemIdentifier),
  171. NSToolbarItem.Identifier(KMDocumentToolToolbarItemIdentifier),
  172. NSToolbarItem.Identifier.space,
  173. NSToolbarItem.Identifier.flexibleSpace,
  174. NSToolbarItem.Identifier(KMDocumentPrintToolbarItemIdentifier),
  175. NSToolbarItem.Identifier(KMDocumentShareToolbarItemIdentifier),
  176. NSToolbarItem.Identifier(KMRightControlToolbarItemIdentifier)]
  177. }
  178. func validateToolbarItem(_ item: NSToolbarItem) -> Bool {
  179. return true
  180. }
  181. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  182. return true
  183. }
  184. }