// // KMUserListItemView.swift // PDF Reader Pro Edition // // Created by tangchao on 2024/7/11. // import Cocoa class KMUserListItemView: NSView, NibLoadable { @IBOutlet weak var box: NSBox! @IBOutlet weak var scrollView: NSScrollView! @IBOutlet weak var tableView: NSTableView! @IBOutlet weak var boxTopConst: NSLayoutConstraint! @IBOutlet weak var tipBox: NSBox! @IBOutlet weak var tipIv: NSImageView! @IBOutlet weak var tipLabel: NSTextField! var validateDropCallback: ((_ info: NSDraggingInfo, _ row: Int, _ dropOperation: NSTableView.DropOperation)->NSDragOperation)? var acceptDropCallback: ((_ info: NSDraggingInfo, _ row: Int, _ dropOperation: NSTableView.DropOperation)->Bool)? var datas: [KMUserFbListModel] = [] { didSet { self.wantsLayer = true self.layer?.borderWidth = 1 if self.datas.isEmpty { self.layer?.backgroundColor = .clear self.layer?.borderColor = .clear self.layer?.cornerRadius = 0 } else { self.layer?.backgroundColor = NSColor(white: 0, alpha: 0.05).cgColor self.layer?.borderColor = NSColor(white: 0, alpha: 0.05).cgColor self.layer?.cornerRadius = 4 } self.tableView.reloadData() } } var itemClick: ((_ idx: Int, _ param: Any?)->Void)? override func awakeFromNib() { super.awakeFromNib() self.box.borderWidth = 0 self.tableView.delegate = self self.tableView.dataSource = self self.tableView.rowHeight = 24 self.tableView.register(.init(nibNamed: "KMUserListItemCellView", bundle: nil), forIdentifier: .init("KMUserListItemCellView")) // self.tableView.enclosingScrollView. self.tableView.enclosingScrollView?.wantsLayer = true self.tableView.enclosingScrollView?.borderType = .noBorder // self.tableView.enclosingScrollView?.layer?.cornerRadius = 5 // self.tableView.enclosingScrollView?.layer?.masksToBounds = true self.tableView.enclosingScrollView?.drawsBackground = false self.tableView.enclosingScrollView?.backgroundColor = .clear self.tableView.enclosingScrollView?.layer?.backgroundColor = .clear self.tableView.backgroundColor = .clear self.tableView.registerForDraggedTypes([.fileURL]) self.tipBox.borderWidth = 0 self.tipBox.cornerRadius = 0 // self.tipBox.fillColor = NSColor.km_init(hex: "#FEE4EC") self.tipIv.image = NSImage(named: "KMImageNameUserFbFileSizeTipIcon") self.tipBox.isHidden = true } func showTip(tip: String) { self.tipLabel.stringValue = tip self.boxTopConst.animator().constant = 30 DispatchQueue.main.async { self.tipBox.isHidden = false } DispatchQueue.main.asyncAfter(deadline: .now()+2) { self.hiddenTip() } } func hiddenTip() { self.boxTopConst.animator().constant = 0 DispatchQueue.main.async { self.tipBox.isHidden = true } } } extension KMUserListItemView: NSTableViewDelegate, NSTableViewDataSource { func numberOfRows(in tableView: NSTableView) -> Int { return self.datas.count } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { var cell = tableView.makeView(withIdentifier: .init("KMUserListItemCellView"), owner: self) if cell == nil { cell = KMUserListItemCellView.createFromNib() // cell?.wantsLayer = true // cell?.layer?.backgroundColor = NSColor.red.cgColor } let cellView = cell as? KMUserListItemCellView let model = self.datas[row] cellView?.fileNameLabel.stringValue = model.fileName ?? "" cellView?.fileSizeLabel.stringValue = model.fileSizeString ?? "" cellView?.button.image = NSImage(named: "KMImageNameUserFbDeleteIcon") cellView?.box.fillColor = .clear cellView?.itemClick = { [weak self] idx in if idx == 1 { // remove self?.itemClick?(row, nil) } } return cell } func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation { if let block = self.validateDropCallback { return block(info, row, dropOperation) } return .generic } func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool { if let block = self.acceptDropCallback { return block(info, row, dropOperation) } let pborad = info.draggingPasteboard guard let _ = pborad.availableType(from: [.fileURL]) else { return false } guard let items = pborad.pasteboardItems else { return false } var fileNames: [String] = [] for item in items { let string = item.propertyList(forType: .fileURL) as? String ?? "" fileNames.append(string) } return true } }