alacritty_terminal/
event.rs

1use std::borrow::Cow;
2use std::fmt::{self, Debug, Formatter};
3use std::sync::Arc;
4
5use crate::term::ClipboardType;
6use crate::vte::ansi::Rgb;
7
8/// Terminal event.
9///
10/// These events instruct the UI over changes that can't be handled by the terminal emulation layer
11/// itself.
12#[derive(Clone)]
13pub enum Event {
14    /// Grid has changed possibly requiring a mouse cursor shape change.
15    MouseCursorDirty,
16
17    /// Window title change.
18    Title(String),
19
20    /// Reset to the default window title.
21    ResetTitle,
22
23    /// Request to store a text string in the clipboard.
24    ClipboardStore(ClipboardType, String),
25
26    /// Request to write the contents of the clipboard to the PTY.
27    ///
28    /// The attached function is a formatter which will correctly transform the clipboard content
29    /// into the expected escape sequence format.
30    ClipboardLoad(ClipboardType, Arc<dyn Fn(&str) -> String + Sync + Send + 'static>),
31
32    /// Request to write the RGB value of a color to the PTY.
33    ///
34    /// The attached function is a formatter which will correctly transform the RGB color into the
35    /// expected escape sequence format.
36    ColorRequest(usize, Arc<dyn Fn(Rgb) -> String + Sync + Send + 'static>),
37
38    /// Write some text to the PTY.
39    PtyWrite(String),
40
41    /// Request to write the text area size.
42    TextAreaSizeRequest(Arc<dyn Fn(WindowSize) -> String + Sync + Send + 'static>),
43
44    /// Cursor blinking state has changed.
45    CursorBlinkingChange,
46
47    /// New terminal content available.
48    Wakeup,
49
50    /// Terminal bell ring.
51    Bell,
52
53    /// Shutdown request.
54    Exit,
55
56    /// Child process exited with an error code.
57    ChildExit(i32),
58}
59
60impl Debug for Event {
61    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
62        match self {
63            Event::ClipboardStore(ty, text) => write!(f, "ClipboardStore({ty:?}, {text})"),
64            Event::ClipboardLoad(ty, _) => write!(f, "ClipboardLoad({ty:?})"),
65            Event::TextAreaSizeRequest(_) => write!(f, "TextAreaSizeRequest"),
66            Event::ColorRequest(index, _) => write!(f, "ColorRequest({index})"),
67            Event::PtyWrite(text) => write!(f, "PtyWrite({text})"),
68            Event::Title(title) => write!(f, "Title({title})"),
69            Event::CursorBlinkingChange => write!(f, "CursorBlinkingChange"),
70            Event::MouseCursorDirty => write!(f, "MouseCursorDirty"),
71            Event::ResetTitle => write!(f, "ResetTitle"),
72            Event::Wakeup => write!(f, "Wakeup"),
73            Event::Bell => write!(f, "Bell"),
74            Event::Exit => write!(f, "Exit"),
75            Event::ChildExit(code) => write!(f, "ChildExit({code})"),
76        }
77    }
78}
79
80/// Byte sequences are sent to a `Notify` in response to some events.
81pub trait Notify {
82    /// Notify that an escape sequence should be written to the PTY.
83    ///
84    /// TODO this needs to be able to error somehow.
85    fn notify<B: Into<Cow<'static, [u8]>>>(&self, _: B);
86}
87
88#[derive(Copy, Clone, Debug)]
89pub struct WindowSize {
90    pub num_lines: u16,
91    pub num_cols: u16,
92    pub cell_width: u16,
93    pub cell_height: u16,
94}
95
96/// Types that are interested in when the display is resized.
97pub trait OnResize {
98    fn on_resize(&mut self, window_size: WindowSize);
99}
100
101/// Event Loop for notifying the renderer about terminal events.
102pub trait EventListener {
103    fn send_event(&self, _event: Event) {}
104}
105
106/// Null sink for events.
107pub struct VoidListener;
108
109impl EventListener for VoidListener {}