KMHomePopViewController.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // KMHomePopViewController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/10/17.
  6. //
  7. import Cocoa
  8. typealias popCellViewDownCallback = (_ downEntered: Bool, _ count: String) -> Void
  9. @objcMembers class KMHomePopViewController: NSViewController {
  10. @IBOutlet weak var customBox: NSBox!
  11. @IBOutlet weak var customBoxWidthLayoutConstraint: NSLayoutConstraint!
  12. @IBOutlet weak var customBoxHeightLayoutConstraint: NSLayoutConstraint!
  13. var downCallback: popCellViewDownCallback?
  14. var popCellViewDownString: String?
  15. var popCellCount: Int?
  16. var dataArr: [String]?
  17. var KMHorizontalLine: String = "KMHorizontalLine"
  18. var background : NSColor = .clear // 背景颜色
  19. var background_hov : NSColor = .clear // 背景颜色
  20. var background_sel : NSColor = .clear // 背景颜色
  21. var background_disabled : NSColor = .clear // 背景颜色
  22. var cornerRadius : Float = 0.0 // 边框圆角
  23. var cornerRadius_hov : Float = 0.0 // 边框圆角
  24. var cornerRadius_sel : Float = 0.0 // 边框圆角
  25. var cornerRadius_disabled : Float = 0.0 // 边框圆角
  26. var lineHeight : CGFloat = 20.0 // 默认 内容行高
  27. var lineHeight_hov : CGFloat = 20.0 // 默认 内容行高
  28. var lineHeight_sel : CGFloat = 20.0 // 默认 内容行高
  29. var lineHeight_disabled : CGFloat = 20.0 // 默认 内容行高
  30. var font : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  31. var font_hov : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  32. var font_sel : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  33. var font_disabled : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  34. var _state: KMDesignTokenState = .Norm
  35. var enabled: Bool = true // 是否可点击
  36. var canHover: Bool = true // 是否可悬浮
  37. var disItems: [String] = []
  38. var selectedItems: [String] = []
  39. func initWithPopViewDataArr(_ popViewDataArr: [String]) -> Self {
  40. // self.dataArr = popViewDataArr.reverseObjectEnumerator().allObjects as NSArray
  41. self.dataArr = popViewDataArr.reversed()
  42. return self
  43. }
  44. override func viewDidLoad() {
  45. super.viewDidLoad()
  46. customBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  47. // self.updateUI()
  48. }
  49. override func viewDidAppear() {
  50. super.viewDidAppear()
  51. self.updateUI()
  52. }
  53. // MARK: Private
  54. func updateUI() {
  55. var widthMax: Float = 0;
  56. let subViews: [NSView] = self.customBox.contentView!.subviews
  57. for subView in subViews {
  58. subView.removeFromSuperview()
  59. }
  60. for string in self.dataArr ?? [] {
  61. if !(string as AnyObject).isEqual(to: KMHorizontalLine) {
  62. let width = self.cellContentAdaptiveWidth(string)
  63. if widthMax < width {
  64. widthMax = width
  65. }
  66. }
  67. }
  68. var formTopFloat: Float = 4.0
  69. // for i in (0..<dataArr!.count).reversed() {
  70. for string in dataArr?.reversed() ?? [] {
  71. if (string as AnyObject).isEqual(to: KMHorizontalLine) {
  72. self.createHonrizontalLineWithFrame(CGRect(x: 12.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+23, height: 11))
  73. formTopFloat += 11
  74. } else {
  75. popCellViewDownString = string
  76. // self.createPopViewCellLabelWithFrame(CGRect(x: 4.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+47, height: 32), stringValue: string)
  77. createPopViewCellLabelWithFrame(formTopFloat, stringValue: string)
  78. formTopFloat += 32;
  79. }
  80. }
  81. customBoxWidthLayoutConstraint.constant = CGFloat(widthMax+47)
  82. customBoxHeightLayoutConstraint.constant = CGFloat(formTopFloat+4.0)
  83. }
  84. func createHonrizontalLineWithFrame(_ frame: CGRect) {
  85. let box: NSBox = NSBox.init(frame: frame)
  86. box.boxType = .custom
  87. box.borderWidth = 0.0
  88. box.contentViewMargins = NSMakeSize(0, 0)
  89. customBox.contentView?.addSubview(box)
  90. let lineBox = NSBox.init(frame: CGRect(x: 0, y: 6, width: frame.width, height: 1))
  91. lineBox.boxType = .separator
  92. box.addSubview(lineBox)
  93. }
  94. // func createPopViewCellLabelWithFrame(_ frame: CGRect, stringValue: String) {
  95. // let box: KMBox = KMBox(frame: frame)
  96. // box.boxType = .custom
  97. // box.borderWidth = 0.0
  98. // box.contentViewMargins = NSMakeSize(0, 0)
  99. // customBox.contentView?.addSubview(box)
  100. //
  101. // let boxLabel: NSTextField = NSTextField.init()
  102. // boxLabel.isEditable = false
  103. // boxLabel.isBordered = false
  104. // boxLabel.stringValue = stringValue
  105. // boxLabel.font = NSFont.systemFont(ofSize: 14.0)
  106. // boxLabel.translatesAutoresizingMaskIntoConstraints = false
  107. // boxLabel.backgroundColor = NSColor.clear
  108. // boxLabel.textColor = NSColor.labelColor
  109. // box.contentView?.addSubview(boxLabel)
  110. //
  111. // box.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-21-[boxLabel]-21-|", metrics: nil, views:["boxLabel": boxLabel]))
  112. // box.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-1-[boxLabel]-1-|", metrics: nil, views:["boxLabel": boxLabel]))
  113. //
  114. // box.contentView?.addConstraint(NSLayoutConstraint(item: boxLabel, attribute:.centerY , relatedBy: .equal, toItem: box.contentView, attribute: .centerY, multiplier: 1, constant: 0))
  115. // box.moveCallback = {(mouseEntered: Bool, mouseBox: KMBox) -> Void in
  116. // if mouseEntered {
  117. // if #available(macOS 10.14, *) {
  118. // box.fillColor = NSColor.controlAccentColor
  119. // boxLabel.textColor = NSColor.white
  120. // } else {
  121. // box.fillColor = NSColor.init(red: 71/255.0, green: 126/255.0, blue: 222/255.0, alpha: 1.0)
  122. // boxLabel.textColor = NSColor.white
  123. // }
  124. // } else {
  125. // box.fillColor = NSColor.clear
  126. // boxLabel.textColor = NSColor.labelColor
  127. // }
  128. // }
  129. // box.downCallback = {(downEntered: Bool, mouseBox: KMBox) -> Void in
  130. // if downEntered {
  131. // if let callback = self.downCallback {
  132. // callback(true, stringValue)
  133. // }
  134. // }
  135. // }
  136. // }
  137. func createPopViewCellLabelWithFrame(_ mas_top: Float, stringValue: String) {
  138. var isDisabled = false
  139. if disItems.contains(stringValue) {
  140. isDisabled = true
  141. }
  142. var isSelected = false
  143. if (isDisabled == false && self.selectedItems.contains(stringValue)) {
  144. isSelected = true
  145. }
  146. let box: KMBox = KMBox(frame: NSZeroRect)
  147. box.boxType = .custom
  148. box.borderWidth = 0.0
  149. box.contentViewMargins = NSMakeSize(0, 0)
  150. box.cornerRadius = 4.0
  151. customBox.contentView?.addSubview(box)
  152. box.mas_makeConstraints { (make) in
  153. make?.left.equalTo()(4.0)
  154. make?.right.equalTo()(-4.0)
  155. make?.height.equalTo()(32.0)
  156. make?.top.equalTo()(customBox.mas_top)?.offset()(CGFloat(mas_top))
  157. }
  158. // let dropdownVC = KMDesignDropdown.init(nibName: "KMDesignDropdown", bundle: nil)
  159. // box.contentView = dropdownVC.view
  160. // dropdownVC.dropdown(bg: "dropdown.m.bg.norm", text: "dropdown.m.mac-text.def")
  161. // dropdownVC.dropdown(bg: "dropdown.m.bg.hov", text: "dropdown.m.mac-text.def", state: .Hov)
  162. // dropdownVC.dropdown(bg: "dropdown.m.bg.sel", text: "dropdown.m.mac-text.sel", state: .Sel)
  163. // dropdownVC.dropdown(bg: "dropdown.m.bg.dis", text: "dropdown.m.mac-text.dis", state: .Disabled)
  164. // dropdownVC.stringValue = str
  165. let boxLabel: NSTextField = NSTextField.init()
  166. boxLabel.isEditable = false
  167. boxLabel.isBordered = false
  168. boxLabel.stringValue = stringValue
  169. boxLabel.font = NSFont.systemFont(ofSize: 14.0)
  170. boxLabel.translatesAutoresizingMaskIntoConstraints = false
  171. boxLabel.backgroundColor = NSColor.clear
  172. boxLabel.textColor = NSColor.km_init(hex: "#252629")
  173. box.contentView?.addSubview(boxLabel)
  174. boxLabel.mas_makeConstraints { (make) in
  175. make?.centerY.equalTo()(0.0)
  176. make?.left.equalTo()(8.0)
  177. }
  178. let textTypography = KMDesignToken.shared.typography(withToken: "dropdown.m.mac-text.def")
  179. var fontFamily: String = textTypography.fontFamily
  180. let fontWeight: String = textTypography.fontWeight
  181. if fontFamily.contains(" ") {
  182. fontFamily = fontFamily.replacingOccurrences(of: " ", with: "")
  183. }
  184. if fontWeight != "" {
  185. fontFamily = String(format: "%@-%@", fontFamily, fontWeight)
  186. }
  187. boxLabel.font = NSFont(name: fontFamily, size: textTypography.fontSize.stringToCGFloat()) ?? NSFont.systemFont(ofSize: textTypography.fontSize.stringToCGFloat())
  188. let paragraphStyle = NSMutableParagraphStyle()
  189. paragraphStyle.lineSpacing = textTypography.lineHeight.stringToCGFloat()
  190. boxLabel.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  191. box.moveCallback = {(mouseEntered: Bool, mouseBox: KMBox) -> Void in
  192. if !isDisabled {
  193. if isSelected { // 选中没有 hover 效果
  194. return
  195. }
  196. if mouseEntered {
  197. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.hov")
  198. } else {
  199. mouseBox.fillColor = NSColor.clear
  200. }
  201. }
  202. }
  203. box.downCallback = {(downEntered, mouseBox, event) -> Void in
  204. if !isDisabled {
  205. if downEntered {
  206. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
  207. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
  208. if let callback = self.downCallback {
  209. callback(true, stringValue)
  210. }
  211. } else {
  212. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.norm")
  213. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.def")
  214. }
  215. }
  216. }
  217. if isDisabled {
  218. box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.dis")
  219. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.dis")
  220. } else if (isSelected) {
  221. box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
  222. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
  223. }
  224. }
  225. func cellContentAdaptiveWidth(_ content: String) -> Float {
  226. if content.isEmpty {
  227. return 0
  228. }
  229. let attributes = [NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14.0)]
  230. let rect : CGRect = content.boundingRect(with: CGSize(width: 0, height: 18),options: .usesLineFragmentOrigin, attributes: attributes,context:nil)
  231. return Float(rect.size.width)
  232. }
  233. func changePopViewCellData(_ count: Int, content: String) {
  234. let boxArray: Array<NSView> = customBox.contentView!.subviews
  235. for subView in boxArray {
  236. subView.removeFromSuperview()
  237. }
  238. var dataMutableArr: [String] = Array((dataArr?.reversed())!)
  239. dataMutableArr[count] = content
  240. dataArr = Array(dataMutableArr.reversed())
  241. self.updateUI()
  242. }
  243. // MARK: - Init Views
  244. fileprivate func initBoxLabel() -> NSTextField {
  245. let label = NSTextField.init()
  246. label.isEditable = false
  247. label.isBordered = false
  248. label.font = NSFont.systemFont(ofSize: 14.0)
  249. label.translatesAutoresizingMaskIntoConstraints = false
  250. label.backgroundColor = NSColor.clear
  251. label.textColor = NSColor.km_init(hex: "#252629")
  252. return label
  253. }
  254. }
  255. class KMScrollPopViewController: KMHomePopViewController {
  256. private var scrollView = NSScrollView()
  257. private var contentView = NSView()
  258. private var currentItemPosition: NSPoint = .zero
  259. convenience init() {
  260. self.init(nibName: "KMHomePopViewController", bundle: nil)
  261. }
  262. override func viewDidLoad() {
  263. super.viewDidLoad()
  264. self.view.addSubview(self.scrollView)
  265. self.scrollView.documentView = self.contentView
  266. }
  267. override func viewDidAppear() {
  268. super.viewDidAppear()
  269. DispatchQueue.main.async {
  270. let contentView = self.scrollView.contentView
  271. let pageH = NSHeight(self.scrollView.bounds)
  272. // KMPrint(NSHeight(self.contentView.bounds))
  273. // KMPrint(pageH)
  274. // KMPrint(self.currentItemPosition)
  275. var numberPages: Int = 0
  276. var currentPage: Int = 0
  277. var scrollY: CGFloat = 0
  278. if (pageH > 0) {
  279. numberPages = Int(NSHeight(self.contentView.bounds) / pageH) + 1
  280. currentPage = Int((self.currentItemPosition.y + 32 + 4 * 2) / pageH)
  281. // KMPrint(numberPages)
  282. // KMPrint(currentPage)
  283. // let _currentPage = numberPages - currentPage - 1
  284. // KMPrint(_currentPage)
  285. if (currentPage == (numberPages - 1)) {
  286. scrollY = 0
  287. } else {
  288. scrollY = NSHeight(self.contentView.bounds) - pageH * CGFloat(currentPage+1)
  289. }
  290. }
  291. // KMPrint(scrollY)
  292. contentView.scroll(to: NSPoint(x: 0, y: scrollY))
  293. }
  294. }
  295. override func viewDidLayout() {
  296. super.viewDidLayout()
  297. self.scrollView.frame = NSMakeRect(0, 0, NSWidth(self.view.frame), NSHeight(self.view.window!.frame))
  298. self.contentView.frame = NSMakeRect(0, 0, NSWidth(self.customBox.frame), NSHeight(self.customBox.frame)+30)
  299. // self.contentView.frame = self.customBox.bounds
  300. }
  301. override func updateUI() {
  302. var widthMax: Float = 0
  303. for subView in self.contentView.subviews {
  304. subView.removeFromSuperview()
  305. }
  306. for string in self.dataArr ?? [] {
  307. if ((string as AnyObject).isEqual(to: KMHorizontalLine)) {
  308. continue
  309. }
  310. let width = self.cellContentAdaptiveWidth(string)
  311. if (widthMax < width) {
  312. widthMax = width
  313. }
  314. }
  315. var formTopFloat: Float = 4.0
  316. for string in dataArr?.reversed() ?? [] {
  317. if (string as AnyObject).isEqual(to: KMHorizontalLine) {
  318. self.createHonrizontalLineWithFrame(CGRect(x: 12.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+23, height: 11))
  319. formTopFloat += 11
  320. } else {
  321. self.popCellViewDownString = string
  322. self.createPopViewCellLabelWithFrame(formTopFloat, stringValue: string)
  323. formTopFloat += 32
  324. }
  325. }
  326. self.customBoxWidthLayoutConstraint.constant = CGFloat(widthMax+47)
  327. self.customBoxHeightLayoutConstraint.constant = CGFloat(formTopFloat+4.0)
  328. }
  329. override func createHonrizontalLineWithFrame(_ frame: CGRect) {
  330. let box: NSBox = NSBox.init(frame: frame)
  331. box.boxType = .custom
  332. box.borderWidth = 0.0
  333. box.contentViewMargins = NSMakeSize(0, 0)
  334. self.contentView.addSubview(box)
  335. let lineBox = NSBox.init(frame: CGRect(x: 0, y: 6, width: frame.width, height: 1))
  336. lineBox.boxType = .separator
  337. box.addSubview(lineBox)
  338. }
  339. override func createPopViewCellLabelWithFrame(_ mas_top: Float, stringValue: String) {
  340. var isDisabled = false
  341. if (self.disItems.contains(stringValue)) {
  342. isDisabled = true
  343. }
  344. var isSelected = false
  345. if (isDisabled == false && self.selectedItems.contains(stringValue)) {
  346. isSelected = true
  347. }
  348. if (isSelected && self.selectedItems.first == stringValue) {
  349. self.currentItemPosition = NSPoint(x: 0, y: Int(mas_top))
  350. }
  351. let box: KMBox = KMBox(frame: NSZeroRect)
  352. box.boxType = .custom
  353. box.borderWidth = 0.0
  354. box.contentViewMargins = NSMakeSize(0, 0)
  355. box.cornerRadius = 4.0
  356. self.contentView.addSubview(box)
  357. box.mas_makeConstraints { (make) in
  358. make?.left.equalTo()(4.0)
  359. make?.right.equalTo()(-4.0)
  360. make?.height.equalTo()(32.0)
  361. make?.top.equalTo()(self.customBox.mas_top)?.offset()(CGFloat(mas_top))
  362. }
  363. let boxLabel = self.initBoxLabel()
  364. boxLabel.stringValue = stringValue
  365. box.contentView?.addSubview(boxLabel)
  366. boxLabel.mas_makeConstraints { (make) in
  367. make?.centerY.equalTo()(0.0)
  368. make?.left.equalTo()(8.0)
  369. }
  370. let textTypography = KMDesignToken.shared.typography(withToken: "dropdown.m.mac-text.def")
  371. var fontFamily: String = textTypography.fontFamily
  372. let fontWeight: String = textTypography.fontWeight
  373. if (fontFamily.contains(" ")) {
  374. fontFamily = fontFamily.replacingOccurrences(of: " ", with: "")
  375. }
  376. if (fontWeight != "") {
  377. fontFamily = String(format: "%@-%@", fontFamily, fontWeight)
  378. }
  379. if NSFont(name: stringValue, size: 12) != nil {
  380. fontFamily = stringValue
  381. } else {
  382. debugPrint("不支持字体" + stringValue)
  383. }
  384. boxLabel.font = NSFont(name: fontFamily, size: textTypography.fontSize.stringToCGFloat()) ?? NSFont.systemFont(ofSize: textTypography.fontSize.stringToCGFloat())
  385. let paragraphStyle = NSMutableParagraphStyle()
  386. paragraphStyle.lineSpacing = textTypography.lineHeight.stringToCGFloat()
  387. boxLabel.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  388. if (isDisabled) {
  389. box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.dis")
  390. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.dis")
  391. } else if (isSelected) {
  392. box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
  393. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
  394. }
  395. box.moveCallback = { mouseEntered, mouseBox in
  396. if (isDisabled) {
  397. return
  398. }
  399. if (isSelected) { // 选中没有 hover 效果
  400. return
  401. }
  402. if (mouseEntered) {
  403. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.hov")
  404. } else {
  405. mouseBox.fillColor = NSColor.clear
  406. }
  407. }
  408. box.downCallback = { [unowned self] downEntered, mouseBox, _ in
  409. if (isDisabled) {
  410. return
  411. }
  412. if (downEntered) {
  413. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
  414. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
  415. guard let callback = self.downCallback else {
  416. return
  417. }
  418. callback(true, stringValue)
  419. } else {
  420. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.norm")
  421. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.def")
  422. }
  423. }
  424. }
  425. }