Search code examples
objective-ccocoaosx-snow-leoparddesktop-applicationnsapplication

How would I develop something like DeskLock from Deskshade?


Sorry to be a nuisance, but I have yet ANOTHER question. How would I do something like DeskLock from macrabbit's Deskshade app? I've made the little window and that's as far as I've come. I know how to "lock" the screen in 10.6 with PresentationOptions, but I don't want to risk it because last time it wouldn't let me back in ;]

EDIT: The DeskShade app actually is meant to cover your desktop, hiding all icons. It also allows you to randomize wallpaper patterns with several fade/swipes. There is one extra feature called DeskLock that actually presents a translucent black bevel (similar to AppSwitcher build into Mac) with a lock icon, and you can place personal text. When you click the lock icon, it presents a modal that asks for a password you can set. You can also just type this password without pressing anything, followed by the Enter key, and it unlocks the screen. This uses the DeskShade feature of hiding the desktop as well.

Thanks!


Solution

  • To create the overlay window you have to subclass NSWindow and set its style mask and background color:

    @implementation BigTransparentWindow
    - (id)initWithContentRect:(NSRect)contentRect 
                    styleMask:(NSUInteger)windowStyle 
                      backing:(NSBackingStoreType)bufferingType 
                        defer:(BOOL)deferCreation
    {
        self = [super initWithContentRect:contentRect 
                                styleMask:NSBorderlessWindowMask //this makes the window transparent
                                  backing:bufferingType
                                    defer:deferCreation];
        if(self)
        {
            [self setOpaque:NO];
            [self setHasShadow:NO];
            [self setBackgroundColor:[[NSColor blackColor] colorWithAlphaComponent:0.5]];
        }
        return self;
    }
    @end
    

    You then need to set the window's frame so that it covers all screens, and you need to set its window level appropriately:

    - (IBAction)showWindow:(id)sender
    {
        //set the window so it covers all available screens
        NSRect screensRect = NSZeroRect;
        for(NSScreen* screen in [NSScreen screens])
        {
            screensRect = NSUnionRect(screensRect,[screen frame]);
        }
        [yourWindow setFrame:screensRect display:YES];
    
        if(coverScreen)
        {
            //set the window so it is above all other windows
            [yourWindow setLevel:kCGMaximumWindowLevel];
        }
        else
        {
            //set the window so it sits just above the desktop icons
            [yourWindow setLevel:kCGDesktopIconWindowLevel + 1];
        }
    }
    

    As you've mentioned, you can use the NSApplicationPresentationOptions settings for NSApp to control how the user can interact with the system. An easy way to test this without locking yourself out is to set an NSTimer that calls a method that pulls the app out of kiosk mode after a timeout period.