ProgressVC.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // ProgressVC.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/9/6.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class ProgressVC: NSViewController {
  10. @IBOutlet weak var progressCircle: ComponentProgressCircle!
  11. @IBOutlet weak var circleSizeBox: NSComboBox!
  12. @IBOutlet weak var circleTypeBox: NSComboBox!
  13. @IBOutlet weak var itemWidthConst: NSLayoutConstraint!
  14. @IBOutlet weak var itemHeightConst: NSLayoutConstraint!
  15. @IBOutlet weak var progressLine: ComponentProgressLine!
  16. @IBOutlet weak var lineTypeBox: NSComboBox!
  17. @IBOutlet weak var progressPopup: ComponentProgressPopup!
  18. var progressTimer: Timer?
  19. var progressPopupWC: ComponentProgressWindowController = ComponentProgressWindowController.windowController()
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. // Do view setup here.
  23. self.circleSizeBox.selectItem(at: 0)
  24. self.circleTypeBox.selectItem(at: 0)
  25. self.lineTypeBox.selectItem(at: 0)
  26. self.reloadData()
  27. }
  28. func reloadData() {
  29. var size: ComponentSize = .m
  30. if self.circleSizeBox.indexOfSelectedItem == 1 {
  31. size = .s
  32. }
  33. var circleType: componentProgressSubType = .circle_Waiting
  34. if self.circleTypeBox.indexOfSelectedItem == 0 {
  35. } else if self.circleTypeBox.indexOfSelectedItem == 1 {
  36. circleType = .circle_ProgressPercent
  37. } else if self.circleTypeBox.indexOfSelectedItem == 2 {
  38. circleType = .circle_ErrorPercent
  39. } else if self.circleTypeBox.indexOfSelectedItem == 3 {
  40. circleType = .circle_ProgressNoPercent
  41. } else if self.circleTypeBox.indexOfSelectedItem == 4 {
  42. circleType = .circle_ErrorNoPercent
  43. } else if self.circleTypeBox.indexOfSelectedItem == 5 {
  44. circleType = .circle_Success
  45. }
  46. //progressItem
  47. let properties: ComponentProgressProperty = ComponentProgressProperty.init(size: size, progressType: .circle, subType: circleType, progress: 0)
  48. self.progressCircle.properties = properties
  49. itemWidthConst.constant = properties.propertyInfo.viewWidth
  50. itemHeightConst.constant = properties.propertyInfo.viewHeight
  51. if (progressTimer != nil) {
  52. progressTimer?.invalidate()
  53. progressTimer = nil
  54. }
  55. if circleType == .circle_ProgressPercent ||
  56. circleType == .circle_ErrorPercent {
  57. self.progressTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(progressTimerAction), userInfo: nil, repeats: true)
  58. }
  59. //Progress Line
  60. var lineType: componentProgressSubType = .line_Progressing
  61. var lineProgress: CGFloat = 0
  62. if self.lineTypeBox.indexOfSelectedItem == 0 {
  63. } else if self.lineTypeBox.indexOfSelectedItem == 1 {
  64. lineType = .line_Error
  65. lineProgress = 1
  66. } else if self.lineTypeBox.indexOfSelectedItem == 2 {
  67. lineType = .line_Success
  68. lineProgress = 1
  69. }
  70. let lineProperties: ComponentProgressProperty = ComponentProgressProperty.init(size: size, progressType: .line, subType: lineType, progress: lineProgress)
  71. self.progressLine.properties = lineProperties
  72. if lineType == .line_Progressing {
  73. self.progressTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(progressTimerAction), userInfo: nil, repeats: true)
  74. }
  75. //Progress PopupLine
  76. let popUpProperties: ComponentProgressProperty = ComponentProgressProperty.init(size: size, progressType: .popup, subType: .popup_Line, progress: 0, titleString: "Progressing")
  77. self.progressPopup.properties = popUpProperties
  78. //Progress Popup
  79. let popUpProgressProperties: ComponentProgressProperty = ComponentProgressProperty.init(size: size, progressType: .popup, subType: .popup_Circle, progress: 0, cancelString: "Cancel")
  80. self.progressPopupWC.properties = popUpProgressProperties
  81. }
  82. @IBAction func boxAction(_ sender: Any) {
  83. self.reloadData()
  84. }
  85. @IBAction func showProgressPopup(_ sender: Any) {
  86. progressPopupWC.showWindow(nil)
  87. }
  88. @objc func progressTimerAction() {
  89. if self.progressCircle.properties.progressSubType == .circle_ProgressPercent ||
  90. self.progressCircle.properties.progressSubType == .circle_ErrorPercent {
  91. self.progressCircle.properties.progress += 0.01
  92. if self.progressCircle.properties.progress >= 1 {
  93. self.progressCircle.properties.progress = 0
  94. }
  95. self.progressCircle.reloadData()
  96. }
  97. if self.progressLine.properties.progressSubType == .line_Progressing {
  98. self.progressLine.properties.progress += 0.01
  99. if self.progressLine.properties.progress >= 1 {
  100. self.progressLine.properties.progress = 0
  101. }
  102. self.progressLine.reloadData()
  103. }
  104. self.progressPopup.properties.progress += 0.01
  105. if self.progressPopup.properties.progress >= 1 {
  106. self.progressPopup.properties.progress = 0
  107. }
  108. self.progressPopup.reloadData()
  109. self.progressPopupWC.properties.progress += 0.01
  110. if self.progressPopupWC.properties.progress >= 1 {
  111. self.progressPopupWC.properties.progress = 0
  112. }
  113. self.progressPopupWC.reloadData()
  114. }
  115. }