A simple demo of showing a progress of possibly anything on the dock of your macOS app!
Code:
We create our CustomDockView:
logo: NSImage to display
progress: The progress of task to show
type: ProgressStyle to display in
tint: Color to display the progress in
structCustomDockView:View{varlogo:NSImagevarprogress:CGFloatvartype:ProgressStylevartint:Colorvarbody:someView{ZStack{Image(nsImage: logo).scaledToFit()GeometryReader{letwidth= $0.size.width
// Limits progress between 0 & 1letcappedProgress=max(min(progress,1),0)
switch type {case.rounded:RoundedRectangle(cornerRadius: width * 0.225).trim(from:0, to: cappedProgress).stroke(tint, lineWidth:6).rotationEffect(.degrees(-90))case.flatLine:ZStack(alignment:.leading){Capsule().fill(.primary.opacity(0.5))Capsule().fill(tint).frame(width: width * cappedProgress)}.frame(height:8).frame(maxWidth:.infinity, alignment:.bottom)}}.padding(15)}}}
Business Logic to update the dock with paramters:
letapplication:NSApplication=.shared
if isVisible == false {
application.dockTile.contentView =nil
application.dockTile.display()return}// fetches current logo `NSImage`
guard let logo = application.applicationIconImage else{return}// Custom Dock ViewletcustomView=NSHostingView(
rootView:CustomDockView(
logo: logo,
progress: progress,
type: progressStyle,
tint: tint
))
customView.layer?.backgroundColor =.clear
customView.frame.size = logo.size
// Adding to the dock
application.dockTile.contentView = customView
// Refresh the dock
application.dockTile.display()
About
A simple demo of updating dock with a certain progress!