From 6cbfa30652326019d1aa5ab8c1dca33ae44f044b Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Mon, 4 Nov 2013 00:03:31 +0000 Subject: [PATCH] Fixed: view coordinate conversion broken by previous commit. Without this fix, -CPView convertPoint:fromView was broken due to the changes made in #1998. In particular, the transform for the fromView would be calculated and then just thrown away if the views were in the same window. This happened to work in some specific cases and the lack of unit tests concealed the error. This fix properly applies the transforms when two views are in the same window without one of them necessarily being inside the other. Refs #1998. --- AppKit/CPView.j | 20 +++++++++----- Tests/AppKit/CPViewTest.j | 57 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/AppKit/CPView.j b/AppKit/CPView.j index 333ccfb29..7eb044c58 100644 --- a/AppKit/CPView.j +++ b/AppKit/CPView.j @@ -3516,7 +3516,6 @@ var _CPViewGetTransform = function(/*CPView*/ fromView, /*CPView */ toView) } view = view._superview; - } // If we hit toView, then we're done. @@ -3555,8 +3554,8 @@ var _CPViewGetTransform = function(/*CPView*/ fromView, /*CPView */ toView) if (view._boundsTransform) { var inverseBoundsTransform = CGAffineTransformMakeIdentity(); - inverseBoundsTransform.tx -= view._inverseBoundsTransform.tx * transform2.a; - inverseBoundsTransform.ty -= view._inverseBoundsTransform.ty * transform2.d; + inverseBoundsTransform.tx -= view._inverseBoundsTransform.tx * transform2.a; + inverseBoundsTransform.ty -= view._inverseBoundsTransform.ty * transform2.d; CGAffineTransformConcatTo(transform2, inverseBoundsTransform, transform2); } @@ -3567,10 +3566,17 @@ var _CPViewGetTransform = function(/*CPView*/ fromView, /*CPView */ toView) transform2.tx = -transform2.tx; transform2.ty = -transform2.ty; - if (sameWindow) - transform = transform2; - else - CGAffineTransformConcatTo(transform, transform2, transform); + if (view === fromView) + { + // toView is inside of fromView + return transform2; + } + + CGAffineTransformConcatTo(transform, transform2, transform); + + return transform; + + /* var views = [], view = toView; diff --git a/Tests/AppKit/CPViewTest.j b/Tests/AppKit/CPViewTest.j index d47eeb0d3..bc44eccb3 100644 --- a/Tests/AppKit/CPViewTest.j +++ b/Tests/AppKit/CPViewTest.j @@ -125,4 +125,61 @@ [self assert:nil equals:[viewA nextValidKeyView]]; } +- (void)testConvertPoint_fromView_shouldChangeNothingForSameView +{ + var tView0 = [CPView new], + aWindow = [CPWindow new]; + + [aWindow setContentView:tView0]; + + [tView0 setFrame:CGRectMake(3, 5, 13, 17)]; + + [self assertTrue:CGPointEqualToPoint(CGPointMake(7, 11), [tView0 convertPoint:CGPointMake(7, 11) fromView:tView0])] +} + +- (void)testConvertPoint_fromView_shouldAddSubviewCoordinatesWhenMovingUp +{ + var tView0 = [CPView new], + subView0 = [CPView new], + aWindow = [CPWindow new]; + + [aWindow setContentView:tView0]; + + [tView0 addSubview:subView0]; + [tView0 setFrame:CGRectMake(30, 50, 130, 170)]; + [subView0 setFrame:CGRectMake(3, 5, 13, 17)]; + + [self assertTrue:CGPointEqualToPoint(CGPointMake(10, 16), [tView0 convertPoint:CGPointMake(7, 11) fromView:subView0])] +} + +- (void)testConvertPoint_fromView_shouldWorkBetweenSiblingViews +{ + var tView0 = [CPView new], + subView0 = [CPView new], + aWindow = [CPWindow new]; + + [[aWindow contentView] addSubview:tView0]; + [[aWindow contentView] addSubview:subView0]; + + [tView0 setFrame:CGRectMake(30, 50, 130, 170)]; + [subView0 setFrame:CGRectMake(3, 5, 13, 17)]; + + [self assertTrue:CGPointEqualToPoint(CGPointMake(34, 56), [subView0 convertPoint:CGPointMake(7, 11) fromView:tView0])] +} + +- (void)testConvertPoint_fromView_shouldSubtractSubviewCoordinatesWhenMovingDown +{ + var tView0 = [CPView new], + subView0 = [CPView new], + aWindow = [CPWindow new]; + + [aWindow setContentView:tView0]; + + [tView0 addSubview:subView0]; + [tView0 setFrame:CGRectMake(30, 50, 130, 170)]; + [subView0 setFrame:CGRectMake(3, 5, 13, 17)]; + + [self assertTrue:CGPointEqualToPoint(CGPointMake(4, 6), [subView0 convertPoint:CGPointMake(7, 11) fromView:tView0])] +} + @end