Skip to main content

Mountain/RPC/CocoonService/Terminal/
ResizeTerminal.rs

1
2//! `resize_terminal` gRPC endpoint. Resizes the PTY backing a terminal so
3//! the shell receives SIGWINCH and readline/shells repaint correctly. Also
4//! emits `sky://terminal/resize` so the Sky xterm.js panel reflows its
5//! viewport to match.
6
7use serde_json::json;
8use tauri::Emitter;
9use tonic::{Response, Status};
10use CommonLibrary::{Environment::Requires::Requires, Terminal::TerminalProvider::TerminalProvider};
11
12use crate::{
13	RPC::CocoonService::CocoonServiceImpl,
14	Vine::Generated::{Empty, ResizeTerminalRequest},
15	dev_log,
16};
17
18pub async fn Fn(Service:&CocoonServiceImpl, Request:ResizeTerminalRequest) -> Result<Response<Empty>, Status> {
19	let TerminalId = Request.terminal_id;
20
21	let Cols = Request.cols.max(1) as u16;
22
23	let Rows = Request.rows.max(1) as u16;
24
25	dev_log!(
26		"cocoon",
27		"[CocoonService] resize_terminal id={} cols={} rows={}",
28		TerminalId,
29		Cols,
30		Rows
31	);
32
33	// Resize the actual PTY (sends SIGWINCH so readline/zsh repaint).
34	let Provider:std::sync::Arc<dyn TerminalProvider> = Service.environment.Require();
35
36	if let Err(Error) = Provider.ResizeTerminal(TerminalId.into(), Cols, Rows).await {
37		dev_log!(
38			"cocoon",
39			"warn: [CocoonService] resize_terminal id={} failed: {}",
40			TerminalId,
41			Error
42		);
43	}
44
45	// Notify Sky so xterm.js reflows its viewport.
46	let _ = Service
47		.environment
48		.ApplicationHandle
49		.emit("sky://terminal/resize", json!({ "id": TerminalId, "cols": Cols, "rows": Rows }));
50
51	Ok(Response::new(Empty {}))
52}