KMMainToolbarController.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 == KMDocumentEditToolbarItemIdentifier {
  89. let menuItem = NSMenuItem.init(title: "edit", action: #selector(editPDFButtonAction), target: self)
  90. item?.setViewWithSizes(self.editModelButton)
  91. item?.menuFormRepresentation = menuItem
  92. } else if itemIdentifier.rawValue == KMDocumentFillSginToolbarItemIdentifier {
  93. let menuItem = NSMenuItem.init(title: "fillsign", action: #selector(leftControllButtonAction), target: self)
  94. item?.setViewWithSizes(self.signatureModelButton)
  95. item?.menuFormRepresentation = menuItem
  96. }else if itemIdentifier.rawValue == KMDocumentFormToolbarItemIdentifier {
  97. let menuItem = NSMenuItem.init(title: "form", action: #selector(leftControllButtonAction), target: self)
  98. item?.setViewWithSizes(self.formModelButton)
  99. item?.menuFormRepresentation = menuItem
  100. } else if itemIdentifier.rawValue == KMDocumentToolToolbarItemIdentifier {
  101. let menuItem = NSMenuItem.init(title: "tool", action: #selector(leftControllButtonAction), target: self)
  102. item?.setViewWithSizes(self.toolModelButton)
  103. item?.menuFormRepresentation = menuItem
  104. } else if itemIdentifier.rawValue == KMDocumentPrintToolbarItemIdentifier {
  105. let menuItem = NSMenuItem.init(title: "print", action: #selector(leftControllButtonAction), target: self)
  106. item?.setViewWithSizes(self.printModelButton)
  107. item?.menuFormRepresentation = menuItem
  108. } else if itemIdentifier.rawValue == KMDocumentShareToolbarItemIdentifier {
  109. let menuItem = NSMenuItem.init(title: "share", action: #selector(leftControllButtonAction), target: self)
  110. item?.setViewWithSizes(self.shareModelButton)
  111. item?.menuFormRepresentation = menuItem
  112. } else if itemIdentifier.rawValue == KMRightControlToolbarItemIdentifier {
  113. let menuItem = NSMenuItem.init(title: "right", action: #selector(leftControllButtonAction), target: self)
  114. item?.setViewWithSizes(self.rightControlButton)
  115. item?.menuFormRepresentation = menuItem
  116. }
  117. }
  118. if flag == false {
  119. item?.isEnabled = true
  120. }
  121. return item
  122. }
  123. func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  124. return [NSToolbarItem.Identifier.space,
  125. NSToolbarItem.Identifier(KMLeftControlToolbarItemIdentifier),
  126. NSToolbarItem.Identifier.flexibleSpace,
  127. NSToolbarItem.Identifier(KMDocumentAnnotationToolbarItemIdentifier),
  128. NSToolbarItem.Identifier(KMDocumentPageToolbarItemIdentifier),
  129. NSToolbarItem.Identifier(KMDocumentConversonToolbarItemIdentifier),
  130. NSToolbarItem.Identifier(KMDocumentScanOCRToolbarItemIdentifier),
  131. NSToolbarItem.Identifier(KMDocumentEditToolbarItemIdentifier),
  132. NSToolbarItem.Identifier(KMDocumentFormToolbarItemIdentifier),
  133. NSToolbarItem.Identifier(KMDocumentFillSginToolbarItemIdentifier),
  134. NSToolbarItem.Identifier(KMDocumentToolToolbarItemIdentifier),
  135. NSToolbarItem.Identifier.space,
  136. NSToolbarItem.Identifier.flexibleSpace,
  137. NSToolbarItem.Identifier(KMDocumentPrintToolbarItemIdentifier),
  138. NSToolbarItem.Identifier(KMDocumentShareToolbarItemIdentifier),
  139. NSToolbarItem.Identifier(KMRightControlToolbarItemIdentifier),
  140. NSToolbarItem.Identifier.space]
  141. }
  142. func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  143. return [NSToolbarItem.Identifier(KMLeftControlToolbarItemIdentifier),
  144. NSToolbarItem.Identifier(KMDocumentAnnotationToolbarItemIdentifier),
  145. NSToolbarItem.Identifier(KMDocumentPageToolbarItemIdentifier),
  146. NSToolbarItem.Identifier(KMDocumentConversonToolbarItemIdentifier),
  147. NSToolbarItem.Identifier(KMDocumentScanOCRToolbarItemIdentifier),
  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. }
  158. func validateToolbarItem(_ item: NSToolbarItem) -> Bool {
  159. return true
  160. }
  161. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  162. return true
  163. }
  164. }