Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/fix-macos-background-color.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

On macOS, implement `background_color` support for WKWebView behind the `transparent` feature. Disables the default white background via the `drawsBackground` KVC key at init and applies `underPageBackgroundColor` on macOS 12+ for both initial creation and runtime `set_background_color` calls.
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ pub struct WebViewAttributes<'a> {
///
/// ## Platform-specific:
///
/// - **macOS**: Not implemented.
/// - **macOS**: Disables the default white WKWebView background via the `drawsBackground` KVC key
/// (same as the `transparent` feature) and sets `underPageBackgroundColor` (macOS 12+) for overscroll areas.
/// - **Windows**:
/// - On Windows 7, transparency is not supported and the alpha value will be ignored.
/// - On Windows higher than 7: translucent colors are not supported so any alpha value other than `0` will be replaced by `255`
Expand Down Expand Up @@ -920,7 +921,8 @@ impl<'a> WebViewBuilder<'a> {
///
/// ## Platfrom-specific:
///
/// - **macOS**: Not implemented.
/// - **macOS**: Disables the default white WKWebView background via the `drawsBackground` KVC key
/// (same as the `transparent` feature) and sets `underPageBackgroundColor` (macOS 12+) for overscroll areas.
/// - **Windows**:
/// - on Windows 7, transparency is not supported and the alpha value will be ignored.
/// - on Windows higher than 7: translucent colors are not supported so any alpha value other than `0` will be replaced by `255`
Expand Down Expand Up @@ -2110,7 +2112,8 @@ impl WebView {
///
/// ## Platfrom-specific:
///
/// - **macOS**: Not implemented.
/// - **macOS**: Disables the default white WKWebView background via the `drawsBackground` KVC key
/// (same as the `transparent` feature) and sets `underPageBackgroundColor` (macOS 12+) for overscroll areas.
/// - **Windows**:
/// - On Windows 7, transparency is not supported and the alpha value will be ignored.
/// - On Windows higher than 7: translucent colors are not supported so any alpha value other than `0` will be replaced by `255`
Expand Down
41 changes: 40 additions & 1 deletion src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl InnerWebView {
}

#[cfg(feature = "transparent")]
if attributes.transparent {
if attributes.transparent || attributes.background_color.is_some() {
let no = NSNumber::numberWithBool(false);
// Equivalent Obj-C:
config.setValue_forKey(Some(&no), ns_string!("drawsBackground"));
Expand Down Expand Up @@ -399,6 +399,22 @@ impl InnerWebView {
};
let webview: Retained<WryWebView> =
objc2::msg_send![super(webview), initWithFrame: frame, configuration: &**config];

// Set the under-page background color for overscroll areas (public API, macOS 12+).
// drawsBackground is already disabled on the config above, so the window background
// shows through. This handles the color visible when scrolling past page bounds.
if os_major_version >= 12 {
if let Some((red, green, blue, alpha)) = attributes.background_color {
let color = objc2_app_kit::NSColor::colorWithSRGBRed_green_blue_alpha(
red as f64 / 255.0,
green as f64 / 255.0,
blue as f64 / 255.0,
alpha as f64 / 255.0,
);
webview.setUnderPageBackgroundColor(Some(&color));
}
}

webview
};
#[cfg(target_os = "ios")]
Expand Down Expand Up @@ -905,6 +921,29 @@ r#"Object.defineProperty(window, 'ipc', {
self.webview.setBackgroundColor(Some(&color));
}

#[cfg(all(target_os = "macos", feature = "transparent"))]
unsafe {
let (red, green, blue, alpha) = _background_color;

// Disable the default white background using the same drawsBackground KVC key
// as the `transparent` feature. On the webview instance (vs config) for runtime changes.
let no = NSNumber::numberWithBool(false);
self
.webview
.setValue_forKey(Some(&no), ns_string!("drawsBackground"));

let (os_major_version, _, _) = util::operating_system_version();
if os_major_version >= 12 {
let color = objc2_app_kit::NSColor::colorWithSRGBRed_green_blue_alpha(
red as f64 / 255.0,
green as f64 / 255.0,
blue as f64 / 255.0,
alpha as f64 / 255.0,
);
self.webview.setUnderPageBackgroundColor(Some(&color));
}
}

Ok(())
}

Expand Down
Loading