Skip to main content

teksilo_webview/
controls.rs

1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4//! Dialogs and pickers a page opens, for the application to present.
5//!
6//! A native engine shows `alert()`, `confirm()`, `prompt()` and a `<select>`'s
7//! list itself. A [`WebViewRendering::Surface`](crate::WebViewRendering)
8//! engine has no window to show them in, so its backend describes each one as
9//! a [`WebViewControl`] and the widget hands it to the
10//! [`WebViewControlPresenter`] installed in app-state. The presenter answers
11//! once, with a [`WebViewControlAnswer`]; with no presenter installed, every
12//! control gets its default answer at once (an alert acknowledged, a confirm
13//! or prompt cancelled, a list left as it was), so no page waits on one.
14//!
15//! `teksilo`'s `install_web_view_default` installs a presenter built on
16//! `MessageBox`, `InputDialog` and `MenuList` when the `widgets` feature is on.
17
18use std::rc::Rc;
19
20use teksilo_canvas::Rect;
21use teksilo_core::widget::EventContext;
22
23/// One option of a `<select>`.
24#[derive(Debug, Clone, PartialEq)]
25pub struct WebViewSelectOption {
26    /// What [`WebViewControlAnswer::Selected`] names it by.
27    pub id: usize,
28    /// The option's text.
29    pub label: String,
30    /// Whether it can be chosen.
31    pub disabled: bool,
32    /// The label of the `<optgroup>` it belongs to, if any.
33    pub group: Option<String>,
34}
35
36/// A dialog or picker a page opened. Its text is the page's, so a presenter
37/// shows it in a way that cannot be mistaken for the application's own UI.
38#[derive(Debug, Clone, PartialEq)]
39#[non_exhaustive]
40pub enum WebViewControl {
41    /// `alert(message)`: acknowledged with [`WebViewControlAnswer::Confirmed`].
42    Alert { message: String },
43    /// `confirm(message)`: [`WebViewControlAnswer::Confirmed`] or
44    /// [`WebViewControlAnswer::Dismissed`].
45    Confirm { message: String },
46    /// `prompt(message, default)`: [`WebViewControlAnswer::Text`] or
47    /// [`WebViewControlAnswer::Dismissed`].
48    Prompt { message: String, default: String },
49    /// A `<select>`'s list, opened at `area` (window-logical): answered with
50    /// [`WebViewControlAnswer::Selected`].
51    Select {
52        options: Vec<WebViewSelectOption>,
53        /// The option chosen now.
54        selected: Option<usize>,
55        area: Rect,
56    },
57}
58
59/// How a presenter answered a [`WebViewControl`].
60#[derive(Debug, Clone, PartialEq, Eq)]
61pub enum WebViewControlAnswer {
62    /// Cancelled, closed, or for a list, left as it was.
63    Dismissed,
64    /// The alert acknowledged, the confirm accepted.
65    Confirmed,
66    /// The prompt's text.
67    Text(String),
68    /// The option chosen in a list, by [`WebViewSelectOption::id`].
69    Selected(usize),
70}
71
72/// The answer callback a presenter calls once.
73pub type WebViewControlReply = Box<dyn FnOnce(WebViewControlAnswer)>;
74
75/// Presents a page's dialogs and pickers. See the [module docs](self).
76pub trait WebViewControlPresenter: 'static {
77    /// Show `control` and call `reply` once with the user's answer.
78    fn present(&self, control: WebViewControl, ctx: &mut EventContext, reply: WebViewControlReply);
79}
80
81/// The presenter, as app-state holds it:
82/// `.app_state(SharedWebViewControlPresenter::new(MyPresenter))`.
83#[derive(Clone)]
84pub struct SharedWebViewControlPresenter(pub Rc<dyn WebViewControlPresenter>);
85
86impl SharedWebViewControlPresenter {
87    /// Wrap `presenter` for app-state.
88    pub fn new(presenter: impl WebViewControlPresenter) -> Self {
89        Self(Rc::new(presenter))
90    }
91}
92
93impl std::fmt::Debug for SharedWebViewControlPresenter {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        f.write_str("SharedWebViewControlPresenter")
96    }
97}