123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- //
- // KMBOTAOutlineCellView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/4/2.
- //
- import Cocoa
- import KMComponentLibrary
- typealias KMBOTAOutlineCellViewIconAction = (_ view: KMBOTAOutlineCellView ) -> ()
- class KMBOTAOutlineCellView: NSTableCellView, NibLoadable {
- @IBOutlet weak var titleLabel: KMTextField!
- @IBOutlet weak var titleLabelBox: KMBox!
- @IBOutlet weak var iconButton: NSButton!
-
- 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_
- }
- }
-
- override func awakeFromNib() {
- self.setup()
- }
-
- func setup() {
- self.titleLabel.maximumNumberOfLines = 0
-
- self.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)
-
- titleLabel.delegate = self
- titleLabel.firstResponderHandler = { [weak self] firstResp in
- if firstResp {
- DispatchQueue.main.async {
- self?.titleLabelBox.borderWidth = 1
- self?.titleLabelBox.borderColor = NSColor.km_init(hex: "#4982E6")
- if KMAppearance.isDarkMode() {
- self?.titleLabelBox.fillColor = .black
- } else {
- self?.titleLabelBox.fillColor = .white
- }
- }
- }
- }
-
- self.updateUI()
- self.updateLanguage()
- }
-
- func reloadData() {
- guard let data = self.model else { return }
-
- let isItemExpanded = data.isItemExpanded
-
- if isItemExpanded {
- iconButton.image = NSImage(named: "KMImageNameBotaNoteExpand")
- } else {
- iconButton.image = NSImage(named: "KMImageNameBotaNoteNoExpand")
- }
- }
-
- func updateUI() {
- self.titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
- self.titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
- }
-
- func updateLanguage() {
- self.reloadData()
- }
-
-
- @IBAction func iconButtonAction(_ sender: Any) {
- guard let callBack = iconAction else { return }
- callBack(self)
- }
- }
- extension KMBOTAOutlineCellView: NSTextFieldDelegate {
- func controlTextDidEndEditing(_ obj: Notification) {
- if self.titleLabel.isEqual(to: obj.object) {
- self.titleLabelBox.borderWidth = 0
- self.titleLabelBox.fillColor = .clear
- }
- }
- }
|