views:

178

answers:

4

I'm trying to re-write an application I have for Windows in Objective-C for my Mac, and I want to be able to do something like Mac's hot corners. If I move my mouse to the left side of the screen it will make a window visible, if I move it outside of the window location the window will hide again. (window would be pushed up to the left side of screen).

Does anyone know where I can find some demo code (or reference) on how to do this, or at least how to tell where the mouse is at, even if the current application is not on top. (not sure how to word this, too used to Windows world).

Thank you

-Brad

+2  A: 

Hi Brad:

You're going to want to implement an invisible window on the edge of the screen with the window order set so it's always on top. Then, you can listen for mouse-moved events in this window.

To set the window to be invisible and on top, make a window subclass use calls like:

[self setBackgroundColor:[NSColor clearColor]];
[self setExcludedFromWindowsMenu:YES];
[self setCanHide:NO];
[self setLevel:NSScreenSaverWindowLevel];
[self setAlphaValue:0.0f];
[self setOpaque:NO];
[self orderFrontRegardless];

then, to turn on mouse moved events,

[self setAcceptsMouseMovedEvents:YES];

will cause the window to get calls to:

- (void)mouseMoved:(NSEvent *)theEvent
{
   NSLog(@"mouse moved into invisible window.");
}

So hopefully that's enough to give you a start.

-Ken

Ken Aspeslagh
The other piece of the puzzle is making the window actually pop in and out. You'll need to put the immediate subviews of the window's content view into another view and make *that* the content view. Set the autoresizing mask of the view-within-the-content-view to not resize and to staple its origin to the left edge (in your example) of the content view. To hide the window, resize the window to 1 pixel wide. To show it, restore its proper size. If you want the window to be resizable, you'll need to switch the view's autoresizing mask after showing/hiding the window.
Peter Hosey
The reason why you wouldn't simply move the window in and out is because the user may have another screen in the area that the window will move out to. Then, you're not hiding the window, just moving it to another screen—not what the user wants. (As for how the user would show the window in this situation, the user could have the window up or down past the edge of the smaller screen, giving him something to hit.)
Peter Hosey
Some things I forgot for the overlay window:[self setHasShadow:NO];and[self setIgnoresMouseEvents:NO];so your invisible window doesn't actually block clicks. I'm 99% sure you'll still get the mouse moved events.
Ken Aspeslagh
To add to what Peter said, Cocoa doesn't allow windows to be completely off the screen. That's why you can't just simply slide it on and off. There is a hacky workaround however. Cocoa does the restricting in NSWindow's constrainFrameRect:toScreen:. So if you override this routine, you can make a window go offscreen. This might make it easier to do without actually having to resize the window. Not sure though.
Ken Aspeslagh
Thanks guys for the input... I appreciate it.
Brad
+1  A: 

Hey Brad, you may look how we did it in the Visor project: http://github.com/darwin/visor/blob/master/src/Visor.m#L1025-1063

Darwin
This will work perfectly! Thanks Darwin..
Brad
A: 

Here's what I came up with. Thanks to Peter for the above tips.

@interface SlidingWindow ()

- (void)adjustWrapperView;

- (void)setWindowWidth:(NSNumber*)width;
- (void)setWindowHeight:(NSNumber*)height;

@end


@implementation SlidingWindow

@synthesize slidingEdge = _slidingEdge;
@synthesize wrapperView = _wrapperView;


