Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw the desktop on Mac OS X?

I want to draw the desktop on Mac OS X (Snow Leopard). Specifically, I want to achieve the same effect as running:

/System/Library/Frameworks/ScreenSaver.framework/Resources/
ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background

(If you’re not near your computer, this displays the screensaver where you would normally see your desktop background.)

I know how to make a window without a border (by subclassing NSWindow and overriding initWithContentRect:styleMask:backing:defer: to set the window style to NSBorderlessWindowMask) and without a shadow (setHasShadow:NO.)

I know that I can call setLevel:kCGDesktopWindowLevel or kCGDesktopIconWindowLevel to put my window below other windows (see question 418791.) However this isn’t exactly what I want, because a window at this level is still on top of the desktop icons. I want to be on top of the desktop background, but below the icons.

My view is opaque. If there is a technique that clobbers the desktop background, that is OK.

like image 543
Dominic Cooney Avatar asked Feb 13 '11 05:02

Dominic Cooney


2 Answers

You should create a subclass of NSWindow and set the level to (kCGDesktopWindowLevel - 1). This will get your window below the icons. You should also ensure that your window doesn't become key or main and that it handles Exposé/Spaces properly by not moving.

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
    self = [super initWithContentRect:contentRect styleMask:windowStyle backing:bufferingType defer:deferCreation];
    if(self)
    {
        [self setLevel:kCGDesktopWindowLevel - 1];
        [self setCollectionBehavior:
            (NSWindowCollectionBehaviorCanJoinAllSpaces | 
             NSWindowCollectionBehaviorStationary | 
             NSWindowCollectionBehaviorIgnoresCycle)];
    }
    return self;
}

- (BOOL)canBecomeMainWindow
{
    return false;
}

- (BOOL)canBecomeKeyWindow
{
    return false;
}
like image 62
Rob Keniger Avatar answered Sep 24 '22 16:09

Rob Keniger


To display above the desktop but below the desktop icons, you need to do two things:

  1. Call [window setLevel:kCGDesktopWindowLevel] to float below other application windows.
  2. Call [window orderBack:self] to layer behind the window that draws the desktop icons.

Something during app launching brings the application to the front, so you should call orderBack: in your application delegate’s applicationDidFinishLaunching: method.

like image 26
Dominic Cooney Avatar answered Sep 22 '22 16:09

Dominic Cooney