123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- //
- // KMHomePopViewController.swift
- // PDF Master
- //
- // Created by wanjun on 2022/10/17.
- //
- import Cocoa
- typealias popCellViewDownCallback = (_ downEntered: Bool, _ count: String) -> Void
- @objcMembers 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..<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)
-
- let lineBox = NSBox.init(frame: CGRect(x: 0, y: 6, width: frame.width, height: 1))
- lineBox.boxType = .separator
- box.addSubview(lineBox)
- }
-
- // func createPopViewCellLabelWithFrame(_ frame: CGRect, stringValue: String) {
- // let box: KMBox = KMBox(frame: frame)
- // box.boxType = .custom
- // box.borderWidth = 0.0
- // box.contentViewMargins = NSMakeSize(0, 0)
- // customBox.contentView?.addSubview(box)
- //
- // 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.labelColor
- // box.contentView?.addSubview(boxLabel)
- //
- // box.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-21-[boxLabel]-21-|", metrics: nil, views:["boxLabel": boxLabel]))
- // box.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-1-[boxLabel]-1-|", metrics: nil, views:["boxLabel": boxLabel]))
- //
- // box.contentView?.addConstraint(NSLayoutConstraint(item: boxLabel, attribute:.centerY , relatedBy: .equal, toItem: box.contentView, attribute: .centerY, multiplier: 1, constant: 0))
- // box.moveCallback = {(mouseEntered: Bool, mouseBox: KMBox) -> 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<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(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
- }
-
- override func viewDidAppear() {
- super.viewDidAppear()
-
- DispatchQueue.main.async {
- let contentView = self.scrollView.contentView
- let pageH = NSHeight(self.scrollView.bounds)
- // KMPrint(NSHeight(self.contentView.bounds))
- // KMPrint(pageH)
- // KMPrint(self.currentItemPosition)
- 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)
- // KMPrint(numberPages)
- // KMPrint(currentPage)
-
- // let _currentPage = numberPages - currentPage - 1
- // KMPrint(_currentPage)
- if (currentPage == (numberPages - 1)) {
- scrollY = 0
- } else {
- scrollY = NSHeight(self.contentView.bounds) - pageH * CGFloat(currentPage+1)
- }
- }
-
- // KMPrint(scrollY)
- 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)
- // self.contentView.frame = self.customBox.bounds
- }
-
- override func updateUI() {
- 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?.left.equalTo()(4.0)
- make?.right.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?.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])
-
- 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")
- }
-
- box.moveCallback = { mouseEntered, mouseBox in
- if (isDisabled) {
- return
- }
- if (isSelected) { // 选中没有 hover 效果
- return
- }
- if (mouseEntered) {
- mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.hov")
- } else {
- mouseBox.fillColor = NSColor.clear
- }
- }
- box.downCallback = { [unowned self] downEntered, mouseBox, _ in
- if (isDisabled) {
- return
- }
-
- if (downEntered) {
- mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
- boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
-
- guard let callback = self.downCallback else {
- return
- }
- 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")
- }
- }
- }
- }
|