- (id)initWithContentRect:(NSRect) contentRect 
                styleMask:(unsigned int) styleMask 
                  backing:(NSBackingStoreType) backingType 
                    defer:(BOOL) flag
{

    if ((self = [super initWithContentRect:contentRect
                                 styleMask:NSBorderlessWindowMask 
                                   backing:backingType
                                     defer:flag])) {
        /* May want to setup some other options, 
         like transparent background or something */

        [self setSlidingEdge:CGRectMinYEdge];
        [self setHidesOnDeactivate:YES];
        [self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
    }

    return self;
}

- (void)adjustWrapperView
{
    if (self.wrapperView == nil) {
        NSRect frame = [self frame];
        NSRect bounds = NSMakeRect(0, 0, frame.size.width, frame.size.height);
        NSView *wrapperView = [[NSView alloc] initWithFrame:bounds];
        NSArray *subviews =  [[[[self contentView] subviews] copy] autorelease];

        for (NSView *view in subviews) {
            [wrapperView addSubview:view];
        }

        [wrapperView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
        [[self contentView] addSubview:wrapperView];

        self.wrapperView = [wrapperView autorelease];
    }

    switch (self.slidingEdge) {
        case CGRectMaxXEdge:
            [self.wrapperView setAutoresizingMask:(NSViewHeightSizable | NSViewMaxXMargin)];
            break;

        case CGRectMaxYEdge:
            [self.wrapperView setAutoresizingMask:(NSViewWidthSizable | NSViewMaxYMargin)];
            break;

        case CGRectMinXEdge:
            [self.wrapperView setAutoresizingMask:(NSViewHeightSizable | NSViewMinXMargin)];
            break;

        case CGRectMinYEdge:
        default:
            [self.wrapperView setAutoresizingMask:(NSViewWidthSizable | NSViewMinYMargin)];
    }
}

- (void)makeKeyAndOrderFront:(id)sender
{
    [self adjustWrapperView];

    if ([self isKeyWindow]) {
        [super makeKeyAndOrderFront:sender];
    }
    else {
        NSRect screenRect = [[NSScreen mainScreen] visibleFrame];
        NSRect windowRect = [self frame];

        CGFloat x;
        CGFloat y;
        NSRect startWindowRect;
        NSRect endWindowRect;

        switch (self.slidingEdge) {
            case CGRectMaxXEdge:
                x = 0;
                y = (screenRect.size.height - windowRect.size.height) / 2 + screenRect.origin.y;
                startWindowRect = NSMakeRect(x - windowRect.size.width, y, 0, windowRect.size.height);
                break;

            case CGRectMaxYEdge:
                x = (screenRect.size.width - windowRect.size.width) / 2 + screenRect.origin.x;
                y = 0;
                startWindowRect = NSMakeRect(x, y - windowRect.size.height, windowRect.size.width, 0);
                break;

            case CGRectMinXEdge:
                x = screenRect.size.width - windowRect.size.width + screenRect.origin.x;
                y = (screenRect.size.height - windowRect.size.height) / 2 + screenRect.origin.y;
                startWindowRect = NSMakeRect(x + windowRect.size.width, y, 0, windowRect.size.height);
                break;

            case CGRectMinYEdge:
            default:
                x = (screenRect.size.width - windowRect.size.width) / 2 + screenRect.origin.x;
                y = screenRect.size.height - windowRect.size.height + screenRect.origin.y;
                startWindowRect = NSMakeRect(x, y + windowRect.size.height, windowRect.size.width, 0);
        }

        endWindowRect = NSMakeRect(x, y, windowRect.size.width, windowRect.size.height);

        [self setFrame:startWindowRect display:NO animate:NO];

        [super makeKeyAndOrderFront:sender];

        [self setFrame:endWindowRect display:YES animate:YES];

        [self performSelector:@selector(makeResizable)
                   withObject:nil
                   afterDelay:1];
    }
}

- (void)makeResizable
{
    NSView *wrapperView = self.wrapperView;
    NSRect frame = [self frame];
    NSRect bounds = NSMakeRect(0, 0, frame.size.width, frame.size.height);

    [wrapperView setFrame:bounds];
    [wrapperView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];

}

- (void)orderOut:(id)sender
{
    [self adjustWrapperView];

    NSRect startWindowRect = [self frame];
    NSRect endWindowRect;

    switch (self.slidingEdge) {
        case CGRectMaxXEdge:
            endWindowRect = NSMakeRect(startWindowRect.origin.x, 
                                       startWindowRect.origin.y,
                                       0,
                                       startWindowRect.size.height);
            break;

        case CGRectMaxYEdge:
            endWindowRect = NSMakeRect(startWindowRect.origin.x, 
                                       startWindowRect.origin.y, 
                                       startWindowRect.size.width, 
                                       0);
            break;

        case CGRectMinXEdge:
            endWindowRect = NSMakeRect(startWindowRect.origin.x + startWindowRect.size.width, 
                                       startWindowRect.origin.y,
                                       0,
                                       startWindowRect.size.height);
            break;

        case CGRectMinYEdge:
        default:
            endWindowRect = NSMakeRect(startWindowRect.origin.x, 
                                       startWindowRect.origin.y + startWindowRect.size.height, 
                                       startWindowRect.size.width, 
                                       0);
    }

    [self setFrame:endWindowRect display:YES animate:YES];

    switch (self.slidingEdge) {
        case CGRectMaxXEdge:
        case CGRectMinXEdge:
            if (startWindowRect.size.width > 0) {
                [self performSelector:@selector(setWindowWidth:)
                           withObject:[NSNumber numberWithDouble:startWindowRect.size.width]
                           afterDelay:0];
            }
            break;

        case CGRectMaxYEdge:
        case CGRectMinYEdge:
        default:
            if (startWindowRect.size.height > 0) {
                [self performSelector:@selector(setWindowHeight:)
                           withObject:[NSNumber numberWithDouble:startWindowRect.size.height]
                           afterDelay:0];
            }
    }


    [super orderOut:sender];
}

- (void)setWindowWidth:(NSNumber*)width
{
    NSRect startWindowRect = [self frame];
    NSRect endWindowRect = NSMakeRect(startWindowRect.origin.x, 
                                      startWindowRect.origin.y, 
                                      [width doubleValue],
                                      startWindowRect.size.height);

    [self setFrame:endWindowRect display:NO animate:NO];    
}

- (void)setWindowHeight:(NSNumber*)height
{
    NSRect startWindowRect = [self frame];
    NSRect endWindowRect = NSMakeRect(startWindowRect.origin.x, 
                                      startWindowRect.origin.y, 
                                      startWindowRect.size.width, 
                                      [height doubleValue]);

    [self setFrame:endWindowRect display:NO animate:NO];    
}

- (void)resignKeyWindow
{
    [self orderOut:self];

    [super resignKeyWindow];
}

- (BOOL)canBecomeKeyWindow
{
    return YES;
}

- (void)performClose:(id)sender
{
    [self close];
}

- (void)dealloc
{
    [_wrapperView release], _wrapperView = nil;

    [super dealloc];
}

@end
Pierre Bernard
Sweet, thanks for the demo code
Brad
Pierre Bernard
A: 

Here's AutoHidingWindow - a subclass of SlidingWindow which pops up when the mouse hits the edge of the screen. Feedback welcome.

@interface ActivationWindow : NSWindow
{
    AutoHidingWindow *_activationDelegate;
    NSTrackingArea *_trackingArea;
}

- (ActivationWindow*)initWithDelegate:(AutoHidingWindow*)activationDelegate;

@property (assign) AutoHidingWindow *activationDelegate;
@property (retain) NSTrackingArea *trackingArea;

- (void)adjustWindowFrame;
- (void)adjustTrackingArea;

@end

@interface AutoHidingWindow ()

- (void)autoShow;
- (void)autoHide;

@end 


@implementation AutoHidingWindow

- (id)initWithContentRect:(NSRect) contentRect 
                styleMask:(unsigned int) styleMask 
                  backing:(NSBackingStoreType) backingType 
                    defer:(BOOL) flag
{

    if ((self = [super initWithContentRect:contentRect
                                 styleMask:NSBorderlessWindowMask 
                                   backing:backingType
                                     defer:flag])) {
        _activationWindow = [[ActivationWindow alloc] initWithDelegate:self];
    }

    return self;
}

@synthesize activationWindow = _activationWindow;

- (void)dealloc
{
    [_activationWindow release], _activationWindow = nil;

    [super dealloc];
}

- (void)makeKeyAndOrderFront:(id)sender
{
    [super makeKeyAndOrderFront:sender];

}

- (void)autoShow
{
    [self makeKeyAndOrderFront:self];

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoHide) object:nil];
    [self performSelector:@selector(autoHide) withObject:nil afterDelay:2];
}

