iced_webview/engines.rs
1use std::collections::HashMap;
2
3use crate::ImageInfo;
4use iced::keyboard;
5use iced::mouse::{self, Interaction};
6use iced::Point;
7use iced::Size;
8
9mod view_manager;
10pub use view_manager::ViewManager;
11
12/// A Blitz implementation of Engine (Stylo + Taffy + Vello)
13#[cfg(feature = "blitz")]
14pub mod blitz;
15
16/// A litehtml implementation of Engine for HTML rendering
17#[cfg(feature = "litehtml")]
18pub mod litehtml;
19
20/// A Servo implementation of Engine (full browser: HTML5, CSS3, JS)
21#[cfg(feature = "servo")]
22pub mod servo;
23
24/// A CEF/Chromium implementation of Engine (full browser via cef-rs)
25#[cfg(feature = "cef")]
26pub mod cef_engine;
27
28/// Creation of new pages to be of a html type or a url
29#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
30pub enum PageType {
31 /// Allows visiting Url web pages
32 Url(String),
33 /// Allows custom html web pages
34 Html(String),
35}
36
37/// Enables browser engines to display their images in different formats
38pub enum PixelFormat {
39 /// RGBA
40 Rgba,
41 /// BGRA
42 Bgra,
43}
44
45/// Alias of usize used for controlling specific views
46/// Only used by advanced to get views, basic simply uses u32
47pub type ViewId = usize;
48
49/// Trait to handle multiple browser engines
50/// Currently only supports cpu renders via pixel_buffer
51/// Passing a View id that does not exist is handled gracefully (no-op / defaults)
52pub trait Engine {
53 /// Used to do work in the actual browser engine
54 fn update(&mut self);
55 /// Request a new render pass from the engine
56 fn render(&mut self);
57 /// Flush a pending render for a specific view, if one is needed.
58 ///
59 /// This does **not** force an unconditional render. It only performs the
60 /// (potentially expensive) render work when the view has been marked dirty
61 /// by a prior state change (`goto`, `resize`, `refresh`, `update`, etc.).
62 /// Callers should treat this as a "render-if-dirty" flush point, not a
63 /// "render right now regardless" command.
64 fn request_render(&mut self, id: ViewId);
65 /// Creates new a new (possibly blank) view and returns the ViewId to interact with it
66 fn new_view(&mut self, size: Size<u32>, content: Option<PageType>) -> ViewId;
67 /// Removes desired view
68 fn remove_view(&mut self, id: ViewId);
69 /// Whether a view with this id currently exists.
70 fn has_view(&self, _id: ViewId) -> bool {
71 false
72 }
73
74 /// Resizes webview
75 fn resize(&mut self, size: Size<u32>);
76 /// Set the display scale factor for HiDPI rendering. Default is no-op.
77 fn set_scale_factor(&mut self, _scale: f32) {}
78
79 /// Whether this engine can fetch and render URLs natively.
80 /// Engines that return `false` rely on the webview layer to fetch HTML.
81 fn handles_urls(&self) -> bool {
82 true
83 }
84
85 /// lets the engine handle keyboard events
86 fn handle_keyboard_event(&mut self, id: ViewId, event: keyboard::Event);
87 /// lets the engine handle mouse events
88 fn handle_mouse_event(&mut self, id: ViewId, point: Point, event: mouse::Event);
89 /// Handles scrolling on view
90 fn scroll(&mut self, id: ViewId, delta: mouse::ScrollDelta);
91
92 /// Go to a specific page type
93 fn goto(&mut self, id: ViewId, page_type: PageType);
94 /// Refresh specific view
95 fn refresh(&mut self, id: ViewId);
96 /// Moves forward on view
97 fn go_forward(&mut self, id: ViewId);
98 /// Moves back on view
99 fn go_back(&mut self, id: ViewId);
100
101 /// Gets current url from view
102 fn get_url(&self, id: ViewId) -> String;
103 /// Gets current title from view
104 fn get_title(&self, id: ViewId) -> String;
105 /// Gets current cursor status from view
106 fn get_cursor(&self, id: ViewId) -> Interaction;
107 /// Gets CPU-rendered webview
108 fn get_view(&self, id: ViewId) -> &ImageInfo;
109
110 /// Current vertical scroll offset (logical pixels).
111 fn get_scroll_y(&self, _id: ViewId) -> f32 {
112 0.0
113 }
114
115 /// Total content height (logical pixels). Zero means the engine manages scrolling.
116 fn get_content_height(&self, _id: ViewId) -> f32 {
117 0.0
118 }
119
120 /// Gets the currently selected text from a view, if any.
121 fn get_selected_text(&self, _id: ViewId) -> Option<String> {
122 None
123 }
124
125 /// Selection highlight rectangles for overlay rendering.
126 /// Returns `[x, y, width, height]` in logical coordinates, scroll-adjusted.
127 fn get_selection_rects(&self, _id: ViewId) -> &[[f32; 4]] {
128 &[]
129 }
130
131 /// Take the last anchor click URL from a view, if any.
132 /// Called after mouse events to detect link navigation.
133 fn take_anchor_click(&mut self, _id: ViewId) -> Option<String> {
134 None
135 }
136
137 /// Scroll to a named fragment (e.g. `"section2"` for `#section2`).
138 /// Returns `true` if the fragment was found and the view scrolled.
139 fn scroll_to_fragment(&mut self, _id: ViewId, _fragment: &str) -> bool {
140 false
141 }
142
143 /// Return image URLs discovered during layout that still need fetching.
144 /// Each entry is `(view_id, raw_src, baseurl, redraw_on_ready)` — the
145 /// consumer resolves URLs against baseurl and threads `redraw_on_ready`
146 /// back through `load_image_from_bytes`.
147 fn take_pending_images(&mut self) -> Vec<(ViewId, String, String, bool)> {
148 Vec::new()
149 }
150
151 /// Pre-load a CSS cache into a view's container so `import_css` can
152 /// resolve stylesheets without network access during parsing.
153 fn set_css_cache(&mut self, _id: ViewId, _cache: HashMap<String, String>) {}
154
155 /// Inject fetched image bytes into a view's container, keyed by the
156 /// raw `src` value from the HTML. When `redraw_on_ready` is true, the
157 /// image doesn't affect layout (CSS background or `<img>` with explicit
158 /// dimensions) so `doc.render()` can be skipped — only a redraw is needed.
159 fn load_image_from_bytes(
160 &mut self,
161 _id: ViewId,
162 _url: &str,
163 _bytes: &[u8],
164 _redraw_on_ready: bool,
165 ) {
166 }
167
168 /// Flush all staged images into the document and redraw.
169 /// Called when all in-flight image fetches have completed so the
170 /// full batch is processed in a single redraw.
171 fn flush_staged_images(&mut self, _id: ViewId, _size: Size<u32>) {}
172
173 /// Return all active view IDs.
174 fn view_ids(&self) -> Vec<ViewId> {
175 Vec::new()
176 }
177}