WaitingView.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. override func draw(_ dirtyRect: NSRect) {
  30. NSColor(calibratedRed: 1.0, green: 1.0, blue: 1.0, alpha: 0.5).set()
  31. NSBezierPath(rect: self.bounds).fill()
  32. }
  33. override func mouseDown(with theEvent: NSEvent) {
  34. }
  35. override func mouseUp(with theEvent: NSEvent) {
  36. }
  37. }
  38. extension NSView{
  39. func center(){
  40. guard let superview = self.superview else { return }
  41. self.frame = NSMakeRect(
  42. 0.5 * (superview.frame.size.width - self.frame.size.width),
  43. 0.5 * (superview.frame.size.height - self.frame.size.height),
  44. self.frame.size.width,
  45. self.frame.size.height
  46. )
  47. }
  48. }