123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- //
- // KMBOTAOutlineCellView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/4/2.
- //
- import Cocoa
- typealias KMBOTAOutlineCellViewIconAction = (_ view: KMBOTAOutlineCellView ) -> ()
- class KMBOTAOutlineCellView: NSTableCellView {
- @IBOutlet var contentView: NSView!
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var iconButton: NSButton!
-
- @IBOutlet weak var iconImageWidthConstrailnt: NSLayoutConstraint!
-
- var iconAction: KMBOTAOutlineCellViewIconAction?
- var model: KMBOTAOutlineItem? {
- didSet {
- self.reloadData()
- }
- }
-
- private lazy var hoverBox_: NSBox = {
- let box = NSBox()
- box.boxType = .custom
- box.titlePosition = .noTitle
- box.contentViewMargins = .zero
- box.borderWidth = 0
- return box
- }()
-
- var hoverBox: NSBox {
- get {
- return hoverBox_
- }
- }
-
- // MARK: 初始化
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- initContentView()
- setup()
- }
-
- required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
- initContentView()
- setup()
- fatalError("init(coder:) has not been implemented")
- }
-
- private func initContentView() {
- //绑定xib
- let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
- bundle: Bundle(for: self.classForCoder.self))!
- resource.instantiate(withOwner: self, topLevelObjects: nil)
- addSubview(contentView)
- contentView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- contentView.topAnchor.constraint(equalTo: topAnchor),
- contentView.leftAnchor.constraint(equalTo: leftAnchor),
- contentView.rightAnchor.constraint(equalTo: rightAnchor),
- contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
- contentView.updateConstraintsForSubtreeIfNeeded()
- }
-
- func setup() {
- self.titleLabel.maximumNumberOfLines = 0
-
- contentView.addSubview(hoverBox_)
- hoverBox_.km_add_leading_constraint(equalTo: iconButton, attribute: .trailing, constant: 4)
- hoverBox_.km_add_top_constraint()
- hoverBox_.km_add_bottom_constraint()
- hoverBox_.km_add_trailing_constraint(constant: -16)
- }
-
- func reloadData() {
- guard let data = self.model else { return }
-
- self.titleLabel.stringValue = data.outline.label ?? ""
- if data.isItemExpanded {
- self.iconButton.image = NSImage(named: "KMImageNameArrowDown")
- } else {
- self.iconButton.image = NSImage(named: "KMImageNameArrowRight")
- }
-
- if data.outline.numberOfChildren > 0 {
- self.iconButton.isHidden = false
- // self.iconImageWidthConstrailnt.constant = 22
- } else {
- self.iconButton.isHidden = true
- // self.iconImageWidthConstrailnt.constant = 0
- }
- }
-
- func updateUI() {
- self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
- self.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
- }
-
- func updateLanguage() {
- self.reloadData()
- }
-
-
- @IBAction func iconButtonAction(_ sender: Any) {
- guard let callBack = iconAction else { return }
- callBack(self)
- }
- }
|