KMSecureLimitAlertView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // KMSecureLimitAlertView.swift
  3. // PDF Office
  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 funcButton = NSButton()
  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.addSubview(self.funcButton)
  32. self.wantsLayer = true
  33. layer?.backgroundColor = NSColor(hex: "#E8F5FF").cgColor
  34. layer?.cornerRadius = 8
  35. self.shadow = NSShadow()
  36. self.layer?.shadowColor = NSColor(hex: "#00000029").cgColor
  37. self.layer?.shadowOpacity = 1
  38. self.layer?.shadowRadius = 8
  39. self.layer?.shadowOffset = CGSize(width: 0, height: -3)
  40. self.titleLabel.stringValue = NSLocalizedString("File Restricted", comment: "")
  41. self.titleLabel.textColor = NSColor.titleColor()
  42. self.titleLabel.font = NSFont.SFProTextRegular(14)
  43. despLabel.stringValue = NSLocalizedString("The file is protected with a password. ", comment: "")
  44. let ps = NSMutableParagraphStyle()
  45. ps.lineSpacing = 7
  46. despLabel.attributedStringValue = NSAttributedString(string: despLabel.stringValue, attributes: [.foregroundColor : NSColor.despColor(),
  47. .font : NSFont.SFProTextRegular(14),
  48. .paragraphStyle : ps
  49. ])
  50. self.deleteButton.isBordered = false
  51. self.deleteButton.image = NSImage(named: "KMImageNameCloseProgress12")
  52. self.deleteButton.target = self
  53. self.deleteButton.action = #selector(deleteButtonAction)
  54. self.funcButton.title = NSLocalizedString("Remove Restrictions", comment: "")
  55. self.funcButton.isBordered = false
  56. self.funcButton.wantsLayer = true
  57. self.funcButton.setTitleColor(NSColor(hex: "#273C62"))
  58. self.funcButton.font = NSFont.SFProTextRegular(14)
  59. self.funcButton.layer?.cornerRadius = 4
  60. self.funcButton.layer?.borderWidth = 1
  61. self.funcButton.layer?.borderColor = NSColor(hex: "#273C62").cgColor
  62. self.funcButton.target = self
  63. self.funcButton.action = #selector(funcButtonAction)
  64. let size = self.funcButton.title.boundingRect(with: NSMakeSize(1000, self.funcButtonSize.height), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [.foregroundColor : NSColor.buttonTitleColor(), .font : NSFont.SFProTextRegular(14)]).size
  65. if (size.width < 40) {
  66. self.funcButtonSize.width = 70
  67. } else {
  68. self.funcButtonSize.width = size.width + 34
  69. }
  70. }
  71. override func layout() {
  72. super.layout()
  73. let width: CGFloat = NSWidth(self.bounds)
  74. let height: CGFloat = NSHeight(self.bounds)
  75. let leftMargin: CGFloat = 16
  76. self.titleLabel.frame = NSMakeRect(leftMargin, 16+4, width-leftMargin-60, 18)
  77. let despX: CGFloat = 16
  78. let despY: CGFloat = self.titleLabel.frame.maxY+8+4
  79. despLabel.frame = NSMakeRect(despX, despY, width-despX-36, height-despY-45)
  80. self.deleteButton.frame = NSMakeRect(width-34, 10, 24, 24)
  81. let funcButtonSize = self.funcButtonSize
  82. self.funcButton.frame = NSMakeRect(width-funcButtonSize.width-leftMargin, height-funcButtonSize.height-16, funcButtonSize.width, funcButtonSize.height)
  83. }
  84. @objc private func deleteButtonAction() {
  85. guard let callback = self.itemClick else {
  86. return
  87. }
  88. callback(1)
  89. }
  90. @objc private func funcButtonAction() {
  91. guard let callback = self.itemClick else {
  92. return
  93. }
  94. callback(2)
  95. }
  96. }