ドックウィンドウのように動作するNSWindowを作成しようとしています。-マウスカーソルが画面の一方の端にあるときに表示されます-フォーカスを取得しません(フォーカスを持っているアプリがフォーカスを保持します)が、マウスイベントを受け取ります
これをどのように実装できるかについてのアイデアはありますか?
よろしくお願いします。
ドックウィンドウのように動作するNSWindowを作成しようとしています。-マウスカーソルが画面の一方の端にあるときに表示されます-フォーカスを取得しません(フォーカスを持っているアプリがフォーカスを保持します)が、マウスイベントを受け取ります
これをどのように実装できるかについてのアイデアはありますか?
よろしくお願いします。
ウィンドウのアルファ値で何かを行うことができます。NSViewのこのサブクラスをウィンドウのコンテンツビューとして使用します。
#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