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        let pixels = vec![255; (Self::WIDTH as usize * Self::HEIGHT as usize) * 4];
68        let raw_pixels = Arc::new(pixels.clone());
69        Self {
70            width: Self::WIDTH,
71            height: Self::HEIGHT,
72            handle: image::Handle::from_rgba(Self::WIDTH, Self::HEIGHT, pixels),
73            raw_pixels,
74        }
75    }
76}
77
78impl ImageInfo {
79    // The default dimensions
80    const WIDTH: u32 = 800;
81    const HEIGHT: u32 = 800;
82
83    fn new(mut pixels: Vec<u8>, format: PixelFormat, width: u32, height: u32) -> Self {
84        // R, G, B, A
85        assert_eq!(pixels.len() % 4, 0);
86
87        if let PixelFormat::Bgra = format {
88            pixels.chunks_mut(4).for_each(|chunk| chunk.swap(0, 2));
89        }
90
91        let raw_pixels = Arc::new(pixels.clone());
92        Self {
93            width,
94            height,
95            handle: image::Handle::from_rgba(width, height, pixels),
96            raw_pixels,
97        }
98    }
99
100    /// Get the image handle for direct rendering.
101    pub fn as_handle(&self) -> image::Handle {
102        self.handle.clone()
103    }
104
105    /// Image width.
106    pub fn image_width(&self) -> u32 {
107        self.width
108    }
109
110    /// Image height.
111    pub fn image_height(&self) -> u32 {
112        self.height
113    }
114
115    /// Raw RGBA pixel data for direct GPU upload (shader widget path).
116    pub fn pixels(&self) -> Arc<Vec<u8>> {
117        Arc::clone(&self.raw_pixels)
118    }
119
120    fn blank(width: u32, height: u32) -> Self {
121        let pixels = vec![255; (width as usize * height as usize) * 4];
122        let raw_pixels = Arc::new(pixels.clone());
123        Self {
124            width,
125            height,
126            handle: image::Handle::from_rgba(width, height, pixels),
127            raw_pixels,
128        }
129    }
130}