Skip to main content

Mountain/RPC/CocoonService/Terminal/
ResizeTerminal.rs

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