From 8f8f1a0224581bcbe90f0bc28ee7aa534397fc77 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Fri, 21 Dec 2012 15:00:20 +0000 Subject: [PATCH] Fixed: token field autocomplete menu remaining after window closed. Without this change, closing the window containing a token field showing an autocomplete menu would result in a detached autocomplete menu hanging around. This fix checks for window close notifications and makes sure to dismiss the menu if the window closes. --- AppKit/CPView.j | 5 +++++ AppKit/_CPAutocompleteMenu.j | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/AppKit/CPView.j b/AppKit/CPView.j index a1b1d42ba..ce63921db 100644 --- a/AppKit/CPView.j +++ b/AppKit/CPView.j @@ -651,6 +651,9 @@ var CPViewFlags = { }, if (_window === aWindow) return; + // Unlike in Cocoa you can observe the window of a view. + [self willChangeValueForKey:@"window"]; + [[self window] _dirtyKeyViewLoop]; // Clear out first responder if we're the first responder and leaving. @@ -678,6 +681,8 @@ var CPViewFlags = { }, [self viewDidMoveToWindow]; [[self window] _dirtyKeyViewLoop]; + + [self didChangeValueForKey:@"window"]; } /*! diff --git a/AppKit/_CPAutocompleteMenu.j b/AppKit/_CPAutocompleteMenu.j index 2aaafd81f..e5254de15 100644 --- a/AppKit/_CPAutocompleteMenu.j +++ b/AppKit/_CPAutocompleteMenu.j @@ -38,6 +38,7 @@ var _CPAutocompleteMenuMaximumHeight = 307; float widestItemWidth; CPWindow _menuWindow; + CPWindow _parentWindow @accessors(property=parentWindow); CPScrollView scrollView; CPTableView tableView; @@ -48,7 +49,8 @@ var _CPAutocompleteMenuMaximumHeight = 307; { if (self = [super init]) { - textField = aTextField; + [self bind:@"parentWindow" toObject:self withKeyPath:@"textField.window" options:nil]; + [self setTextField:aTextField]; _menuWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(0, 0, 100, 100) styleMask:CPBorderlessWindowMask]; @@ -258,6 +260,25 @@ var _CPAutocompleteMenuMaximumHeight = 307; [textField _complete:self]; } +- (void)setParentWindow:(CPWindow)aWindow +{ + if (aWindow === _parentWindow) + return; + + if (_parentWindow) + [[CPNotificationCenter defaultCenter] removeObserver:self]; + + _parentWindow = aWindow; + + if (_parentWindow) + [[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(parentWindowWillClose:) name:CPWindowWillCloseNotification object:_parentWindow]; +} + +- (void)parentWindowWillClose:(CPNotification)aNotification +{ + [self _hideCompletions]; +} + @end