terminal_element.rs

  1use alacritty_terminal::{
  2    grid::{Dimensions, GridIterator, Indexed, Scroll},
  3    index::{Column as GridCol, Line as GridLine, Point, Side},
  4    selection::{Selection, SelectionRange, SelectionType},
  5    sync::FairMutex,
  6    term::{
  7        cell::{Cell, Flags},
  8        SizeInfo,
  9    },
 10    Term,
 11};
 12use editor::{Cursor, CursorShape, HighlightedRange, HighlightedRangeLine};
 13use gpui::{
 14    color::Color,
 15    elements::*,
 16    fonts::{TextStyle, Underline},
 17    geometry::{
 18        rect::RectF,
 19        vector::{vec2f, Vector2F},
 20    },
 21    json::json,
 22    text_layout::{Line, RunStyle},
 23    Event, FontCache, KeyDownEvent, MouseRegion, PaintContext, Quad, ScrollWheelEvent,
 24    SizeConstraint, TextLayoutCache, WeakModelHandle,
 25};
 26use itertools::Itertools;
 27use ordered_float::OrderedFloat;
 28use settings::Settings;
 29use theme::TerminalStyle;
 30use util::ResultExt;
 31
 32use std::{cmp::min, ops::Range, rc::Rc, sync::Arc};
 33use std::{fmt::Debug, ops::Sub};
 34
 35use crate::{color_translation::convert_color, connection::TerminalConnection, ZedListener};
 36
 37///Scrolling is unbearably sluggish by default. Alacritty supports a configurable
 38///Scroll multiplier that is set to 3 by default. This will be removed when I
 39///Implement scroll bars.
 40const ALACRITTY_SCROLL_MULTIPLIER: f32 = 3.;
 41
 42///Used to display the grid as passed to Alacritty and the TTY.
 43///Useful for debugging inconsistencies between behavior and display
 44#[cfg(debug_assertions)]
 45const DEBUG_GRID: bool = false;
 46
 47///The GPUI element that paints the terminal.
 48///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection?
 49pub struct TerminalEl {
 50    connection: WeakModelHandle<TerminalConnection>,
 51    view_id: usize,
 52    modal: bool,
 53}
 54
 55///New type pattern so I don't mix these two up
 56struct CellWidth(f32);
 57struct LineHeight(f32);
 58
 59struct LayoutLine {
 60    cells: Vec<LayoutCell>,
 61    highlighted_range: Option<Range<usize>>,
 62}
 63
 64///New type pattern to ensure that we use adjusted mouse positions throughout the code base, rather than
 65struct PaneRelativePos(Vector2F);
 66
 67///Functionally the constructor for the PaneRelativePos type, mutates the mouse_position
 68fn relative_pos(mouse_position: Vector2F, origin: Vector2F) -> PaneRelativePos {
 69    PaneRelativePos(mouse_position.sub(origin)) //Avoid the extra allocation by mutating
 70}
 71
 72#[derive(Clone, Debug, Default)]
 73struct LayoutCell {
 74    point: Point<i32, i32>,
 75    text: Line, //NOTE TO SELF THIS IS BAD PERFORMANCE RN!
 76    background_color: Color,
 77}
 78
 79impl LayoutCell {
 80    fn new(point: Point<i32, i32>, text: Line, background_color: Color) -> LayoutCell {
 81        LayoutCell {
 82            point,
 83            text,
 84            background_color,
 85        }
 86    }
 87}
 88
 89///The information generated during layout that is nescessary for painting
 90pub struct LayoutState {
 91    layout_lines: Vec<LayoutLine>,
 92    line_height: LineHeight,
 93    em_width: CellWidth,
 94    cursor: Option<Cursor>,
 95    background_color: Color,
 96    cur_size: SizeInfo,
 97    terminal: Arc<FairMutex<Term<ZedListener>>>,
 98    selection_color: Color,
 99}
