// // KMHomePopViewController.swift // PDF Reader Pro // // Created by wanjun on 2022/10/17. // import Cocoa public enum DataNavigationViewButtonActionType : Int, CaseIterable { case Batch = 0 // 批量处理 case ConvertPDF // 转换PDF case ImageToPDF // 图片转PDF case MergePDF // 合并 case Compression // 压缩 case Security // 安全 case PDFToPPT // PDF转PPT case PDFToExcel // PDF转Excel case PDFToWord // PDF转Word case PDFToImage // PDF转图片 case Watermark // 水印 case Background // 背景 case HeaderAndFooter// 页眉页脚 case BatesCode // 贝茨码 case Print // 打印 case PrintMultipage // 多页 case PrintPoster // 海报 case PrintBooklet // 小册子 case BatchRemove // 批量移除 case Insert // 插入 case BreakUp // 拆分 case Extract // 提取 case MarkCipher // 标记密文 case PageEdit // 页面编辑 case OCR // 批量OCR case AutomaticFormRecognition //表单自动识别 case FileCompare // 文件对比 case ComparativeTable // 比较表 case equity // 权益弹窗 case AITools //AI Tools } typealias popCellViewDownCallback = (_ downEntered: Bool, _ count: String) -> Void typealias popCellViewWillShow = (_ cellView: KMBox, _ index: Int) -> Void @objcMembers class KMHomePopViewController: NSViewController { @IBOutlet weak var customBox: NSBox! @IBOutlet weak var customBoxWidthLayoutConstraint: NSLayoutConstraint! @IBOutlet weak var customBoxHeightLayoutConstraint: NSLayoutConstraint! var downCallback: popCellViewDownCallback? var viewWillShow: popCellViewWillShow? var popCellViewDownString: String? var popCellCount: Int? var dataArr: [String]? var KMHorizontalLine: String = "KMHorizontalLine" var enterFillColor : NSColor = .black var textColor : NSColor = .black // 背景颜色 var background : NSColor = .white // 背景颜色 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] = [] var showVerticalScroller: Bool = false 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 = background//NSColor.km_init(hex: "#FFFFFF") } override func viewDidAppear() { super.viewDidAppear() self.updateUI() } // MARK: Private func updateUI() { customBox.fillColor = background 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.. 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() } // MARK: - Init Views fileprivate func initBoxLabel() -> NSTextField { let label = NSTextField.init() label.isEditable = false label.isBordered = false label.font = NSFont.systemFont(ofSize: 14.0) label.translatesAutoresizingMaskIntoConstraints = false label.backgroundColor = NSColor.clear label.textColor = NSColor.km_init(hex: "#252629") return label } } class KMScrollPopViewController: KMHomePopViewController { private var scrollView = NSScrollView() private var contentView = NSView() private var currentItemPosition: NSPoint = .zero convenience init() { self.init(nibName: "KMHomePopViewController", bundle: nil) } override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(self.scrollView) self.scrollView.documentView = self.contentView self.scrollView.documentView?.backgroundColor(self.background) self.scrollView.hasVerticalScroller = self.showVerticalScroller self.scrollView.borderType = .noBorder self.scrollView.drawsBackground = false } override func viewDidAppear() { super.viewDidAppear() DispatchQueue.main.async { let contentView = self.scrollView.contentView let pageH = NSHeight(self.scrollView.bounds) var numberPages: Int = 0 var currentPage: Int = 0 var scrollY: CGFloat = 0 if (pageH > 0) { numberPages = Int(NSHeight(self.contentView.bounds) / pageH) + 1 currentPage = Int((self.currentItemPosition.y + 32 + 4 * 2) / pageH) if (currentPage == (numberPages - 1)) { scrollY = 0 } else { scrollY = NSHeight(self.contentView.bounds) - pageH * CGFloat(currentPage+1) } } contentView.scroll(to: NSPoint(x: 0, y: scrollY)) } } override func viewDidLayout() { super.viewDidLayout() self.scrollView.frame = NSMakeRect(0, 0, NSWidth(self.view.frame), NSHeight(self.view.window!.frame)) self.contentView.frame = NSMakeRect(0, 0, NSWidth(self.customBox.frame), NSHeight(self.customBox.frame)+30) } override func updateUI() { super.updateUI() customBox.fillColor = background var widthMax: Float = 0 for subView in self.contentView.subviews { subView.removeFromSuperview() } for string in self.dataArr ?? [] { if ((string as AnyObject).isEqual(to: KMHorizontalLine)) { continue } let width = self.cellContentAdaptiveWidth(string) if (widthMax < width) { widthMax = width } } var formTopFloat: Float = 4.0 for string in dataArr?.reversed() ?? [] { if (string as AnyObject).isEqual(to: KMHorizontalLine) { self.createHonrizontalLineWithFrame(CGRect(x: 12.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+23, height: 11)) formTopFloat += 11 } else { self.popCellViewDownString = string self.createPopViewCellLabelWithFrame(formTopFloat, stringValue: string) formTopFloat += 32 } } self.customBoxWidthLayoutConstraint.constant = CGFloat(widthMax+47) self.customBoxHeightLayoutConstraint.constant = CGFloat(formTopFloat+4.0) } override func createHonrizontalLineWithFrame(_ frame: CGRect) { let box: NSBox = NSBox.init(frame: frame) box.boxType = .custom box.borderWidth = 0.0 box.contentViewMargins = NSMakeSize(0, 0) self.contentView.addSubview(box) let lineBox = NSBox.init(frame: CGRect(x: 0, y: 6, width: frame.width, height: 1)) lineBox.boxType = .separator box.addSubview(lineBox) } override func createPopViewCellLabelWithFrame(_ mas_top: Float, stringValue: String) { var isDisabled = false if (self.disItems.contains(stringValue)) { isDisabled = true } var isSelected = false if (isDisabled == false && self.selectedItems.contains(stringValue)) { isSelected = true } if (isSelected && self.selectedItems.first == stringValue) { self.currentItemPosition = NSPoint(x: 0, y: Int(mas_top)) } let box: KMBox = KMBox(frame: NSZeroRect) box.boxType = .custom box.borderWidth = 0.0 box.contentViewMargins = NSMakeSize(0, 0) box.cornerRadius = 4.0 self.contentView.addSubview(box) box.mas_makeConstraints { (make) in make?.leading.equalTo()(4.0) make?.trailing.equalTo()(-4.0) make?.height.equalTo()(32.0) make?.top.equalTo()(self.customBox.mas_top)?.offset()(CGFloat(mas_top)) } let boxLabel = self.initBoxLabel() boxLabel.stringValue = stringValue box.contentView?.addSubview(boxLabel) boxLabel.mas_makeConstraints { (make) in make?.centerY.equalTo()(0.0) make?.leading.equalTo()(8.0) make?.trailing.mas_equalTo()(0) } box.moveCallback = { mouseEntered, mouseBox in if (isDisabled) { return } if (isSelected) { // 选中没有 hover 效果 return } if mouseEntered { mouseBox.fillColor = self.enterFillColor } else { mouseBox.fillColor = NSColor.clear } } box.downCallback = { [unowned self] downEntered, mouseBox, _ in if (isDisabled) { return } } let idx = self.dataArr?.index(of: stringValue) ?? 0 self.viewWillShow?(box, idx) } }