Skip to content

Commit

Permalink
Merge pull request #24 from cybozu/bounces
Browse files Browse the repository at this point in the history
Added a modifier allowsScrollViewBounces()
  • Loading branch information
elmetal authored Oct 17, 2024
2 parents f790ed7 + 14c616a commit 901503b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Examples/ExamplesUITests/ExamplesUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ final class ExamplesUITests: XCTestCase {
// WebView.allowsLinkPreview(_:)
// Skip this test case as it is not possible to reproduce the operation.

// WebView.allowsScrollViewBounces(_:)
// Skip this test case as it is not possible to reproduce the operation.

XCTContext.runActivity(named: "WebView.refreshable()") { _ in
let historyLabel = app.webViews.staticTexts["History"]
XCTAssertTrue(historyLabel.waitForExistence(timeout: 3))
Expand Down
8 changes: 8 additions & 0 deletions Sources/WebUI/EnhancedWKWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class EnhancedWKWebView: WKWebView {
}
}

var allowsScrollViewBounces = true {
willSet {
#if os(iOS)
self.scrollView.bounces = newValue
#endif
}
}

var isRefreshable = false {
willSet {
#if os(iOS)
Expand Down
12 changes: 12 additions & 0 deletions Sources/WebUI/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public struct WebView {
private var isInspectable = false
private var allowsBackForwardNavigationGestures = false
private var allowsLinkPreview = true
private var allowsScrollViewBounces = true
private var isRefreshable = false

/// Creates new WebView.
Expand Down Expand Up @@ -87,6 +88,16 @@ public struct WebView {
return modified
}

/// Sets value for scrollView.bounces to WebView.
/// - Parameters:
/// - enabled: A Boolean value that controls whether the scroll view bounces past the edge of content and back again.
/// - Returns: WebView that controls whether the scroll view bounces past the edge of content and back again.
public func allowsScrollViewBounces(_ enabled: Bool) -> Self {
var modified = self
modified.allowsScrollViewBounces = enabled
return modified
}

/// Marks this view as refreshable.
///
/// Applying this modifier to a WebView reloads page contents when users perform an action to refresh.
Expand All @@ -106,6 +117,7 @@ public struct WebView {
webView.isInspectable = isInspectable
webView.allowsBackForwardNavigationGestures = allowsBackForwardNavigationGestures
webView.allowsLinkPreview = allowsLinkPreview
webView.allowsScrollViewBounces = allowsScrollViewBounces
webView.isRefreshable = isRefreshable
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/WebUITests/WebViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ final class WebViewTests: XCTestCase {
XCTAssertTrue(webViewMock.allowsLinkPreview)
}

@MainActor
func test_applyModifiers_allowsScrollViewBounces() {
let sut = WebView().allowsScrollViewBounces(true)
let webViewMock = EnhancedWKWebViewMock()
sut.applyModifiers(to: webViewMock)
XCTAssertTrue(webViewMock.allowsScrollViewBounces)
}

@MainActor
func test_applyModifiers_isRefreshable() {
let sut = WebView().refreshable()
Expand Down

0 comments on commit 901503b

Please sign in to comment.