100
101impl TerminalEl {
102    pub fn new(
103        view_id: usize,
104        connection: WeakModelHandle<TerminalConnection>,
105        modal: bool,
106    ) -> TerminalEl {
107        TerminalEl {
108            view_id,
109            connection,
110            modal,
111        }
112    }
113}
114
115impl Element for TerminalEl {
116    type LayoutState = LayoutState;
117    type PaintState = ();
118
119    fn layout(
120        &mut self,
121        constraint: gpui::SizeConstraint,
122        cx: &mut gpui::LayoutContext,
123    ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) {
124        //Settings immutably borrows cx here for the settings and font cache
125        //and we need to modify the cx to resize the terminal. So instead of
126        //storing Settings or the font_cache(), we toss them ASAP and then reborrow later
127        let text_style = make_text_style(cx.font_cache(), cx.global::<Settings>());
128        let line_height = LineHeight(cx.font_cache().line_height(text_style.font_size));
129        let cell_width = CellWidth(
130            cx.font_cache()
131                .em_advance(text_style.font_id, text_style.font_size),
132        );
133        let connection_handle = self.connection.upgrade(cx).unwrap();
134
135        //Tell the view our new size. Requires a mutable borrow of cx and the view
136        let cur_size = make_new_size(constraint, &cell_width, &line_height);
137        //Note that set_size locks and mutates the terminal.
138        connection_handle.update(cx.app, |connection, _| connection.set_size(cur_size));
139
140        let (selection_color, terminal_theme) = {
141            let theme = &(cx.global::<Settings>()).theme;
142            (theme.editor.selection.selection, &theme.terminal)
143        };
144
145        let terminal_mutex = connection_handle.read(cx).term.clone();
146        let term = terminal_mutex.lock();
147        let grid = term.grid();
148        let cursor_point = grid.cursor.point;
149        let cursor_text = grid[cursor_point.line][cursor_point.column].c.to_string();
150
151        let content = term.renderable_content();
152
153        let layout_lines = layout_lines(
154            content.display_iter,
155            &text_style,
156            terminal_theme,
157            cx.text_layout_cache,
158            self.modal,
159            content.selection,
160        );
161
162        let block_text = cx.text_layout_cache.layout_str(
163            &cursor_text,
164            text_style.font_size,
165            &[(
166                cursor_text.len(),
167                RunStyle {
168                    font_id: text_style.font_id,
169                    color: terminal_theme.colors.background,
170                    underline: Default::default(),
171                },
172            )],
173        );
174
175        let cursor = get_cursor_shape(
176            content.cursor.point.line.0 as usize,
177            content.cursor.point.column.0 as usize,
178            content.display_offset,
179            &line_height,
180            &cell_width,
181            cur_size.total_lines(),
182            &block_text,
183        )
184        .map(move |(cursor_position, block_width)| {
185            let block_width = if block_width != 0.0 {
186                block_width
187            } else {
188                cell_width.0
189            };
190
191            Cursor::new(
192                cursor_position,
193                block_width,
194                line_height.0,
195                terminal_theme.colors.cursor,
196                CursorShape::Block,
197                Some(block_text.clone()),
198            )
199        });
200        drop(term);
201
202        let background_color = if self.modal {
203            terminal_theme.colors.modal_background
204        } else {
205            terminal_theme.colors.background
206        };
207
208        (
209            constraint.max,
210            LayoutState {
211                layout_lines,
212                line_height,
213                em_width: cell_width,
214                cursor,
215                cur_size,
216                background_color,
217                terminal: terminal_mutex,
218                selection_color,
219            },
220        )
221    }
222
223    fn paint(
224        &mut self,
225        bounds: gpui::geometry::rect::RectF,
226        visible_bounds: gpui::geometry::rect::RectF,
227        layout: &mut Self::LayoutState,
228        cx: &mut gpui::PaintContext,
229    ) -> Self::PaintState {
230        //Setup element stuff
231        let clip_bounds = Some(visible_bounds);
232
233        cx.paint_layer(clip_bounds, |cx| {
234            let cur_size = layout.cur_size.clone();
235            let origin = bounds.origin() + vec2f(layout.em_width.0, 0.);
236
237            //Elements are ephemeral, only at paint time do we know what could be clicked by a mouse
238            attach_mouse_handlers(
239                origin,
240                cur_size,
241                self.view_id,
242                &layout.terminal,
243                visible_bounds,
244                cx,
245            );
246
247            cx.paint_layer(clip_bounds, |cx| {
248                //Start with a background color
249                cx.scene.push_quad(Quad {
250                    bounds: RectF::new(bounds.origin(), bounds.size()),
251                    background: Some(layout.background_color),
252                    border: Default::default(),
253                    corner_radius: 0.,
254                });
255
256                //Draw cell backgrounds
257                for layout_line in &layout.layout_lines {
258                    for layout_cell in &layout_line.cells {
259                        let position = vec2f(
260                            (origin.x() + layout_cell.point.column as f32 * layout.em_width.0)
261                                .floor(),
262                            origin.y() + layout_cell.point.line as f32 * layout.line_height.0,
263                        );
264                        let size = vec2f(layout.em_width.0.ceil(), layout.line_height.0);
265
266                        cx.scene.push_quad(Quad {
267                            bounds: RectF::new(position, size),
268                            background: Some(layout_cell.background_color),
269                            border: Default::default(),
270                            corner_radius: 0.,
271                        })
272                    }
273                }
274            });
275
276            //Draw Selection
277            cx.paint_layer(clip_bounds, |cx| {
278                let mut highlight_y = None;
279                let highlight_lines = layout
280                    .layout_lines
281                    .iter()
282                    .filter_map(|line| {
283                        if let Some(range) = &line.highlighted_range {
284                            if let None = highlight_y {
285                                highlight_y = Some(
286                                    origin.y()
287                                        + line.cells[0].point.line as f32 * layout.line_height.0,
288                                );
289                            }
290                            let start_x = origin.x()
291                                + line.cells[range.start].point.column as f32 * layout.em_width.0;
292                            let end_x = origin.x()
293                                + line.cells[range.end].point.column as f32 * layout.em_width.0
294                                + layout.em_width.0;
295
296                            return Some(HighlightedRangeLine { start_x, end_x });
297                        } else {
298                            return None;
299                        }
300                    })
301                    .collect::<Vec<HighlightedRangeLine>>();
302
303                if let Some(y) = highlight_y {
304                    let hr = HighlightedRange {
305                        start_y: y, //Need to change this
306                        line_height: layout.line_height.0,
307                        lines: highlight_lines,
308                        color: layout.selection_color,
309                        //Copied from editor. TODO: move to theme or something
310                        corner_radius: 0.15 * layout.line_height.0,
311                    };
312                    hr.paint(bounds, cx.scene);
313                }
314            });
315
316            cx.paint_layer(clip_bounds, |cx| {
317                for layout_line in &layout.layout_lines {
318                    for layout_cell in &layout_line.cells {
319                        let point = layout_cell.point;
320
321                        //Don't actually know the start_x for a line, until here:
322                        let cell_origin = vec2f(
323                            (origin.x() + point.column as f32 * layout.em_width.0).floor(),
324                            origin.y() + point.line as f32 * layout.line_height.0,
325                        );
326
327                        layout_cell.text.paint(
328                            cell_origin,
329                            visible_bounds,
330                            layout.line_height.0,
331                            cx,
332                        );
333                    }
334                }
335            });
336
337            //Draw cursor
338            if let Some(cursor) = &layout.cursor {
339                cx.paint_layer(clip_bounds, |cx| {
340                    cursor.paint(origin, cx);
341                })
342            }
343
344            #[cfg(debug_assertions)]
345            if DEBUG_GRID {
346                cx.paint_layer(clip_bounds, |cx| {
347                    draw_debug_grid(bounds, layout, cx);
348                })
349            }
350        });
351    }
352
353    fn dispatch_event(
354        &mut self,
355        event: &gpui::Event,
356        _bounds: gpui::geometry::rect::RectF,
357        visible_bounds: gpui::geometry::rect::RectF,
358        layout: &mut Self::LayoutState,
359        _paint: &mut Self::PaintState,
360        cx: &mut gpui::EventContext,
361    ) -> bool {
362        match event {
363            Event::ScrollWheel(ScrollWheelEvent {
364                delta, position, ..
365            }) => visible_bounds
366                .contains_point(*position)
367                .then(|| {
368                    let vertical_scroll =
369                        (delta.y() / layout.line_height.0) * ALACRITTY_SCROLL_MULTIPLIER;
370
371                    if let Some(connection) = self.connection.upgrade(cx.app) {
372                        connection.update(cx.app, |connection, _| {
373                            connection
374                                .term
375                                .lock()
376                                .scroll_display(Scroll::Delta(vertical_scroll.round() as i32));
377                        })
378                    }
379                })
380                .is_some(),
381            Event::KeyDown(KeyDownEvent { keystroke, .. }) => {
382                if !cx.is_parent_view_focused() {
383                    return false;
384                }
385
386                self.connection
387                    .upgrade(cx.app)
388                    .map(|connection| {
389                        connection
390                            .update(cx.app, |connection, _| connection.try_keystroke(keystroke))
391                    })
392                    .unwrap_or(false)
393            }
394            _ => false,
395        }
396    }
397
398    fn debug(
399        &self,
400        _bounds: gpui::geometry::rect::RectF,
401        _layout: &Self::LayoutState,
402        _paint: &Self::PaintState,
403        _cx: &gpui::DebugContext,
404    ) -> gpui::serde_json::Value {
405        json!({
406            "type": "TerminalElement",
407        })
408    }
409}
410
411pub fn mouse_to_cell_data(
412    pos: Vector2F,
413    origin: Vector2F,
414    cur_size: SizeInfo,
415    display_offset: usize,
416) -> (Point, alacritty_terminal::index::Direction) {
417    let relative_pos = relative_pos(pos, origin);
418    let point = grid_cell(&relative_pos, cur_size, display_offset);
419    let side = cell_side(&relative_pos, cur_size);
420    (point, side)
421}
422
423///Configures a text style from the current settings.
424fn make_text_style(font_cache: &FontCache, settings: &Settings) -> TextStyle {
425    // Pull the font family from settings properly overriding
426    let family_id = settings
427        .terminal_overrides
428        .font_family
429        .as_ref()
430        .and_then(|family_name| font_cache.load_family(&[family_name]).log_err())
431        .or_else(|| {
432            settings
433                .terminal_defaults
434                .font_family
435                .as_ref()
436                .and_then(|family_name| font_cache.load_family(&[family_name]).log_err())
437        })
438        .unwrap_or(settings.buffer_font_family);
439
440    TextStyle {
441        color: settings.theme.editor.text_color,
442        font_family_id: family_id,
443        font_family_name: font_cache.family_name(family_id).unwrap(),
444        font_id: font_cache
445            .select_font(family_id, &Default::default())
446            .unwrap(),
447        font_size: settings
448            .terminal_overrides
449            .font_size
450            .or(settings.terminal_defaults.font_size)
451            .unwrap_or(settings.buffer_font_size),
452        font_properties: Default::default(),
453        underline: Default::default(),
454    }
455}
456
457///Configures a size info object from the given information.
458fn make_new_size(
459    constraint: SizeConstraint,
460    cell_width: &CellWidth,
461    line_height: &LineHeight,
462) -> SizeInfo {
463    SizeInfo::new(
464        constraint.max.x() - cell_width.0,
465        constraint.max.y(),
466        cell_width.0,
467        line_height.0,
468        0.,
469        0.,
470        false,
471    )
472}
473
474fn layout_lines(
475    grid: GridIterator<Cell>,
476    text_style: &TextStyle,
477    terminal_theme: &TerminalStyle,
478    text_layout_cache: &TextLayoutCache,
479    modal: bool,
480    selection_range: Option<SelectionRange>,
481) -> Vec<LayoutLine> {
482    let lines = grid.group_by(|i| i.point.line);
483    lines
484        .into_iter()
485        .enumerate()
486        .map(|(line_index, (_, line))| {
487            let mut highlighted_range = None;
488            let cells = line
489                .enumerate()
490                .map(|(x_index, indexed_cell)| {
491                    if selection_range
492                        .map(|range| range.contains(indexed_cell.point))
493                        .unwrap_or(false)
494                    {
495                        let mut range = highlighted_range.take().unwrap_or(x_index..x_index);
496                        range.end = range.end.max(x_index);
497                        highlighted_range = Some(range);
498                    }
499
500                    let cell_text = &indexed_cell.c.to_string();
501
502                    let cell_style = cell_style(&indexed_cell, terminal_theme, text_style, modal);
503
504                    //This is where we might be able to get better performance
505                    let layout_cell = text_layout_cache.layout_str(
506                        cell_text,
507                        text_style.font_size,
508                        &[(cell_text.len(), cell_style)],
509                    );
510
511                    LayoutCell::new(
512                        Point::new(line_index as i32, indexed_cell.point.column.0 as i32),
513                        layout_cell,
514                        convert_color(&indexed_cell.bg, &terminal_theme.colors, modal),
515                    )
516                })
517                .collect::<Vec<LayoutCell>>();
518
519            LayoutLine {
520                cells,
521                highlighted_range,
522            }
523        })
524        .collect::<Vec<LayoutLine>>()
525}
526
527// Compute the cursor position and expected block width, may return a zero width if x_for_index returns
528// the same position for sequential indexes. Use em_width instead
529//TODO: This function is messy, too many arguments and too many ifs. Simplify.
530fn get_cursor_shape(
531    line: usize,
532    line_index: usize,
533    display_offset: usize,
534    line_height: &LineHeight,
535    cell_width: &CellWidth,
536    total_lines: usize,
537    text_fragment: &Line,
538) -> Option<(Vector2F, f32)> {
539    let cursor_line = line + display_offset;
540    if cursor_line <= total_lines {
541        let cursor_width = if text_fragment.width() == 0. {
542            cell_width.0
543        } else {
544            text_fragment.width()
545        };
546
547        Some((
548            vec2f(
549                line_index as f32 * cell_width.0,
550                cursor_line as f32 * line_height.0,
551            ),
552            cursor_width,
553        ))
554    } else {
555        None
556    }
557}
558
559///Convert the Alacritty cell styles to GPUI text styles and background color
560fn cell_style(
561    indexed: &Indexed<&Cell>,
562    style: &TerminalStyle,
563    text_style: &TextStyle,
564    modal: bool,
565) -> RunStyle {
566    let flags = indexed.cell.flags;
567    let fg = convert_color(&indexed.cell.fg, &style.colors, modal);
568
569    let underline = flags
570        .contains(Flags::UNDERLINE)
571        .then(|| Underline {
572            color: Some(fg),
573            squiggly: false,
574            thickness: OrderedFloat(1.),
575        })
576        .unwrap_or_default();
577
578    RunStyle {
579        color: fg,
580        font_id: text_style.font_id,
581        underline,
582    }
583}
584
585fn attach_mouse_handlers(
586    origin: Vector2F,
587    cur_size: SizeInfo,
588    view_id: usize,
589    terminal_mutex: &Arc<FairMutex<Term<ZedListener>>>,
590    visible_bounds: RectF,
591    cx: &mut PaintContext,
592) {
593    let click_mutex = terminal_mutex.clone();
594    let drag_mutex = terminal_mutex.clone();
595    let mouse_down_mutex = terminal_mutex.clone();
596
597    cx.scene.push_mouse_region(MouseRegion {
598        view_id,
599        mouse_down: Some(Rc::new(move |pos, _| {
600            let mut term = mouse_down_mutex.lock();
601            let (point, side) = mouse_to_cell_data(
602                pos,
603                origin,
604                cur_size,
605                term.renderable_content().display_offset,
606            );
607            term.selection = Some(Selection::new(SelectionType::Simple, point, side))
608        })),
609        click: Some(Rc::new(move |pos, click_count, cx| {
610            let mut term = click_mutex.lock();
611
612            let (point, side) = mouse_to_cell_data(
613                pos,
614                origin,
615                cur_size,
616                term.renderable_content().display_offset,
617            );
618
619            let selection_type = match click_count {
620                0 => return, //This is a release
621                1 => Some(SelectionType::Simple),
622                2 => Some(SelectionType::Semantic),
623                3 => Some(SelectionType::Lines),
624                _ => None,
625            };
626
627            let selection =
628                selection_type.map(|selection_type| Selection::new(selection_type, point, side));
629
630            term.selection = selection;
631            cx.focus_parent_view();
632            cx.notify();
633        })),
634        bounds: visible_bounds,
635        drag: Some(Rc::new(move |_delta, pos, cx| {
636            let mut term = drag_mutex.lock();
637
638            let (point, side) = mouse_to_cell_data(
639                pos,
640                origin,
641                cur_size,
642                term.renderable_content().display_offset,
643            );
644
645            if let Some(mut selection) = term.selection.take() {
646                selection.update(point, side);
647                term.selection = Some(selection);
648            }
649
650            cx.notify();
651        })),
652        ..Default::default()
653    });
654}
655
656///Copied (with modifications) from alacritty/src/input.rs > Processor::cell_side()
657fn cell_side(pos: &PaneRelativePos, cur_size: SizeInfo) -> Side {
658    let x = pos.0.x() as usize;
659    let cell_x = x.saturating_sub(cur_size.cell_width() as usize) % cur_size.cell_width() as usize;
660    let half_cell_width = (cur_size.cell_width() / 2.0) as usize;
661
662    let additional_padding =
663        (cur_size.width() - cur_size.cell_width() * 2.) % cur_size.cell_width();
664    let end_of_grid = cur_size.width() - cur_size.cell_width() - additional_padding;
665
666    if cell_x > half_cell_width
667            // Edge case when mouse leaves the window.
668            || x as f32 >= end_of_grid
669    {
670        Side::Right
671    } else {
672        Side::Left
673    }
674}
675
676///Copied (with modifications) from alacritty/src/event.rs > Mouse::point()
677///Position is a pane-relative position. That means the top left corner of the mouse
678///Region should be (0,0)
679fn grid_cell(pos: &PaneRelativePos, cur_size: SizeInfo, display_offset: usize) -> Point {
680    let pos = pos.0;
681    let col = pos.x() / cur_size.cell_width(); //TODO: underflow...
682    let col = min(GridCol(col as usize), cur_size.last_column());
683
684    let line = pos.y() / cur_size.cell_height();
685    let line = min(line as i32, cur_size.bottommost_line().0);
686
687    //when clicking, need to ADD to get to the top left cell
688    //e.g. total_lines - viewport_height, THEN subtract display offset
689    //0 -> total_lines - viewport_height - display_offset + mouse_line
690
691    Point::new(GridLine(line - display_offset as i32), col)
692}
693
694///Draws the grid as Alacritty sees it. Useful for checking if there is an inconsistency between
695///Display and conceptual grid.
696#[cfg(debug_assertions)]
697fn draw_debug_grid(bounds: RectF, layout: &mut LayoutState, cx: &mut PaintContext) {
698    let width = layout.cur_size.width();
699    let height = layout.cur_size.height();
700    //Alacritty uses 'as usize', so shall we.
701    for col in 0..(width / layout.em_width.0).round() as usize {
702        cx.scene.push_quad(Quad {
703            bounds: RectF::new(
704                bounds.origin() + vec2f((col + 1) as f32 * layout.em_width.0, 0.),
705                vec2f(1., height),
706            ),
707            background: Some(Color::green()),
708            border: Default::default(),
709            corner_radius: 0.,
710        });
711    }
712    for row in 0..((height / layout.line_height.0) + 1.0).round() as usize {
713        cx.scene.push_quad(Quad {
714            bounds: RectF::new(
715                bounds.origin() + vec2f(layout.em_width.0, row as f32 * layout.line_height.0),
716                vec2f(width, 1.),
717            ),
718            background: Some(Color::green()),
719            border: Default::default(),
720            corner_radius: 0.,
721        });
722    }
723}
724
725mod test {
726
727    #[test]
728    fn test_mouse_to_selection() {
729        let term_width = 100.;
730        let term_height = 200.;
731        let cell_width = 10.;
732        let line_height = 20.;
733        let mouse_pos_x = 100.; //Window relative
734        let mouse_pos_y = 100.; //Window relative
735        let origin_x = 10.;
736        let origin_y = 20.;
737
738        let cur_size = alacritty_terminal::term::SizeInfo::new(
739            term_width,
740            term_height,
741            cell_width,
742            line_height,
743            0.,
744            0.,
745            false,
746        );
747
748        let mouse_pos = gpui::geometry::vector::vec2f(mouse_pos_x, mouse_pos_y);
749        let origin = gpui::geometry::vector::vec2f(origin_x, origin_y); //Position of terminal window, 1 'cell' in
750        let (point, _) =
751            crate::terminal_element::mouse_to_cell_data(mouse_pos, origin, cur_size, 0);
752        assert_eq!(
753            point,
754            alacritty_terminal::index::Point::new(
755                alacritty_terminal::index::Line(((mouse_pos_y - origin_y) / line_height) as i32),
756                alacritty_terminal::index::Column(((mouse_pos_x - origin_x) / cell_width) as usize),
757            )
758        );
759    }
760
761    #[test]
762    fn test_mouse_to_selection_off_edge() {
763        let term_width = 100.;
764        let term_height = 200.;
765        let cell_width = 10.;
766        let line_height = 20.;
767        let mouse_pos_x = 100.; //Window relative
768        let mouse_pos_y = 100.; //Window relative
769        let origin_x = 10.;
770        let origin_y = 20.;
771
772        let cur_size = alacritty_terminal::term::SizeInfo::new(
773            term_width,
774            term_height,
775            cell_width,
776            line_height,
777            0.,
778            0.,
779            false,
780        );
781
782        let mouse_pos = gpui::geometry::vector::vec2f(mouse_pos_x, mouse_pos_y);
783        let origin = gpui::geometry::vector::vec2f(origin_x, origin_y); //Position of terminal window, 1 'cell' in
784        let (point, _) =
785            crate::terminal_element::mouse_to_cell_data(mouse_pos, origin, cur_size, 0);
786        assert_eq!(
787            point,
788            alacritty_terminal::index::Point::new(
789                alacritty_terminal::index::Line(((mouse_pos_y - origin_y) / line_height) as i32),
790                alacritty_terminal::index::Column(((mouse_pos_x - origin_x) / cell_width) as usize),
791            )
792        );
793    }
794}