Sign in

    webview

    Low-level MoonBit bindings to webview/webview

    webview
    Download zip
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    4 months ago
    Downloads
    37

    No dependencies

    #shiguri-01/webview

    Low-level MoonBit bindings to webview/webview through MoonBit native FFI.

    Use this module when you need direct access to a native webview window, JavaScript execution, JavaScript-to-native calls, window events, or the native window handle.

    The module provides:

    • Webview lifecycle management.
    • HTML and URL loading.
    • JavaScript initialization and evaluation.
    • JavaScript-to-native bindings.
    • Native window controls and window events.
    • Access to the platform-native window handle.

    #Example

    fn main {
    try! @webview.WebView(debug=true)
    ..set_title("MoonBit webview")
    ..set_size(800, 600)
    ..set_html("<h1>Hello from MoonBit</h1>")
    ..run()
    }

    Run the example package with:

    moon run src/examples/basic --target native

    #Native Build Notes

    Runnable packages must link the C++ runtime and platform libraries. On Windows, the bundled package configuration uses cl for the C++ stub. Runnable packages that import shiguri-01/webview should also link the WebView2-related platform libraries in their moon.pkg:

    options( "is-main": true, link: { "native": { "cc-link-flags": "advapi32.lib ole32.lib shell32.lib shlwapi.lib user32.lib version.lib", }, }, )

    #License and Credits

    License: Apache-2.0.

    Native credits: webview/webview (MIT) and Microsoft WebView2 SDK header (Microsoft license and notices). Vendored license and notice files are included under src/vendor.

    WebViewError

    pub(all) suberror WebViewError {
    CreationFailed
    MissingDependency
    Canceled
    InvalidState
    InvalidArgument
    Unspecified
    Duplicate
    NotFound
    Unknown(Int)
    } derive(Eq,
    Debug
    )

    Errors reported by the native webview backend.

    NativeWindow

    #external
    pub type NativeWindow

    RawWebView

    type RawWebView

    Opaque native webview handle used by the FFI bridge.

    SizeHint

    pub(all) enum SizeHint {
    None
    Min
    Max
    Fixed
    } derive(Eq,
    Debug
    )

    Window size hint used by WebView::set_size.

    WebView

    pub struct WebView {
    // private fields
    }
    fn WebView::WebView(debug? : Bool) -> WebView raise WebViewError

    Handle to a native webview instance.

    WebView::bind

    fn WebView::bind(self : WebView, name : String, handler : (String, String) -> Unit) -> Unit raise WebViewError

    Bind a JavaScript function to a native MoonBit callback.

    The callback receives the webview request id and the raw JSON argument array supplied by webview_bind.

    WebView::close_window

    fn WebView::close_window(self : WebView) -> Unit raise WebViewError

    Close the native top-level window without asking the runtime to confirm.

    WebView::destroy

    fn WebView::destroy(self : WebView) -> Unit raise WebViewError

    Destroy the native webview if it has not already been destroyed.

    WebView::dispatch

    fn WebView::dispatch(self : WebView, handler : () -> Unit) -> Unit raise WebViewError

    Schedule a callback on the native GUI event-loop thread.

    This is the safe way to return to webview APIs from work that completes outside the binding callback path.

    WebView::eval

    fn WebView::eval(self : WebView, js : String) -> Unit raise WebViewError

    Evaluate JavaScript in the current page.

    WebView::focus_window

    fn WebView::focus_window(self : WebView) -> Unit raise WebViewError

    Focus the native top-level window.

    WebView::hide_window

    fn WebView::hide_window(self : WebView) -> Unit raise WebViewError

    Hide the native top-level window.

    WebView::init

    fn WebView::init(self : WebView, js : String) -> Unit raise WebViewError

    Inject JavaScript to run before window.onload on future page loads.

    WebView::is_valid

    fn WebView::is_valid(self : WebView) -> Bool

    Return whether this handle currently owns a live native webview.

    WebView::is_window_closed

    fn WebView::is_window_closed(self : WebView) -> Bool

    Return whether the native top-level window has been closed.

    WebView::maximize_window

    fn WebView::maximize_window(self : WebView) -> Unit raise WebViewError

    Maximize the native top-level window.

    WebView::minimize_window

    fn WebView::minimize_window(self : WebView) -> Unit raise WebViewError

    Minimize the native top-level window.

    WebView::native_window

    fn WebView::native_window(self : WebView) -> NativeWindow raise WebViewError

    Return the borrowed native top-level window handle for this webview.

    The returned handle is opaque and only valid while this WebView remains alive. It is intended for higher-level native shell integrations such as dialogs and menus.

    WebView::navigate

    fn WebView::navigate(self : WebView, url : String) -> Unit raise WebViewError

    Navigate to a URL or data URI.

    WebView::on_window_event

    fn WebView::on_window_event(self : WebView, handler : (WindowEvent) -> Unit) -> Unit raise WebViewError

    Register a native top-level window event callback.

    WebView::restore_window

    fn WebView::restore_window(self : WebView) -> Unit raise WebViewError

    Restore the native top-level window from minimized or maximized state.

    WebView::return_

    fn WebView::return_(self : WebView, id : String, status : Int, result : String) -> Unit raise WebViewError

    Resolve or reject a JavaScript binding call.

    result must be a JSON value, or the empty string for JavaScript undefined.

    WebView::run

    fn WebView::run(self : WebView) -> Unit raise WebViewError

    Run the GUI event loop until the webview is terminated.

    WebView::set_html

    fn WebView::set_html(self : WebView, html : String) -> Unit raise WebViewError

    Load an HTML string into the webview.

    WebView::set_size

    fn WebView::set_size(self : WebView, width : Int, height : Int, hint? : SizeHint) -> Unit raise WebViewError

    Set the native window size.

    WebView::set_title

    fn WebView::set_title(self : WebView, title : String) -> Unit raise WebViewError

    Set the native window title.

    WebView::show_window

    fn WebView::show_window(self : WebView) -> Unit raise WebViewError

    Show the native top-level window.

    WebView::terminate

    fn WebView::terminate(self : WebView) -> Unit raise WebViewError

    Stop the GUI event loop. This can be called from another thread.

    WebView::unbind

    fn WebView::unbind(self : WebView, name : String) -> Unit raise WebViewError

    Remove a JavaScript binding created by bind.

    WindowEvent

    pub(all) enum WindowEvent {
    CloseRequested
    Closed
    Focused
    Blurred
    Minimized
    Maximized
    Restored
    } derive(Eq,
    Debug
    )

    Native top-level window events reported by the host backend.

    version_number

    fn version_number() -> String

    Return the bundled webview library version string.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io