buffer_element.rs

  1use super::{BufferView, DisplayPoint, SelectAction};
  2use gpui::{
  3    color::{ColorF, ColorU},
  4    geometry::{
  5        rect::RectF,
  6        vector::{vec2f, Vector2F},
  7        PathBuilder,
  8    },
  9    text_layout::{self, TextLayoutCache},
 10    AfterLayoutContext, AppContext, Border, Element, Event, EventContext, FontCache, LayoutContext,
 11    MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewHandle,
 12};
 13use smallvec::SmallVec;
 14use std::cmp::Ordering;
 15use std::{
 16    cmp::{self},
 17    sync::Arc,
 18};
 19
 20pub struct BufferElement {
 21    view: ViewHandle<BufferView>,
 22}
 23
 24impl BufferElement {
 25    pub fn new(view: ViewHandle<BufferView>) -> Self {
 26        Self { view }
 27    }
 28
 29    fn mouse_down(
 30        &self,
 31        position: Vector2F,
 32        cmd: bool,
 33        layout: &mut LayoutState,
 34        paint: &mut PaintState,
 35        ctx: &mut EventContext,
 36    ) -> bool {
 37        if paint.text_bounds.contains_point(position) {
 38            let view = self.view.as_ref(ctx.app);
 39            let position =
 40                paint.point_for_position(view, layout, position, ctx.font_cache, ctx.app);
 41            ctx.dispatch_action("buffer:select", SelectAction::Begin { position, add: cmd });
 42            true
 43        } else {
 44            false
 45        }
 46    }
 47
 48    fn mouse_up(&self, _position: Vector2F, ctx: &mut EventContext) -> bool {
 49        if self.view.as_ref(ctx.app).is_selecting() {
 50            ctx.dispatch_action("buffer:select", SelectAction::End);
 51            true
 52        } else {
 53            false
 54        }
 55    }
 56
 57    fn mouse_dragged(
 58        &self,
 59        position: Vector2F,
 60        layout: &mut LayoutState,
 61        paint: &mut PaintState,
 62        ctx: &mut EventContext,
 63    ) -> bool {
 64        let view = self.view.as_ref(ctx.app);
 65
 66        if view.is_selecting() {
 67            let rect = paint.text_bounds;
 68            let mut scroll_delta = Vector2F::zero();
 69
 70            let vertical_margin = view.line_height(ctx.font_cache).min(rect.height() / 3.0);
 71            let top = rect.origin_y() + vertical_margin;
 72            let bottom = rect.lower_left().y() - vertical_margin;
 73            if position.y() < top {
 74                scroll_delta.set_y(-scale_vertical_mouse_autoscroll_delta(top - position.y()))
 75            }
 76            if position.y() > bottom {
 77                scroll_delta.set_y(scale_vertical_mouse_autoscroll_delta(position.y() - bottom))
 78            }
 79
 80            let horizontal_margin = view.line_height(ctx.font_cache).min(rect.width() / 3.0);
 81            let left = rect.origin_x() + horizontal_margin;
 82            let right = rect.upper_right().x() - horizontal_margin;
 83            if position.x() < left {
 84                scroll_delta.set_x(-scale_horizontal_mouse_autoscroll_delta(
 85                    left - position.x(),
 86                ))
 87            }
 88            if position.x() > right {
 89                scroll_delta.set_x(scale_horizontal_mouse_autoscroll_delta(
 90                    position.x() - right,
 91                ))
 92            }
 93
 94            ctx.dispatch_action(
 95                "buffer:select",
 96                SelectAction::Update {
 97                    position: paint.point_for_position(
 98                        view,
 99                        layout,
100                        position,
101                        ctx.font_cache,
102                        ctx.app,
103                    ),
104                    scroll_position: (view.scroll_position() + scroll_delta).clamp(
105                        Vector2F::zero(),
106                        layout.scroll_max(view, ctx.font_cache, ctx.text_layout_cache, ctx.app),
107                    ),
108                },
109            );
110            true
111        } else {
112            false
113        }
114    }
115
116    fn key_down(&self, chars: &str, ctx: &mut EventContext) -> bool {
117        if self.view.is_focused(ctx.app) {
118            if chars.is_empty() {
119                false
120            } else {
121                if chars.chars().any(|c| c.is_control()) {
122                    false
123                } else {
124                    ctx.dispatch_action("buffer:insert", chars.to_string());
125                    true
126                }
127            }
128        } else {
129            false
130        }
131    }
132
133    fn scroll(
134        &self,
135        position: Vector2F,
136        delta: Vector2F,
137        precise: bool,
138        layout: &mut LayoutState,
139        paint: &mut PaintState,
140        ctx: &mut EventContext,
141    ) -> bool {
142        if !paint.bounds.contains_point(position) {
143            return false;
144        }
145
146        if !precise {
147            todo!("still need to handle non-precise scroll events from a mouse wheel");
148        }
149
150        let view = self.view.as_ref(ctx.app);
151        let font_cache = &ctx.font_cache;
152        let layout_cache = &ctx.text_layout_cache;
153        let max_glyph_width = view.em_width(font_cache);
154        let line_height = view.line_height(font_cache);
155
156        let x = (view.scroll_position().x() * max_glyph_width - delta.x()) / max_glyph_width;
157        let y = (view.scroll_position().y() * line_height - delta.y()) / line_height;
158        let scroll_position = vec2f(x, y).clamp(
159            Vector2F::zero(),
160            layout.scroll_max(view, font_cache, layout_cache, ctx.app),
161        );
162
163        ctx.dispatch_action("buffer:scroll", scroll_position);
164
165        true
166    }
167
168    fn paint_gutter(&mut self, rect: RectF, layout: &LayoutState, ctx: &mut PaintContext) {
169        let view = self.view.as_ref(ctx.app);
170        let line_height = view.line_height(ctx.font_cache);
171        let scroll_top = view.scroll_position().y() * line_height;
172
173        ctx.scene.push_layer(Some(rect));
174        ctx.scene.push_quad(Quad {
175            bounds: rect,
176            background: Some(ColorU::white()),
177            border: Border::new(0., ColorU::transparent_black()),
178            corner_radius: 0.,
179        });
180
181        for (ix, line) in layout.line_number_layouts.iter().enumerate() {
182            let line_origin = rect.origin()
183                + vec2f(
184                    rect.width() - line.width - layout.gutter_padding,
185                    ix as f32 * line_height - (scroll_top % line_height),
186                );
187            line.paint(
188                RectF::new(line_origin, vec2f(line.width, line_height)),
189                &[(0..line.len, ColorU::black())],
190                ctx,
191            );
192        }
193
194        ctx.scene.pop_layer();
195    }
196
197    fn paint_text(&mut self, bounds: RectF, layout: &LayoutState, ctx: &mut PaintContext) {
198        let view = self.view.as_ref(ctx.app);
199        let line_height = view.line_height(ctx.font_cache);
200        let descent = view.font_descent(ctx.font_cache);
201        let start_row = view.scroll_position().y() as u32;
202        let scroll_top = view.scroll_position().y() * line_height;
203        let end_row = ((scroll_top + bounds.height()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
204        let max_glyph_width = view.em_width(ctx.font_cache);
205        let scroll_left = view.scroll_position().x() * max_glyph_width;
206
207        ctx.scene.push_layer(Some(bounds));
208        ctx.scene.push_quad(Quad {
209            bounds,
210            background: Some(ColorU::white()),
211            border: Border::new(0., ColorU::transparent_black()),
212            corner_radius: 0.,
213        });
214
215        // Draw selections
216        let corner_radius = 2.5;
217        let mut cursors = SmallVec::<[Cursor; 32]>::new();
218
219        let content_origin = bounds.origin() + vec2f(-descent, 0.0);
220
221        for selection in view.selections_in_range(
222            DisplayPoint::new(start_row, 0)..DisplayPoint::new(end_row, 0),
223            ctx.app,
224        ) {
225            if selection.start != selection.end {
226                let range_start = cmp::min(selection.start, selection.end);
227                let range_end = cmp::max(selection.start, selection.end);
228                let row_range = if range_end.column() == 0 {
229                    cmp::max(range_start.row(), start_row)..cmp::min(range_end.row(), end_row)
230                } else {
231                    cmp::max(range_start.row(), start_row)..cmp::min(range_end.row() + 1, end_row)
232                };
233
234                let selection = Selection {
235                    line_height,
236                    start_y: content_origin.y() + row_range.start as f32 * line_height - scroll_top,
237                    lines: row_range
238                        .into_iter()
239                        .map(|row| {
240                            let line_layout = &layout.line_layouts[(row - start_row) as usize];
241                            SelectionLine {
242                                start_x: if row == range_start.row() {
243                                    content_origin.x()
244                                        + line_layout.x_for_index(range_start.column() as usize)
245                                        - scroll_left
246                                } else {
247                                    content_origin.x() - scroll_left
248                                },
249                                end_x: if row == range_end.row() {
250                                    content_origin.x()
251                                        + line_layout.x_for_index(range_end.column() as usize)
252                                        - scroll_left
253                                } else {
254                                    content_origin.x() + line_layout.width + corner_radius * 2.0
255                                        - scroll_left
256                                },
257                            }
258                        })
259                        .collect(),
260                };
261
262                selection.paint(ctx.scene);
263            }
264
265            if view.cursors_visible() {
266                let cursor_position = selection.end;
267                if (start_row..end_row).contains(&cursor_position.row()) {
268                    let cursor_row_layout =
269                        &layout.line_layouts[(selection.end.row() - start_row) as usize];
270                    let x = cursor_row_layout.x_for_index(selection.end.column() as usize)
271                        - scroll_left;
272                    let y = selection.end.row() as f32 * line_height - scroll_top;
273                    cursors.push(Cursor {
274                        origin: content_origin + vec2f(x, y),
275                        line_height,
276                    });
277                }
278            }
279        }
280
281        // Draw glyphs
282        for (ix, line) in layout.line_layouts.iter().enumerate() {
283            let row = start_row + ix as u32;
284            let line_origin =
285                content_origin + vec2f(-scroll_left, row as f32 * line_height - scroll_top);
286
287            line.paint(
288                RectF::new(line_origin, vec2f(line.width, line_height)),
289                &[(0..line.len, ColorU::black())],
290                ctx,
291            );
292        }
293
294        ctx.scene.push_layer(Some(bounds));
295        for cursor in cursors {
296            cursor.paint(ctx);
297        }
298        ctx.scene.pop_layer();
299
300        ctx.scene.pop_layer();
301    }
302}
303
304impl Element for BufferElement {
305    type LayoutState = Option<LayoutState>;
306    type PaintState = Option<PaintState>;
307
308    fn layout(
309        &mut self,
310        constraint: SizeConstraint,
311        ctx: &mut LayoutContext,
312    ) -> (Vector2F, Self::LayoutState) {
313        let app = ctx.app;
314        let mut size = constraint.max;
315        if size.y().is_infinite() {
316            let view = self.view.as_ref(app);
317            size.set_y((view.max_point(app).row() + 1) as f32 * view.line_height(ctx.font_cache));
318        }
319        if size.x().is_infinite() {
320            unimplemented!("we don't yet handle an infinite width constraint on buffer elements");
321        }
322
323        let view = self.view.as_ref(app);
324        let font_cache = &ctx.font_cache;
325        let layout_cache = &ctx.text_layout_cache;
326        let line_height = view.line_height(font_cache);
327
328        let gutter_padding;
329        let gutter_width;
330        if view.is_gutter_visible() {
331            gutter_padding = view.em_width(ctx.font_cache);
332            match view.max_line_number_width(ctx.font_cache, ctx.text_layout_cache, app) {
333                Err(error) => {
334                    log::error!("error computing max line number width: {}", error);
335                    return (size, None);
336                }
337                Ok(width) => gutter_width = width + gutter_padding * 2.0,
338            }
339        } else {
340            gutter_padding = 0.0;
341            gutter_width = 0.0
342        };
343
344        let gutter_size = vec2f(gutter_width, size.y());
345        let text_size = size - vec2f(gutter_width, 0.0);
346
347        let autoscroll_horizontally = view.autoscroll_vertically(size.y(), line_height, app);
348
349        let line_number_layouts = if view.is_gutter_visible() {
350            match view.layout_line_numbers(size.y(), ctx.font_cache, ctx.text_layout_cache, app) {
351                Err(error) => {
352                    log::error!("error laying out line numbers: {}", error);
353                    return (size, None);
354                }
355                Ok(layouts) => layouts,
356            }
357        } else {
358            Vec::new()
359        };
360
361        let start_row = view.scroll_position().y() as u32;
362        let scroll_top = view.scroll_position().y() * line_height;
363        let end_row = ((scroll_top + size.y()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
364
365        let mut max_visible_line_width = 0.0;
366        let line_layouts =
367            match view.layout_lines(start_row..end_row, font_cache, layout_cache, app) {
368                Err(error) => {
369                    log::error!("error laying out lines: {}", error);
370                    return (size, None);
371                }
372                Ok(layouts) => {
373                    for line in &layouts {
374                        if line.width > max_visible_line_width {
375                            max_visible_line_width = line.width;
376                        }
377                    }
378
379                    layouts
380                }
381            };
382
383        (
384            size,
385            Some(LayoutState {
386                size,
387                gutter_size,
388                gutter_padding,
389                text_size,
390                line_layouts,
391                line_number_layouts,
392                max_visible_line_width,
393                autoscroll_horizontally,
394            }),
395        )
396    }
397
398    fn after_layout(
399        &mut self,
400        _: Vector2F,
401        layout: &mut Option<LayoutState>,
402        ctx: &mut AfterLayoutContext,
403    ) {
404        if let Some(layout) = layout {
405            let app = ctx.app.downgrade();
406
407            let view = self.view.as_ref(app);
408            view.clamp_scroll_left(
409                layout
410                    .scroll_max(view, ctx.font_cache, ctx.text_layout_cache, app)
411                    .x(),
412            );
413
414            if layout.autoscroll_horizontally {
415                view.autoscroll_horizontally(
416                    view.scroll_position().y() as u32,
417                    layout.text_size.x(),
418                    layout.scroll_width(view, ctx.font_cache, ctx.text_layout_cache, app),
419                    view.em_width(ctx.font_cache),
420                    &layout.line_layouts,
421                    app,
422                );
423            }
424        }
425    }
426
427    fn paint(
428        &mut self,
429        bounds: RectF,
430        layout: &mut Self::LayoutState,
431        ctx: &mut PaintContext,
432    ) -> Self::PaintState {
433        if let Some(layout) = layout {
434            let gutter_bounds = RectF::new(bounds.origin(), layout.gutter_size);
435            let text_bounds = RectF::new(
436                bounds.origin() + vec2f(layout.gutter_size.x(), 0.0),
437                layout.text_size,
438            );
439
440            if self.view.as_ref(ctx.app).is_gutter_visible() {
441                self.paint_gutter(gutter_bounds, layout, ctx);
442            }
443            self.paint_text(text_bounds, layout, ctx);
444
445            Some(PaintState {
446                bounds,
447                text_bounds,
448            })
449        } else {
450            None
451        }
452    }
453
454    fn dispatch_event(
455        &mut self,
456        event: &Event,
457        _: RectF,
458        layout: &mut Self::LayoutState,
459        paint: &mut Self::PaintState,
460        ctx: &mut EventContext,
461    ) -> bool {
462        if let (Some(layout), Some(paint)) = (layout, paint) {
463            match event {
464                Event::LeftMouseDown { position, cmd } => {
465                    self.mouse_down(*position, *cmd, layout, paint, ctx)
466                }
467                Event::LeftMouseUp { position } => self.mouse_up(*position, ctx),
468                Event::LeftMouseDragged { position } => {
469                    self.mouse_dragged(*position, layout, paint, ctx)
470                }
471                Event::ScrollWheel {
472                    position,
473                    delta,
474                    precise,
475                } => self.scroll(*position, *delta, *precise, layout, paint, ctx),
476                Event::KeyDown { chars, .. } => self.key_down(chars, ctx),
477            }
478        } else {
479            false
480        }
481    }
482}
483
484pub struct LayoutState {
485    size: Vector2F,
486    gutter_size: Vector2F,
487    gutter_padding: f32,
488    text_size: Vector2F,
489    line_layouts: Vec<Arc<text_layout::Line>>,
490    line_number_layouts: Vec<Arc<text_layout::Line>>,
491    max_visible_line_width: f32,
492    autoscroll_horizontally: bool,
493}
494
495impl LayoutState {
496    fn scroll_width(
497        &self,
498        view: &BufferView,
499        font_cache: &FontCache,
500        layout_cache: &TextLayoutCache,
501        app: &AppContext,
502    ) -> f32 {
503        let row = view.rightmost_point(app).row();
504        let longest_line_width = view
505            .layout_line(row, font_cache, layout_cache, app)
506            .unwrap()
507            .width;
508        longest_line_width.max(self.max_visible_line_width) + view.em_width(font_cache)
509    }
510
511    fn scroll_max(
512        &self,
513        view: &BufferView,
514        font_cache: &FontCache,
515        layout_cache: &TextLayoutCache,
516        app: &AppContext,
517    ) -> Vector2F {
518        vec2f(
519            ((self.scroll_width(view, font_cache, layout_cache, app) - self.text_size.x())
520                / view.em_width(font_cache))
521            .max(0.0),
522            view.max_point(app).row().saturating_sub(1) as f32,
523        )
524    }
525}
526
527pub struct PaintState {
528    bounds: RectF,
529    text_bounds: RectF,
530}
531
532impl PaintState {
533    fn point_for_position(
534        &self,
535        view: &BufferView,
536        layout: &LayoutState,
537        position: Vector2F,
538        font_cache: &FontCache,
539        app: &AppContext,
540    ) -> DisplayPoint {
541        let scroll_position = view.scroll_position();
542        let position = position - self.text_bounds.origin();
543        let y = position.y().max(0.0).min(layout.size.y());
544        let row = ((y / view.line_height(font_cache)) + scroll_position.y()) as u32;
545        let row = cmp::min(row, view.max_point(app).row());
546        let line = &layout.line_layouts[(row - scroll_position.y() as u32) as usize];
547        let x = position.x() + (scroll_position.x() * view.em_width(font_cache));
548
549        let column = if x >= 0.0 {
550            line.index_for_x(x)
551                .map(|ix| ix as u32)
552                .unwrap_or(view.line_len(row, app).unwrap())
553        } else {
554            0
555        };
556
557        DisplayPoint::new(row, column)
558    }
559}
560
561struct Cursor {
562    origin: Vector2F,
563    line_height: f32,
564}
565
566impl Cursor {
567    fn paint(&self, ctx: &mut PaintContext) {
568        ctx.scene.push_quad(Quad {
569            bounds: RectF::new(self.origin, vec2f(2.0, self.line_height)),
570            background: Some(ColorU::black()),
571            border: Border::new(0., ColorU::black()),
572            corner_radius: 0.,
573        });
574    }
575}
576
577#[derive(Debug)]
578struct Selection {
579    start_y: f32,
580    line_height: f32,
581    lines: Vec<SelectionLine>,
582}
583
584#[derive(Debug)]
585struct SelectionLine {
586    start_x: f32,
587    end_x: f32,
588}
589
590impl Selection {
591    fn paint(&self, scene: &mut Scene) {
592        if self.lines.len() >= 2 && self.lines[0].start_x > self.lines[1].end_x {
593            self.paint_lines(self.start_y, &self.lines[0..1], scene);
594            self.paint_lines(self.start_y + self.line_height, &self.lines[1..], scene);
595        } else {
596            self.paint_lines(self.start_y, &self.lines, scene);
597        }
598    }
599
600    fn paint_lines(&self, start_y: f32, lines: &[SelectionLine], scene: &mut Scene) {
601        if lines.is_empty() {
602            return;
603        }
604
605        let mut path = PathBuilder::new();
606        let corner_radius = 0.25 * self.line_height;
607        let first_line = lines.first().unwrap();
608        let last_line = lines.last().unwrap();
609        let corner = vec2f(first_line.end_x, start_y);
610        let radius_x = vec2f(corner_radius, 0.);
611        let radius_y = vec2f(0., corner_radius);
612
613        path.reset(corner - radius_x);
614        path.curve_to(corner + radius_y, corner);
615
616        let mut iter = lines.iter().enumerate().peekable();
617        while let Some((ix, line)) = iter.next() {
618            let corner = vec2f(line.end_x, start_y + (ix + 1) as f32 * self.line_height);
619
620            if let Some((_, next_line)) = iter.peek() {
621                let next_corner = vec2f(next_line.end_x, corner.y());
622
623                match next_corner.x().partial_cmp(&corner.x()).unwrap() {
624                    Ordering::Equal => {
625                        path.line_to(corner);
626                    }
627                    Ordering::Less => {
628                        path.line_to(corner - radius_y);
629                        path.curve_to(corner - radius_x, corner);
630                        path.line_to(next_corner + radius_x);
631                        path.curve_to(next_corner + radius_y, next_corner);
632                    }
633                    Ordering::Greater => {
634                        path.line_to(corner - radius_y);
635                        path.curve_to(corner + radius_x, corner);
636                        path.line_to(next_corner - radius_x);
637                        path.curve_to(next_corner + radius_y, next_corner);
638                    }
639                }
640            } else {
641                path.line_to(corner - radius_y);
642                path.curve_to(corner - radius_x, corner);
643
644                let corner = vec2f(line.start_x, corner.y());
645                path.line_to(corner + radius_x);
646                path.curve_to(corner - radius_y, corner);
647            }
648        }
649
650        if first_line.start_x > last_line.start_x {
651            let corner = vec2f(last_line.start_x, start_y + self.line_height);
652            path.line_to(corner + radius_y);
653            path.curve_to(corner + radius_x, corner);
654            let corner = vec2f(first_line.start_x, corner.y());
655            path.line_to(corner - radius_x);
656            path.curve_to(corner - radius_y, corner);
657        }
658
659        let corner = vec2f(first_line.start_x, start_y);
660        path.line_to(corner + radius_y);
661        path.curve_to(corner + radius_x, corner);
662        path.line_to(vec2f(first_line.end_x, start_y) - radius_x);
663
664        scene.push_path(path.build(ColorF::new(0.639, 0.839, 1.0, 1.0).to_u8()));
665    }
666}
667
668fn scale_vertical_mouse_autoscroll_delta(delta: f32) -> f32 {
669    delta.powf(1.5) / 100.0
670}
671
672fn scale_horizontal_mouse_autoscroll_delta(delta: f32) -> f32 {
673    delta.powf(1.2) / 300.0
674}