KMHomePopViewController.swift 21 KB

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