SKProgressController.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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: NSWindowController {
  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. // self.window?.appearance = NSAppearance(named: .aqua)
  42. let button = NSButton()
  43. self.window?.contentView?.addSubview(button)
  44. let width = self.window?.contentView?.frame.width ?? 0
  45. let height = self.window?.contentView?.frame.height ?? 0
  46. let size = CGSize(width: 20, height: 20)
  47. let buttonY: CGFloat = 28
  48. let buttonX = width - size.width - 10
  49. button.frame = NSRect(x: buttonX, y: buttonY, width: size.width, height: size.height)
  50. button.autoresizingMask = [.minXMargin, .maxYMargin]
  51. button.isBordered = false
  52. button.image = NSImage(named: "KMImageNameWhiteClose")
  53. button.target = self
  54. button.action = #selector(buttonAction(_:))
  55. button.isHidden = !self.showClose
  56. }
  57. //MARK: Get & Set
  58. var message: String? {
  59. get {
  60. return progressField?.stringValue ?? ""
  61. }
  62. set {
  63. progressField.stringValue = newValue!
  64. self.window?.title = newValue!
  65. }
  66. }
  67. var indeterminate: Bool {
  68. get {
  69. return self.progressBar.isIndeterminate
  70. }
  71. set {
  72. self.progressBar.isIndeterminate = newValue
  73. }
  74. }
  75. var maxValue: Double {
  76. get {
  77. return self.progressBar.maxValue
  78. }
  79. set {
  80. self.progressBar.maxValue = newValue
  81. self.progressBar.doubleValue = 0.0
  82. }
  83. }
  84. var doubleValue: Double {
  85. get {
  86. return self.progressBar.doubleValue
  87. }
  88. set {
  89. DispatchQueue.main.async {
  90. self.progressBar.doubleValue = newValue
  91. self.progressBar.displayIfNeeded()
  92. }
  93. }
  94. }
  95. func increment(by delta: Double) {
  96. self.progressBar.increment(by: delta)
  97. self.progressBar.displayIfNeeded()
  98. }
  99. //MARK: Action
  100. @objc func buttonAction(_ sender: NSButton) {
  101. // Your button action code here
  102. if let closeBlock = self.closeBlock {
  103. closeBlock()
  104. }
  105. self.dismissSheet(sender)
  106. }
  107. //didEndSheet(returnCode:contextInfo:)
  108. // func beginSheetModal(for window: NSWindow, completionHandler handler: ((NSInteger) -> Void)?) {
  109. // progressBar.startAnimation(self)
  110. //
  111. // NSApp.beginSheet(window,
  112. // modalFor: window,
  113. // modalDelegate: self,
  114. // didEnd: #selector(didEndSheet(returnCode:contextInfo:)),
  115. // contextInfo: handler != nil ? Unmanaged.passRetained(handler as AnyObject).toOpaque() : nil)
  116. // }
  117. @IBAction func dismissSheet(_ sender: Any) {
  118. self.stopAnimation()
  119. if #available(macOS 10.13, *) {
  120. NSApp.endSheet(self.window!, returnCode: (sender as AnyObject).tag)
  121. } else {
  122. NSApp.endSheet(self.window!)
  123. }
  124. self.window!.orderOut(self)
  125. }
  126. func stopAnimation() {
  127. self.progressBar.stopAnimation(self)
  128. }
  129. }