Skip to content

Commit 80f82e6

Browse files
committed
docs: document required subscription and on_action() for litehtml/blitz engines
1 parent 0ad5201 commit 80f82e6

3 files changed

Lines changed: 46 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
## [Unreleased]
44

5+
### Added
6+
- Doc comments on WebView structs noting the required `Action::Update` subscription
7+
- Improved `on_action()` docs — now states it's required for litehtml/blitz engines
8+
59
### Changed
610
- Reduced hot-path allocations — URL/title strings moved instead of cloned, pixel buffer avoids double copy
11+
- `on_action()` error messages now suggest the fix; doc comments name affected engines
712

813
### Fixed
914
- Widget crash on stale/invalid view index — lookups now return Option with graceful fallback

src/webview/advanced.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,19 @@ pub enum Action {
5151
ImageFetchComplete(ViewId, String, Result<Vec<u8>, String>, bool, u64),
5252
}
5353

54-
/// The Advanced WebView widget that creates and shows webview(s)
54+
/// The Advanced WebView widget that creates and shows webview(s).
55+
///
56+
/// **Important:** You must drive the webview with a periodic
57+
/// [`Action::Update`] / [`Action::UpdateAll`] subscription (e.g. via
58+
/// `iced::time::every`). Without it the webview will never render and the
59+
/// screen stays blank.
60+
///
61+
/// ```rust,ignore
62+
/// fn subscription(&self) -> iced::Subscription<Message> {
63+
/// iced::time::every(std::time::Duration::from_millis(16))
64+
/// .map(|_| Message::WebView(Action::UpdateAll))
65+
/// }
66+
/// ```
5567
pub struct WebView<Engine, Message>
5668
where
5769
Engine: engines::Engine,
@@ -141,9 +153,10 @@ impl<Engine: engines::Engine + Default, Message: Send + Clone + 'static> WebView
141153
self
142154
}
143155

144-
/// Provide a mapper from Action to Message so the webview can spawn async
145-
/// tasks (e.g. URL fetches) that route back through the update loop.
146-
/// Required for URL navigation on engines that don't handle URLs natively.
156+
/// Provide a mapper from [`Action`] to `Message` so the webview can spawn
157+
/// async tasks that route back through the iced update loop. **Required**
158+
/// for litehtml and blitz engines — without it, URL navigation and image
159+
/// loading will not work.
147160
pub fn on_action(mut self, mapper: impl Fn(Action) -> Message + Send + Sync + 'static) -> Self {
148161
self.action_mapper = Some(Arc::new(mapper));
149162
self
@@ -198,11 +211,11 @@ impl<Engine: engines::Engine + Default, Message: Send + Clone + 'static> WebView
198211
move |result| mapper(Action::FetchComplete(id, url_clone, result)),
199212
));
200213
} else {
201-
eprintln!("iced_webview: on_action() mapper required for URL navigation with this engine");
214+
eprintln!("iced_webview: .on_action() is required for URL navigation and image loading when the engine does not handle URLs natively. Call .on_action(Message::YourVariant) on your WebView builder.");
202215
}
203216

204217
#[cfg(not(any(feature = "litehtml", feature = "blitz")))]
205-
eprintln!("iced_webview: on_action() mapper required for URL navigation with this engine");
218+
eprintln!("iced_webview: .on_action() is required for URL navigation and image loading when the engine does not handle URLs natively. Call .on_action(Message::YourVariant) on your WebView builder.");
206219

207220
id
208221
} else {
@@ -245,13 +258,13 @@ impl<Engine: engines::Engine + Default, Message: Send + Clone + 'static> WebView
245258
move |result| mapper(Action::FetchComplete(id, url_str, result)),
246259
));
247260
} else {
248-
eprintln!("iced_webview: on_action() mapper required for URL navigation with this engine");
261+
eprintln!("iced_webview: .on_action() is required for URL navigation and image loading when the engine does not handle URLs natively. Call .on_action(Message::YourVariant) on your WebView builder.");
249262
}
250263
}
251264

