Skip to content

Commit

Permalink
feat: add Dock Progress Bar
Browse files Browse the repository at this point in the history
  • Loading branch information
EETagent committed Feb 3, 2024
1 parent 43cb88f commit d4410b0
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Send.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
4634032228BE740600C759DB /* SettingsStackView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4634031F28BE70AE00C759DB /* SettingsStackView.m */; };
4634033228BE805200C759DB /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4634033428BE805200C759DB /* Localizable.strings */; };
46371F2C28BA4F5F008F6DFF /* FileItemView.m in Sources */ = {isa = PBXBuildFile; fileRef = 46371F2B28BA4F5F008F6DFF /* FileItemView.m */; };
46B9E8D22B6E8D1700BFD528 /* DockProgress.m in Sources */ = {isa = PBXBuildFile; fileRef = 46B9E8D12B6E8D1700BFD528 /* DockProgress.m */; };
46BC690328F1D31200E3D268 /* Sparkle in Frameworks */ = {isa = PBXBuildFile; productRef = 46BC690228F1D31200E3D268 /* Sparkle */; };
46BFCF3228BBEF66006919D0 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 46BFCF3128BBEF66006919D0 /* Credits.rtf */; };
46CAAA0828B802430015008F /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 46CAAA0728B802430015008F /* AppDelegate.m */; };
Expand Down Expand Up @@ -149,6 +150,8 @@
46371F2B28BA4F5F008F6DFF /* FileItemView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = FileItemView.m; sourceTree = "<group>"; };
4651F4B328BB9E2A00FF3E4D /* DropDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = DropDelegate.h; sourceTree = "<group>"; };
46769B3F28BC038700F06426 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
46B9E8D02B6E8D1700BFD528 /* DockProgress.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DockProgress.h; sourceTree = "<group>"; };
46B9E8D12B6E8D1700BFD528 /* DockProgress.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DockProgress.m; sourceTree = "<group>"; };
46BFCF3128BBEF66006919D0 /* Credits.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = Credits.rtf; sourceTree = "<group>"; };
46CAAA0328B802430015008F /* Send.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Send.app; sourceTree = BUILT_PRODUCTS_DIR; };
46CAAA0628B802430015008F /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = AppDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -358,6 +361,15 @@
path = Class;
sourceTree = "<group>";
};
46B9E8CF2B6E8D0100BFD528 /* DockProgress */ = {
isa = PBXGroup;
children = (
46B9E8D02B6E8D1700BFD528 /* DockProgress.h */,
46B9E8D12B6E8D1700BFD528 /* DockProgress.m */,
);
path = DockProgress;
sourceTree = "<group>";
};
46CAA9FA28B802430015008F = {
isa = PBXGroup;
children = (
Expand All @@ -382,6 +394,7 @@
46CAAA0528B802430015008F /* Send */ = {
isa = PBXGroup;
children = (
46B9E8CF2B6E8D0100BFD528 /* DockProgress */,
4634032328BE741300C759DB /* Shared */,
46769B3F28BC038700F06426 /* Info.plist */,
4618301328BA7D3600BFB7E8 /* Model */,
Expand Down Expand Up @@ -672,6 +685,7 @@
462E688A28BA28C8001F569F /* FilesViewController.m in Sources */,
46CF0F4328BD11FF00298A84 /* ResultView.m in Sources */,
46CAAA3928B807050015008F /* UploadViewController.m in Sources */,
46B9E8D22B6E8D1700BFD528 /* DockProgress.m in Sources */,
4634032228BE740600C759DB /* SettingsStackView.m in Sources */,
46CAAA1228B802440015008F /* main.m in Sources */,
46CAAA0828B802430015008F /* AppDelegate.m in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions Send/DockProgress/DockProgress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// DockProgress.h
// Send
// Ported from https://github.com/sindresorhus/DockProgress
//

#include <Cocoa/Cocoa.h>

@interface DockProgress : NSObject

@property (class, nonatomic, assign) double progress;

+ (void)resetProgress;
+ (void)hideProgress;

@end
107 changes: 107 additions & 0 deletions Send/DockProgress/DockProgress.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// DockProgress.m
// Send
// Ported from https://github.com/sindresorhus/DockProgress
//

#import <Cocoa/Cocoa.h>

#import "DockProgress.h"

@implementation DockProgress

+ (void)initialize {
if (self == [DockProgress self]) {
[self setDockImageView:[NSImageView new]];
[[NSApp dockTile] setContentView:[self dockImageView]];
}
}

static NSImageView *_dockImageView;

+ (NSImageView *)dockImageView {
return _dockImageView;
}

+ (void)setDockImageView:(NSImageView *)dockImageView {
_dockImageView = dockImageView;
}

static double _previousProgress = 0;
static double _progress = 0;

+ (double)progress {
return _progress;
}

+ (void)setProgress:(double)progress {
if (_previousProgress == 0 || fabs(progress - _previousProgress) > 0.01) {
_previousProgress = progress;
[self updateDockIcon];
}
_progress = progress;
}

+ (void)resetProgress {
_progress = 0;
_previousProgress = 0;
[self updateDockIcon];
}

+ (void)hideProgress {
dispatch_async(dispatch_get_main_queue(), ^{
NSImage *appIcon = [[NSApplication sharedApplication] applicationIconImage];

[_dockImageView setImage:appIcon];

[[NSApp dockTile] display];
});
}


+ (void)updateDockIcon {
dispatch_async(dispatch_get_main_queue(), ^{
NSImage *appIcon = [[NSApplication sharedApplication] applicationIconImage];

NSImage *icon = (_progress >= 0 && _progress < 1) ? [self draw:appIcon] : appIcon;

[_dockImageView setImage:icon];

[[NSApp dockTile] display];
});
}

+ (NSImage *)draw:(NSImage *)appIcon {
NSImage *newImage = [[NSImage alloc] initWithSize:appIcon.size];
[newImage lockFocusFlipped:NO];

[NSGraphicsContext.currentContext setImageInterpolation:NSImageInterpolationHigh];
[appIcon drawInRect:NSMakeRect(0, 0, newImage.size.width, newImage.size.height)];

[self drawProgressBar:NSMakeRect(0, 0, newImage.size.width, newImage.size.height)];

[newImage unlockFocus];

return newImage;
}

+ (void)drawProgressBar:(CGRect)dstRect {
void (^roundedRect)(CGRect) = ^(CGRect rect) {
[[NSBezierPath bezierPathWithRoundedRect:rect xRadius:rect.size.height / 2 yRadius:rect.size.height / 2] fill];
};

CGRect bar = CGRectMake(0, 20, dstRect.size.width, 10);
[[NSColor colorWithWhite:1.0 alpha:0.8] set];
roundedRect(bar);

CGRect barInnerBg = CGRectInset(bar, 0.5, 0.5);
[[NSColor colorWithWhite:0.0 alpha:0.8] set];
roundedRect(barInnerBg);

CGRect barProgress = CGRectInset(bar, 1, 1);
barProgress.size.width = barProgress.size.width * _progress;
[[NSColor whiteColor] set];
roundedRect(barProgress);
}

@end

0 comments on commit d4410b0

Please sign in to comment.