123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- //
- // 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..<dataArr!.count).reversed() {
- 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 {
- popCellViewDownString = string
- // self.createPopViewCellLabelWithFrame(CGRect(x: 4.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+47, height: 32), stringValue: string)
- createPopViewCellLabelWithFrame(formTopFloat, stringValue: string)
- formTopFloat += 32;
- }
- }
-
- customBoxWidthLayoutConstraint.constant = CGFloat(widthMax+47)
- customBoxHeightLayoutConstraint.constant = CGFloat(formTopFloat+4.0)
- }
-
- func createHonrizontalLineWithFrame(_ frame: CGRect) {
- let box: NSBox = NSBox.init(frame: frame)
- box.boxType = .custom
- box.borderWidth = 0.0
- box.contentViewMargins = NSMakeSize(0, 0)
- customBox.contentView?.addSubview(box)
- box.mas_makeConstraints { (make) in
- make?.leading.equalTo()(frame.origin.x)
- make?.trailing.equalTo()(-frame.origin.x)
- make?.height.equalTo()(frame.size.height)
- make?.top.equalTo()(customBox.mas_top)?.offset()(CGFloat(frame.origin.y))
- }
-
- let lineBox = NSBox.init(frame: CGRect(x: 0, y: 6, width: frame.width, height: 1))
- lineBox.boxType = .separator
- box.addSubview(lineBox)
- }
-
- 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?.leading.equalTo()(4.0)
- make?.trailing.equalTo()(-4.0)
- make?.height.equalTo()(32.0)
- make?.top.equalTo()(customBox.mas_top)?.offset()(CGFloat(mas_top))
- }
-
-
-
- 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 = textColor//NSColor.km_init(hex: "#252629")
- box.contentView?.addSubview(boxLabel)
- boxLabel.mas_makeConstraints { (make) in
- make?.centerY.equalTo()(0.0)
- make?.leading.equalTo()(8.0)
- }
-
-
-
- let idx = self.dataArr?.index(of: stringValue) ?? 0
- self.viewWillShow?(box, idx)
- }
-
- 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<NSView> = 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)
- }
- }
|