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/ios-bg-color.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
wry: patch
---

Added iOS support for `set_background_color`.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ objc2-ui-kit = { version = "0.3.0", default-features = false, features = [
"UIWindow",
"UIApplication",
"UIEvent",
"UIColor"
] }

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ pub struct WebViewAttributes<'a> {
///
/// ## Platform-specific:
///
/// - **macOS / iOS**: Not implemented.
/// - **macOS**: Not implemented.
/// - **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 @@ -844,7 +844,7 @@ impl<'a> WebViewBuilder<'a> {
///
/// ## Platfrom-specific:
///
/// - **macOS / iOS**: Not implemented.
/// - **macOS**: Not implemented.
/// - **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 @@ -1972,7 +1972,7 @@ impl WebView {
///
/// ## Platfrom-specific:
///
/// - **macOS / iOS**: Not implemented.
/// - **macOS**: Not implemented.
/// - **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
37 changes: 37 additions & 0 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,24 @@ impl InnerWebView {
let frame = ns_view.frame();
let webview: Retained<WryWebView> =
objc2::msg_send![super(webview), initWithFrame: frame, configuration: &**config];
if let Some((red, green, blue, alpha)) = attributes.background_color {
// This is required first since the webview color is applied too late.
webview.setOpaque(false);

let color = objc2_ui_kit::UIColor::colorWithRed_green_blue_alpha(
red as f64 / 255.0,
green as f64 / 255.0,
blue as f64 / 255.0,
alpha as f64 / 255.0,
);

if !is_child {
ns_view.setBackgroundColor(Some(&color));
}
// This has to be monitored as it may clash with isOpaque = true.
// The webview background color may also applied too late so actually not that useful.
webview.setBackgroundColor(Some(&color));
}
webview
};

Expand Down Expand Up @@ -831,6 +849,25 @@ r#"Object.defineProperty(window, 'ipc', {
}

pub fn set_background_color(&self, _background_color: RGBA) -> Result<()> {
#[cfg(target_os = "ios")]
unsafe {
let (red, green, blue, alpha) = _background_color;

let color = objc2_ui_kit::UIColor::colorWithRed_green_blue_alpha(
red as f64 / 255.0,
green as f64 / 255.0,
blue as f64 / 255.0,
alpha as f64 / 255.0,
);

if !self.is_child {
self.ns_view.setBackgroundColor(Some(&color));
}
// This has to be monitored as it may clash with isOpaque = true.
// The webview background color may also applied too late so actually not that useful.
self.webview.setBackgroundColor(Some(&color));
}

Ok(())
}

Expand Down