KMRemovePasswordResultTipView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // KMRemovePasswordResultTipView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/11/29.
  6. //
  7. import Cocoa
  8. enum KMRemovePasswordResult: Int {
  9. case success = 1
  10. case failure = 2
  11. }
  12. class KMRemovePasswordResultTipView: NSView {
  13. private var iconImageView = NSImageView()
  14. private var iconSelectImageView = NSImageView()
  15. private var label = NSTextField(labelWithString: "")
  16. private var myResult: KMRemovePasswordResult = .success
  17. var result: KMRemovePasswordResult {
  18. get {
  19. return myResult
  20. }
  21. set {
  22. myResult = newValue
  23. if myResult == .success {
  24. label.stringValue = NSLocalizedString("The file has been successfully decrypted.", comment: "")
  25. iconImageView.image = NSImage(named: "KMImageNameNewTipSelected")
  26. } else {
  27. label.stringValue = NSLocalizedString("Decryption failed with unknown error.", comment: "")
  28. iconImageView.image = NSImage(named: "KMImageNameSecureTipFailure")
  29. }
  30. }
  31. }
  32. override init(frame frameRect: NSRect) {
  33. super.init(frame: frameRect)
  34. wantsLayer = true
  35. self.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  36. layer?.cornerRadius = 4
  37. self.shadow = NSShadow()
  38. self.layer?.shadowColor = NSColor.km_init(hex: "#00000029").cgColor
  39. self.layer?.shadowOpacity = 1
  40. self.layer?.shadowRadius = 8
  41. self.layer?.shadowOffset = CGSize(width: 0, height: -3)
  42. addSubview(iconImageView)
  43. addSubview(label)
  44. // self.iconImageView.addSubview(self.iconSelectImageView)
  45. self.label.textColor = NSColor.white
  46. label.font = NSFont.systemFont(ofSize: 14)
  47. // iconImageView.image = NSImage(named: "KMImageNameTipSelectedBackgroud")
  48. // self.iconSelectImageView.image = NSImage(named: "KMImageNameTipSelected")
  49. }
  50. required init?(coder: NSCoder) {
  51. fatalError("init(coder:) has not been implemented")
  52. }
  53. override func layout() {
  54. super.layout()
  55. let width = NSWidth(self.bounds)
  56. let heigth = NSHeight(self.bounds)
  57. let iconSize: CGFloat = 14
  58. iconImageView.frame = NSMakeRect(17, (heigth-iconSize)*0.5, iconSize, iconSize)
  59. self.iconSelectImageView.frame = NSMakeRect(3, 3, 8, 8)
  60. let labelX = iconImageView.frame.maxX + 5
  61. let labelH: CGFloat = 22
  62. label.frame = NSMakeRect(labelX, (heigth-labelH)*0.5, width-labelX, labelH)
  63. }
  64. func showInView(superView: NSView) {
  65. var style = NSMutableParagraphStyle()
  66. style.lineBreakMode = .byWordWrapping
  67. var attribute = [NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14), NSAttributedString.Key.paragraphStyle : style]
  68. let size = label.stringValue.boundingRect(with: NSMakeSize(min(NSWidth(superView.frame)-80, 500), NSHeight(superView.frame)-40), options: NSString.DrawingOptions(rawValue: 3), attributes: attribute).size
  69. let offsetSize = NSMakeSize(size.width+60, size.height+20)
  70. self.frame = NSMakeRect((NSWidth(superView.frame)-offsetSize.width)*0.5, NSHeight(superView.frame)-offsetSize.height-10, offsetSize.width, offsetSize.height)
  71. superView.addSubview(self)
  72. let ani = CABasicAnimation(keyPath: "opacity")
  73. ani.fromValue = 0
  74. ani.toValue = 1
  75. ani.duration = 0.5
  76. layer?.add(ani, forKey: "animation")
  77. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.3, execute: {
  78. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2, execute: {
  79. let ani = CABasicAnimation(keyPath: "opacity")
  80. ani.fromValue = 1
  81. ani.toValue = 0
  82. ani.duration = 0.5
  83. self.layer?.add(ani, forKey: "animation")
  84. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.3, execute: {
  85. self.removeFromSuperview()
  86. })
  87. })
  88. })
  89. }
  90. }