SKProgressController.swift 4.4 KB

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