[iOS][WK2] Add a SPI to exclude the extended background from some areas of WKWebView
authorbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 30 Apr 2014 23:45:27 +0000 (23:45 +0000)
committerbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 30 Apr 2014 23:45:27 +0000 (23:45 +0000)
https://bugs.webkit.org/show_bug.cgi?id=132406
<rdar://problem/16762197>

Reviewed by Beth Dakin.

Move the extended background to a separate layer bellow the UIScrollView.

The geometry of that layer is then changed based on ExtendedBackgroundExclusionInsets
as needed.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView initWithFrame:configuration:]):
(-[WKWebView _updateScrollViewBackground]):
(-[WKWebView _frameOrBoundsChanged]):
(-[WKWebView _setExtendedBackgroundExclusionInsets:]):
(-[WKWebView _extendedBackgroundExclusionInsets]):
(-[WKWebView pageExtendedBackgroundColor]): Deleted.
* UIProcess/API/Cocoa/WKWebViewPrivate.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@168063 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit2/UIProcess/API/Cocoa/WKWebViewPrivate.h

index b3630efe41d40e864520cc35438e07dce1307f04..d70e4d3dcfc113895735dbf05d79a06688978d12 100644 (file)
@@ -1,3 +1,25 @@
+2014-04-30  Benjamin Poulain  <benjamin@webkit.org>
+
+        [iOS][WK2] Add a SPI to exclude the extended background from some areas of WKWebView
+        https://bugs.webkit.org/show_bug.cgi?id=132406
+        <rdar://problem/16762197>
+
+        Reviewed by Beth Dakin.
+
+        Move the extended background to a separate layer bellow the UIScrollView.
+
+        The geometry of that layer is then changed based on ExtendedBackgroundExclusionInsets
+        as needed.
+
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView initWithFrame:configuration:]):
+        (-[WKWebView _updateScrollViewBackground]):
+        (-[WKWebView _frameOrBoundsChanged]):
+        (-[WKWebView _setExtendedBackgroundExclusionInsets:]):
+        (-[WKWebView _extendedBackgroundExclusionInsets]):
+        (-[WKWebView pageExtendedBackgroundColor]): Deleted.
+        * UIProcess/API/Cocoa/WKWebViewPrivate.h:
+
 2014-04-30  Simon Fraser  <simon.fraser@apple.com>
 
         Fix build error when building for iOS simulator.
index 04f54c7cfd71001420cae4f2b658f5531e94b07e..48f1ddb686ab72029c7ba661282ffa4878bd958d 100644 (file)
 
     UIEdgeInsets _obscuredInsets;
     bool _isChangingObscuredInsetsInteractively;
+
+    UIEdgeInsets _extendedBackgroundExclusionInsets;
+    CALayer *_extendedBackgroundLayer;
+
     BOOL _isAnimatingResize;
     CATransform3D _resizeAnimationTransformAdjustments;
     RetainPtr<UIView> _resizeAnimationView;
     _scrollView = adoptNS([[WKScrollView alloc] initWithFrame:bounds]);
     [_scrollView setInternalDelegate:self];
     [_scrollView setBouncesZoom:YES];
-    [_scrollView setBackgroundColor:[UIColor whiteColor]];
 
     [self addSubview:_scrollView.get()];
 
+    {
+        RetainPtr<CALayer> backgroundLayer = [[CALayer alloc] init];
+        _extendedBackgroundLayer = backgroundLayer.get();
+        _extendedBackgroundLayer.frame = bounds;
+        _extendedBackgroundLayer.backgroundColor = cachedCGColor(WebCore::Color(WebCore::Color::white), WebCore::ColorSpaceDeviceRGB);
+        [[self layer] insertSublayer:_extendedBackgroundLayer below:[_scrollView layer]];
+    }
+
     _contentView = adoptNS([[WKContentView alloc] initWithFrame:bounds context:context configuration:std::move(webPageConfiguration) webView:self]);
     _page = [_contentView page];
     [_contentView layer].anchorPoint = CGPointZero;
     [_customContentView web_setContentProviderData:data suggestedFilename:suggestedFilename];
 }
 
