// // KMBatesTemplateItem.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/8. // import Cocoa import KMComponentLibrary @objc protocol KMBatesTemplateItemDelegate: AnyObject { @objc optional func kmBatesTemplateItemDidEdit(_ view: KMBatesTemplateItem) @objc optional func kmBatesTemplateItemDidDelete(_ view: KMBatesTemplateItem) } class KMBatesTemplateItem: NSCollectionViewItem { @IBOutlet var watermarkItem: ComponentCardWatermark! @IBOutlet var infoContendView: NSView! @IBOutlet var titleLabel: NSTextField! var dataModel: KMBatesModel? weak open var delegate: KMBatesTemplateItemDelegate? var watermarkProperty: ComponentCardWMProperty = ComponentCardWMProperty(state: .normal, icon: nil, text: "", editIcon: NSImage(named: "watermark_edit"), deleteIcon: NSImage(named: "pageEdit_delete")) override func viewDidLoad() { super.viewDidLoad() // Do view setup here. view.wantsLayer = true view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/hight").cgColor view.layer?.borderWidth = 1 view.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider").cgColor if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/s") { let currentValue = value as! CGFloat view.layer?.cornerRadius = currentValue } watermarkItem.properties = watermarkProperty watermarkItem.delegate = self titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium") } //Setter override var isSelected: Bool { get{ return super.isSelected } set{ super.isSelected = newValue refresh() } } private func refresh() { guard let dataModel = dataModel else { return } watermarkItem.properties.state = isSelected ? .pressed : .normal watermarkItem.reloadData() } func reloadData() { guard let dataModel = dataModel else { return } watermarkItem.properties.state = isSelected ? .pressed : .normal watermarkItem.reloadData() titleLabel.stringValue = dataModel.name infoContendView.wantsLayer = true infoContendView.layer?.masksToBounds = true let subviews = infoContendView.subviews for view in subviews { view.removeFromSuperview() } var yValue = CGRectGetHeight(infoContendView.frame) if dataModel.topLeftString.count > 0 { yValue -= 24 let view = getPositionView(0, dataModel.topLeftString) view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24) view.autoresizingMask = [.width, .minYMargin] infoContendView.addSubview(view) yValue -= 8 } if dataModel.topCenterString.count > 0 { yValue -= 24 let view = getPositionView(1, dataModel.topCenterString) view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24) view.autoresizingMask = [.width, .minYMargin] infoContendView.addSubview(view) yValue -= 8 } if dataModel.topRightString.count > 0 { yValue -= 24 let view = getPositionView(2, dataModel.topRightString) view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24) view.autoresizingMask = [.width, .minYMargin] infoContendView.addSubview(view) yValue -= 8 } if dataModel.bottomLeftString.count > 0 { yValue -= 24 let view = getPositionView(3, dataModel.bottomLeftString) view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24) view.autoresizingMask = [.width, .minYMargin] infoContendView.addSubview(view) yValue -= 8 } if dataModel.bottomCenterString.count > 0 { yValue -= 24 let view = getPositionView(4, dataModel.bottomCenterString) view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24) view.autoresizingMask = [.width, .minYMargin] infoContendView.addSubview(view) yValue -= 8 } if dataModel.bottomRightString.count > 0 { yValue -= 24 let view = getPositionView(5, dataModel.bottomRightString) view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24) view.autoresizingMask = [.width, .minYMargin] infoContendView.addSubview(view) yValue -= 8 } } func getPositionView(_ type: Int, _ text: String) -> NSView { let view = NSView(frame: CGRectMake(0, 0, 200, 24)) let label = NSTextField(frame: CGRectMake(56, 0, 40, 20)) label.stringValue = text label.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1") label.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular") label.sizeToFit() label.frame = CGRectMake(56, (24-label.frame.size.height)/2, label.frame.size.width, label.frame.size.height) label.autoresizingMask = [.maxXMargin, .minYMargin] label.isBordered = false label.drawsBackground = false label.isEditable = false view.addSubview(label) let position = ComponentCPosition() position.frame = CGRectMake(0, 0, 48, 24) position.properties = ComponentCPositionProperty(rowCount: 2, columnCount: 3, dash: true, isDisable: true) if type == 0 { position.properties.selRow = 1 position.properties.selColumn = 0 } else if type == 1 { position.properties.selRow = 1 position.properties.selColumn = 1 } else if type == 2 { position.properties.selRow = 1 position.properties.selColumn = 2 } else if type == 3 { position.properties.selRow = 0 position.properties.selColumn = 0 } else if type == 4 { position.properties.selRow = 0 position.properties.selColumn = 1 } else if type == 5 { position.properties.selRow = 0 position.properties.selColumn = 2 } position.reloadData() view.addSubview(position) return view } } //MARK: - ComponentCardWatermarkDelegate extension KMBatesTemplateItem: ComponentCardWatermarkDelegate { func componentSliderDidEdit(_ view: ComponentCardWatermark) { delegate?.kmBatesTemplateItemDidEdit?(self) } func componentSliderDidDelete(_ view: ComponentCardWatermark) { delegate?.kmBatesTemplateItemDidDelete?(self) } }