SKProgressController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // SKProgressController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/10/7.
  6. //
  7. import Cocoa
  8. class SKProgressController_ProgressIndicator: NSProgressIndicator {
  9. override func draw(_ dirtyRect: NSRect) {
  10. let color = NSColor(red: 206/255.0, green: 208/255.0, blue: 212/255.0, alpha: 1.0)
  11. color.setStroke()
  12. color.setFill()
  13. let lineRect = NSRect(x: 1.5, y: (self.bounds.height - 2.4) * 0.5, width: self.bounds.width - 2, height: 2.4)
  14. let linePath = NSBezierPath(roundedRect: lineRect, xRadius: 0, yRadius: 0)
  15. linePath.fill()
  16. linePath.stroke()
  17. super.draw(dirtyRect)
  18. }
  19. }
  20. class SKProgressController: KMNBaseWindowController {
  21. @IBOutlet var progressBar: NSProgressIndicator!
  22. @IBOutlet var progressField: NSTextField!
  23. var _message: String?
  24. var _indeterminate: Bool = false
  25. var _maxValue: Double = 100.0
  26. var _doubleValue: Double = 0.0
  27. // 是否显示关闭按钮 [默认是显示]
  28. var showClose: Bool = true
  29. var closeBlock: (() -> Void)?
  30. override var windowNibName: NSNib.Name? {
  31. return NSNib.Name("ProgressSheet")
  32. }
  33. override func awakeFromNib() {
  34. super.awakeFromNib()
  35. KMPrint("")
  36. }
  37. override func windowDidLoad() {
  38. super.windowDidLoad()
  39. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  40. progressBar.usesThreadedAnimation = true
  41. let button = NSButton()
  42. self.window?.contentView?.addSubview(button)
  43. let width = self.window?.contentView?.frame.width ?? 0
  44. let height = self.window?.contentView?.frame.height ?? 0
  45. let size = CGSize(width: 20, height: 20)
  46. let buttonY: CGFloat = 28
  47. let buttonX = width - size.width - 10
  48. button.frame = NSRect(x: buttonX, y: buttonY, width: size.width, height: size.height)
  49. button.autoresizingMask = [.minXMargin, .maxYMargin]
  50. button.isBordered = false
  51. button.image = NSImage(named: "KMImageNameWhiteClose")
  52. button.target = self
  53. button.action = #selector(buttonAction(_:))
  54. button.isHidden = !self.showClose
  55. }
  56. //MARK: Get & Set
  57. var message: String? {
  58. get {
  59. return progressField?.stringValue ?? ""
  60. }
  61. set {
  62. progressField.stringValue = newValue!
  63. self.window?.title = newValue!
  64. }
  65. }
  66. var indeterminate: Bool {
  67. get {
  68. return self.progressBar.isIndeterminate
  69. }
  70. set {
  71. self.progressBar.isIndeterminate = newValue
  72. }
  73. }
  74. var maxValue: Double {
  75. get {
  76. return self.progressBar.maxValue
  77. }
  78. set {
  79. self.progressBar.maxValue = newValue
  80. self.progressBar.doubleValue = 0.0
  81. }
  82. }
  83. var doubleValue: Double {
  84. get {
  85. return self.progressBar.doubleValue
  86. }
  87. set {
  88. DispatchQueue.main.async {
  89. self.progressBar.doubleValue = newValue
  90. self.progressBar.displayIfNeeded()
  91. }
  92. }
  93. }
  94. func increment(by delta: Double) {
  95. self.progressBar.increment(by: delta)
  96. self.progressBar.displayIfNeeded()
  97. }
  98. //MARK: Action
  99. @objc func buttonAction(_ sender: NSButton) {
  100. // Your button action code here
  101. if let closeBlock = self.closeBlock {
  102. closeBlock()
  103. }
  104. self.dismissSheet(sender)
  105. }
  106. @IBAction func dismissSheet(_ sender: Any) {
  107. self.stopAnimation()
  108. self.own_closeEndSheet()
  109. self.window!.orderOut(self)
  110. }
  111. func stopAnimation() {
  112. self.progressBar.stopAnimation(self)
  113. }
  114. }