KMSecureLimitAlertView.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // KMSecureLimitAlertView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/2/7.
  6. //
  7. import Cocoa
  8. typealias KMSecureLimitAlertViewItemClick = (Int) -> ()
  9. class KMSecureLimitAlertView: NSView {
  10. var titleLabel = NSTextField(labelWithString: "")
  11. private var despLabel = NSTextField(wrappingLabelWithString: "")
  12. var deleteButton = NSButton()
  13. var funcButtonVC: KMDesignButton?
  14. var itemClick: KMSecureLimitAlertViewItemClick?
  15. private var funcButtonSize: NSSize = NSMakeSize(163, 32)
  16. override var isFlipped: Bool {
  17. return true
  18. }
  19. override init(frame frameRect: NSRect) {
  20. super.init(frame: frameRect)
  21. initSubViews()
  22. }
  23. required init?(coder: NSCoder) {
  24. super.init(coder: coder)
  25. initSubViews()
  26. }
  27. public func initSubViews() {
  28. self.addSubview(self.titleLabel)
  29. addSubview(despLabel)
  30. self.addSubview(self.deleteButton)
  31. self.funcButtonVC = KMDesignButton(withType: .Text)
  32. self.addSubview(self.funcButtonVC!.view)
  33. self.wantsLayer = true
  34. layer?.backgroundColor = NSColor.km_init(hex: "#E8F5FF").cgColor
  35. layer?.cornerRadius = 8
  36. self.shadow = NSShadow()
  37. self.layer?.shadowColor = NSColor.km_init(hex: "#00000029").cgColor
  38. self.layer?.shadowOpacity = 1
  39. self.layer?.shadowRadius = 8
  40. self.layer?.shadowOffset = CGSize(width: 0, height: -3)
  41. self.titleLabel.stringValue = NSLocalizedString("File Restricted", comment: "")
  42. self.titleLabel.textColor = NSColor.titleColor()
  43. self.titleLabel.font = NSFont.SFProTextRegularFont(14)
  44. despLabel.stringValue = NSLocalizedString("The file is protected with a password. ", comment: "")
  45. let ps = NSMutableParagraphStyle()
  46. ps.lineSpacing = 7
  47. despLabel.attributedStringValue = NSAttributedString(string: despLabel.stringValue, attributes: [.foregroundColor : NSColor.despColor(),
  48. .font : NSFont.SFProTextRegularFont(14),
  49. .paragraphStyle : ps
  50. ])
  51. self.deleteButton.isBordered = false
  52. self.deleteButton.image = NSImage(named: "KMImageNameCloseProgress12")
  53. self.deleteButton.target = self
  54. self.deleteButton.action = #selector(deleteButtonAction)
  55. self.funcButtonVC?.stringValue = NSLocalizedString("Enter Password", comment: "")
  56. self.funcButtonVC?.target = self
  57. self.funcButtonVC?.action = #selector(funcButtonAction)
  58. self.funcButtonVC?.button(type: .Ghost, size: .m)
  59. let size = self.funcButtonVC?.stringValue.boundingRect(with: NSMakeSize(1000, self.funcButtonSize.height), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [.foregroundColor : NSColor.buttonTitleColor(), .font : NSFont.SFProTextRegularFont(14)]).size
  60. if (size == nil || size!.width < 40) {
  61. self.funcButtonSize.width = 70
  62. } else {
  63. self.funcButtonSize.width = size!.width + 34
  64. }
  65. }
  66. override func layout() {
  67. super.layout()
  68. let width: CGFloat = NSWidth(self.bounds)
  69. let height: CGFloat = NSHeight(self.bounds)
  70. let leftMargin: CGFloat = 16
  71. self.titleLabel.frame = NSMakeRect(leftMargin, 16+4, width-leftMargin-60, 18)
  72. let despX: CGFloat = 16
  73. let despY: CGFloat = self.titleLabel.frame.maxY+8+4
  74. despLabel.frame = NSMakeRect(despX, despY, width-despX-36, height-despY-45)
  75. self.deleteButton.frame = NSMakeRect(width-34, 10, 24, 24)
  76. let funcButtonSize = self.funcButtonSize
  77. self.funcButtonVC?.view.frame = NSMakeRect(width-funcButtonSize.width-leftMargin, height-funcButtonSize.height-16, funcButtonSize.width, funcButtonSize.height)
  78. }
  79. @objc private func deleteButtonAction() {
  80. guard let callback = self.itemClick else {
  81. return
  82. }
  83. callback(1)
  84. }
  85. @objc private func funcButtonAction() {
  86. guard let callback = self.itemClick else {
  87. return
  88. }
  89. callback(2)
  90. }
  91. }