1use std::sync::Arc;
28
29use iced::widget::image;
30
31pub 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#[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 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 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 pub fn as_handle(&self) -> image::Handle {
95 self.handle.clone()
96 }
97
98 pub fn image_width(&self) -> u32 {
100 self.width
101 }
102
103 pub fn image_height(&self) -> u32 {
105 self.height
106 }
107
108 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}