Custom NSProgressIndicator drawrect always called while hidden

641 views Asked by At

my custom ProgressIndicator's -drawrect method are called once at start and many times during a copy progress, thats fine. But after the copy Progress the progress indicator are redrawing all the time also when the progress indicator is hidden.

@implementation NewProgressBar

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        indeterminate_X_1 = 0;
        indeterminate_X_2 = 0;
        indeterminate_X_3 = 0;
    }
    return self;
}

-(void)setDoubleValue:(double)value {
    [super setDoubleValue:value];
    if (![self isDisplayedWhenStopped] && value == [self maxValue]) {
        [self stopAnimation:self];
    }
}

- (void)startAnimation:(id)sender
{
    if (!animator) {
        animator = [NSTimer scheduledTimerWithTimeInterval:1.0/120 target:self selector:@selector(activateAnimation:) userInfo:nil repeats:YES];
    }
}

-(void)activateAnimation:(NSTimer*)timer {
    [self setNeedsDisplay:YES];
}

-(void)stopAnimation:(id)sender
{
    animator = nil;
}

- (void)drawRect:(NSRect)dirtyRect
{
    NSLog(@"%@", animator);
    NSGradient* gradient = [[NSGradient alloc] initWithColorsAndLocations:
                            STROKE_COLOR, 0.03,
                            [NSColor colorWithCalibratedRed: 0.837 green: 0.837 blue: 0.837 alpha: 1], 0.46,
                            BAR_COLOR, 0.49,
                            FILL_COLOR, 1.0, nil];

    CGFloat length = dirtyRect.size.width;
    length -= 3;

    NSBezierPath* roundedRectanglePath = [NSBezierPath bezierPathWithRoundedRect: NSMakeRect(0.5, 0.5, dirtyRect.size.width, 10) xRadius: 6.5 yRadius: 6.5];
    [[NSColor lightGrayColor] setFill];
    [roundedRectanglePath fill];

    if ([self isIndeterminate]) {
        indeterminate_X_1++;
        if (indeterminate_X_1 == dirtyRect.size.width) {
            indeterminate_X_1 = 0;
        }
        indeterminate_X_2++;
        if (indeterminate_X_1 == 14) {
            indeterminate_X_2 = 0;
        }
        indeterminate_X_3++;
        if (indeterminate_X_1 == 28) {
            indeterminate_X_3 = 0;
        }
        [self drawAnimationInRect:dirtyRect];
    } else
    {
        CGFloat width = floor(dirtyRect.size.width * [self doubleValue]/[self maxValue]);
        if (width >= 1) {
            NSRect fillRect = NSMakeRect(2, 2, floor(length * [self doubleValue]/[self maxValue]), 7);
            NSBezierPath* roundedRectangle2Path = [NSBezierPath bezierPathWithRoundedRect: fillRect xRadius: 6 yRadius: 6];
            [gradient drawInBezierPath: roundedRectangle2Path angle: 90];
        }
    }
}

- (void)drawAnimationInRect:(NSRect)rect
{
    NSColor* fillColor = [NSColor colorWithCalibratedRed: 1 green: 1 blue: 1 alpha: 1];
    NSColor* strokeColor = [NSColor colorWithCalibratedRed: 0.804 green: 0.804 blue: 0.804 alpha: 1];
    NSColor* barColor = [NSColor colorWithCalibratedRed: 0.87 green: 0.87 blue: 0.87 alpha: 1];
    NSGradient* gradient = [[NSGradient alloc] initWithColorsAndLocations:
                            strokeColor, 0.03,
                            [NSColor colorWithCalibratedRed: 0.837 green: 0.837 blue: 0.837 alpha: 1], 0.46,
                            barColor, 0.49,
                            fillColor, 1.0, nil];
    NSRect ovalRect = NSMakeRect(indeterminate_X_1, rect.origin.y + 1, 9, 9);
    NSBezierPath *oval = [NSBezierPath bezierPathWithOvalInRect:ovalRect];
    if (indeterminate_X_2 > 0) {
        NSRect ovalRect2 = NSMakeRect(indeterminate_X_2, rect.origin.y + 1, 9, 9);
        NSBezierPath *oval2 = [NSBezierPath bezierPathWithOvalInRect:ovalRect2];
        [gradient drawInBezierPath:oval2 angle:90];
    }
    if (indeterminate_X_3 > 0) {
        NSRect ovalRect3 = NSMakeRect(indeterminate_X_3, rect.origin.y + 1, 9, 9);
        NSBezierPath *oval3 = [NSBezierPath bezierPathWithOvalInRect:ovalRect3];
        [gradient drawInBezierPath:oval3 angle:90];
    }
    [gradient drawInBezierPath:oval angle:90];
}

@end

After the copy progress i set the doubleValue to 0 and hide the indicator. How can i prevent the redrawing ?

Konobi

0

There are 0 answers