WaitingView.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // WaitingView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/9.
  6. //
  7. import Cocoa
  8. @objcMembers class WaitingView: NSView{
  9. var indicator: NSProgressIndicator?
  10. override init(frame frameRect: NSRect) {
  11. super.init(frame: frameRect)
  12. var _indicator = NSProgressIndicator()
  13. _indicator.isBezeled = false
  14. _indicator.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  15. _indicator.style = .spinning
  16. _indicator.controlSize = .regular
  17. _indicator.usesThreadedAnimation = true
  18. _indicator.sizeToFit()
  19. self.addSubview(_indicator)
  20. _indicator.center()
  21. self.indicator = _indicator
  22. }
  23. required init?(coder: NSCoder) {
  24. super.init(coder: coder)
  25. }
  26. func startAnimation() {
  27. indicator!.startAnimation(nil)
  28. }
  29. func stopAnimation() {
  30. indicator?.stopAnimation(nil)
  31. }
  32. override func draw(_ dirtyRect: NSRect) {
  33. NSColor(calibratedRed: 1.0, green: 1.0, blue: 1.0, alpha: 0.5).set()
  34. NSBezierPath(rect: self.bounds).fill()
  35. }
  36. override func mouseDown(with theEvent: NSEvent) {
  37. }
  38. override func mouseUp(with theEvent: NSEvent) {
  39. }
  40. }
  41. extension NSView{
  42. func center(){
  43. guard let superview = self.superview else { return }
  44. self.frame = NSMakeRect(
  45. 0.5 * (superview.frame.size.width - self.frame.size.width),
  46. 0.5 * (superview.frame.size.height - self.frame.size.height),
  47. self.frame.size.width,
  48. self.frame.size.height
  49. )
  50. }
  51. }