1
//! Per-channel TUI state: keystroke decoder, input sender and the PTY
2
//! geometry the client announced.
3

            
4
use crate::key_decoder::SharedDecoder;
5
use tokio::sync::mpsc::Sender;
6
use tokio::sync::watch;
7
use tui::transport::{DEFAULT_CELL_PIXELS, RawEvent};
8

            
9
/// Largest terminal a client may announce; the render buffers scale with it.
10
const MAX_COLS: u16 = 500;
11
const MAX_ROWS: u16 = 200;
12

            
13
/// Largest pixel size of one cell; a client-announced size above it is not believed,
14
/// because images are sized as cells times this.
15
const MAX_CELL_PIXELS: u16 = 128;
16

            
17
/// Pulls a client-announced cell count into `1..=max`.
18
70
fn clamp_cells(cells: u32, max: u16) -> u16 {
19
70
    u16::try_from(cells.clamp(1, u32::from(max))).unwrap_or(max)
20
70
}
21

            
22
/// Terminal size in cells and pixels as the client last reported it.
23
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24
pub struct Geometry {
25
    pub cols: u16,
26
    pub rows: u16,
27
    pix_width: u32,
28
    pix_height: u32,
29
}
30

            
31
impl Default for Geometry {
32
2
    fn default() -> Self {
33
2
        Self {
34
2
            cols: 80,
35
2
            rows: 24,
36
2
            pix_width: 0,
37
2
            pix_height: 0,
38
2
        }
39
2
    }
40
}
41

            
42
impl Geometry {
43
35
    fn new(cols: u32, rows: u32, pix_width: u32, pix_height: u32) -> Self {
44
35
        Self {
45
35
            cols: clamp_cells(cols, MAX_COLS),
46
35
            rows: clamp_cells(rows, MAX_ROWS),
47
35
            pix_width,
48
35
            pix_height,
49
35
        }
50
35
    }
51

            
52
    /// The window's pixel size as the client reported it; 0 when it did not.
53
    #[must_use]
54
2
    pub fn pixels(self) -> (u32, u32) {
55
2
        (self.pix_width, self.pix_height)
56
2
    }
57

            
58
    /// Pixel size of one cell; an unknown (0) or implausible dimension gets the 10x20 fallback.
59
    #[must_use]
60
249
    pub fn cell_pixels(self) -> (u16, u16) {
61
498
        let cell = |pixels: u32, cells: u16, fallback: u16| {
62
498
            pixels
63
498
                .checked_div(u32::from(cells))
64
498
                .and_then(|size| u16::try_from(size).ok())
65
496
                .filter(|size| (1..=MAX_CELL_PIXELS).contains(size))
66
498
                .unwrap_or(fallback)
67
498
        };
68
249
        (
69
249
            cell(self.pix_width, self.cols, DEFAULT_CELL_PIXELS.0),
70
249
            cell(self.pix_height, self.rows, DEFAULT_CELL_PIXELS.1),
71
249
        )
72
249
    }
73
}
74

            
75
/// Created in `pty_request`, completed in `shell_request`, dropped in
76
/// `channel_close`.
77
pub struct TuiChannelState {
78
    pub decoder: SharedDecoder,
79
    pub input_tx: Option<Sender<RawEvent>>,
80
    pub term: Option<String>,
81
    geometry: watch::Sender<Geometry>,
82
}
83

            
84
impl TuiChannelState {
85
25
    pub fn new(term: &str, cols: u32, rows: u32, pix_width: u32, pix_height: u32) -> Self {
86
25
        let geometry = Geometry::new(cols, rows, pix_width, pix_height);
87
25
        Self {
88
25
            decoder: SharedDecoder::default(),
89
25
            input_tx: None,
90
25
            term: Some(term.to_string()),
91
25
            geometry: watch::channel(geometry).0,
92
25
        }
93
25
    }
94

            
95
9
    pub fn geometry(&self) -> Geometry {
96
9
        *self.geometry.borrow()
97
9
    }
98

            
99
20
    pub fn subscribe(&self) -> watch::Receiver<Geometry> {
100
20
        self.geometry.subscribe()
101
20
    }
102

            
103
10
    pub fn resize(&self, cols: u32, rows: u32, pix_width: u32, pix_height: u32) -> Geometry {
104
10
        let resized = Geometry::new(cols, rows, pix_width, pix_height);
105
10
        self.geometry.send_replace(resized);
106
10
        resized
107
10
    }
108
}