// // KMBOTAOutlineRowView.swift // PDF Reader Pro // // Created by lizhe on 2023/4/2. // import Cocoa import KMComponentLibrary typealias KMBOTAOutlineRowViewHoverCallback = (_ mouseEntered: Bool, _ mouseBox: KMBox) -> Void typealias KMBOTAOutlineRowViewRightMouseCallback = (_ view: KMBOTAOutlineRowView, _ event: NSEvent) -> Void typealias KMBOTAOutlineRowViewMouseDownCallback = (_ view: KMBOTAOutlineRowView, _ event: NSEvent) -> Void class KMBOTAOutlineRowView: NSTableRowView { var box: KMBox? var contentBox: KMBox? var model: KMBOTAOutlineItem? { didSet { self.reloadData() } } var hoverCallback: KMBOTAOutlineRowViewHoverCallback? var rightMouseCallback: KMBOTAOutlineRowViewRightMouseCallback? var mouseDownCallback: KMBOTAOutlineRowViewMouseDownCallback? var itemSelect: Bool = false { didSet { self.needsDisplay = true } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) self.addBox() self.drawView() } func addBox() { if self.box == nil { let rect = self.bounds self.box?.wantsLayer = true self.box = KMBox(frame: rect) self.box?.borderWidth = 1 self.box?.borderColor = NSColor.km_init(hex: "#EDEEF0") self.box?.layer?.cornerRadius = 4 self.box?.boxType = .custom self.box?.autoresizingMask = [.width, .height] self.box?.moveCallback = { [unowned self] (mouseEntered, mouseBox) in self.hoverCallback?(mouseEntered, mouseBox) self.reloadData() } self.addSubview(self.box!, positioned: NSWindow.OrderingMode.below, relativeTo: self) } var rect = self.bounds rect.origin.x = self.bounds.origin.x + 8 rect.origin.y = self.bounds.origin.y rect.size.height = self.bounds.size.height rect.size.width = self.bounds.size.width - 16 self.box?.frame = rect if self.contentBox == nil { let rect = self.bounds self.contentBox?.wantsLayer = true self.contentBox = KMBox(frame: rect) self.contentBox?.borderWidth = 0 self.contentBox?.boxType = .custom self.contentBox?.autoresizingMask = [.width, .height] self.contentBox?.rightDownCallback = { [unowned self] (downEntered, mouseBox, event) in guard let callBack = rightMouseCallback else { return } callBack(self, event) } } self.addSubview(self.contentBox!, positioned: NSWindow.OrderingMode.above, relativeTo: self) } override func drawSelection(in dirtyRect: NSRect) { // self.model.select = true self.reloadData() } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) guard let callBack = mouseDownCallback else { return } callBack(self, event) } } extension KMBOTAOutlineRowView { func reloadData() { self.drawView() } func drawView(_ color: NSColor = NSColor.km_init(hex: "#EDEEF0")) { box?.fillColor = .clear box?.borderWidth = 0 var cellView: KMBOTAOutlineCellView? if self.numberOfColumns > 0 { cellView = (self.view(atColumn: 0) as? KMBOTAOutlineCellView) } let box = cellView?.hoverBox if self.model?.select == true { let color = ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/bg-opacity-dark") box?.backgroundColor(color) if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/xs") { let currentValue = value as? CGFloat ?? 0 box?.layer?.cornerRadius = currentValue } else { box?.layer?.cornerRadius = 4 } box?.borderColor = color box?.borderWidth = 0 } else if self.model?.hover == true { box?.backgroundColor(ComponentLibrary.shared.getComponentColorFromKey("colorFill/hov-opacity")) if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/xs") { let currentValue = value as? CGFloat ?? 0 box?.layer?.cornerRadius = currentValue } else { box?.layer?.cornerRadius = 4 } box?.borderWidth = 0 } else { // box?.backgroundColor(NSColor.km_init(hex: "#F7F8FA")) box?.backgroundColor(.clear) box?.borderWidth = 0 } } }