Warning, /kdevelop/kdevelop/kdevplatform/shell/macdockprogressview.mm is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2016 René J.V. Bertin <rjvbertin@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "macdockprogressview.h"
0008 #include <QtGlobal>
0009 #include <utility>
0010 
0011 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
0012 #import <AppKit/NSDockTile.h>
0013 #import <AppKit/NSApplication.h>
0014 #import <AppKit/NSImageView.h>
0015 #import <AppKit/NSCIImageRep.h>
0016 #import <AppKit/NSBezierPath.h>
0017 #import <AppKit/NSColor.h>
0018 #import <Foundation/NSTimer.h>
0019 
0020 using namespace KDevelop;
0021 
0022 @interface MacDockProgressViewPrivate : NSView {
0023     int min;
0024     int max;
0025     int value;
0026     NSColor *bgColour, *fgColour;
0027     NSTimer *indeterminateTimer;
0028 }
0029 
0030 + (MacDockProgressViewPrivate *)sharedProgressView;
0031 
0032 - (id)init;
0033 - (void)dealloc;
0034 - (void)removeTimer;
0035 - (void)rangeStartsAt:(int)v1 endsAt:(int)v2;
0036 - (void)setValue:(int)v;
0037 - (void)updateBadge;
0038 
0039 @end
0040 
0041 static MacDockProgressViewPrivate *sharedProgressView = nil;
0042 
0043 @implementation MacDockProgressViewPrivate
0044 
0045 + (MacDockProgressViewPrivate *)sharedProgressView
0046 {
0047     if (sharedProgressView == nil) {
0048         sharedProgressView = [[MacDockProgressViewPrivate alloc] init];
0049         [sharedProgressView rangeStartsAt:0 endsAt:100];
0050     }
0051     return sharedProgressView;
0052 }
0053 
0054 - (id)init
0055 {
0056     self = [super init];
0057     bgColour = fgColour = nil;
0058     indeterminateTimer = nil;
0059     min = max = value = 0;
0060     return self;
0061 }
0062 
0063 - (void)dealloc
0064 {
0065     [self removeTimer];
0066     [bgColour release];
0067     [fgColour release];
0068     [super dealloc];
0069 }
0070 
0071 - (void)removeTimer
0072 {
0073     if (indeterminateTimer != nil) {
0074         [indeterminateTimer invalidate];
0075         [indeterminateTimer release];
0076         indeterminateTimer = nil;
0077     }
0078 }
0079 
0080 - (void)rangeStartsAt:(int)v1 endsAt:(int)v2
0081 {
0082     min = v1;
0083     max = v2;
0084     // (re)set the colours to the standard progressbar colour scheme
0085     [bgColour release];
0086     [fgColour release];
0087     bgColour = [[NSColor blackColor] retain];
0088     fgColour = [[NSColor lightGrayColor] retain];
0089     if (v1 == v2 ) {
0090         if (indeterminateTimer == nil) {
0091             indeterminateTimer = [[NSTimer timerWithTimeInterval:1
0092                                             target:self
0093                                             selector:@selector(updateBadge)
0094                                             userInfo:nil
0095                                             repeats:YES] retain];
0096             if (indeterminateTimer) {
0097                 [[NSRunLoop currentRunLoop] addTimer:indeterminateTimer forMode:NSDefaultRunLoopMode];
0098             }
0099         }
0100     } else {
0101         [self removeTimer];
0102     }
0103     [self updateBadge];
0104 }
0105 
0106 - (void)setValue:(int)v
0107 {
0108     value = v;
0109     [self updateBadge];
0110 }
0111 
0112 - (void)updateBadge
0113 {
0114     [[NSApp dockTile] display];
0115 }
0116 
0117 - (void)drawRect:(NSRect)rect
0118 {
0119     Q_UNUSED(rect)
0120     NSRect boundary = [self bounds];
0121     [[NSApp applicationIconImage] drawInRect:boundary
0122                                 fromRect:NSZeroRect
0123                                 operation:NSCompositeCopy
0124                                 fraction:1.0];
0125     NSRect progressBoundary = boundary;
0126     progressBoundary.size.height *= 0.1;
0127     progressBoundary.size.width *= 0.8;
0128     progressBoundary.origin.x = (NSWidth(boundary) - NSWidth(progressBoundary)) / 2.0;
0129     progressBoundary.origin.y = NSHeight(boundary) * 0.1;
0130 
0131     double percent = 0.50;
0132     if (min != max) {
0133         double range = max - min;
0134         percent = (value - min) / range;
0135         if (percent > 1) {
0136             percent = 1;
0137         } else if (percent < 0) {
0138             percent = 0;
0139         }
0140     } else {
0141         // poor man's indefinite busy progressbar obtained by swapping
0142         // fg and bg colours with the bar at the 50% point.
0143         std::swap(fgColour, bgColour);
0144     }
0145 
0146     NSRect currentProgress = progressBoundary;
0147     currentProgress.size.width *= percent;
0148     [bgColour setFill];
0149     [NSBezierPath fillRect:progressBoundary];
0150     [fgColour setFill];
0151     [NSBezierPath fillRect:currentProgress];
0152     [bgColour setStroke];
0153     [NSBezierPath strokeRect:progressBoundary];
0154 }
0155 
0156 @end
0157 
0158 void MacDockProgressView::setRange(int min, int max)
0159 {
0160     [[MacDockProgressViewPrivate sharedProgressView] rangeStartsAt:min endsAt:max];
0161 }
0162 
0163 void MacDockProgressView::setProgress(int value)
0164 {
0165     [[MacDockProgressViewPrivate sharedProgressView] setValue:value];
0166 }
0167 
0168 void MacDockProgressView::setProgressVisible(bool visible)
0169 {
0170     if (visible) {
0171         [[NSApp dockTile] setContentView:[MacDockProgressViewPrivate sharedProgressView]];
0172     } else {
0173         [[NSApp dockTile] setContentView:nil];
0174     }
0175     [[NSApp dockTile] display];
0176 }
0177 
0178 #else
0179 
0180 void MacDockProgressView::setRange(int min, int max)
0181 {
0182     Q_UNUSED(min)
0183     Q_UNUSED(max)
0184 }
0185 
0186 void MacDockProgressView::setProgress(int value)
0187 {
0188     Q_UNUSED(value)
0189 }
0190 
0191 void MacDockProgressView::setProgressVisible(bool visible)
0192 {
0193     Q_UNUSED(visible)
0194 }
0195 
0196 #endif