// // KMHomePopViewController.swift // PDF Master // // Created by wanjun on 2022/10/17. // import Cocoa typealias popCellViewDownCallback = (_ downEntered: Bool, _ count: String) -> Void class KMHomePopViewController: NSViewController { @IBOutlet weak var customBox: NSBox! @IBOutlet weak var customBoxWidthLayoutConstraint: NSLayoutConstraint! @IBOutlet weak var customBoxHeightLayoutConstraint: NSLayoutConstraint! var downCallback: popCellViewDownCallback? var popCellViewDownString: String? var popCellCount: Int? var dataArr: [String]? var KMHorizontalLine: String = "KMHorizontalLine" var background : NSColor = .clear // 背景颜色 var background_hov : NSColor = .clear // 背景颜色 var background_sel : NSColor = .clear // 背景颜色 var background_disabled : NSColor = .clear // 背景颜色 var cornerRadius : Float = 0.0 // 边框圆角 var cornerRadius_hov : Float = 0.0 // 边框圆角 var cornerRadius_sel : Float = 0.0 // 边框圆角 var cornerRadius_disabled : Float = 0.0 // 边框圆角 var lineHeight : CGFloat = 20.0 // 默认 内容行高 var lineHeight_hov : CGFloat = 20.0 // 默认 内容行高 var lineHeight_sel : CGFloat = 20.0 // 默认 内容行高 var lineHeight_disabled : CGFloat = 20.0 // 默认 内容行高 var font : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体 var font_hov : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体 var font_sel : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体 var font_disabled : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体 var _state: KMDesignTokenState = .Norm var enabled: Bool = true // 是否可点击 var canHover: Bool = true // 是否可悬浮 var disItems: [String] = [] var selectedItems: [String] = [] func initWithPopViewDataArr(_ popViewDataArr: [String]) -> Self { // self.dataArr = popViewDataArr.reverseObjectEnumerator().allObjects as NSArray self.dataArr = popViewDataArr.reversed() return self } override func viewDidLoad() { super.viewDidLoad() customBox.fillColor = NSColor(hex: "#FFFFFF") // self.updateUI() } override func viewDidAppear() { super.viewDidAppear() self.updateUI() } // MARK: Private func updateUI() { var widthMax: Float = 0; let subViews: [NSView] = self.customBox.contentView!.subviews for subView in subViews { subView.removeFromSuperview() } for string in self.dataArr ?? [] { if !(string as AnyObject).isEqual(to: KMHorizontalLine) { let width = self.cellContentAdaptiveWidth(string) if widthMax < width { widthMax = width } } } var formTopFloat: Float = 4.0 // for i in (0.. Void in // if mouseEntered { // if #available(macOS 10.14, *) { // box.fillColor = NSColor.controlAccentColor // boxLabel.textColor = NSColor.white // } else { // box.fillColor = NSColor.init(red: 71/255.0, green: 126/255.0, blue: 222/255.0, alpha: 1.0) // boxLabel.textColor = NSColor.white // } // } else { // box.fillColor = NSColor.clear // boxLabel.textColor = NSColor.labelColor // } // } // box.downCallback = {(downEntered: Bool, mouseBox: KMBox) -> Void in // if downEntered { // if let callback = self.downCallback { // callback(true, stringValue) // } // } // } // } func createPopViewCellLabelWithFrame(_ mas_top: Float, stringValue: String) { var isDisabled = false if disItems.contains(stringValue) { isDisabled = true } var isSelected = false if (isDisabled == false && self.selectedItems.contains(stringValue)) { isSelected = true } let box: KMBox = KMBox(frame: NSZeroRect) box.boxType = .custom box.borderWidth = 0.0 box.contentViewMargins = NSMakeSize(0, 0) box.cornerRadius = 4.0 customBox.contentView?.addSubview(box) box.mas_makeConstraints { (make) in make?.left.equalTo()(4.0) make?.right.equalTo()(-4.0) make?.height.equalTo()(32.0) make?.top.equalTo()(customBox.mas_top)?.offset()(CGFloat(mas_top)) } // let dropdownVC = KMDesignDropdown.init(nibName: "KMDesignDropdown", bundle: nil) // box.contentView = dropdownVC.view // dropdownVC.dropdown(bg: "dropdown.m.bg.norm", text: "dropdown.m.mac-text.def") // dropdownVC.dropdown(bg: "dropdown.m.bg.hov", text: "dropdown.m.mac-text.def", state: .Hov) // dropdownVC.dropdown(bg: "dropdown.m.bg.sel", text: "dropdown.m.mac-text.sel", state: .Sel) // dropdownVC.dropdown(bg: "dropdown.m.bg.dis", text: "dropdown.m.mac-text.dis", state: .Disabled) // dropdownVC.stringValue = str let boxLabel: NSTextField = NSTextField.init() boxLabel.isEditable = false boxLabel.isBordered = false boxLabel.stringValue = stringValue boxLabel.font = NSFont.systemFont(ofSize: 14.0) boxLabel.translatesAutoresizingMaskIntoConstraints = false boxLabel.backgroundColor = NSColor.clear boxLabel.textColor = NSColor(hex: "#252629") box.contentView?.addSubview(boxLabel) boxLabel.mas_makeConstraints { (make) in make?.centerY.equalTo()(0.0) make?.left.equalTo()(8.0) } let textTypography = KMDesignToken.shared.typography(withToken: "dropdown.m.mac-text.def") var fontFamily: String = textTypography.fontFamily let fontWeight: String = textTypography.fontWeight if fontFamily.contains(" ") { fontFamily = fontFamily.replacingOccurrences(of: " ", with: "") } if fontWeight != "" { fontFamily = String(format: "%@-%@", fontFamily, fontWeight) } boxLabel.font = NSFont(name: fontFamily, size: textTypography.fontSize.stringToCGFloat()) ?? NSFont.systemFont(ofSize: textTypography.fontSize.stringToCGFloat()) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = textTypography.lineHeight.stringToCGFloat() boxLabel.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle]) box.moveCallback = {(mouseEntered: Bool, mouseBox: KMBox) -> Void in if !isDisabled { if isSelected { // 选中没有 hover 效果 return } if mouseEntered { mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.hov") } else { mouseBox.fillColor = NSColor.clear } } } box.downCallback = {(downEntered, mouseBox, event) -> Void in if !isDisabled { if downEntered { mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel") boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel") if let callback = self.downCallback { callback(true, stringValue) } } else { mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.norm") boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.def") } } } if isDisabled { box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.dis") boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.dis") } else if (isSelected) { box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel") boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel") } } func cellContentAdaptiveWidth(_ content: String) -> Float { if content.isEmpty { return 0 } let attributes = [NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14.0)] let rect : CGRect = content.boundingRect(with: CGSize(width: 0, height: 18),options: .usesLineFragmentOrigin, attributes: attributes,context:nil) return Float(rect.size.width) } func changePopViewCellData(_ count: Int, content: String) { let boxArray: Array = customBox.contentView!.subviews for subView in boxArray { subView.removeFromSuperview() } var dataMutableArr: [String] = Array((dataArr?.reversed())!) dataMutableArr[count] = content dataArr = Array(dataMutableArr.reversed()) self.updateUI() } }