Skip to main content

standard_plugin/
embed.rs

1//! What the viewer tells a UI plugin about the programs its pixels
2//! surfaces host ([`Surface::bind_embed`]): each arrives as a plugin event
3//! named `system.embed.*`.
4//!
5//! [`Surface::bind_embed`]: crate::surface::Surface::bind_embed
6
7use alloc::string::String;
8
9use serde::Deserialize;
10
11/// The viewer opened the surface's socket and terminal.
12pub const READY_EVENT: &str = "system.embed.ready";
13/// The program joined the socket.
14pub const JOINED_EVENT: &str = "system.embed.joined";
15/// The program named its content.
16pub const TITLE_EVENT: &str = "system.embed.title";
17/// The program copied text; the viewer has written it to its clipboard.
18pub const CLIPBOARD_EVENT: &str = "system.embed.clipboard";
19/// The program left, or never joined before the viewer gave up on it.
20pub const CLOSED_EVENT: &str = "system.embed.closed";
21
22/// One event about an embedded program.
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub enum EmbedEvent {
25    /// Start the program with `PIXEL_EMBED` set to `socket` and
26    /// `PIXEL_TTY` to `tty`.
27    Ready {
28        surface: String,
29        socket: String,
30        tty: String,
31    },
32    Joined {
33        surface: String,
34    },
35    Title {
36        surface: String,
37        text: String,
38    },
39    Clipboard {
40        surface: String,
41        text: String,
42    },
43    Closed {
44        surface: String,
45    },
46}
47
48#[derive(Deserialize)]
49struct Payload {
50    surface: String,
51    #[serde(default)]
52    socket: String,
53    #[serde(default)]
54    tty: String,
55    #[serde(default)]
56    text: String,
57}
58
59impl EmbedEvent {
60    /// The embed event an event carries, if it is one.
61    pub fn from_event(event: &crate::Event) -> Option<Self> {
62        let crate::Event::Plugin { name, payload } = event else {
63            return None;
64        };
65        if !name.starts_with("system.embed.") {
66            return None;
67        }
68        let Payload {
69            surface,
70            socket,
71            tty,
72            text,
73        } = payload.parse().ok()?;
74        Some(match name.as_str() {
75            READY_EVENT => Self::Ready {
76                surface,
77                socket,
78                tty,
79            },
80            JOINED_EVENT => Self::Joined { surface },
81            TITLE_EVENT => Self::Title { surface, text },
82            CLIPBOARD_EVENT => Self::Clipboard { surface, text },
83            CLOSED_EVENT => Self::Closed { surface },
84            _ => return None,
85        })
86    }
87}