Skip to content

Commit

Permalink
move webview code to separate dir
Browse files Browse the repository at this point in the history
  • Loading branch information
MoAlyousef committed Jan 17, 2024
1 parent 7cd9202 commit aefbdc0
Show file tree
Hide file tree
Showing 23 changed files with 10 additions and 52,429 deletions.
1 change: 0 additions & 1 deletion bind.sh

This file was deleted.

3 changes: 3 additions & 0 deletions fltk-webview-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ categories = ["gui"]
readme = "../README.md"
license = "MIT"

[dependencies]
wv-sys = "0.1"

[build-dependencies]
pkg-config = "0.3"
cc = "1"
1 change: 0 additions & 1 deletion fltk-webview-sys/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# fltk-webview-sys
A binding to a tiny cross-platform [webview](https://github.com/webview/webview) library
129 changes: 5 additions & 124 deletions fltk-webview-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::env;
use std::path::PathBuf;

fn main() {
compile_webview();
println!("cargo:rerun-if-changed=src/gtk_helper.c");
println!("cargo:rerun-if-changed=src/cocoa_helper.m");

#[cfg(target_os = "macos")]
compile_cocoa_helper();

#[cfg(not(any(target_os = "macos", target_os = "windows")))]
compile_gtk_helper();
}

#[cfg(not(any(target_os = "macos", target_os = "windows")))]
Expand All @@ -30,123 +31,3 @@ fn compile_cocoa_helper() {
build.file("src/cocoa_helper.m");
build.compile("cocoa");
}

fn compile_webview() {
let target = env::var("TARGET").unwrap();
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let exe_pth = out_dir.clone();

println!("cargo:rerun-if-changed=webview/webview.h");
println!("cargo:rerun-if-changed=webview/webview.cc");
println!("cargo:rerun-if-changed=src/gtk_helper.c");
println!("cargo:rerun-if-changed=src/cocoa_helper.m");

let mut build = cc::Build::new();
if !target.contains("windows-gnu") {
build
.cpp(true)
.define("WEBVIEW_STATIC", "")
.file("webview/webview.cc")
.flag_if_supported("-w");
}

if target.contains("windows") {
let edge_weview_native =
"webview/build/native".to_string();
if target.contains("msvc") {
let mut include = edge_weview_native.clone();
include.push_str("/include");
build.flag("/DWEBVIEW_EDGE");
build.flag("/std:c++17");
build.include(include);
}

for &lib in &[
"user32",
"oleaut32",
"ole32",
"version",
"shell32",
"advapi32",
"shlwapi"
] {
println!("cargo:rustc-link-lib={}", lib);
}

let wv_arch = if target.contains("x86_64") {
"x64"
} else if target.contains("i686") {
"x86"
} else {
"arm64"
};

let mut wv_path = manifest_dir;
if target.contains("msvc") {
wv_path.push(edge_weview_native);
} else {
wv_path.push("webview");
wv_path.push("build");
wv_path.push("native");
}
wv_path.push(wv_arch);
let webview2_dir = wv_path.as_path().to_str().unwrap();
println!("cargo:rustc-link-search={}", webview2_dir);
println!("cargo:rustc-link-search={}", out_dir.join("../../..").display());
if target.contains("msvc") {
println!("cargo:rustc-link-lib=WebView2LoaderStatic");
} else {
if !target.contains("aarch64") {
println!("cargo:rustc-link-lib=WebView2Loader");
println!("cargo:rustc-link-lib=webview");
for entry in std::fs::read_dir(wv_path).expect("Can't read DLL dir") {
let entry_path = entry.expect("Invalid fs entry").path();
let file_name_result = entry_path.file_name();
let mut exe_pth = exe_pth.clone();
if let Some(file_name) = file_name_result {
let file_name = file_name.to_str().unwrap();
if file_name.ends_with(".dll") {
exe_pth.push("../../..");
let mut for_examples_exe_pth = exe_pth.clone();
for_examples_exe_pth.push("examples");
exe_pth.push(file_name);
std::fs::copy(&entry_path, exe_pth.as_path())
.expect("Can't copy from DLL dir /target/..");

// Copy .dll to examples folder too, in order to run examples when cross compiling from linux.
for_examples_exe_pth.push(file_name);
std::fs::copy(&entry_path, for_examples_exe_pth.as_path())
.expect("Can't copy from DLL dir to /target/../examples");
}
}
}
} else {
panic!("{:?} not supported yet", target)
}
}
} else if target.contains("apple") {
build.flag("-DWEBVIEW_COCOA");
build.flag("-std=c++11");
println!("cargo:rustc-link-lib=framework=Cocoa");
println!("cargo:rustc-link-lib=framework=WebKit");
} else if target.contains("linux") || target.contains("bsd") {
build.flag("-DWEBVIEW_GTK");
build.flag("-std=c++11");
let lib = pkg_config::Config::new()
.atleast_version("2.8")
.probe("webkit2gtk-4.1")
.unwrap();
for path in lib.include_paths {
build.include(path);
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
compile_gtk_helper();
} else {
panic!("Unsupported platform");
}

if !target.contains("windows-gnu") {
build.compile("webview");
}
}
2 changes: 1 addition & 1 deletion fltk-webview-sys/src/cocoa_helper.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void add_nsmenu(bool val) {
}
}

void make_delegate(NSWindow *child, NSWindow *parent, add_menu: bool) {
void make_delegate(NSWindow *child, NSWindow *parent, add_menu: int) {
[parent setDelegate:(id)child];
[child orderWindow:NSWindowAbove relativeTo:[parent windowNumber]];
Method old_method =
Expand Down
156 changes: 1 addition & 155 deletions fltk-webview-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,155 +1 @@
#![allow(non_camel_case_types)]

// Uses code from https://github.com/webview/webview_rust/blob/dev/src/webview.rs

mod sys;
pub use sys::*;

use std::{
ffi::{CStr, CString},
mem,
os::raw,
sync::Arc,
};

#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SizeHint {
None = 0,
Min = 1,
Max = 2,
Fixed = 3,
}

/// Webview wrapper
#[derive(Clone)]
pub struct Webview {
inner: Arc<webview_t>,
}

unsafe impl Send for Webview {}
unsafe impl Sync for Webview {}

impl Drop for Webview {
fn drop(&mut self) {
if Arc::strong_count(&self.inner) == 0 {
unsafe {
webview_terminate(*self.inner);
webview_destroy(*self.inner);
}
}
}
}

impl Webview {
/// Navigate to a url
pub fn navigate(&self, url: &str) {
let url = std::ffi::CString::new(url).unwrap();
unsafe {
webview_navigate(*self.inner, url.as_ptr() as _);
}
}

/// Set the html content of the weview window
pub fn set_html(&self, html: &str) {
// MS Edge chromium based also requires utf-8
self.navigate(&(String::from("data:text/html;charset=utf-8,") + html));
}

/// Injects JavaScript code at the initialization of the new page
pub fn init(&self, js: &str) {
let js = CString::new(js).unwrap();
unsafe {
webview_init(*self.inner, js.as_ptr());
}
}

/// Evaluates arbitrary JavaScript code. Evaluation happens asynchronously
pub fn eval(&self, js: &str) {
let js = CString::new(js).unwrap();
unsafe {
webview_eval(*self.inner, js.as_ptr());
}
}

/// Posts a function to be executed on the main thread
pub fn dispatch<F>(&mut self, f: F)
where
F: FnOnce(Webview) + Send + 'static,
{
let closure = Box::into_raw(Box::new(f));
extern "C" fn callback<F>(webview: webview_t, arg: *mut raw::c_void)
where
F: FnOnce(Webview) + Send + 'static,
{
let webview = Webview {
inner: Arc::new(webview),
};
let closure: Box<F> = unsafe { Box::from_raw(arg as *mut F) };
(*closure)(webview);
}
unsafe { webview_dispatch(*self.inner, Some(callback::<F>), closure as *mut _) }
}

/// Binds a native C callback so that it will appear under the given name as a global JavaScript function
pub fn bind<F>(&self, name: &str, f: F)
where
F: FnMut(&str, &str),
{
let name = CString::new(name).unwrap();
let closure = Box::new(f);
extern "C" fn callback<F: FnMut(&str, &str)>(
seq: *const raw::c_char,
req: *const raw::c_char,
arg: *mut raw::c_void,
) {
let seq = unsafe {
CStr::from_ptr(seq)
.to_str()
.expect("No null bytes in parameter seq")
};
let req = unsafe {
CStr::from_ptr(req)
.to_str()
.expect("No null bytes in parameter req")
};
let mut f: Box<F> = unsafe { Box::from_raw(arg as *mut F) };
(*f)(seq, req);
mem::forget(f);
}
unsafe {
webview_bind(
*self.inner,
name.as_ptr(),
Some(callback::<F>),
Box::into_raw(closure) as *mut _,
)
};
}

/// Unbinds a native C callback so that it will appear under the given name as a global JavaScript function
pub fn unbind(&self, name: &str) {
let name = CString::new(name).unwrap();
let _move = move || unsafe { webview_unbind(*self.inner, name.as_ptr()) };
_move();
}

/// Allows to return a value from the native binding.
pub fn return_(&self, seq: &str, status: i32, result: &str) {
let seq = CString::new(seq).unwrap();
let result = CString::new(result).unwrap();
unsafe { webview_return(*self.inner, seq.as_ptr(), status, result.as_ptr()) }
}

/// Set the size of the webview window
pub fn set_size(&self, width: i32, height: i32, hints: SizeHint) {
unsafe { webview_set_size(*self.inner, width, height, hints as i32) }
}

/// Create a Webview from an `Arc<webview_t>`
pub fn from_raw(inner: Arc<webview_t>) -> Webview {
Self {
inner
}
}
}
pub use wv_sys::*;
Loading

0 comments on commit aefbdc0

Please sign in to comment.