Skip to content

Commit

Permalink
feat: get webview version (#259)
Browse files Browse the repository at this point in the history
* feat(macOS): Implement basic `version()`

* feat(linux): basic implementation of `version()`

* Add changefile

* feat(win32): Implement basic `version()`

* Expose global `webview_version` function

* [ci skip] update changelog

* Add `webview_version` test

* Seems the compiler prefer `panic!()`

* Panic with trace

* Install webview2 on the CI
  • Loading branch information
lemarier authored May 12, 2021
1 parent 29b9a8a commit 4df310e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/get-version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Expose `webview_version` function in the `webview` module.
7 changes: 7 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ jobs:
sudo apt-get update
sudo apt-get install -y webkit2gtk-4.0 libgtksourceview-3.0-dev libappindicator3-dev
- name: install webview2 (windows only)
if: matrix.platform == 'windows-latest'
shell: pwsh
run: |
Invoke-WebRequest https://go.microsoft.com/fwlink/p/?LinkId=2124703 -OutFile installwebview.exe -UseBasicParsing
cmd /C start /wait installwebview.exe /silent /install
- name: build
run: cargo build
- name: test
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tao = { version = "0.2", default-features = false, features = [ "serde" ] }

[target."cfg(target_os = \"linux\")".dependencies]
webkit2gtk = { version = "0.11", features = [ "v2_10" ] }
webkit2gtk-sys = "0.13.0"
gio = "0.9"
glib = "0.10"
gtk = "0.9"
Expand Down
14 changes: 14 additions & 0 deletions src/webview/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use webkit2gtk::{
WebContextBuilder, WebContextExt, WebView, WebViewExt, WebViewExtManual,
WebsiteDataManagerBuilder,
};
use webkit2gtk_sys::{
webkit_get_major_version, webkit_get_micro_version, webkit_get_minor_version,
};

use crate::{
application::{platform::unix::*, window::Window},
Expand Down Expand Up @@ -234,3 +237,14 @@ impl InnerWebView {
Ok(())
}
}

pub fn platform_webview_version() -> Result<String> {
let (major, minor, patch) = unsafe {
(
webkit_get_major_version(),
webkit_get_minor_version(),
webkit_get_micro_version(),
)
};
Ok(format!("{}.{}.{}", major, minor, patch).into())
}
12 changes: 12 additions & 0 deletions src/webview/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ impl InnerWebView {
}
}

pub fn platform_webview_version() -> Result<String> {
unsafe {
let bundle: id =
msg_send![class!(NSBundle), bundleWithIdentifier: NSString::new("com.apple.WebKit")];
let dict: id = msg_send![bundle, infoDictionary];
let webkit_version: id = msg_send![dict, objectForKey: NSString::new("CFBundleVersion")];
let nsstring = NSString(Id::from_ptr(webkit_version));
let () = msg_send![bundle, unload];
Ok(nsstring.to_str().to_string())
}
}

impl Drop for InnerWebView {
fn drop(&mut self) {
// We need to drop handler closures here
Expand Down
17 changes: 17 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,20 @@ pub enum FileDropEvent {
/// The file drop was aborted.
Cancelled,
}

/// Get Webview/Webkit version on current platform.
pub fn webview_version() -> Result<String> {
platform_webview_version()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn should_get_webview_version() {
if let Err(error) = webview_version() {
panic!("{}", error);
}
}
}
8 changes: 8 additions & 0 deletions src/webview/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,11 @@ impl InnerWebView {
Ok(())
}
}

pub fn platform_webview_version() -> Result<String> {
let webview_builder = webview2::EnvironmentBuilder::new();
let version = webview_builder
.get_available_browser_version_string()
.expect("Unable to get webview2 version");
Ok(version)
}
5 changes: 5 additions & 0 deletions src/webview/winrt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,8 @@ where

result
}

// not supported yet
pub fn platform_webview_version() -> Result<String> {
Ok("Unknown".into())
}

0 comments on commit 4df310e

Please sign in to comment.