// // KMBGTemplateController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/7. // import Cocoa import KMComponentLibrary @objc protocol KMBGTemplateControllerDelegate: AnyObject { @objc optional func templateControllerDidAddData(_ controller: KMBGTemplateController) @objc optional func templateControllerDidEditData(_ controller: KMBGTemplateController, _ data: KMBackgroundModel) //选中内容切换 @objc optional func templateControllerDidSelectedChanged(_ controller: KMBGTemplateController) } class KMBGTemplateController: NSViewController { @IBOutlet var contendView: NSView! @IBOutlet var titleLabel: NSTextField! @IBOutlet var addButton: ComponentButton! @IBOutlet var emptyView: ComponentEmpty! @IBOutlet var scrollView: NSScrollView! @IBOutlet var collectionView: NSCollectionView! weak open var delegate: KMBGTemplateControllerDelegate? var selectedBackground: KMBackgroundModel? override func viewDidLoad() { super.viewDidLoad() // Do view setup here. configUI() setupProperty() reloadData() } func setupProperty() { addButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "watermark_template_add"), keepPressState: false) addButton.setTarget(self, action: #selector(buttonClicked(_:))) emptyView.properties = ComponentEmptyProperty(emptyType: .noWatermark, text: KMLocalizedString("No Template"), subText: KMLocalizedString("Here is the description.")) } func configUI() { view.wantsLayer = true view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor titleLabel.stringValue = KMLocalizedString("Templates") titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold") collectionView.backgroundColors = [NSColor.clear] collectionView.wantsLayer = true collectionView.layer?.backgroundColor = NSColor.clear.cgColor collectionView.delegate = self collectionView.dataSource = self collectionView.allowsEmptySelection = false collectionView.allowsMultipleSelection = false collectionView.register(KMBGTemplateItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBGTemplateItem")) } //刷新所有数据 public func reloadData() { if KMBackgroundManager.defaultManager.datas.count == 0 { collectionView.isHidden = true emptyView.isHidden = false } else { emptyView.isHidden = true collectionView.isHidden = false collectionView.reloadData() updateSelectedState() } } func updateSelectedState() { if let selData = selectedBackground { if let index = KMBackgroundManager.defaultManager.datas.firstIndex(of: selData) { let indexpath = IndexPath(item: index, section: 0) var set = Set() set.insert(indexpath) collectionView.selectItems(at: set, scrollPosition: .top) } } else { collectionView.deselectAll(nil) } } private func collectionViewSelectedChanged() { let indexs = collectionView.selectionIndexPaths if indexs.count > 0 { for index in indexs { let data = KMBackgroundManager.defaultManager.datas[index.item] selectedBackground = data } } else { selectedBackground = nil } delegate?.templateControllerDidSelectedChanged?(self) } //MARK: - action @objc func buttonClicked(_ sender: ComponentButton) { if sender == addButton { if selectedBackground != nil { selectedBackground = nil reloadData() delegate?.templateControllerDidSelectedChanged?(self) } delegate?.templateControllerDidAddData?(self) } } } //MARK: - NSCollectionViewDelegate, NSCollectionViewDataSource extension KMBGTemplateController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout { public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { return KMBackgroundManager.defaultManager.datas.count } public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { if indexPath.item >= KMBackgroundManager.defaultManager.datas.count { return NSCollectionViewItem() } let item: KMBGTemplateItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBGTemplateItem"), for: indexPath) as! KMBGTemplateItem item.delegate = self item.backgroundModel = KMBackgroundManager.defaultManager.datas[indexPath.item] if let selData = selectedBackground { if item.backgroundModel == selData { item.isSelected = true } } item.reloadData() return item } public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize { return CGSize(width: 168, height: 237) } func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 8 } func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 8 } func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets { return NSEdgeInsets(top: 8, left: 0, bottom: 8, right: 0) } public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { collectionViewSelectedChanged() } public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set) { collectionViewSelectedChanged() } } //MARK: - kmNWatermarkTemplateItemDelegate extension KMBGTemplateController: KMBGTemplateItemDelegate { func kmNBGTemplateItemDidEdit(_ view: KMBGTemplateItem) { if let data = view.backgroundModel { delegate?.templateControllerDidEditData?(self, data) } } func kmNBGTemplateItemDidDelete(_ view: KMBGTemplateItem) { if let data = view.backgroundModel { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = String(format: KMLocalizedString("Are you sure you want to remove the background?")) alert.addButton(withTitle: KMLocalizedString("Delete")) alert.addButton(withTitle: KMLocalizedString("Cancel")) alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in if response == .alertFirstButtonReturn { let _ = KMBackgroundManager.defaultManager.deleteTemplate(model: data) if data == self.selectedBackground { self.selectedBackground = nil self.delegate?.templateControllerDidSelectedChanged?(self) } self.reloadData() } } } } // func kmNBGTemplateItemDidUpdateSelectedState(_ view: KMBGTemplateItem) { // if view.itemSelected { // selectedBackground = view.backgroundModel // } else { // selectedBackground = nil // } // // collectionViewSelectedChanged() // } }