KMHomeOpenView.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // KMHomeOpenView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMHomeOpenViewDelegate: AnyObject {
  10. //选择文件
  11. @objc optional func homeOpenViewDidChooseFileURL(_ view: KMHomeOpenView?, _ url:URL)
  12. }
  13. class KMHomeOpenView: BaseXibView {
  14. @IBOutlet weak var contendView: NSView!
  15. @IBOutlet weak var openFileButton: ComponentButton!
  16. @IBOutlet var createFileButton: ComponentButton!
  17. var groupView: ComponentGroup!
  18. var createWC: KMURLCreatePDFWindowController = KMURLCreatePDFWindowController(windowNibName: "KMURLCreatePDFWindowController")
  19. weak open var delegate: KMHomeOpenViewDelegate?
  20. override func draw(_ dirtyRect: NSRect) {
  21. super.draw(dirtyRect)
  22. // Drawing code here.
  23. }
  24. public required init?(coder decoder: NSCoder) {
  25. super.init(coder: decoder)
  26. }
  27. override init(frame frameRect: NSRect) {
  28. super.init(frame: frameRect)
  29. }
  30. public override func awakeFromNib() {
  31. super.awakeFromNib()
  32. setUpButtonProperty()
  33. }
  34. func setUpButtonProperty() {
  35. openFileButton.properties = ComponentButtonProperty(type: .primary,
  36. size: .m,
  37. state: .normal,
  38. buttonText: KMLocalizedString("Open Files"),
  39. keepPressState: false)
  40. openFileButton.setTarget(self, action: #selector(openFileAction(_:)))
  41. createFileButton.properties = ComponentButtonProperty(type: .default_tertiary,
  42. size: .m,
  43. showRightIcon: true,
  44. buttonText: KMLocalizedString("Create PDF"))
  45. createFileButton.properties.propertyInfo.rightIcon_nor = NSImage(named: "arrowDown")
  46. createFileButton.properties.propertyInfo.rightIcon_press = NSImage(named: "arrowDown_press")
  47. createFileButton.reloadData()
  48. createFileButton.setTarget(self, action: #selector(createFileAction(_:)))
  49. }
  50. //MARK: - GroupView
  51. func showGroupView() {
  52. var viewHeight: CGFloat = 8
  53. var menuItemArr: [ComponentMenuitemProperty] = []
  54. for i in ["New From Images", "New Blank Page", "New From Web Page", "New From Clipboard", " ", "Import From Scanner"] {
  55. if i == " " {
  56. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  57. menuItemArr.append(property)
  58. viewHeight += 8
  59. } else {
  60. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  61. itemSelected: false,
  62. isDisabled: false,
  63. keyEquivalent: nil,
  64. text: KMLocalizedString(i))
  65. menuItemArr.append(properties_Menuitem)
  66. viewHeight += 36
  67. }
  68. }
  69. if groupView == nil {
  70. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  71. }
  72. groupView.groupDelegate = self
  73. groupView?.frame = CGRectMake(310, 0, CGRectGetWidth(self.bounds), viewHeight)
  74. groupView.updateGroupInfo(menuItemArr)
  75. var point = convert(createFileButton.frame.origin, to: nil)
  76. point.y -= viewHeight
  77. groupView.showWithPoint(point, relativeTo: createFileButton)
  78. createFileButton.properties.state = .pressed
  79. createFileButton.reloadData()
  80. }
  81. func removeGroupView() {
  82. if groupView != nil {
  83. groupView.removeFromSuperview()
  84. }
  85. createFileButton.properties.state = .normal
  86. createFileButton.reloadData()
  87. }
  88. //MARK: - Action
  89. @objc func openFileAction(_ sender: NSView) {
  90. let openPanel = NSOpenPanel()
  91. openPanel.allowedFileTypes = ["pdf", "PDF"]
  92. openPanel.allowsMultipleSelection = false
  93. openPanel.beginSheetModal(for: self.window!) { [weak self] result in
  94. if result == NSApplication.ModalResponse.OK {
  95. if let url = openPanel.url {
  96. self?.delegate?.homeOpenViewDidChooseFileURL?(self, url)
  97. }
  98. }
  99. }
  100. }
  101. @objc func createFileAction(_ sender: NSView) {
  102. showGroupView()
  103. }
  104. }
  105. //MARK: - ComponentDropdownDelegate
  106. extension KMHomeOpenView: ComponentDropdownDelegate {
  107. func componentDropdownDidShowMenuItem(dropdown: ComponentDropdown) {
  108. showGroupView()
  109. }
  110. }
  111. //MARK: - ComponentGroupDelegate
  112. extension KMHomeOpenView: ComponentGroupDelegate {
  113. func componentGroupDidDismiss(group: ComponentGroup?) {
  114. removeGroupView()
  115. }
  116. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  117. if let selItem = menuItemProperty {
  118. let index = group?.menuItemArr.firstIndex(of: selItem)
  119. if index == 0 {
  120. if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
  121. _windowC.newFromImageFile()
  122. }
  123. } else if index == 1 {
  124. if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
  125. _windowC.newBlankPage()
  126. }
  127. } else if index == 2 {
  128. if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
  129. _windowC.newFromWebPage()
  130. }
  131. } else if index == 3 {
  132. if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
  133. _windowC.newFromClipboard()
  134. }
  135. } else if index == 5 {
  136. if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
  137. _windowC.importFromScanner()
  138. }
  139. }
  140. }
  141. }
  142. }