252265
#[cfg(not(any(feature = "litehtml", feature = "blitz")))]
253266
if !self.engine.handles_urls() {
254-
eprintln!("iced_webview: on_action() mapper required for URL navigation with this engine");
267+
eprintln!("iced_webview: .on_action() is required for URL navigation and image loading when the engine does not handle URLs natively. Call .on_action(Message::YourVariant) on your WebView builder.");
255268
}
256269

257270
self.engine.request_render(id, self.view_size);

src/webview/basic.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,18 @@ pub enum Action {
5353
ImageFetchComplete(ViewId, String, Result<Vec<u8>, String>, bool, u64),
5454
}
5555

56-
/// The Basic WebView widget that creates and shows webview(s)
56+
/// The Basic WebView widget that creates and shows webview(s).
57+
///
58+
/// **Important:** You must drive the webview with a periodic [`Action::Update`]
59+
/// subscription (e.g. via `iced::time::every`). Without it the webview will
60+
/// never render and the screen stays blank.
61+
///
62+
/// ```rust,ignore
63+
/// fn subscription(&self) -> iced::Subscription<Message> {
64+
/// iced::time::every(std::time::Duration::from_millis(16))
65+
/// .map(|_| Message::WebView(Action::Update))
66+
/// }
67+
/// ```
5768
pub struct WebView<Engine, Message>
5869
where
5970
Engine: engines::Engine,
@@ -165,9 +176,10 @@ impl<Engine: engines::Engine + Default, Message: Send + Clone + 'static> WebView
165176
self
166177
}
167178

168-
/// Provide a mapper from Action to Message so the webview can spawn async
169-
/// tasks (e.g. URL fetches) that route back through the update loop.
170-
/// Required for URL navigation on engines that don't handle URLs natively.
179+
/// Provide a mapper from [`Action`] to `Message` so the webview can spawn
180+
/// async tasks that route back through the iced update loop. **Required**
181+
/// for litehtml and blitz engines — without it, URL navigation and image
182+
/// loading will not work.
171183
pub fn on_action(mut self, mapper: impl Fn(Action) -> Message + Send + Sync + 'static) -> Self {
172184
self.action_mapper = Some(Arc::new(mapper));
173185
self
@@ -264,11 +276,11 @@ impl<Engine: engines::Engine + Default, Message: Send + Clone + 'static> WebView
264276
move |result| mapper(Action::FetchComplete(id, url_clone, result)),
265277
));
266278
} else {
267-
eprintln!("iced_webview: on_action() mapper required for URL navigation with this engine");
279+
eprintln!("iced_webview: .on_action() is required for URL navigation and image loading when the engine does not handle URLs natively. Call .on_action(Message::YourVariant) on your WebView builder.");
268280
}
269281

270282
#[cfg(not(any(feature = "litehtml", feature = "blitz")))]
271-
eprintln!("iced_webview: on_action() mapper required for URL navigation with this engine");
283+
eprintln!("iced_webview: .on_action() is required for URL navigation and image loading when the engine does not handle URLs natively. Call .on_action(Message::YourVariant) on your WebView builder.");
272284
} else {
273285
let id = self
274286
.engine
@@ -314,13 +326,13 @@ impl<Engine: engines::Engine + Default, Message: Send + Clone + 'static> WebView
314326
},
315327
));
316328
} else {
317-
eprintln!("iced_webview: on_action() mapper required for URL navigation with this engine");
329+
eprintln!("iced_webview: .on_action() is required for URL navigation and image loading when the engine does not handle URLs natively. Call .on_action(Message::YourVariant) on your WebView builder.");
318330
}
319331
}
320332

321333
#[cfg(not(any(feature = "litehtml", feature = "blitz")))]
322334
if !self.engine.handles_urls() {
323-
eprintln!("iced_webview: on_action() mapper required for URL navigation with this engine");
335+
eprintln!("iced_webview: .on_action() is required for URL navigation and image loading when the engine does not handle URLs natively. Call .on_action(Message::YourVariant) on your WebView builder.");
324336
}
325337
}
326338
}

0 commit comments

Comments
 (0)