- (void)autoHide
{
    NSPoint mouseLocation = [NSEvent mouseLocation];
    NSRect windowFrame = [self frame];

    if (NSPointInRect(mouseLocation, windowFrame)) {
        [self performSelector:@selector(autoHide) withObject:nil afterDelay:2];
    }
    else {
        [self orderOut:self];
    }
}

@end


@implementation ActivationWindow 

- (ActivationWindow*)initWithDelegate:(AutoHidingWindow*)activationDelegate
{   
    if ((self = [super initWithContentRect:[[NSScreen mainScreen] frame]
                                 styleMask:NSBorderlessWindowMask
                                   backing:NSBackingStoreBuffered
                                     defer:NO]) != nil) {
        _activationDelegate = activationDelegate;

        [self setBackgroundColor:[NSColor clearColor]];
        [self setExcludedFromWindowsMenu:YES];
        [self setCanHide:NO];
        [self setHasShadow:NO];
        [self setLevel:NSScreenSaverWindowLevel];
        [self setAlphaValue:0.0];
        [self setIgnoresMouseEvents:YES];
        [self setOpaque:NO];
        [self orderFrontRegardless];

        [self adjustWindowFrame];
        [self.activationDelegate addObserver:self
                                 forKeyPath:@"slidingEdge"
                                    options:0
                                    context:@"slidingEdge"];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(screenParametersChanged:) 
                                                     name:NSApplicationDidChangeScreenParametersNotification 
                                                   object:nil];     
    }

    return self;
}

