// // KMBatesTemplateController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/8. // import Cocoa import KMComponentLibrary @objc protocol KMBatesTemplateControllerDelegate: AnyObject { @objc optional func templateControllerDidAddData(_ controller: KMBatesTemplateController) @objc optional func templateControllerDidEditData(_ controller: KMBatesTemplateController, _ data: KMBatesModel) //选中内容切换 @objc optional func templateControllerDidSelectedChanged(_ controller: KMBatesTemplateController) } class KMBatesTemplateController: 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: KMBatesTemplateControllerDelegate? var selectedDataModel: KMBatesModel? 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 = true collectionView.allowsMultipleSelection = false collectionView.register(KMBatesTemplateItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatesTemplateItem")) } //刷新所有数据 public func reloadData() { if KMBatesManager.defaultManager.datas.count == 0 { collectionView.isHidden = true emptyView.isHidden = false } else { emptyView.isHidden = true collectionView.isHidden = false collectionView.reloadData() updateSelectedState() } } public func updateSelectedState() { if let selData = selectedDataModel { if let index = KMBatesManager.defaultManager.datas.firstIndex(of: selData) { let indexpath = IndexPath(item: index, section: 0) var set = Set() set.insert(indexpath) collectionView.selectItems(at: set, scrollPosition: .top) } } } private func collectionViewSelectedChanged() { let indexs = collectionView.selectionIndexPaths if indexs.count > 0 { for index in indexs { let data = KMBatesManager.defaultManager.datas[index.item] selectedDataModel = data } } else { selectedDataModel = nil } DispatchQueue.main.async { self.delegate?.templateControllerDidSelectedChanged?(self) } } //MARK: - action @objc func buttonClicked(_ sender: ComponentButton) { if sender == addButton { if selectedDataModel != nil { selectedDataModel = nil delegate?.templateControllerDidSelectedChanged?(self) } reloadData() delegate?.templateControllerDidAddData?(self) } } } //MARK: - NSCollectionViewDelegate, NSCollectionViewDataSource extension KMBatesTemplateController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout { public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { return KMBatesManager.defaultManager.datas.count } public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { if indexPath.item >= KMBatesManager.defaultManager.datas.count { return NSCollectionViewItem() } let item: KMBatesTemplateItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatesTemplateItem"), for: indexPath) as! KMBatesTemplateItem item.delegate = self item.dataModel = KMBatesManager.defaultManager.datas[indexPath.item] item.reloadData() return item } public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize { var itemHeight = 56 let dataModel = KMBatesManager.defaultManager.datas[indexPath.item] if dataModel.topLeftString.count > 0 { itemHeight += 24 itemHeight += 8 } if dataModel.topCenterString.count > 0 { itemHeight += 24 itemHeight += 8 } if dataModel.topRightString.count > 0 { itemHeight += 24 itemHeight += 8 } if dataModel.bottomLeftString.count > 0 { itemHeight += 24 itemHeight += 8 } if dataModel.bottomCenterString.count > 0 { itemHeight += 24 itemHeight += 8 } if dataModel.bottomRightString.count > 0 { itemHeight += 24 itemHeight += 8 } itemHeight -= 8 return CGSize(width: 232, height: itemHeight) } 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 KMBatesTemplateController: KMBatesTemplateItemDelegate { func kmBatesTemplateItemDidEdit(_ view: KMBatesTemplateItem) { if let data = view.dataModel { delegate?.templateControllerDidEditData?(self, data) } } func kmBatesTemplateItemDidDelete(_ view: KMBatesTemplateItem) { if let data = view.dataModel { 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 _ = KMBatesManager.defaultManager.deleteTemplate(data) if data == self.selectedDataModel { self.selectedDataModel = nil self.delegate?.templateControllerDidSelectedChanged?(self) } self.reloadData() } } } } }