From 3bff2a4f7369f7c2b0cd369273a473a47f41d7bc Mon Sep 17 00:00:00 2001 From: michael starke Date: Fri, 12 Dec 2014 22:13:35 +0100 Subject: [PATCH] Overlay windows now can be display floating on the screen. --- MacPass/MPAutotypeDaemon.m | 13 ++++++++ MacPass/MPOverlayWindowController.h | 12 +++++-- MacPass/MPOverlayWindowController.m | 52 +++++++++++++++++++---------- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/MacPass/MPAutotypeDaemon.m b/MacPass/MPAutotypeDaemon.m index f2b19ff8..806da747 100644 --- a/MacPass/MPAutotypeDaemon.m +++ b/MacPass/MPAutotypeDaemon.m @@ -57,11 +57,17 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey"; toObject:[NSUserDefaultsController sharedUserDefaultsController] withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyGlobalAutotypeKeyDataKey] options:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_applicationWillBecomeActive:) + name:NSApplicationWillBecomeActiveNotification + object:[NSApplication sharedApplication]]; } return self; } - (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; [self unbind:NSStringFromSelector(@selector(enabled))]; [self unbind:NSStringFromSelector(@selector(hotKeyData))]; } @@ -259,6 +265,13 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey"; [self _performAutotypeUsingCurrentWindowAndApplication:NO]; } +#pragma mark - +#pragma mark NSApplication Notifications +- (void)_applicationWillBecomeActive:(NSNotification *)notification { + //[self _updateTargetApplicationAndWindow]; + //NSLog(@"_applicaiontWillBecomActive"); +} + #pragma mark - #pragma mark Application information diff --git a/MacPass/MPOverlayWindowController.h b/MacPass/MPOverlayWindowController.h index d823855b..f1cb1645 100644 --- a/MacPass/MPOverlayWindowController.h +++ b/MacPass/MPOverlayWindowController.h @@ -12,9 +12,15 @@ + (MPOverlayWindowController *)sharedController; -/* - Displays an overlay centered at the given view. The Windows is added as a childwindow to the view window +/** + * Displays an overlay HUD style image with the given text, image centerd at the given view. + * There are two use cases. Either displaying information over a view by providing on, + * or displaying information on the main screen by using nil for the view. + * + * @param imageOrNil Image to be displayed. If nil, no image will be shown. + * @param labelOrNil Text to be displayed. If nil, no text will be rendered + * @param viewOrNil View to center the Overlay in. If nil, the window will be center and displayed on the main screen */ -- (void)displayOverlayImage:(NSImage *)imageOrNil label:(NSString *)labelOrNil atView:(NSView *)view; +- (void)displayOverlayImage:(NSImage *)imageOrNil label:(NSString *)labelOrNil atView:(NSView *)viewOrNil; @end diff --git a/MacPass/MPOverlayWindowController.m b/MacPass/MPOverlayWindowController.m index fa9b7140..0428f736 100644 --- a/MacPass/MPOverlayWindowController.m +++ b/MacPass/MPOverlayWindowController.m @@ -33,11 +33,11 @@ } - (id)initWithWindow:(NSWindow *)window { - self = [super initWithWindow:window]; - if (self) { - // Initialization code here. - } - return self; + self = [super initWithWindow:window]; + if (self) { + // Initialization code here. + } + return self; } - (void)windowDidLoad { @@ -45,15 +45,19 @@ [self.window setStyleMask:NSBorderlessWindowMask]; [self.window setAlphaValue:0]; [self.window setOpaque:NO]; - [self.window setHasShadow:NO]; + [self.window setHasShadow:YES]; [[self.textField cell] setBackgroundStyle:NSBackgroundStyleLowered]; [[self.imageView cell] setBackgroundStyle:NSBackgroundStyleDark]; [[self.imageView cell] setImageAlignment:NSImageAlignCenter]; } - (void)displayOverlayImage:(NSImage *)imageOrNil label:(NSString *)labelOrNil atView:(NSView *)view { + if(self.isAnimating) { + return; + } + self.isAnimating = YES; /* make window transparent */ - + [self.window setAlphaValue:0]; [self.window setIsVisible:YES]; @@ -70,22 +74,36 @@ Center in view */ if(view) { - NSWindow *parentWindow = [view window]; - NSRect parentFrame = [parentWindow frame]; - NSRect myFrame = [self.window frame]; + self.window.level = NSNormalWindowLevel; + NSWindow *parentWindow = view.window; + NSRect parentFrame = parentWindow.frame; + NSRect myFrame = self.window.frame; NSPoint newOrigin = parentFrame.origin; newOrigin.x += 0.5 * (parentFrame.size.width - myFrame.size.width); newOrigin.y += 0.5 * (parentFrame.size.height - myFrame.size.height); [self.window setFrameOrigin:newOrigin]; [parentWindow addChildWindow:self.window ordered:NSWindowAbove]; - [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { - context.duration = 0.2; - [[self.window animator]setAlphaValue:1]; - } completionHandler:^{ - [self.window performSelector:@selector(close) withObject:nil afterDelay:0.5]; - }]; - } + else { + self.window.level = NSStatusWindowLevel; + NSRect parentFrame = [NSScreen mainScreen].frame; + NSRect myFrame = self.window.frame; + NSPoint newOrigin = parentFrame.origin; + newOrigin.x += 0.5 * (parentFrame.size.width - myFrame.size.width); + newOrigin.y += 0.5 * (parentFrame.size.height - myFrame.size.height); + [self.window setFrameOrigin:newOrigin]; + } + [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { + context.duration = 0.2; + [[self.window animator]setAlphaValue:1]; + } completionHandler:^{ + [self performSelector:@selector(_didEndAnimation) withObject:nil afterDelay:0.5]; + }]; +} + +- (void)_didEndAnimation { + self.isAnimating = NO; + [self.window close]; } @end