Skip to content

Commit

Permalink
feat: add feature to zoom webview contents, closes #388 (#564)
Browse files Browse the repository at this point in the history
* feat: add feature to zoom webview contents, closes #388

* Update src/webview/wkwebview/mod.rs

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Update src/webview/mod.rs

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
  • Loading branch information
keiya01 and amrbashir committed May 2, 2022
1 parent 9776fc4 commit 34b6cbc
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/impl-zoom-feature.md
@@ -0,0 +1,5 @@
---
"wry": minor
---

Add `WebView::zoom` method.
2 changes: 2 additions & 0 deletions src/webview/android/mod.rs
Expand Up @@ -118,6 +118,8 @@ impl InnerWebView {
}
}
}

pub fn zoom(&self, scale_factor: f64) {}
}

pub struct UnsafeIpc(*mut c_void);
Expand Down
11 changes: 11 additions & 0 deletions src/webview/mod.rs
Expand Up @@ -489,6 +489,17 @@ impl WebView {
self.window.inner_size()
}

/// Set the webview zoom level
///
/// ## Platform-specific:
///
/// - **Android**: Not supported.
/// - **macOS**: available on macOS 11+ only.
/// - **iOS**: available on iOS 14+ only.
pub fn zoom(&self, scale_factor: f64) {
self.webview.zoom(scale_factor);
}

#[cfg(target_os = "android")]
pub fn run(self, env: JNIEnv, jclass: JClass, jobject: JObject) -> jobject {
self.webview.run(env, jclass, jobject).unwrap()
Expand Down
4 changes: 4 additions & 0 deletions src/webview/webkitgtk/mod.rs
Expand Up @@ -386,6 +386,10 @@ impl InnerWebView {
pub fn is_devtools_open(&self) -> bool {
self.is_inspector_open.load(Ordering::Relaxed)
}

pub fn zoom(&self, scale_factor: f64) {
WebViewExt::set_zoom_level(&*self.webview, scale_factor);
}
}

pub fn platform_webview_version() -> Result<String> {
Expand Down
4 changes: 4 additions & 0 deletions src/webview/webview2/mod.rs
Expand Up @@ -614,6 +614,10 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
pub fn is_devtools_open(&self) -> bool {
false
}

pub fn zoom(&self, scale_factor: f64) {
let _ = unsafe { self.controller.SetZoomFactor(scale_factor) };
}
}

pub fn platform_webview_version() -> Result<String> {
Expand Down
6 changes: 6 additions & 0 deletions src/webview/wkwebview/mod.rs
Expand Up @@ -590,6 +590,12 @@ r#"Object.defineProperty(window, 'ipc', {
(view_frame.size.width as f64, view_frame.size.height as f64).into();
logical.to_physical(scale_factor)
}

pub fn zoom(&self, scale_factor: f64) {
unsafe {
let _: () = msg_send![self.webview, setPageZoom: scale_factor];
}
}
}

pub fn platform_webview_version() -> Result<String> {
Expand Down

0 comments on commit 34b6cbc

Please sign in to comment.