KMHomePopViewController.swift 20 KB

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