1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
SPDX-FileCopyrightText: 2016 René J.V. Bertin <rjvbertin@gmail.com>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "macdockprogressview.h"
#include <QtGlobal>
#include <utility>
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
#import <AppKit/NSDockTile.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSImageView.h>
#import <AppKit/NSCIImageRep.h>
#import <AppKit/NSBezierPath.h>
#import <AppKit/NSColor.h>
#import <Foundation/NSTimer.h>
using namespace KDevelop;
@interface MacDockProgressViewPrivate : NSView {
int min;
int max;
int value;
NSColor *bgColour, *fgColour;
NSTimer *indeterminateTimer;
}
+ (MacDockProgressViewPrivate *)sharedProgressView;
- (id)init;
- (void)dealloc;
- (void)removeTimer;
- (void)rangeStartsAt:(int)v1 endsAt:(int)v2;
- (void)setValue:(int)v;
- (void)updateBadge;
@end
static MacDockProgressViewPrivate *sharedProgressView = nil;
@implementation MacDockProgressViewPrivate
+ (MacDockProgressViewPrivate *)sharedProgressView
{
if (sharedProgressView == nil) {
sharedProgressView = [[MacDockProgressViewPrivate alloc] init];
[sharedProgressView rangeStartsAt:0 endsAt:100];
}
return sharedProgressView;
}
- (id)init
{
self = [super init];
bgColour = fgColour = nil;
indeterminateTimer = nil;
min = max = value = 0;
return self;
}
- (void)dealloc
{
[self removeTimer];
[bgColour release];
[fgColour release];
[super dealloc];
}
- (void)removeTimer
{
if (indeterminateTimer != nil) {
[indeterminateTimer invalidate];
[indeterminateTimer release];
indeterminateTimer = nil;
}
}
- (void)rangeStartsAt:(int)v1 endsAt:(int)v2
{
min = v1;
max = v2;
// (re)set the colours to the standard progressbar colour scheme
[bgColour release];
[fgColour release];
bgColour = [[NSColor blackColor] retain];
fgColour = [[NSColor lightGrayColor] retain];
if (v1 == v2 ) {
if (indeterminateTimer == nil) {
indeterminateTimer = [[NSTimer timerWithTimeInterval:1
target:self
selector:@selector(updateBadge)
userInfo:nil
repeats:YES] retain];
if (indeterminateTimer) {
[[NSRunLoop currentRunLoop] addTimer:indeterminateTimer forMode:NSDefaultRunLoopMode];
}
}
} else {
[self removeTimer];
}
[self updateBadge];
}
- (void)setValue:(int)v
{
value = v;
[self updateBadge];
}
- (void)updateBadge
{
[[NSApp dockTile] display];
}
- (void)drawRect:(NSRect)rect
{
Q_UNUSED(rect)
NSRect boundary = [self bounds];
[[NSApp applicationIconImage] drawInRect:boundary
fromRect:NSZeroRect
operation:NSCompositeCopy
fraction:1.0];
NSRect progressBoundary = boundary;
progressBoundary.size.height *= 0.1;
progressBoundary.size.width *= 0.8;
progressBoundary.origin.x = (NSWidth(boundary) - NSWidth(progressBoundary)) / 2.0;
progressBoundary.origin.y = NSHeight(boundary) * 0.1;
double percent = 0.50;
if (min != max) {
double range = max - min;
percent = (value - min) / range;
if (percent > 1) {
percent = 1;
} else if (percent < 0) {
percent = 0;
}
} else {
// poor man's indefinite busy progressbar obtained by swapping
// fg and bg colours with the bar at the 50% point.
std::swap(fgColour, bgColour);
}
NSRect currentProgress = progressBoundary;
currentProgress.size.width *= percent;
[bgColour setFill];
[NSBezierPath fillRect:progressBoundary];
[fgColour setFill];
[NSBezierPath fillRect:currentProgress];
[bgColour setStroke];
[NSBezierPath strokeRect:progressBoundary];
}
@end
void MacDockProgressView::setRange(int min, int max)
{
[[MacDockProgressViewPrivate sharedProgressView] rangeStartsAt:min endsAt:max];
}
void MacDockProgressView::setProgress(int value)
{
[[MacDockProgressViewPrivate sharedProgressView] setValue:value];
}
void MacDockProgressView::setProgressVisible(bool visible)
{
if (visible) {
[[NSApp dockTile] setContentView:[MacDockProgressViewPrivate sharedProgressView]];
} else {
[[NSApp dockTile] setContentView:nil];
}
[[NSApp dockTile] display];
}
#else
void MacDockProgressView::setRange(int min, int max)
{
Q_UNUSED(min)
Q_UNUSED(max)
}
void MacDockProgressView::setProgress(int value)
{
Q_UNUSED(value)
}
void MacDockProgressView::setProgressVisible(bool visible)
{
Q_UNUSED(visible)
}
#endif
|