123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- //
- // 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
- }
- }
- }
|