Crate wew

Source
Expand description

Wew is a cross-platform WebView rendering library based on Chromium Embedded Framework (CEF). It supports mouse, keyboard, touch, input methods, off-screen rendering, and communication with web pages.

§Thread Considerations

In the current project, WebView and Runtime calls are best executed on the UI thread, which is the main thread of the application process.

Creating a Runtime must be completed on the UI thread, and all message loop calls must also be operated on the UI thread.

Other calls should be executed on the UI thread whenever possible, unless it is truly unavoidable. Although these calls can run on any thread, there is currently no guarantee that they will not cause other side effects.

However, it is important to note that if the WebView manages window events on its own, such as not using off-screen rendering, then the WebView can be created on any thread.

§Examples

use std::{
    sync::mpsc::{Sender, channel},
    thread,
};

use wew::{
    MainThreadMessageLoop, MessageLoopAbstract, NativeWindowWebView,
    runtime::{LogLevel, RuntimeHandler},
    webview::{WebViewAttributes, WebViewHandler, WebViewState},
};

struct RuntimeObserver {
    tx: Sender<()>,
}

impl RuntimeHandler for RuntimeObserver {
    fn on_context_initialized(&self) {
        self.tx.send(()).unwrap();
    }
}

struct WebViewObserver;

impl WebViewHandler for WebViewObserver {
    fn on_state_change(&self, state: WebViewState) {
        if state == WebViewState::Close {
            std::process::exit(0);
        }
    }
}

fn main() {
    if wew::is_subprocess() {
        wew::execute_subprocess();

        return;
    }

    #[cfg(target_os = "macos")]
    wew::utils::startup_nsapplication();

    let message_loop = MainThreadMessageLoop::default();

    let mut runtime_attributes_builder =
        message_loop.create_runtime_attributes_builder::<NativeWindowWebView>();

    runtime_attributes_builder = runtime_attributes_builder
        // Set cache path, here we use environment variables passed by the build script.
        .with_root_cache_path(option_env!("CACHE_PATH").unwrap())
        .with_cache_path(option_env!("CACHE_PATH").unwrap())
        .with_log_severity(LogLevel::Info);

    let (tx, rx) = channel();

    // Create runtime, wait for the `on_context_initialized` event to be triggered
    // before considering the creation successful.
    let runtime = runtime_attributes_builder
        .build()
        .create_runtime(RuntimeObserver { tx })
        .unwrap();

    thread::spawn(move || {
        rx.recv().unwrap();

        let webview = runtime
            .create_webview(
                "https://www.google.com",
                WebViewAttributes::default(),
                WebViewObserver,
            )
            .unwrap();

        std::mem::forget(webview);
        std::mem::forget(runtime);
    });

    message_loop.block_run();
}

Re-exports§

pub use winit;winit
pub use raw_window_handle;

Modules§

events
Used to handle window events.
request
Network request related, including implementing custom request interception.
runtime
This module is used to manage the runtime.
utils
This module is used to provide some utility functions and types.
webview
This module is used to manage a single web page.

Structs§

MainThreadMessageLoop
Main thread message loop
MessagePumpLoop
Message loop pump
MultiThreadMessageLoop
Multi-threaded message loop
NativeWindowWebView
Native window mode
Rect
Represents a rectangular area
WindowlessRenderWebView
Off-screen rendering mode

Enums§

Error

Traits§

MessageLoopAbstract
Message loop abstraction
WebViewAbstract
WebView abstraction

Functions§

execute_subprocess
Execute subprocess
is_subprocess
Check if current process is a subprocess