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