Created
October 17, 2016 18:29
-
-
Save owenzhao/dc88392da571faeedadb8e1cb7c0d843 to your computer and use it in GitHub Desktop.
progress bar with file dealing process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - progress bar with file dealing process | |
private var counter:Int = 0 | |
func process() { | |
// get total | |
// prepare alert | |
let screenFrame = NSScreen.main()!.frame | |
let window = NSWindow(contentRect: NSMakeRect(screenFrame.width / 2 - 140, screenFrame.height * 0.66 + 50, 280, 20), | |
styleMask: NSBorderlessWindowMask, | |
backing: .buffered, defer: false) | |
window.isOpaque = false | |
let alert = NSAlert() | |
alert.addButton(withTitle: "OK") | |
alert.buttons.first!.isHidden = true | |
// prepare prgreessBar | |
let progressBar = NSProgressIndicator(frame: NSMakeRect(0,0,296,20)) | |
progressBar.isIndeterminate = false | |
progressBar.maxValue = 1000 | |
progressBar.doubleValue = 0 | |
let step = progressBar.maxValue / Double(total) | |
alert.accessoryView = progressBar | |
alert.messageText = "0/\(total)" | |
alert.beginSheetModal(for: window) { [unowned self] (buttonId) in | |
alert.window.orderOut(self) | |
} | |
let userInfo:[String:Any] = ["progress bar": progressBar, "step": step, "alert": alert, "total": total] | |
// timer to update progress bar UI | |
self.counter = 0 | |
let timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateProgressbar(timer:)), userInfo: userInfo, repeats: true) | |
DispatchQueue.global().async() { [unowned self] () -> () in | |
func excute() { | |
// MARK: - do works | |
// for in loop | |
for _ in 0 ... total { | |
// update counter | |
self.counter += 1 | |
} | |
} | |
excute() | |
DispatchQueue.main.async { [unowned self] () -> () in | |
timer.invalidate() | |
progressBar.doubleValue = 1000 | |
alert.messageText = "\(total)/\(total)" | |
alert.buttons.first!.performClick(self) | |
} | |
} | |
} | |
func updateProgressbar(timer:Timer) { | |
DispatchQueue.main.async { [unowned self] () -> () in | |
if let dic = timer.userInfo as? [String:Any], let progressBar = dic["progress bar"] as? NSProgressIndicator, let step = dic["step"] as? Double, let alert = dic["alert"] as? NSAlert, let total = dic["total"] as? Int { | |
progressBar.doubleValue = step * Double(self.counter) | |
alert.messageText = "\(self.counter)/\(total)" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, very useful. Also you can add
window.level = Int(CGWindowLevelForKey(.maximumWindow))
to make the alert always on top and visible.