123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- //
- // ActivityButton.swift
- // KdanAuto
- //
- // Created by 朱东勇 on 2022/12/7.
- //
- import Foundation
- import AppKit
- enum ActivityStatus : Int {
-
- case Normal = 0
-
- case Wait = 1
-
- case Process = 2
-
- case Finished = 3
-
- }
- class ActivityView : NSView {
- var _status : ActivityStatus = ActivityStatus(rawValue: 0) ?? .Normal
-
- var _timer : Timer = Timer()
-
- var _titleLbl : NSTextField = NSTextField()
- let kActivityTitleHeight = 25.0
-
- var _activityView : NSProgressIndicator = NSProgressIndicator()
- // var _activityView : NSView = NSView()
- // var _angle : CGFloat = 0
- // var _shapeLayer : CAShapeLayer = CAShapeLayer()
- var _degree : Double = 0
-
-
- public override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- loadSubviews()
- }
-
-
- public required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- loadSubviews()
- }
-
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- loadSubviews()
-
- updateStatus()
- }
-
-
- override func layout() {
- reloadSubviews()
- }
-
-
- // Reload subviews
- func loadSubviews() {
- reloadSubviews()
- }
-
- func reloadSubviews() {
- self.wantsLayer = true
- self.layer?.backgroundColor = NSColor.clear.cgColor
-
- let width = self.frame.width
- let height = self.frame.height
-
- // if nil == _titleLbl {
- // _titleLbl = NSTextField.init(frame: self.frame)
- // }
- _titleLbl.isSelectable = false
- _titleLbl.isEditable = false
- if nil == _titleLbl.superview {
- self.addSubview(_titleLbl)
- }
- _titleLbl.wantsLayer = true
- _titleLbl.frame = NSMakeRect(0, (height - kActivityTitleHeight) / 2.0, width, kActivityTitleHeight)
- _titleLbl.backgroundColor = NSColor.clear
- _titleLbl.font = NSFont.systemFont(ofSize: 12.0)
- _titleLbl.layer?.backgroundColor = NSColor.clear.cgColor
- _titleLbl.autoresizingMask = .width.union(.minXMargin).union(.maxXMargin).union(.height)
- _titleLbl.alignment = .center
- _titleLbl.layer?.borderWidth = 1
- _titleLbl.layer?.cornerRadius = 4
-
- // if nil == _activityView {
- // _activityView = NSProgressIndicator.init(frame: self.bounds)
- // }
- _activityView.controlSize = .small
- _activityView.style = .spinning
- if nil == _activityView.superview {
- self.addSubview(_activityView)
- }
- _activityView.startAnimation(self)
- let activitySize = kActivityTitleHeight
- // min(_activityView.frame.width, _activityView.frame.height)
- _activityView.frame = NSMakeRect((width - activitySize)/2.0,
- (height - activitySize)/2.0,
- activitySize,
- activitySize)
- //
- // if nil == _shapeLayer {
- // _shapeLayer = CAShapeLayer()
- // }
- // _shapeLayer.frame = _activityView.bounds
- // _activityView.wantsLayer = true;
- // _activityView.layer?.addSublayer(_shapeLayer)
- //
- // var mulPath = CGMutablePath()
- // let inRadius = activitySize/2 - 3
- // let outRadius = activitySize/2
- // let endRage = _angle - Double.pi / 3.0
- // mulPath.move(to: CGPointMake(activitySize/2.0 + inRadius*sin(_angle),
- // activitySize/2.0 + inRadius * cos(_angle)))
- // mulPath.addLine(to: CGPointMake(activitySize/2.0 + outRadius*sin(_angle),
- // activitySize/2.0 + outRadius * cos(_angle)))
- // mulPath.addArc(center: CGPoint(x: activitySize/2.0, y: activitySize/2.0),
- // radius: outRadius,
- // startAngle: _angle, endAngle: endRage, clockwise: true)
- // mulPath.addLine(to: CGPointMake(activitySize/2 + inRadius*sin(endRage),
- // activitySize/2 + inRadius * cos(endRage)))
- // mulPath.addArc(center: CGPoint(x: activitySize/2.0, y: activitySize/2.0),
- // radius: inRadius,
- // startAngle: endRage, endAngle: _angle, clockwise: false)
- // _shapeLayer.path = mulPath
- //// _shapeLayer.lineWidth = 5;
- //// _shapeLayer.lineJoin = .bevel
- //// _shapeLayer.lineCap = .square
- //// _shapeLayer.fillRule = .nonZero
- // _shapeLayer.fillColor = NSColor.blue.cgColor
- }
-
- /// Update Status
- func updateStatus() {
- _titleLbl.isHidden = (_status == .Normal || _status == .Process)
- // _titleLbl.layer?.opacity = (_status == .Normal || _status == .Process) ? 0:1
- _activityView.isHidden = (_status == .Normal || _status == .Wait || _status == .Finished)
- // _activityView.layer?.opacity = (_status == .Normal || _status == .Wait || _status == .Finished) ? 0:1
-
- switch _status {
-
- case .Wait: do {
- _titleLbl.textColor = NSColor.gray
- _titleLbl.layer?.borderColor = NSColor.lightGray.cgColor
- _titleLbl.stringValue = "等待中..."
- _timer.invalidate()
- _timer = Timer()
- }
- case .Process: do {
- // _timer = Timer.scheduledTimer(timeInterval: 1.0/30,
- // target: self,
- // selector: #selector(updateAnge),
- // userInfo: nil, repeats: true)
- }
- case .Finished: do {
- _titleLbl.stringValue = NSString(format: "完成 %.0f%%", floor(_degree)) as String
-
- if fabs(_degree-100)>=0.1 {
- _titleLbl.textColor = NSColor.red
- _titleLbl.layer?.borderColor = NSColor.red.cgColor
- }else {
- _titleLbl.textColor = NSColor.blue
- _titleLbl.layer?.borderColor = NSColor.blue.cgColor
- }
- _timer.invalidate()
- _timer = Timer()
- }
-
- default: do {
- _titleLbl.stringValue = ""
- _timer.invalidate()
- _timer = Timer()
- }
-
- }
-
- }
-
- /// Setter & Getter
- ///
- func setActivityStatus(_ status:ActivityStatus) {
- // if (_status != status) {
- _status = status
-
- updateStatus()
- // }
- }
-
- func activityStatus() -> ActivityStatus {
- return _status
- }
-
- func setDegree(_ degree:Double) {
- _degree = degree;
-
- updateStatus()
- }
-
- }
|