0
votes

I am looking to create a NSWindow behaving like the dock window: - Appears when the mouse cursor stays at one edge of the screen - Does not takes the focus (the app having the focus keeps it) but reveives mouse events

Any idea on how I can implement this?

Thanks in advance for your help,

1

1 Answers

2
votes

You could do something with the alpha value of the window. Use this subclass of NSView as the content view of your window.

#import <Cocoa/Cocoa.h>

@interface IEFMouseOverView : NSView {
    BOOL canHide;
    BOOL canShow;
}
- (id)initWithFrame:(NSRect)r;
@end


@interface IEFMouseOverView (PrivateMethods)
- (void)showWindow:(NSTimer *)theTimer;
- (void)hideWindow:(NSTimer *)theTimer;
@end

@implementation IEFMouseOverView
- (void)awakeFromNib {
    [[self window] setAcceptsMouseMovedEvents:YES];
    [self addTrackingRect:[self bounds] owner:self userData:nil
             assumeInside:NO];
}
- (id)initWithFrame:(NSRect)r {
    self = [super initWithFrame:r];
    if(self) {
        NSLog(@"Gutentag");
        [[self window] setAcceptsMouseMovedEvents:YES];
        [self addTrackingRect:[self bounds] owner:self userData:nil
                 assumeInside:NO];
    }
    return self;
}

- (void)mouseEntered:(NSEvent *)ev {
    canShow = YES;
    canHide = NO;
    NSTimer *showTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 
                                                          target:self 
                                                        selector:@selector(showWindow:) 
                                                        userInfo:nil 
                                                         repeats:YES];
    [showTimer fire];
}

- (void)mouseExited:(NSEvent *)ev {
    canShow = NO;
    canHide = YES;
    NSTimer *hideTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 
                                                          target:self 
                                                        selector:@selector(hideWindow:) 
                                                        userInfo:nil
                                                         repeats:YES];
    [hideTimer fire];
}

- (void)showWindow:(NSTimer *)theTimer {
    NSWindow *myWindow = [self window];
    float originalAlpha = [myWindow alphaValue];
    if(originalAlpha >= 1 || canShow == NO) {
        [theTimer invalidate];
        return;
    }
    [myWindow setAlphaValue:originalAlpha + 0.1];
}

- (void)hideWindow:(NSTimer *)theTimer {
    NSWindow *myWindow = [self window];
    float originalAlpha = [myWindow alphaValue];
    if(originalAlpha <= 0 || canHide == NO) {
        [theTimer invalidate];
        return;
    }
    [myWindow setAlphaValue:originalAlpha - 0.1];
}
@end