@synthesize activationDelegate = _activationDelegate;
@synthesize trackingArea = _trackingArea;

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([@"slidingEdge" isEqual:context]) {
        [self adjustTrackingArea];
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [self.activationDelegate removeObserver:self forKeyPath:@"slidingEdge"];
    _activationDelegate = nil;

    [_trackingArea release], _trackingArea = nil;

    [super dealloc];
}

- (void)screenParametersChanged:(NSNotification *)notification
{
    [self adjustWindowFrame];
}

- (void)adjustWindowFrame
{
    NSScreen *mainScreen = [NSScreen mainScreen];
    CGFloat menuBarHeight = [NSMenuView menuBarHeight];
    NSRect windowFrame = [mainScreen frame];

    windowFrame.size.height -= menuBarHeight;

    [self setFrame:windowFrame display:NO];
    [self adjustTrackingArea];
}

- (void)adjustTrackingArea
{
    NSView *contentView = [self contentView];
    NSRect trackingRect = contentView.bounds;   
    CGRectEdge slidingEdge = self.activationDelegate.slidingEdge;
    CGFloat trackingRectSize = 2.0;

    switch (slidingEdge) {
        case CGRectMaxXEdge:
            trackingRect.origin.x = trackingRect.origin.x + trackingRect.size.width - trackingRectSize;
            trackingRect.size.width = trackingRectSize;
            break;

        case CGRectMaxYEdge:
            trackingRect.origin.y = trackingRect.origin.y + trackingRect.size.height - trackingRectSize;
            trackingRect.size.height = trackingRectSize;
            break;

        case CGRectMinXEdge:
            trackingRect.origin.x = 0;
            trackingRect.size.width = trackingRectSize;
            break;

        case CGRectMinYEdge:
        default:
            trackingRect.origin.y = 0;
            trackingRect.size.height = trackingRectSize;
    }


    NSTrackingAreaOptions options =
    NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved |
    NSTrackingActiveAlways |
    NSTrackingEnabledDuringMouseDrag;

    NSTrackingArea *trackingArea = self.trackingArea;

    if (trackingArea != nil) {
        [contentView removeTrackingArea:trackingArea];
    }

    trackingArea = [[NSTrackingArea alloc] initWithRect:trackingRect
                                                options:options
                                                  owner:self
                                               userInfo:nil];

    [contentView addTrackingArea:trackingArea];

    self.trackingArea = [trackingArea autorelease];
}

- (void)mouseEntered:(NSEvent *)theEvent
{
    [self.activationDelegate autoShow];
}


- (void)mouseMoved:(NSEvent *)theEvent
{
    [self.activationDelegate autoShow];
}

- (void)mouseExited:(NSEvent *)theEvent
{
}

@end
Pierre Bernard