Search code examples
objective-cmacoscocoascreensaver

Why doesn't this really simple blank white screen saver work? (It's black)


I'm trying to make a simple macOS screen saver via Objective-C in XCode that just fills the whole screen with white. (Because of this.) Simple, right? I thought so too, but no matter what I do, I get a blank, black screen. It doesn't seem like my drawRect method is even getting called. Any idea what I'm missing?

#import "Blank_WhiteView.h"

@implementation Blank_WhiteView

- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview {
    [super animateOneFrame];
    [self setNeedsDisplay:YES];
    return self;
}

- (void)drawRect:(NSRect)rect {

    [super drawRect:rect];
    CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(context, NSRectToCGRect(rect));
}

- (BOOL)hasConfigureSheet {
    return NO;
}

@end

Solution

  • ScreenSaverView draws everything through -animateOneFrame. To be able to use -drawRect (and in your situation, you should, because you aren't animating anything) implement animateOneFrame such that:

    - (void)animateOneFrame {
        [self setNeedsDisplay:YES]; // -drawRect will be called next draw loop
    }
    

    This way, -drawRect will be called when appropriate.