Skip to main content

Vine/Server/Notification/
TerminalLifecycle.rs

1//! Cocoon → `terminal.sendText` / `terminal.show` / `terminal.hide` /
2//! `terminal.dispose` notifications.
3//!
4//! Two concerns per invocation:
5//! 1. Relay `sky://terminal/<suffix>` to the renderer so the xterm panel can
6//!    show/hide / print text / remove itself.
7//! 2. Drive the underlying PTY via `VineHost::SpawnSendTextToTerminal` or
8//!    `VineHost::SpawnDisposeTerminal` so the OS process sees the text /
9//!    receives SIGHUP on dispose. (No-op for embedders without terminal
10//!    support.)
11
12use serde_json::Value;
13
14use crate::{Host::VineHost, dev_log};
15
16pub async fn TerminalLifecycle(Host:&dyn VineHost, MethodName:&str, Parameter:&Value) {
17	let EventName = format!("sky://terminal/{}", &MethodName["terminal.".len()..]);
18
19	Host.EmitToRenderer(&EventName, Parameter.clone());
20
21	// Terminal handles from Cocoon arrive as `"terminal:N"`; strip the
22	// prefix to recover the numeric id the provider expects.
23	let HandleNumeric = Parameter
24		.get("handle")
25		.and_then(|H| H.as_str())
26		.and_then(|S| S.trim_start_matches("terminal:").parse::<u64>().ok());
27
28	if let Some(TerminalId) = HandleNumeric {
29		match MethodName {
30			"terminal.sendText" => {
31				let Text = Parameter.get("text").and_then(|T| T.as_str()).unwrap_or("").to_string();
32
33				dev_log!("grpc", "[TerminalLifecycle] sendText id={} len={}", TerminalId, Text.len());
34
35				Host.SpawnSendTextToTerminal(TerminalId, Text);
36			},
37
38			"terminal.dispose" => {
39				dev_log!("grpc", "[TerminalLifecycle] dispose id={}", TerminalId);
40
41				Host.SpawnDisposeTerminal(TerminalId);
42			},
43
44			_ => {},
45		}
46	}
47}