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