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