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
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,16 @@ class WryWebViewPanel(
}

private object NativeBindings {
init {
setNativeLogger(
object : NativeLogger {
override fun handleLog(data: String) {
println(data)
}
}
)
}

fun createWebview(parentHandle: ULong, width: Int, height: Int, url: String, handler: NavigationHandler): ULong {
return io.github.kdroidfilter.webview.wry.createWebview(parentHandle, width, height, url, handler)
}
Expand Down
39 changes: 23 additions & 16 deletions wrywebview/src/main/rust/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::num::NonZeroIsize;
use wry::raw_window_handle::Win32WindowHandle;

use crate::error::WebViewError;
use crate::log_enabled;
use crate::wry_log;

/// Wrapper around a raw window handle for WebView creation.
pub struct RawWindow {
Expand Down Expand Up @@ -49,35 +49,42 @@ pub fn raw_window_handle_from(parent_handle: u64) -> Result<RawWindowHandle, Web

#[cfg(target_os = "windows")]
{
let hwnd = NonZeroIsize::new(parent_handle as isize)
.ok_or(WebViewError::InvalidWindowHandle)?;
let hwnd =
NonZeroIsize::new(parent_handle as isize).ok_or(WebViewError::InvalidWindowHandle)?;
let handle = RawWindowHandle::Win32(Win32WindowHandle::new(hwnd));
if log_enabled() {
eprintln!("[wrywebview] raw_window_handle Win32=0x{:x}", parent_handle);
}
wry_log!("[wrywebview] raw_window_handle Win32=0x{:x}", parent_handle);
// if log_enabled() {
// eprintln!("[wrywebview] raw_window_handle Win32=0x{:x}", parent_handle);
// }
return Ok(handle);
}

#[cfg(target_os = "macos")]
{
let ns_view = crate::platform::macos::appkit_ns_view_from_handle(parent_handle)?;
let handle = RawWindowHandle::AppKit(AppKitWindowHandle::new(ns_view));
if log_enabled() {
eprintln!(
"[wrywebview] raw_window_handle AppKit=0x{:x} ns_view=0x{:x}",
parent_handle,
ns_view.as_ptr() as usize
);
}
wry_log!(
"[wrywebview] raw_window_handle AppKit=0x{:x} ns_view=0x{:x}",
parent_handle,
ns_view.as_ptr() as usize
);
// if log_enabled() {
// eprintln!(
// "[wrywebview] raw_window_handle AppKit=0x{:x} ns_view=0x{:x}",
// parent_handle,
// ns_view.as_ptr() as usize
// );
// }
return Ok(handle);
}

#[cfg(target_os = "linux")]
{
let handle = RawWindowHandle::Xlib(XlibWindowHandle::new(parent_handle as c_ulong));
if log_enabled() {
eprintln!("[wrywebview] raw_window_handle Xlib=0x{:x}", parent_handle);
}
// if log_enabled() {
// eprintln!("[wrywebview] raw_window_handle Xlib=0x{:x}", parent_handle);
// }
wry_log!("[wrywebview] raw_window_handle Xlib=0x{:x}", handle);
return Ok(handle);
}

Expand Down
50 changes: 40 additions & 10 deletions wrywebview/src/main/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod state;

use std::str::FromStr;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::{Arc, OnceLock, RwLock};

use wry::cookie::time::OffsetDateTime;
use wry::cookie::{Cookie, Expiration, SameSite};
Expand Down Expand Up @@ -159,14 +159,46 @@ pub fn set_log_enabled(enabled: bool) {
LOG_ENABLED.store(enabled, Ordering::Relaxed);
}

#[uniffi::export(callback_interface)]
pub trait NativeLogger: Send + Sync {
fn handle_log(&self, data: String);
}

static GLOBAL_LOGGER: OnceLock<RwLock<Option<Box<dyn NativeLogger>>>> = OnceLock::new();

fn get_logger_registry() -> &'static RwLock<Option<Box<dyn NativeLogger>>> {
GLOBAL_LOGGER.get_or_init(|| RwLock::new(None))
}

#[uniffi::export]
pub fn set_native_logger(logger: Box<dyn NativeLogger>) {
let mut lock = get_logger_registry().write().unwrap();
*lock = Some(logger);
}

#[macro_export]
macro_rules! wry_log {
($($arg:tt)*) => {
if log_enabled() {
eprintln!($($arg)*);
}
$crate::do_internal_log(format_args!($($arg)*));
};
}

#[doc(hidden)]
pub fn do_internal_log(args: std::fmt::Arguments) {
if !log_enabled() {
return;
}
let log_string = args.to_string();

if let Ok(lock) = crate::get_logger_registry().read() {
if let Some(ref logger) = *lock {
logger.handle_log(log_string);
return;
}
}
eprintln!("{}", log_string);
}

// ============================================================================
// WebView Creation
// ============================================================================
Expand Down Expand Up @@ -364,12 +396,10 @@ pub fn create_webview_with_user_agent(
// ============================================================================

fn set_bounds_inner(id: u64, x: i32, y: i32, width: i32, height: i32) -> Result<(), WebViewError> {
if log_enabled() {
wry_log!(
"[wrywebview] set_bounds id={} pos=({}, {}) size={}x{}",
id, x, y, width, height
);
}
wry_log!(
"[wrywebview] set_bounds id={} pos=({}, {}) size={}x{}",
id, x, y, width, height
);
let bounds = make_bounds(x, y, width, height);
with_webview(id, |webview| webview.set_bounds(bounds).map_err(WebViewError::from))
}
Expand Down
27 changes: 16 additions & 11 deletions wrywebview/src/main/rust/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use std::ffi::CStr;
use std::ptr::NonNull;

use dispatch2::run_on_main;
pub use dispatch2::DispatchQueue;
use objc2::msg_send;
use objc2::runtime::{AnyClass, AnyObject};
pub use objc2::MainThreadMarker;
pub use dispatch2::DispatchQueue;

use crate::error::WebViewError;
use crate::log_enabled;
use crate::wry_log;

/// Runs a closure on the main thread using GCD.
pub fn run_on_main_thread<F, R>(f: F) -> Result<R, WebViewError>
Expand All @@ -30,9 +30,10 @@ pub fn appkit_ns_view_from_handle(parent_handle: u64) -> Result<NonNull<c_void>,
.ok_or(WebViewError::InvalidWindowHandle)?;
let obj = unsafe { &*(ptr.as_ptr() as *mut AnyObject) };
let class_name = obj.class().name().to_string_lossy();
if log_enabled() {
eprintln!("[wrywebview] appkit handle class={}", class_name);
}
// if log_enabled() {
// eprintln!("[wrywebview] appkit handle class={}", class_name);
// }
wry_log!("[wrywebview] appkit handle class={}", class_name);

let nswindow_name = unsafe { CStr::from_bytes_with_nul_unchecked(b"NSWindow\0") };
let nsview_name = unsafe { CStr::from_bytes_with_nul_unchecked(b"NSView\0") };
Expand All @@ -43,12 +44,16 @@ pub fn appkit_ns_view_from_handle(parent_handle: u64) -> Result<NonNull<c_void>,
if msg_send![obj, isKindOfClass: nswindow_cls] {
let view: *mut AnyObject = msg_send![obj, contentView];
let view = NonNull::new(view).ok_or(WebViewError::InvalidWindowHandle)?;
if log_enabled() {
eprintln!(
"[wrywebview] appkit handle is NSWindow, contentView=0x{:x}",
view.as_ptr() as usize
);
}
// if log_enabled() {
// eprintln!(
// "[wrywebview] appkit handle is NSWindow, contentView=0x{:x}",
// view.as_ptr() as usize
// );
// }
wry_log!(
"[wrywebview] appkit handle is NSWindow, contentView=0x{:x}",
view.as_ptr() as usize
);
return Ok(view.cast());
}
if msg_send![obj, isKindOfClass: nsview_cls] {
Expand Down