From 9107322d28b66edc04afbd22b563bdbb1802d8d4 Mon Sep 17 00:00:00 2001 From: Didier Korthoudt Date: Sat, 16 Jun 2018 17:51:52 +0200 Subject: [PATCH] setGlobalScrollerStyle-fixes --- AppKit/CPScrollView.j | 49 ++++++++--------------------- Tests/AppKit/CPScrollViewTest.j | 55 +++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 56 deletions(-) diff --git a/AppKit/CPScrollView.j b/AppKit/CPScrollView.j index c59178b2f..074bc2440 100644 --- a/AppKit/CPScrollView.j +++ b/AppKit/CPScrollView.j @@ -95,6 +95,10 @@ var TIMER_INTERVAL = 0.2, var CPScrollerStyleGlobal = CPScrollerStyleOverlay, CPScrollerStyleGlobalChangeNotification = @"CPScrollerStyleGlobalChangeNotification"; +// _CPScrollViews will hold all created CPScrollView's in order to propagate changes +// of scroller global style +var _CPScrollViews; + /*! @ingroup appkit @class CPScrollView @@ -151,6 +155,8 @@ var CPScrollerStyleGlobal = CPScrollerStyleOverlay, CPScrollerStyleGlobal = _isBrowserUsingOverlayScrollers() ? CPScrollerStyleOverlay : CPScrollerStyleLegacy else CPScrollerStyleGlobal = globalValue; + + _CPScrollViews = @[]; } + (CPString)defaultThemeClass @@ -233,7 +239,10 @@ var CPScrollerStyleGlobal = CPScrollerStyleOverlay, + (void)setGlobalScrollerStyle:(int)aStyle { CPScrollerStyleGlobal = aStyle; - [[CPNotificationCenter defaultCenter] postNotificationName:CPScrollerStyleGlobalChangeNotification object:nil]; + + // We propagate the new scroller global style to all existing CPScrollView's + for (var i = 0, count = [_CPScrollViews count]; i < count; i++) + [_CPScrollViews[i] setScrollerStyle:CPScrollerStyleGlobal]; } @@ -271,6 +280,8 @@ var CPScrollerStyleGlobal = CPScrollerStyleOverlay, _delegate = nil; _scrollTimer = nil; _implementedDelegateMethods = 0; + + [_CPScrollViews addObject:self]; } return self; @@ -1272,37 +1283,6 @@ Notifies the delegate when the scroll view has finished scrolling. #pragma mark - #pragma mark Overrides - -- (void)_removeObservers -{ - if (!_isObserving) - return; - - [[CPNotificationCenter defaultCenter] removeObserver:self - name:CPScrollerStyleGlobalChangeNotification - object:nil]; - - [super _removeObservers]; -} - -- (void)_addObservers -{ - if (_isObserving) - return; - - //Make sure to have the last global style for the scroller - [self _didReceiveDefaultStyleChange:nil]; - - [[CPNotificationCenter defaultCenter] addObserver:self - selector:@selector(_didReceiveDefaultStyleChange:) - name:CPScrollerStyleGlobalChangeNotification - object:nil]; - - [super _addObservers]; -} - - - - (void)drawRect:(CGRect)aRect { [super drawRect:aRect]; @@ -1568,10 +1548,7 @@ var CPScrollViewContentViewKey = @"CPScrollViewContentView", _scrollerStyle = [aCoder decodeObjectForKey:CPScrollViewScrollerStyleKey] || CPScrollerStyleGlobal; _scrollerKnobStyle = [aCoder decodeObjectForKey:CPScrollViewScrollerKnobStyleKey] || CPScrollerKnobStyleDefault; - [[CPNotificationCenter defaultCenter] addObserver:self - selector:@selector(_didReceiveDefaultStyleChange:) - name:CPScrollerStyleGlobalChangeNotification - object:nil]; + [_CPScrollViews addObject:self]; } return self; diff --git a/Tests/AppKit/CPScrollViewTest.j b/Tests/AppKit/CPScrollViewTest.j index cd06f7830..330cc73f9 100644 --- a/Tests/AppKit/CPScrollViewTest.j +++ b/Tests/AppKit/CPScrollViewTest.j @@ -321,34 +321,16 @@ [self assertFalse:hasScrolled]; } --(void)testNotificationsRegistered -{ - var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], - theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 1024.0, 768.0) - styleMask:CPWindowNotSizable]; - - [self assert:[CPNotificationCenterHelper registeredNotificationsForObserver:scrollView] equals:[] message:@"Notications registered for the scrollView in the notification center are wrong"]; - - [[theWindow contentView] addSubview:scrollView]; - [self assert:[CPNotificationCenterHelper registeredNotificationsForObserver:scrollView] equals:[@"CPScrollerStyleGlobalChangeNotification"] message:@"Notications registered for the scrollView in the notification center are wrong"]; - - [[theWindow contentView] addSubview:scrollView]; - [self assert:[CPNotificationCenterHelper registeredNotificationsForObserver:scrollView] equals:[@"CPScrollerStyleGlobalChangeNotification"] message:@"Notications registered for the scrollView in the notification center are wrong"]; - - [scrollView removeFromSuperview]; - [self assert:[CPNotificationCenterHelper registeredNotificationsForObserver:scrollView] equals:[] message:@"Notications registered for the scrollView in the notification center are wrong"]; -} - - (void)testDocumentVisibleRect { - [CPScrollView setGlobalScrollerStyle:CPScrollerStyleOverlay]; - var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 1024.0, 768.0) styleMask:CPWindowNotSizable], windowView = [theWindow contentView], aScrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], aDocumentView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; + [CPScrollView setGlobalScrollerStyle:CPScrollerStyleOverlay]; + [aScrollView setDocumentView:aDocumentView]; [windowView addSubview:aScrollView]; @@ -365,6 +347,39 @@ [self assertRect:CGRectMake(80, 80, 200, 200) equals:[aScrollView documentVisibleRect] message:@"documentVisibleRect is wrong in CPScrollView"]; } +- (void)testSetGlobalScrollerStyle +{ + // In this test, we set global scroller style to legacy, we create a scroll view, change global scroller style to overlay BEFORE placing the + // scroll view in the view hierarchy, then we insert the scroll view in the view hierarchy and it should then have overlay as scroller style. + + [CPScrollView setGlobalScrollerStyle:CPScrollerStyleLegacy]; + + var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 1024.0, 768.0) + styleMask:CPWindowNotSizable], + windowView = [theWindow contentView], + aScrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], + aDocumentView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; + + [CPScrollView setGlobalScrollerStyle:CPScrollerStyleOverlay]; + + [aScrollView setDocumentView:aDocumentView]; + [windowView addSubview:aScrollView]; + + [self assert:CPScrollerStyleOverlay equals:[aScrollView scrollerStyle] message:@"Global scroller style was not applied to this CPScrollView"]; + + // In this test, we have global scroller style set to overlay. We create a scroll view, set the scroller style to legacy BEFORE placing the + // scroll view in the view hierarchy, then insert it. It should have legacy as scroller style. + + var anotherScrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)], + anotherDocumentView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; + + [anotherScrollView setScrollerStyle:CPScrollerStyleLegacy]; + [anotherScrollView setDocumentView:anotherDocumentView]; + [windowView addSubview:anotherScrollView]; + + [self assert:CPScrollerStyleLegacy equals:[anotherScrollView scrollerStyle] message:@"Specific scroller style was overrided by global scroller style"]; +} + - (void)assertPoint:(CGPoint)expected equals:(CGPoint)actual message:(CPString)message { [self assert:expected.x equals:actual.x message:@"X: " + message];