Draw directly in Dock tile contentView (#17)

Closes #2
This commit is contained in:
Chris Heyes 2023-05-17 14:08:27 +01:00 committed by GitHub
parent 90993316e9
commit 708dd530fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 32 deletions

View File

@ -6,7 +6,7 @@ public enum DockProgress {
private static var progressObserver: NSKeyValueObservation?
private static var finishedObserver: NSKeyValueObservation?
private static let dockImageView = with(NSImageView()) {
private static let dockContentView = with(ContentView()) {
NSApp.dockTile.contentView = $0
}
@ -84,38 +84,35 @@ public enum DockProgress {
// TODO: Make the progress smoother by also animating the steps between each call to `updateDockIcon()`
private static func updateDockIcon() {
// TODO: If the `progress` is 1, draw the full circle, then schedule another draw in n milliseconds to hide it
guard let appIcon = NSApp.applicationIconImage else {
return
}
let icon = progress > 0 && progress < 1 ? draw(appIcon) : appIcon
// TODO: Make this better by drawing in the `contentView` directly instead of using an image
dockImageView.image = icon
dockContentView.needsDisplay = true;
NSApp.dockTile.display()
}
private static func draw(_ appIcon: NSImage) -> NSImage {
NSImage(size: appIcon.size, flipped: false) { [self] dstRect in
private class ContentView: NSView {
override func draw(_ dirtyRect: NSRect) {
NSGraphicsContext.current?.imageInterpolation = .high
appIcon.draw(in: dstRect)
NSApp.applicationIconImage?.draw(in: dirtyRect)
// TODO: If the `progress` is 1, draw the full circle, then schedule another draw in n milliseconds to hide it
if (progress <= 0 || progress >= 1) {
return
}
switch style {
case .bar:
drawProgressBar(dstRect)
drawProgressBar(dirtyRect)
case .squircle(let inset, let color):
drawProgressSquircle(dstRect, inset: inset, color: color)
drawProgressSquircle(dirtyRect, inset: inset, color: color)
case .circle(let radius, let color):
drawProgressCircle(dstRect, radius: radius, color: color)
drawProgressCircle(dirtyRect, radius: radius, color: color)
case .badge(let color, let badgeValue):
drawProgressBadge(dstRect, color: color, badgeLabel: badgeValue())
drawProgressBadge(dirtyRect, color: color, badgeLabel: badgeValue())
case .pie(let color):
drawProgressBadge(dstRect, color: color, badgeLabel: 0, isPie: true)
drawProgressBadge(dirtyRect, color: color, badgeLabel: 0, isPie: true)
case .custom(let drawingHandler):
drawingHandler(dstRect)
drawingHandler(dirtyRect)
}
return true
}
}