Safe browsing warning should layout buttons vertically in narrow WKWebViews
authorachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 10 Dec 2018 20:56:52 +0000 (20:56 +0000)
committerachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 10 Dec 2018 20:56:52 +0000 (20:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=192535
<rdar://problem/46308364>

Reviewed by Tim Horton.

They're usually next to each other, but in narrow WKWebViews they currently get clipped.
In order to make it more likely that both buttons are completely visible, put one above
the other if the WKWebView is too narrow to completely show both.  This also helps with
languages where the translations of "Go Back" or "Show Details" are rendered wider than English.

* UIProcess/Cocoa/WKSafeBrowsingWarning.mm:
(buttonWidth):
(-[WKSafeBrowsingWarning addContent]):

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

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/Cocoa/WKSafeBrowsingWarning.mm

index d680454..bc0c269 100644 (file)
@@ -1,3 +1,20 @@
+2018-12-10  Alex Christensen  <achristensen@webkit.org>
+
+        Safe browsing warning should layout buttons vertically in narrow WKWebViews
+        https://bugs.webkit.org/show_bug.cgi?id=192535
+        <rdar://problem/46308364>
+
+        Reviewed by Tim Horton.
+
+        They're usually next to each other, but in narrow WKWebViews they currently get clipped.
+        In order to make it more likely that both buttons are completely visible, put one above
+        the other if the WKWebView is too narrow to completely show both.  This also helps with
+        languages where the translations of "Go Back" or "Show Details" are rendered wider than English.
+
+        * UIProcess/Cocoa/WKSafeBrowsingWarning.mm:
+        (buttonWidth):
+        (-[WKSafeBrowsingWarning addContent]):
+
 2018-12-10  Simon Fraser  <simon.fraser@apple.com>
 
         Allow control over child order when adding nodes to the scrolling tree
index 49bf5c8..d16db8f 100644 (file)
@@ -217,6 +217,17 @@ static ButtonType *makeButton(WarningItem item, WKSafeBrowsingWarning *warning,
 #endif
 }
 
+#if HAVE(SAFE_BROWSING)
+static CGFloat buttonWidth(ButtonType *button)
+{
+#if PLATFORM(MAC)
+    return button.frame.size.width;
+#else
+    return button.titleLabel.intrinsicContentSize.width;
+#endif
+}
+#endif
+
 static ViewType *makeLabel(NSAttributedString *attributedString)
 {
 #if PLATFORM(MAC)
@@ -306,14 +317,25 @@ static void setBackground(ViewType *view, ColorType *color)
 
         [[title.topAnchor anchorWithOffsetToAnchor:exclamationPoint.topAnchor] constraintEqualToAnchor:[exclamationPoint.bottomAnchor anchorWithOffsetToAnchor:title.bottomAnchor]],
 
-        [goBack.topAnchor constraintEqualToAnchor:showDetails.topAnchor],
-        [[showDetails.trailingAnchor anchorWithOffsetToAnchor:goBack.leadingAnchor] constraintEqualToConstant:marginSize],
-
         [[box.topAnchor anchorWithOffsetToAnchor:title.topAnchor] constraintEqualToConstant:marginSize],
         [[title.bottomAnchor anchorWithOffsetToAnchor:warning.topAnchor] constraintEqualToConstant:marginSize],
         [[warning.bottomAnchor anchorWithOffsetToAnchor:goBack.topAnchor] constraintEqualToConstant:marginSize],
-        [[goBack.bottomAnchor anchorWithOffsetToAnchor:box.bottomAnchor] constraintEqualToConstant:marginSize]
     ]];
+    
+    bool needsVerticalButtonLayout = buttonWidth(showDetails) + buttonWidth(goBack) + 3 * marginSize > self.frame.size.width;
+    if (needsVerticalButtonLayout) {
+        [NSLayoutConstraint activateConstraints:@[
+            [[showDetails.trailingAnchor anchorWithOffsetToAnchor:box.trailingAnchor] constraintEqualToConstant:marginSize],
+            [[goBack.bottomAnchor anchorWithOffsetToAnchor:showDetails.topAnchor] constraintEqualToConstant:marginSize],
+            [[goBack.bottomAnchor anchorWithOffsetToAnchor:box.bottomAnchor] constraintEqualToConstant:marginSize * 2 + showDetails.frame.size.height],
+        ]];
+    } else {
+        [NSLayoutConstraint activateConstraints:@[
+            [[showDetails.trailingAnchor anchorWithOffsetToAnchor:goBack.leadingAnchor] constraintEqualToConstant:marginSize],
+            [goBack.topAnchor constraintEqualToAnchor:showDetails.topAnchor],
+            [[goBack.bottomAnchor anchorWithOffsetToAnchor:box.bottomAnchor] constraintEqualToConstant:marginSize],
+        ]];
+    }
 #endif
 }