diff --git a/AppKit/CPPopover.j b/AppKit/CPPopover.j index 75aece952..6d0f06cce 100644 --- a/AppKit/CPPopover.j +++ b/AppKit/CPPopover.j @@ -39,7 +39,7 @@ CPPopoverBehaviorSemitransient = 2; var CPPopoverDelegate_popover_willShow_ = 1 << 0, CPPopoverDelegate_popover_didShow_ = 1 << 1, CPPopoverDelegate_popover_shouldClose_ = 1 << 2, - CPPopoverDelegate_popover_willClose_ = 1 << 3, + CPPopoverDelegate_popover_willClose_ = 1 << 3, CPPopoverDelegate_popover_didClose_ = 1 << 4; @@ -73,7 +73,7 @@ var CPPopoverDelegate_popover_willShow_ = 1 << 0, #pragma mark - -#pragma mark INitialization +#pragma mark Initialization /*! Initialize the CPPopover witn default values @@ -221,6 +221,7 @@ var CPPopoverDelegate_popover_willShow_ = 1 << 0, [_attachedWindow setAppearance:_appearance]; [_attachedWindow setAnimates:_animates]; + [_attachedWindow setDelegate:self]; [_attachedWindow setMovableByWindowBackground:NO]; [_attachedWindow setFrame:[_attachedWindow frameRectForContentRect:[[_contentViewController view] frame]]]; [_attachedWindow setContentView:[_contentViewController view]]; @@ -268,8 +269,28 @@ var CPPopoverDelegate_popover_willShow_ = 1 << 0, [self close]; } + +#pragma mark - +#pragma mark Delegates + +/*! @ignore */ +- (BOOL)attachedWindowShouldClose:(_CPAttachedWindow)anAttachedWindow +{ + [self close]; + + // we return NO, because we want the CPPopover to compute + // if the attached can be close in order to send delegate messages + return NO; +} + @end +var CPPopoverNeedsComputeKey = @"CPPopoverNeedsComputeKey", + CPPopoverAppearanceKey = @"CPPopoverAppearanceKey", + CPPopoverAnimatesKey = @"CPPopoverAnimatesKey", + CPPopoverContentViewControllerKey = @"CPPopoverContentViewControllerKey", + CPPopoverDelegateKey = @"CPPopoverDelegateKey", + CPPopoverBehaviorKey = @"CPPopoverBehaviorKey"; @implementation CPPopover (CPCoding) @@ -279,12 +300,12 @@ var CPPopoverDelegate_popover_willShow_ = 1 << 0, if (self) { - _needsCompute = [aCoder decodeIntForKey:@"_needsCompute"]; - _appearance = [aCoder decodeIntForKey:@"_appearance"]; - _animates = [aCoder decodeBoolForKey:@"_animates"]; - _contentViewController = [aCoder decodeObjectForKey:@"_contentViewController"]; - [self setDelegate:[aCoder decodeObjectForKey:@"_delegate"]]; - [self setBehaviour:[aCoder decodeIntForKey:@"_behavior"]]; + _needsCompute = [aCoder decodeIntForKey:CPPopoverNeedsComputeKey]; + _appearance = [aCoder decodeIntForKey:CPPopoverAppearanceKey]; + _animates = [aCoder decodeBoolForKey:CPPopoverAnimatesKey]; + _contentViewController = [aCoder decodeObjectForKey:CPPopoverContentViewControllerKey]; + [self setDelegate:[aCoder decodeObjectForKey:CPPopoverDelegateKey]]; + [self setBehaviour:[aCoder decodeIntForKey:CPPopoverBehaviorKey]]; } return self; } @@ -293,12 +314,12 @@ var CPPopoverDelegate_popover_willShow_ = 1 << 0, { [super encodeWithCoder:aCoder]; - [aCoder encodeInt:_behavior forKey:@"_behavior"]; - [aCoder encodeInt:_appearance forKey:@"_appearance"]; - [aCoder encodeBool:_needsCompute forKey:@"_needsCompute"]; - [aCoder encodeObject:_contentViewController forKey:@"_contentViewController"]; - [aCoder encodeObject:_delegate forKey:@"_delegate"]; - [aCoder encodeObject:_animates forKey:@"_animates"]; + [aCoder encodeBool:_needsCompute forKey:CPPopoverNeedsComputeKey]; + [aCoder encodeInt:_appearance forKey:CPPopoverAppearanceKey]; + [aCoder encodeObject:_animates forKey:CPPopoverAnimatesKey]; + [aCoder encodeObject:_contentViewController forKey:CPPopoverContentViewControllerKey]; + [aCoder encodeObject:_delegate forKey:CPPopoverDelegateKey]; + [aCoder encodeInt:_behavior forKey:CPPopoverBehaviorKey]; } @end diff --git a/AppKit/_CPAttachedWindow.j b/AppKit/_CPAttachedWindow.j index 70126b632..ad69465ef 100644 --- a/AppKit/_CPAttachedWindow.j +++ b/AppKit/_CPAttachedWindow.j @@ -30,6 +30,9 @@ CPClosableOnBlurWindowMask = 1 << 4; CPPopoverAppearanceMinimal = 0; CPPopoverAppearanceHUD = 1; +var _CPAttachedWindow_attachedWindowShouldClose_ = 1 << 0, + _CPAttachedWindow_attachedWindowDidClose_ = 1 << 1; + /*! @ignore @@ -48,6 +51,7 @@ CPPopoverAppearanceHUD = 1; BOOL _shouldPerformAnimation; CPButton _closeButton; float _animationDuration; + CPInteger _implementedDelegateMethods; } /*! @@ -153,6 +157,21 @@ CPPopoverAppearanceHUD = 1; [_windowView setAppearance:anAppearance]; } +- (void)setDelegate:(id)aDelegate +{ + if (_delegate == aDelegate) + return; + + _delegate = aDelegate; + _implementedDelegateMethods = 0; + + if ([_delegate respondsToSelector:@selector(attachedWindowShouldClose:)]) + _implementedDelegateMethods |= _CPAttachedWindow_attachedWindowShouldClose_; + + if ([_delegate respondsToSelector:@selector(attachedWindowDidClose:)]) + _implementedDelegateMethods |= _CPAttachedWindow_attachedWindowDidClose_; +} + #pragma mark - #pragma mark Observer @@ -408,12 +427,9 @@ CPPopoverAppearanceHUD = 1; { if (_closeOnBlur && !_isClosed) { - // set a close flag to avoid infinite loop - _isClosed = YES; + if (!_delegate || ((_implementedDelegateMethods & _CPAttachedWindow_attachedWindowShouldClose_) + && [_delegate attachedWindowShouldClose:self])) [self close]; - - if (_delegate && [_delegate respondsToSelector:@selector(didAttachedWindowClose:)]) - [_delegate didAttachedWindowClose:self]; } } @@ -478,6 +494,9 @@ CPPopoverAppearanceHUD = 1; */ - (void)close { + // set a close flag to avoid infinite loop + _isClosed = YES; + if (_animates && typeof(_DOMElement.style.WebkitTransform) != "undefined") { _DOMElement.style.opacity = 0; @@ -494,8 +513,8 @@ CPPopoverAppearanceHUD = 1; _shouldPerformAnimation = _animates; - if (_delegate && [_delegate respondsToSelector:@selector(didAttachedWindowClose:)]) - [_delegate didAttachedWindowClose:self]; + if (_implementedDelegateMethods & _CPAttachedWindow_attachedWindowDidClose_) + [_delegate attachedWindowDidClose:self]; } @end diff --git a/Tests/Manual/CPPopover/AppController.j b/Tests/Manual/CPPopover/AppController.j index 722aa6489..bb3175f7a 100644 --- a/Tests/Manual/CPPopover/AppController.j +++ b/Tests/Manual/CPPopover/AppController.j @@ -84,7 +84,7 @@ - (IBAction)open:(id)sender { var g; - switch([buttonGravity title]) + switch ([buttonGravity title]) { case "Automatic": g = nil; @@ -104,7 +104,7 @@ } var a; - switch([buttonStyle title]) + switch ([buttonStyle title]) { case "Minimal": a = CPPopoverAppearanceMinimal; @@ -115,7 +115,7 @@ } var p = [[CPPopover alloc] init], - viewC = [[CPViewController alloc] init]; + viewC = [[CPViewController alloc] init], view = [[CPView alloc] initWithFrame:CPRectMake(0.0, 0.0, 320, 300)], label = [CPTextField labelWithTitle:[buttonGravity title]]; @@ -136,8 +136,8 @@ [p setDelegate:self]; [p showRelativeToRect:nil ofView:sender preferredEdge:g]; CPLog.info("content size - w:" + [p contentSize].width + " h:" + [p contentSize].width); - CPLog.info("positionning rect - x: " + [p positionningRect].origin.x + " y: " + [p positionningRect].origin.x - + " w:" + [p positionningRect].size.width + " h:" + [p positionningRect].size.width); + CPLog.info("positioning rect - x: " + [p positioningRect].origin.x + " y: " + [p positioningRect].origin.x + + " w:" + [p positioningRect].size.width + " h:" + [p positioningRect].size.width); }