// // KMHomeOpenView.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/10/8. // import Cocoa import KMComponentLibrary @objc protocol KMHomeOpenViewDelegate: AnyObject { //选择文件 @objc optional func homeOpenViewDidChooseFileURL(_ view: KMHomeOpenView?, _ url:URL) } class KMHomeOpenView: BaseXibView { @IBOutlet weak var contendView: NSView! @IBOutlet weak var openFileButton: ComponentButton! @IBOutlet var createFileButton: ComponentButton! var groupView: ComponentGroup! var createWC: KMURLCreatePDFWindowController = KMURLCreatePDFWindowController(windowNibName: "KMURLCreatePDFWindowController") weak open var delegate: KMHomeOpenViewDelegate? override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } public required init?(coder decoder: NSCoder) { super.init(coder: decoder) } override init(frame frameRect: NSRect) { super.init(frame: frameRect) } public override func awakeFromNib() { super.awakeFromNib() setUpButtonProperty() } func setUpButtonProperty() { openFileButton.properties = ComponentButtonProperty(type: .primary, size: .m, state: .normal, buttonText: KMLocalizedString("Open Files"), keepPressState: false) openFileButton.setTarget(self, action: #selector(openFileAction(_:))) createFileButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .m, showRightIcon: true, buttonText: KMLocalizedString("Create PDF")) createFileButton.properties.propertyInfo.rightIcon_nor = NSImage(named: "arrowDown") createFileButton.properties.propertyInfo.rightIcon_press = NSImage(named: "arrowDown_press") createFileButton.reloadData() createFileButton.setTarget(self, action: #selector(createFileAction(_:))) } //MARK: - GroupView func showGroupView() { var viewHeight: CGFloat = 8 var menuItemArr: [ComponentMenuitemProperty] = [] for i in ["New From Images", "New Blank Page", "New From Web Page", "New From Clipboard", " ", "Import From Scanner"] { if i == " " { let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider() menuItemArr.append(property) viewHeight += 8 } else { let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: KMLocalizedString(i)) menuItemArr.append(properties_Menuitem) viewHeight += 36 } } if groupView == nil { groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle()) } groupView.groupDelegate = self groupView?.frame = CGRectMake(310, 0, CGRectGetWidth(self.bounds), viewHeight) groupView.updateGroupInfo(menuItemArr) var point = convert(createFileButton.frame.origin, to: nil) point.y -= viewHeight groupView.showWithPoint(point, relativeTo: createFileButton) createFileButton.properties.state = .pressed createFileButton.reloadData() } func removeGroupView() { if groupView != nil { groupView.removeFromSuperview() } createFileButton.properties.state = .normal createFileButton.reloadData() } //MARK: - Action @objc func openFileAction(_ sender: NSView) { let openPanel = NSOpenPanel() openPanel.allowedFileTypes = ["pdf", "PDF"] openPanel.allowsMultipleSelection = false openPanel.beginSheetModal(for: self.window!) { [weak self] result in if result == NSApplication.ModalResponse.OK { if let url = openPanel.url { self?.delegate?.homeOpenViewDidChooseFileURL?(self, url) } } } } @objc func createFileAction(_ sender: NSView) { showGroupView() } } //MARK: - ComponentDropdownDelegate extension KMHomeOpenView: ComponentDropdownDelegate { func componentDropdownDidShowMenuItem(dropdown: ComponentDropdown) { showGroupView() } } //MARK: - ComponentGroupDelegate extension KMHomeOpenView: ComponentGroupDelegate { func componentGroupDidDismiss(group: ComponentGroup?) { removeGroupView() } func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) { if let selItem = menuItemProperty { let index = group?.menuItemArr.firstIndex(of: selItem) if index == 0 { if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController { _windowC.newFromImageFile() } } else if index == 1 { if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController { _windowC.newBlankPage() } } else if index == 2 { if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController { _windowC.newFromWebPage() } } else if index == 3 { if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController { _windowC.newFromClipboard() } } else if index == 5 { if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController { _windowC.importFromScanner() } } } } }