KMLightMemberAlertView.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // KMLightMemberAlertView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/2/24.
  6. //
  7. import Cocoa
  8. class KMLightMemberAlertView: KMBaseXibView {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var titleContentView: NSView!
  11. var timer: DispatchSourceTimer?
  12. var result: Result = Result() {
  13. didSet {
  14. self.alphaValue = 1.0
  15. self.reloadData()
  16. }
  17. }
  18. override func draw(_ dirtyRect: NSRect) {
  19. super.draw(dirtyRect)
  20. // Drawing code here.
  21. }
  22. override func setup() {
  23. super.setup()
  24. self.result = Result(code: 0)
  25. }
  26. override func updateUI() {
  27. super.updateUI()
  28. self.contentView.backgroundColor(NSColor.km_init(hex: "#FDC7C6"))
  29. self.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  30. self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  31. }
  32. override func reloadData() {
  33. super.reloadData()
  34. if result.code == 200 || result.code == 0 {
  35. self.contentView.isHidden = true
  36. } else {
  37. self.contentView.isHidden = false
  38. }
  39. self.titleLabel.stringValue = NSLocalizedString(KMRequestServerErrorCodeType.typeOfMessage(type: KMRequestServerErrorCodeType(rawValue: result.code)), comment: "")
  40. self.callMethodWithDelay()
  41. }
  42. func fetchAlertHeight() -> CGFloat {
  43. let string: NSString = self.titleLabel.stringValue as NSString
  44. let paragraphStyle = NSMutableParagraphStyle()
  45. // paragraphStyle.lineHeightMultiple = 1.32
  46. paragraphStyle.alignment = .left
  47. let size: NSSize = string.boundingRect(with: NSSize(width: 356, height: 100), options: NSString.DrawingOptions(rawValue: 3), attributes: [NSAttributedString.Key.font : NSFont.SFProTextRegularFont(14), NSAttributedString.Key.paragraphStyle : paragraphStyle]).size
  48. return size.height + 32
  49. }
  50. func callMethodWithDelay() {
  51. // 取消之前的计时器
  52. timer?.cancel()
  53. // 创建一个新的计时器,延迟 2 秒
  54. timer = DispatchSource.makeTimerSource()
  55. timer?.schedule(deadline: .now() + 3, leeway: .milliseconds(10))
  56. timer?.setEventHandler(handler: { [weak self] in
  57. // 2 秒后执行的代码
  58. self?.methodToExecuteAfterDelay()
  59. })
  60. timer?.resume()
  61. }
  62. func methodToExecuteAfterDelay() {
  63. // 执行的代码
  64. self.fadeOut()
  65. }
  66. func fadeOut() {
  67. NSAnimationContext.runAnimationGroup({ (context) in
  68. context.duration = 1
  69. animator().alphaValue = 0.0
  70. }, completionHandler: nil)
  71. }
  72. override func updateLanguage() {
  73. super.updateLanguage()
  74. }
  75. }