KMMainToolbarController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 == KMDocumentAnnotationToolbarItemIdentifier {
  72. let menuItem = NSMenuItem.init(title: "Annotation", action: #selector(annotationButtonAction), target: self)
  73. item?.toolTip = NSLocalizedString("Annotate PDF with highlight, text box and markup tools", comment: "")
  74. item?.setViewWithSizes(self.annotationModelButton)
  75. item?.menuFormRepresentation = menuItem
  76. } else if itemIdentifier.rawValue == KMDocumentPageToolbarItemIdentifier {
  77. let menuItem = NSMenuItem.init(title: "page", action: #selector(leftControllButtonAction), target: self)
  78. item?.setViewWithSizes(self.pageModelButton)
  79. item?.menuFormRepresentation = menuItem
  80. } else if itemIdentifier.rawValue == KMDocumentConversonToolbarItemIdentifier {
  81. let menuItem = NSMenuItem.init(title: "Converson", action: #selector(leftControllButtonAction), target: self)
  82. item?.setViewWithSizes(self.conversionModelButton)
  83. item?.menuFormRepresentation = menuItem
  84. } else if itemIdentifier.rawValue == KMDocumentScanOCRToolbarItemIdentifier {
  85. let menuItem = NSMenuItem.init(title: "scan", action: #selector(leftControllButtonAction), target: self)
  86. item?.setViewWithSizes(self.scanORCModelButton)
  87. item?.menuFormRepresentation = menuItem
  88. } else if itemIdentifier.rawValue == KMDocumentAIToolsToolbarItemIdentifier {
  89. let menuItem = NSMenuItem.init(title: "AI Tools", action: #selector(leftControllButtonAction), target: self)
  90. item?.setViewWithSizes(self.scanORCModelButton)
  91. item?.menuFormRepresentation = menuItem
  92. } else if itemIdentifier.rawValue == KMDocumentEditToolbarItemIdentifier {
  93. let menuItem = NSMenuItem.init(title: "edit", action: #selector(editPDFButtonAction), target: self)
  94. item?.setViewWithSizes(self.editModelButton)
  95. item?.menuFormRepresentation = menuItem
  96. } else if itemIdentifier.rawValue == KMDocumentFillSginToolbarItemIdentifier {
  97. let menuItem = NSMenuItem.init(title: "fillsign", action: #selector(leftControllButtonAction), target: self)
  98. item?.setViewWithSizes(self.signatureModelButton)
  99. item?.menuFormRepresentation = menuItem
  100. }else if itemIdentifier.rawValue == KMDocumentFormToolbarItemIdentifier {
  101. let menuItem = NSMenuItem.init(title: "form", action: #selector(leftControllButtonAction), target: self)
  102. item?.setViewWithSizes(self.formModelButton)
  103. item?.menuFormRepresentation = menuItem
  104. } else if itemIdentifier.rawValue == KMDocumentToolToolbarItemIdentifier {
  105. let menuItem = NSMenuItem.init(title: "tool", action: #selector(leftControllButtonAction), target: self)
  106. item?.setViewWithSizes(self.toolModelButton)
  107. item?.menuFormRepresentation = menuItem
  108. } else if itemIdentifier.rawValue == KMDocumentPrintToolbarItemIdentifier {
  109. let menuItem = NSMenuItem.init(title: "print", action: #selector(leftControllButtonAction), target: self)
  110. item?.setViewWithSizes(self.printModelButton)
  111. item?.menuFormRepresentation = menuItem
  112. } else if itemIdentifier.rawValue == KMDocumentShareToolbarItemIdentifier {
  113. let menuItem = NSMenuItem.init(title: "share", action: #selector(leftControllButtonAction), target: self)
  114. item?.setViewWithSizes(self.shareModelButton)
  115. item?.menuFormRepresentation = menuItem
  116. } else if itemIdentifier.rawValue == KMRightControlToolbarItemIdentifier {
  117. let menuItem = NSMenuItem.init(title: "right", action: #selector(leftControllButtonAction), target: self)
  118. item?.setViewWithSizes(self.rightControlButton)
  119. item?.menuFormRepresentation = menuItem
  120. }
  121. }
  122. if flag == false {
  123. item?.isEnabled = true
  124. }
  125. return item
  126. }
  127. func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  128. return [NSToolbarItem.Identifier.space,
  129. NSToolbarItem.Identifier(KMLeftControlToolbarItemIdentifier),
  130. NSToolbarItem.Identifier.flexibleSpace,
  131. NSToolbarItem.Identifier(KMDocumentAnnotationToolbarItemIdentifier),
  132. NSToolbarItem.Identifier(KMDocumentPageToolbarItemIdentifier),
  133. NSToolbarItem.Identifier(KMDocumentConversonToolbarItemIdentifier),
  134. NSToolbarItem.Identifier(KMDocumentScanOCRToolbarItemIdentifier),
  135. NSToolbarItem.Identifier(KMDocumentAIToolsToolbarItemIdentifier),
  136. NSToolbarItem.Identifier(KMDocumentEditToolbarItemIdentifier),
  137. NSToolbarItem.Identifier(KMDocumentFormToolbarItemIdentifier),
  138. NSToolbarItem.Identifier(KMDocumentFillSginToolbarItemIdentifier),
  139. NSToolbarItem.Identifier(KMDocumentToolToolbarItemIdentifier),
  140. NSToolbarItem.Identifier.space,
  141. NSToolbarItem.Identifier.flexibleSpace,
  142. NSToolbarItem.Identifier(KMDocumentPrintToolbarItemIdentifier),
  143. NSToolbarItem.Identifier(KMDocumentShareToolbarItemIdentifier),
  144. NSToolbarItem.Identifier(KMRightControlToolbarItemIdentifier),
  145. NSToolbarItem.Identifier.space]
  146. }
  147. func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  148. return [NSToolbarItem.Identifier(KMLeftControlToolbarItemIdentifier),
  149. NSToolbarItem.Identifier(KMDocumentAnnotationToolbarItemIdentifier),
  150. NSToolbarItem.Identifier(KMDocumentPageToolbarItemIdentifier),
  151. NSToolbarItem.Identifier(KMDocumentConversonToolbarItemIdentifier),
  152. NSToolbarItem.Identifier(KMDocumentScanOCRToolbarItemIdentifier),
  153. NSToolbarItem.Identifier(KMDocumentAIToolsToolbarItemIdentifier),
  154. NSToolbarItem.Identifier(KMDocumentEditToolbarItemIdentifier),
  155. NSToolbarItem.Identifier(KMDocumentFormToolbarItemIdentifier),
  156. NSToolbarItem.Identifier(KMDocumentFillSginToolbarItemIdentifier),
  157. NSToolbarItem.Identifier(KMDocumentToolToolbarItemIdentifier),
  158. NSToolbarItem.Identifier.space,
  159. NSToolbarItem.Identifier.flexibleSpace,
  160. NSToolbarItem.Identifier(KMDocumentPrintToolbarItemIdentifier),
  161. NSToolbarItem.Identifier(KMDocumentShareToolbarItemIdentifier),
  162. NSToolbarItem.Identifier(KMRightControlToolbarItemIdentifier)]
  163. }
  164. func validateToolbarItem(_ item: NSToolbarItem) -> Bool {
  165. return true
  166. }
  167. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  168. return true
  169. }
  170. }