connected_view.rs

  1use alacritty_terminal::term::TermMode;
  2use gpui::{
  3    actions, keymap::Keystroke, AppContext, Element, ElementBox, ModelHandle, MutableAppContext,
  4    View, ViewContext,
  5};
  6
  7use crate::{connected_el::TerminalEl, Event, Terminal};
  8
  9///Event to transmit the scroll from the element to the view
 10#[derive(Clone, Debug, PartialEq)]
 11pub struct ScrollTerminal(pub i32);
 12
 13actions!(
 14    terminal,
 15    [Up, Down, CtrlC, Escape, Enter, Clear, Copy, Paste,]
 16);
 17
 18pub fn init(cx: &mut MutableAppContext) {
 19    //Global binding overrrides
 20    cx.add_action(ConnectedView::ctrl_c);
 21    cx.add_action(ConnectedView::up);
 22    cx.add_action(ConnectedView::down);
 23    cx.add_action(ConnectedView::escape);
 24    cx.add_action(ConnectedView::enter);
 25    //Useful terminal views
 26    cx.add_action(ConnectedView::copy);
 27    cx.add_action(ConnectedView::paste);
 28    cx.add_action(ConnectedView::clear);
 29}
 30
 31///A terminal view, maintains the PTY's file handles and communicates with the terminal
 32pub struct ConnectedView {
 33    terminal: ModelHandle<Terminal>,
 34    has_new_content: bool,
 35    //Currently using iTerm bell, show bell emoji in tab until input is received
 36    has_bell: bool,
 37    // Only for styling purposes. Doesn't effect behavior
 38    modal: bool,
 39}
 40
 41impl ConnectedView {
 42    pub fn from_terminal(
 43        terminal: ModelHandle<Terminal>,
 44        modal: bool,
 45        cx: &mut ViewContext<Self>,
 46    ) -> Self {
 47        cx.observe(&terminal, |_, _, cx| cx.notify()).detach();
 48        cx.subscribe(&terminal, |this, _, event, cx| match event {
 49            Event::Wakeup => {
 50                if !cx.is_self_focused() {
 51                    this.has_new_content = true;
 52                    cx.notify();
 53                    cx.emit(Event::Wakeup);
 54                }
 55            }
 56            Event::Bell => {
 57                this.has_bell = true;
 58                cx.emit(Event::Wakeup);
 59            }
 60
 61            _ => cx.emit(*event),
 62        })
 63        .detach();
 64
 65        Self {
 66            terminal,
 67            has_new_content: true,
 68            has_bell: false,
 69            modal,
 70        }
 71    }
 72
 73    pub fn handle(&self) -> ModelHandle<Terminal> {
 74        self.terminal.clone()
 75    }
 76
 77    pub fn has_new_content(&self) -> bool {
 78        self.has_new_content
 79    }
 80
 81    pub fn has_bell(&self) -> bool {
 82        self.has_bell
 83    }
 84
 85    pub fn clear_bel(&mut self, cx: &mut ViewContext<ConnectedView>) {
 86        self.has_bell = false;
 87        cx.emit(Event::Wakeup);
 88    }
 89
 90    fn clear(&mut self, _: &Clear, cx: &mut ViewContext<Self>) {
 91        self.terminal.update(cx, |term, _| term.clear());
 92    }
 93
 94    ///Attempt to paste the clipboard into the terminal
 95    fn copy(&mut self, _: &Copy, cx: &mut ViewContext<Self>) {
 96        self.terminal.update(cx, |term, _| term.copy())
 97    }
 98
 99    ///Attempt to paste the clipboard into the terminal
100    fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) {
101        cx.read_from_clipboard().map(|item| {
102            self.terminal.read(cx).paste(item.text());
103        });
104    }
105
106    ///Synthesize the keyboard event corresponding to 'up'
107    fn up(&mut self, _: &Up, cx: &mut ViewContext<Self>) {
108        self.clear_bel(cx);
109        self.terminal
110            .read(cx)
111            .try_keystroke(&Keystroke::parse("up").unwrap());
112    }
113
114    ///Synthesize the keyboard event corresponding to 'down'
115    fn down(&mut self, _: &Down, cx: &mut ViewContext<Self>) {
116        self.clear_bel(cx);
117        self.terminal
118            .read(cx)
119            .try_keystroke(&Keystroke::parse("down").unwrap());
120    }
121
122    ///Synthesize the keyboard event corresponding to 'ctrl-c'
123    fn ctrl_c(&mut self, _: &CtrlC, cx: &mut ViewContext<Self>) {
124        self.clear_bel(cx);
125        self.terminal
126            .read(cx)
127            .try_keystroke(&Keystroke::parse("ctrl-c").unwrap());
128    }
129
130    ///Synthesize the keyboard event corresponding to 'escape'
131    fn escape(&mut self, _: &Escape, cx: &mut ViewContext<Self>) {
132        self.clear_bel(cx);
133        self.terminal
134            .read(cx)
135            .try_keystroke(&Keystroke::parse("escape").unwrap());
136    }
137
138    ///Synthesize the keyboard event corresponding to 'enter'
139    fn enter(&mut self, _: &Enter, cx: &mut ViewContext<Self>) {
140        self.clear_bel(cx);
141        self.terminal
142            .read(cx)
143            .try_keystroke(&Keystroke::parse("enter").unwrap());
144    }
145}
146
147impl View for ConnectedView {
148    fn ui_name() -> &'static str {
149        "Terminal"
150    }
151
152    fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox {
153        let terminal_handle = self.terminal.clone().downgrade();
154        TerminalEl::new(cx.handle(), terminal_handle, self.modal)
155            .contained()
156            .boxed()
157    }
158
159    fn on_focus(&mut self, _cx: &mut ViewContext<Self>) {
160        self.has_new_content = false;
161    }
162
163    fn selected_text_range(&self, cx: &AppContext) -> Option<std::ops::Range<usize>> {
164        if self
165            .terminal
166            .read(cx)
167            .last_mode
168            .contains(TermMode::ALT_SCREEN)
169        {
170            None
171        } else {
172            Some(0..0)
173        }
174    }
175
176    fn replace_text_in_range(
177        &mut self,
178        _: Option<std::ops::Range<usize>>,
179        text: &str,
180        cx: &mut ViewContext<Self>,
181    ) {
182        self.terminal
183            .update(cx, |terminal, _| terminal.write_to_pty(text.into()));
184    }
185
186    fn keymap_context(&self, _: &gpui::AppContext) -> gpui::keymap::Context {
187        let mut context = Self::default_keymap_context();
188        if self.modal {
189            context.set.insert("ModalTerminal".into());
190        }
191        context
192    }
193}