123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- //
- // 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 = true
- 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<IndexPath>()
- 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 = KMBackgroundManager.defaultManager.datas[index.item]
- selectedBackground = data
- }
- } else {
- selectedBackground = nil
- }
-
- delegate?.templateControllerDidSelectedChanged?(self)
- }
-
- //MARK: - action
- @objc func buttonClicked(_ sender: ComponentButton) {
- if sender == addButton {
-
- 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<IndexPath>) {
- collectionViewSelectedChanged()
- }
-
- public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
- 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()
- }
- }
- }
- }
- }
|