Merge pull request #1518 from zed-industries/add-terminal-focus

Added focus-in and focus-out behavior to terminal
This commit is contained in:
Mikayla Maki 2022-08-15 16:14:05 -07:00 committed by GitHub
commit bb2245ab18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 12 deletions

View file

@ -200,6 +200,7 @@ pub struct TerminalEl {
terminal: WeakModelHandle<Terminal>, terminal: WeakModelHandle<Terminal>,
view: WeakViewHandle<ConnectedView>, view: WeakViewHandle<ConnectedView>,
modal: bool, modal: bool,
focused: bool,
} }
impl TerminalEl { impl TerminalEl {
@ -207,11 +208,13 @@ impl TerminalEl {
view: WeakViewHandle<ConnectedView>, view: WeakViewHandle<ConnectedView>,
terminal: WeakModelHandle<Terminal>, terminal: WeakModelHandle<Terminal>,
modal: bool, modal: bool,
focused: bool,
) -> TerminalEl { ) -> TerminalEl {
TerminalEl { TerminalEl {
view, view,
terminal, terminal,
modal, modal,
focused,
} }
} }
@ -644,6 +647,13 @@ impl Element for TerminalEl {
let cursor_point = DisplayCursor::from(cursor.point, display_offset); let cursor_point = DisplayCursor::from(cursor.point, display_offset);
let cursor_text = { let cursor_text = {
let str_trxt = cursor_text.to_string(); let str_trxt = cursor_text.to_string();
let color = if self.focused {
terminal_theme.colors.background
} else {
terminal_theme.colors.foreground
};
cx.text_layout_cache.layout_str( cx.text_layout_cache.layout_str(
&str_trxt, &str_trxt,
text_style.font_size, text_style.font_size,
@ -651,7 +661,7 @@ impl Element for TerminalEl {
str_trxt.len(), str_trxt.len(),
RunStyle { RunStyle {
font_id: text_style.font_id, font_id: text_style.font_id,
color: terminal_theme.colors.background, color,
underline: Default::default(), underline: Default::default(),
}, },
)], )],
@ -660,12 +670,18 @@ impl Element for TerminalEl {
TerminalEl::shape_cursor(cursor_point, dimensions, &cursor_text).map( TerminalEl::shape_cursor(cursor_point, dimensions, &cursor_text).map(
move |(cursor_position, block_width)| { move |(cursor_position, block_width)| {
let (shape, color) = if self.focused {
(CursorShape::Block, terminal_theme.colors.cursor)
} else {
(CursorShape::Underscore, terminal_theme.colors.foreground)
};
Cursor::new( Cursor::new(
cursor_position, cursor_position,
block_width, block_width,
dimensions.line_height, dimensions.line_height,
terminal_theme.colors.cursor, color,
CursorShape::Block, shape,
Some(cursor_text), Some(cursor_text),
) )
}, },

View file

@ -181,9 +181,15 @@ impl View for ConnectedView {
fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox { fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox {
let terminal_handle = self.terminal.clone().downgrade(); let terminal_handle = self.terminal.clone().downgrade();
let self_id = cx.view_id();
let focused = cx
.focused_view_id(cx.window_id())
.filter(|view_id| *view_id == self_id)
.is_some();
Stack::new() Stack::new()
.with_child( .with_child(
TerminalEl::new(cx.handle(), terminal_handle, self.modal) TerminalEl::new(cx.handle(), terminal_handle, self.modal, focused)
.contained() .contained()
.boxed(), .boxed(),
) )
@ -191,8 +197,15 @@ impl View for ConnectedView {
.boxed() .boxed()
} }
fn on_focus_in(&mut self, _: AnyViewHandle, _cx: &mut ViewContext<Self>) { fn on_focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
self.has_new_content = false; self.has_new_content = false;
self.terminal.read(cx).focus_in();
cx.notify();
}
fn on_focus_out(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
self.terminal.read(cx).focus_out();
cx.notify();
} }
fn selected_text_range(&self, cx: &AppContext) -> Option<std::ops::Range<usize>> { fn selected_text_range(&self, cx: &AppContext) -> Option<std::ops::Range<usize>> {

View file

@ -601,18 +601,23 @@ impl Terminal {
f(content, cursor_text) f(content, cursor_text)
} }
// fn estimate_utilization(last_processed: usize) -> f32 {
// let buffer_utilization = (last_processed as f32 / (READ_BUFFER_SIZE as f32)).clamp(0., 1.);
// //Scale result to bias low, then high
// buffer_utilization * buffer_utilization
// }
///Scroll the terminal ///Scroll the terminal
pub fn scroll(&mut self, scroll: Scroll) { pub fn scroll(&mut self, scroll: Scroll) {
self.events.push(InternalEvent::Scroll(scroll)); self.events.push(InternalEvent::Scroll(scroll));
} }
pub fn focus_in(&self) {
if self.last_mode.contains(TermMode::FOCUS_IN_OUT) {
self.notify_pty("\x1b[I".to_string());
}
}
pub fn focus_out(&self) {
if self.last_mode.contains(TermMode::FOCUS_IN_OUT) {
self.notify_pty("\x1b[O".to_string());
}
}
pub fn click(&mut self, point: Point, side: Direction, clicks: usize) { pub fn click(&mut self, point: Point, side: Direction, clicks: usize) {
let selection_type = match clicks { let selection_type = match clicks {
0 => return, //This is a release 0 => return, //This is a release