// // KMBookMarkTableRowView.swift // PDF Reader Pro // // Created by lizhe on 2023/3/14. // import Cocoa import KMComponentLibrary typealias KMBookMarkTableRowViewHoverCallback = (_ mouseEntered: Bool, _ mouseBox: KMBox) -> Void typealias KMBookMarkTableRowViewMouseDown = (_ view: KMBookMarkTableRowView, _ event: NSEvent) -> Void typealias KMBookMarkTableRowViewRightMouseDown = (_ view: KMBookMarkTableRowView, _ event: NSEvent) -> Void class KMBookMarkTableRowView: NSTableRowView { var box: KMBox? var contentBox: KMBox? var model: KMBookMarkItem! { didSet { self.reloadData() } } var hoverCallback: KMBookMarkTableRowViewHoverCallback? var mouseDownAction: KMBookMarkTableRowViewMouseDown? var rightMouseDownAction: KMBookMarkTableRowViewRightMouseDown? var contentBackgroundColor: NSColor? var menuClickedAction: ((_ clickPoint: NSPoint) -> NSMenu)? override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) self.addBox() self.drawView() } func addBox() { if self.box == nil { var rect = self.bounds rect.origin.x = self.bounds.origin.x + 8.0 rect.size.width = self.bounds.size.width - 16.0 self.box?.wantsLayer = true self.box?.layer?.masksToBounds = true self.box = KMBox(frame: rect) self.box?.borderWidth = 0 self.box?.boxType = .custom self.addSubview(self.box!, positioned: NSWindow.OrderingMode.below, relativeTo: self) self.box?.autoresizingMask = [.width, .height] self.box?.moveCallback = { [unowned self] (mouseEntered, mouseBox) in self.hoverCallback?(mouseEntered, mouseBox) self.reloadData() } self.box?.downCallback = { [unowned self] (mouseEntered, mouseBox, event) in self.mouseDownAction?(self, event) } self.box?.rightDownCallback = { [unowned self] (mouseEntered, mouseBox, event) in self.rightMouseDownAction?(self, event) _ = menuClickedAction?(event.locationInWindow) } self.contentBox?.wantsLayer = true self.contentBox?.layer?.masksToBounds = true self.contentBox = KMBox(frame: self.bounds) self.contentBox?.borderWidth = 0 self.contentBox?.layer?.cornerRadius = 8 self.contentBox?.boxType = .custom self.contentBox?.autoresizingMask = [.width, .height] self.contentBox?.rightDownCallback = { [unowned self] (downEntered, mouseBox, event) in guard let callBack = rightMouseDownAction else { return } callBack(self, event) _ = menuClickedAction?(event.locationInWindow) } } self.addSubview(self.contentBox!, positioned: NSWindow.OrderingMode.above, relativeTo: self) } override func drawSelection(in dirtyRect: NSRect) { // super.drawSelection(in: dirtyRect) self.reloadData() } override func menu(for event: NSEvent) -> NSMenu? { let point = event.locationInWindow if menuClickedAction != nil { let menu = menuClickedAction!(point) return menu } return super.menu(for: event) } } extension KMBookMarkTableRowView { func reloadData() { self.drawView() } func drawView(_ color: NSColor = NSColor.km_init(hex: "#EDEEF0")) { if self.model!.select { self.box?.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorPrimary/bg-opacity-dark") } else if self.model.hover { self.box?.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/hov-opacity") } else { self.box?.fillColor = .clear } } }