-// This is a convenience method that will convert _page->pageExtendedBackgroundColor() from a WebCore::Color to a UIColor *.
-- (UIColor *)pageExtendedBackgroundColor
-{
-    WebCore::Color color = _page->pageExtendedBackgroundColor();
-    if (!color.isValid())
-        return nil;
-
-    return [UIColor colorWithRed:(color.red() / 255.0) green:(color.green() / 255.0) blue:(color.blue() / 255.0) alpha:(color.alpha() / 255.0)];
-}
-
 static CGFloat contentZoomScale(WKWebView* webView)
 {
     UIView *zoomView;
@@ -434,17 +435,17 @@ static CGFloat contentZoomScale(WKWebView* webView)
 
 - (void)_updateScrollViewBackground
 {
-    UIColor *pageExtendedBackgroundColor = [self pageExtendedBackgroundColor];
+    WebCore::Color color = _page->pageExtendedBackgroundColor();
+    RetainPtr<CGColorRef> cgColor = cachedCGColor(color, WebCore::ColorSpaceDeviceRGB);
 
     CGFloat zoomScale = contentZoomScale(self);
     CGFloat minimumZoomScale = [_scrollView minimumZoomScale];
     if (zoomScale < minimumZoomScale) {
         CGFloat slope = 12;
         CGFloat opacity = std::max(1 - slope * (minimumZoomScale - zoomScale), static_cast<CGFloat>(0));
-        pageExtendedBackgroundColor = [pageExtendedBackgroundColor colorWithAlphaComponent:opacity];
+        cgColor = adoptCF(CGColorCreateCopyWithAlpha(cgColor.get(), opacity));
     }
-
-    [_scrollView setBackgroundColor:pageExtendedBackgroundColor];
+    _extendedBackgroundLayer.backgroundColor = cgColor.get();
 }
 
 - (void)_didCommitLayerTree:(const WebKit::RemoteLayerTreeTransaction&)layerTreeTransaction
@@ -717,6 +718,10 @@ static inline void setViewportConfigurationMinimumLayoutSize(WebKit::WebPageProx
 - (void)_frameOrBoundsChanged
 {
     CGRect bounds = self.bounds;
+
+    CGRect backgroundLayerRect = UIEdgeInsetsInsetRect(bounds, _extendedBackgroundExclusionInsets);
+    _extendedBackgroundLayer.frame = backgroundLayerRect;
+
     if (!_hasStaticMinimumLayoutSize && !_isAnimatingResize)
         setViewportConfigurationMinimumLayoutSize(*_page, bounds.size);
     [_scrollView setFrame:bounds];
@@ -1302,6 +1307,20 @@ static inline WebKit::FindOptions toFindOptions(_WKFindOptions wkFindOptions)
     return _page->backgroundExtendsBeyondPage();
 }
 
+- (void)_setExtendedBackgroundExclusionInsets:(UIEdgeInsets)extendedBackgroundExclusionInsets
+{
+    if (UIEdgeInsetsEqualToEdgeInsets(extendedBackgroundExclusionInsets, _extendedBackgroundExclusionInsets))
+        return;
+
+    _extendedBackgroundExclusionInsets = extendedBackgroundExclusionInsets;
+    _extendedBackgroundLayer.frame = UIEdgeInsetsInsetRect(self.bounds, _extendedBackgroundExclusionInsets);
+}
+
+- (UIEdgeInsets)_extendedBackgroundExclusionInsets
+{
+    return _extendedBackgroundExclusionInsets;
+}
+
 - (void)_beginInteractiveObscuredInsetsChange
 {
     ASSERT(!_isChangingObscuredInsetsInteractively);
index 638c6ef68e827caf9647f5d414ffea62bbcef9bf..01e20cf42e2d11a3360f481fa06ecbf6cf813400 100644 (file)
@@ -101,6 +101,10 @@ typedef NS_OPTIONS(NSUInteger, _WKFindOptions) {
 
 @property (nonatomic, setter=_setBackgroundExtendsBeyondPage:) BOOL _backgroundExtendsBeyondPage;
 
+// If BackgroundExtendsBeyondPage is set, the background of a page extend to the edges of the webview.
+// The extendedBackgroundExclusionInsets defines insets where the background is not extended. The view is transparent in those areas.
+@property (nonatomic, setter=_setExtendedBackgroundExclusionInsets:) UIEdgeInsets _extendedBackgroundExclusionInsets;
+
 // FIXME: Remove these three properties once we expose WKWebViewContentProvider as API.
 @property (nonatomic, readonly, getter=_isDisplayingPDF) BOOL _displayingPDF;
 @property (nonatomic, readonly) NSData *_dataForDisplayedPDF;