Skip to main content

iced_webview/
lib.rs

1//! A library to embed web views in iced applications.
2//!
3//! Supports [Blitz](https://github.com/DioxusLabs/blitz) (Rust-native, modern CSS),
4//! [litehtml](https://github.com/franzos/litehtml-rs) (lightweight, CPU-based), and
5//! [Servo](https://servo.org/) (full browser: HTML5, CSS3, JS).
6//!
7//! Has two separate widgets: Basic, and Advanced.
8//! The basic widget is simple to implement — use abstractions like `CloseCurrent` and `ChangeView`.
9//! The advanced widget gives you direct `ViewId` control for multiple simultaneous views.
10//!
11//! # Basic usage
12//!
13//! ```rust,ignore
14//! enum Message {
15//!    WebView(iced_webview::Action),
16//!    Update,
17//! }
18//!
19//! struct State {
20//!    webview: iced_webview::WebView<iced_webview::Blitz, Message>,
21//! }
22//! ```
23//!
24//! Then call the usual `view/update` methods — see
25//! [examples](https://github.com/franzos/iced_webview_v2/tree/main/examples) for full working code.
26//!
27use std::sync::Arc;
28
29use iced::widget::image;
30
31/// Engine Trait and Engine implementations
32pub mod engines;
33pub use engines::{Engine, PageType, PixelFormat, ViewId};
34
35mod webview;
36pub use basic::{Action, WebView};
37pub use webview::{advanced, basic};
38
39#[cfg(feature = "blitz")]
40pub use engines::blitz::Blitz;
41
42#[cfg(feature = "litehtml")]
43pub use engines::litehtml::Litehtml;
44
45#[cfg(feature = "servo")]
46pub use engines::servo::Servo;
47
48#[cfg(feature = "cef")]
49pub use engines::cef_engine::{cef_subprocess_check, Cef};
50
51pub(crate) mod util;
52
53#[cfg(any(feature = "litehtml", feature = "blitz"))]
54pub(crate) mod fetch;
55
56/// Image details for passing the view around
57#[derive(Clone, Debug)]
58pub struct ImageInfo {
59    width: u32,
60    height: u32,
61    handle: image::Handle,
62    raw_pixels: Arc<Vec<u8>>,
63}
64
65impl Default for ImageInfo {
66    fn default() -> Self {
67        Self::blank(Self::WIDTH, Self::HEIGHT)
68    }
69}
70
71impl ImageInfo {
72    // The default dimensions
73    const WIDTH: u32 = 800;
74    const HEIGHT: u32 = 800;
75
76    fn new(mut pixels: Vec<u8>, format: PixelFormat, width: u32, height: u32) -> Self {
77        // R, G, B, A
78        assert_eq!(pixels.len() % 4, 0);
79
80        if let PixelFormat::Bgra = format {
81            pixels.chunks_mut(4).for_each(|chunk| chunk.swap(0, 2));
82        }
83
84        let raw_pixels = Arc::new(pixels);
85        Self {
86            width,
87            height,
88            handle: image::Handle::from_rgba(width, height, (*raw_pixels).clone()),
89            raw_pixels,
90        }
91    }
92
93    /// Get the image handle for direct rendering.
94    pub fn as_handle(&self) -> image::Handle {
95        self.handle.clone()
96    }
97
98    /// Image width.
99    pub fn image_width(&self) -> u32 {
100        self.width
101    }
102
103    /// Image height.
104    pub fn image_height(&self) -> u32 {
105        self.height
106    }
107
108    /// Raw RGBA pixel data for direct GPU upload (shader widget path).
109    pub fn pixels(&self) -> Arc<Vec<u8>> {
110        Arc::clone(&self.raw_pixels)
111    }
112
113    fn blank(width: u32, height: u32) -> Self {
114        let (w, h) = (width as usize)
115            .checked_mul(height as usize)
116            .and_then(|n| n.checked_mul(4))
117            .map_or((1u32, 1u32), |_| (width, height));
118
119        let pixels = vec![255; (w as usize * h as usize) * 4];
120        let raw_pixels = Arc::new(pixels.clone());
121        Self {
122            width: w,
123            height: h,
124            handle: image::Handle::from_rgba(w, h, pixels),
125            raw_pixels,
126        }
127    }
128}