1use super::{DisplayPoint, Editor, EditorMode, Insert, Scroll, Select, SelectPhase, Snapshot};
2use crate::{theme::EditorStyle, time::ReplicaId};
3use gpui::{
4 color::Color,
5 geometry::{
6 rect::RectF,
7 vector::{vec2f, Vector2F},
8 PathBuilder,
9 },
10 json::{self, ToJson},
11 text_layout::{self, TextLayoutCache},
12 AppContext, Axis, Border, Element, Event, EventContext, FontCache, LayoutContext,
13 MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
14};
15use json::json;
16use smallvec::SmallVec;
17use std::{
18 cmp::{self, Ordering},
19 collections::{BTreeMap, HashMap},
20 ops::Range,
21};
22
23pub struct EditorElement {
24 view: WeakViewHandle<Editor>,
25 style: EditorStyle,
26}
27
28impl EditorElement {
29 pub fn new(view: WeakViewHandle<Editor>, style: EditorStyle) -> Self {
30 Self { view, style }
31 }
32
33 fn view<'a>(&self, cx: &'a AppContext) -> &'a Editor {
34 self.view.upgrade(cx).unwrap().read(cx)
35 }
36
37 fn update_view<F, T>(&self, cx: &mut MutableAppContext, f: F) -> T
38 where
39 F: FnOnce(&mut Editor, &mut ViewContext<Editor>) -> T,
40 {
41 self.view.upgrade(cx).unwrap().update(cx, f)
42 }
43
44 fn snapshot(&self, cx: &mut MutableAppContext) -> Snapshot {
45 self.update_view(cx, |view, cx| view.snapshot(cx))
46 }
47
48 fn mouse_down(
49 &self,
50 position: Vector2F,
51 cmd: bool,
52 layout: &mut LayoutState,
53 paint: &mut PaintState,
54 cx: &mut EventContext,
55 ) -> bool {
56 if paint.text_bounds.contains_point(position) {
57 let snapshot = self.snapshot(cx.app);
58 let position = paint.point_for_position(&snapshot, layout, position);
59 cx.dispatch_action(Select(SelectPhase::Begin { position, add: cmd }));
60 true
61 } else {
62 false
63 }
64 }
65
66 fn mouse_up(&self, _position: Vector2F, cx: &mut EventContext) -> bool {
67 if self.view(cx.app.as_ref()).is_selecting() {
68 cx.dispatch_action(Select(SelectPhase::End));
69 true
70 } else {
71 false
72 }
73 }
74
75 fn mouse_dragged(
76 &self,
77 position: Vector2F,
78 layout: &mut LayoutState,
79 paint: &mut PaintState,
80 cx: &mut EventContext,
81 ) -> bool {
82 let view = self.view(cx.app.as_ref());
83
84 if view.is_selecting() {
85 let rect = paint.text_bounds;
86 let mut scroll_delta = Vector2F::zero();
87
88 let vertical_margin = layout.line_height.min(rect.height() / 3.0);
89 let top = rect.origin_y() + vertical_margin;
90 let bottom = rect.lower_left().y() - vertical_margin;
91 if position.y() < top {
92 scroll_delta.set_y(-scale_vertical_mouse_autoscroll_delta(top - position.y()))
93 }
94 if position.y() > bottom {
95 scroll_delta.set_y(scale_vertical_mouse_autoscroll_delta(position.y() - bottom))
96 }
97
98 let horizontal_margin = layout.line_height.min(rect.width() / 3.0);
99 let left = rect.origin_x() + horizontal_margin;
100 let right = rect.upper_right().x() - horizontal_margin;
101 if position.x() < left {
102 scroll_delta.set_x(-scale_horizontal_mouse_autoscroll_delta(
103 left - position.x(),
104 ))
105 }
106 if position.x() > right {
107 scroll_delta.set_x(scale_horizontal_mouse_autoscroll_delta(
108 position.x() - right,
109 ))
110 }
111
112 let font_cache = cx.font_cache.clone();
113 let text_layout_cache = cx.text_layout_cache.clone();
114 let snapshot = self.snapshot(cx.app);
115 let position = paint.point_for_position(&snapshot, layout, position);
116
117 cx.dispatch_action(Select(SelectPhase::Update {
118 position,
119 scroll_position: (snapshot.scroll_position() + scroll_delta).clamp(
120 Vector2F::zero(),
121 layout.scroll_max(&font_cache, &text_layout_cache),
122 ),
123 }));
124 true
125 } else {
126 false
127 }
128 }
129
130 fn key_down(&self, chars: &str, cx: &mut EventContext) -> bool {
131 let view = self.view.upgrade(cx.app).unwrap();
132
133 if view.is_focused(cx.app) {
134 if chars.is_empty() {
135 false
136 } else {
137 if chars.chars().any(|c| c.is_control()) {
138 false
139 } else {
140 cx.dispatch_action(Insert(chars.to_string()));
141 true
142 }
143 }
144 } else {
145 false
146 }
147 }
148
149 fn scroll(
150 &self,
151 position: Vector2F,
152 mut delta: Vector2F,
153 precise: bool,
154 layout: &mut LayoutState,
155 paint: &mut PaintState,
156 cx: &mut EventContext,
157 ) -> bool {
158 if !paint.bounds.contains_point(position) {
159 return false;
160 }
161
162 let snapshot = self.snapshot(cx.app);
163 let font_cache = &cx.font_cache;
164 let layout_cache = &cx.text_layout_cache;
165 let max_glyph_width = layout.em_width;
166 if !precise {
167 delta *= vec2f(max_glyph_width, layout.line_height);
168 }
169
170 let scroll_position = snapshot.scroll_position();
171 let x = (scroll_position.x() * max_glyph_width - delta.x()) / max_glyph_width;
172 let y = (scroll_position.y() * layout.line_height - delta.y()) / layout.line_height;
173 let scroll_position = vec2f(x, y).clamp(
174 Vector2F::zero(),
175 layout.scroll_max(font_cache, layout_cache),
176 );
177
178 cx.dispatch_action(Scroll(scroll_position));
179
180 true
181 }
182
183 fn paint_background(
184 &self,
185 gutter_bounds: RectF,
186 text_bounds: RectF,
187 layout: &LayoutState,
188 cx: &mut PaintContext,
189 ) {
190 let bounds = gutter_bounds.union_rect(text_bounds);
191 let scroll_top = layout.snapshot.scroll_position().y() * layout.line_height;
192 let editor = self.view(cx.app);
193 cx.scene.push_quad(Quad {
194 bounds: gutter_bounds,
195 background: Some(self.style.gutter_background),
196 border: Border::new(0., Color::transparent_black()),
197 corner_radius: 0.,
198 });
199 cx.scene.push_quad(Quad {
200 bounds: text_bounds,
201 background: Some(self.style.background),
202 border: Border::new(0., Color::transparent_black()),
203 corner_radius: 0.,
204 });
205
206 if let EditorMode::Full = editor.mode {
207 let mut active_rows = layout.active_rows.iter().peekable();
208 while let Some((start_row, contains_non_empty_selection)) = active_rows.next() {
209 let mut end_row = *start_row;
210 while active_rows.peek().map_or(false, |r| {
211 *r.0 == end_row + 1 && r.1 == contains_non_empty_selection
212 }) {
213 active_rows.next().unwrap();
214 end_row += 1;
215 }
216
217 if !contains_non_empty_selection {
218 let origin = vec2f(
219 bounds.origin_x(),
220 bounds.origin_y() + (layout.line_height * *start_row as f32) - scroll_top,
221 );
222 let size = vec2f(
223 bounds.width(),
224 layout.line_height * (end_row - start_row + 1) as f32,
225 );
226 cx.scene.push_quad(Quad {
227 bounds: RectF::new(origin, size),
228 background: Some(self.style.active_line_background),
229 border: Border::default(),
230 corner_radius: 0.,
231 });
232 }
233 }
234 }
235 }
236
237 fn paint_gutter(
238 &mut self,
239 bounds: RectF,
240 visible_bounds: RectF,
241 layout: &LayoutState,
242 cx: &mut PaintContext,
243 ) {
244 let scroll_top = layout.snapshot.scroll_position().y() * layout.line_height;
245 for (ix, line) in layout.line_number_layouts.iter().enumerate() {
246 if let Some(line) = line {
247 let line_origin = bounds.origin()
248 + vec2f(
249 bounds.width() - line.width() - layout.gutter_padding,
250 ix as f32 * layout.line_height - (scroll_top % layout.line_height),
251 );
252 line.paint(line_origin, visible_bounds, layout.line_height, cx);
253 }
254 }
255 }
256
257 fn paint_text(
258 &mut self,
259 bounds: RectF,
260 visible_bounds: RectF,
261 layout: &LayoutState,
262 cx: &mut PaintContext,
263 ) {
264 let view = self.view(cx.app);
265 let settings = self.view(cx.app).settings.borrow();
266 let theme = &settings.theme.editor;
267 let scroll_position = layout.snapshot.scroll_position();
268 let start_row = scroll_position.y() as u32;
269 let scroll_top = scroll_position.y() * layout.line_height;
270 let end_row = ((scroll_top + bounds.height()) / layout.line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
271 let max_glyph_width = layout.em_width;
272 let scroll_left = scroll_position.x() * max_glyph_width;
273
274 cx.scene.push_layer(Some(bounds));
275
276 // Draw selections
277 let corner_radius = 2.5;
278 let mut cursors = SmallVec::<[Cursor; 32]>::new();
279
280 let content_origin = bounds.origin() + layout.text_offset;
281
282 for (replica_id, selections) in &layout.selections {
283 let style_ix = *replica_id as usize % (theme.guest_selections.len() + 1);
284 let style = if style_ix == 0 {
285 &theme.selection
286 } else {
287 &theme.guest_selections[style_ix - 1]
288 };
289
290 for selection in selections {
291 if selection.start != selection.end {
292 let range_start = cmp::min(selection.start, selection.end);
293 let range_end = cmp::max(selection.start, selection.end);
294 let row_range = if range_end.column() == 0 {
295 cmp::max(range_start.row(), start_row)..cmp::min(range_end.row(), end_row)
296 } else {
297 cmp::max(range_start.row(), start_row)
298 ..cmp::min(range_end.row() + 1, end_row)
299 };
300
301 let selection = Selection {
302 color: style.selection,
303 line_height: layout.line_height,
304 start_y: content_origin.y() + row_range.start as f32 * layout.line_height
305 - scroll_top,
306 lines: row_range
307 .into_iter()
308 .map(|row| {
309 let line_layout = &layout.line_layouts[(row - start_row) as usize];
310 SelectionLine {
311 start_x: if row == range_start.row() {
312 content_origin.x()
313 + line_layout.x_for_index(range_start.column() as usize)
314 - scroll_left
315 } else {
316 content_origin.x() - scroll_left
317 },
318 end_x: if row == range_end.row() {
319 content_origin.x()
320 + line_layout.x_for_index(range_end.column() as usize)
321 - scroll_left
322 } else {
323 content_origin.x()
324 + line_layout.width()
325 + corner_radius * 2.0
326 - scroll_left
327 },
328 }
329 })
330 .collect(),
331 };
332
333 selection.paint(bounds, cx.scene);
334 }
335
336 if view.cursors_visible() {
337 let cursor_position = selection.end;
338 if (start_row..end_row).contains(&cursor_position.row()) {
339 let cursor_row_layout =
340 &layout.line_layouts[(selection.end.row() - start_row) as usize];
341 let x = cursor_row_layout.x_for_index(selection.end.column() as usize)
342 - scroll_left;
343 let y = selection.end.row() as f32 * layout.line_height - scroll_top;
344 cursors.push(Cursor {
345 color: style.cursor,
346 origin: content_origin + vec2f(x, y),
347 line_height: layout.line_height,
348 });
349 }
350 }
351 }
352 }
353
354 if let Some(visible_text_bounds) = bounds.intersection(visible_bounds) {
355 // Draw glyphs
356 for (ix, line) in layout.line_layouts.iter().enumerate() {
357 let row = start_row + ix as u32;
358 line.paint(
359 content_origin
360 + vec2f(-scroll_left, row as f32 * layout.line_height - scroll_top),
361 visible_text_bounds,
362 layout.line_height,
363 cx,
364 );
365 }
366 }
367
368 cx.scene.push_layer(Some(bounds));
369 for cursor in cursors {
370 cursor.paint(cx);
371 }
372 cx.scene.pop_layer();
373
374 cx.scene.pop_layer();
375 }
376}
377
378impl Element for EditorElement {
379 type LayoutState = Option<LayoutState>;
380 type PaintState = Option<PaintState>;
381
382 fn layout(
383 &mut self,
384 constraint: SizeConstraint,
385 cx: &mut LayoutContext,
386 ) -> (Vector2F, Self::LayoutState) {
387 let mut size = constraint.max;
388 if size.x().is_infinite() {
389 unimplemented!("we don't yet handle an infinite width constraint on buffer elements");
390 }
391
392 let font_cache = &cx.font_cache;
393 let layout_cache = &cx.text_layout_cache;
394 let snapshot = self.snapshot(cx.app);
395 let line_height = snapshot.line_height(font_cache);
396
397 let gutter_padding;
398 let gutter_width;
399 if snapshot.mode == EditorMode::Full {
400 gutter_padding = snapshot.em_width(cx.font_cache);
401 match snapshot.max_line_number_width(cx.font_cache, cx.text_layout_cache) {
402 Err(error) => {
403 log::error!("error computing max line number width: {}", error);
404 return (size, None);
405 }
406 Ok(width) => gutter_width = width + gutter_padding * 2.0,
407 }
408 } else {
409 gutter_padding = 0.0;
410 gutter_width = 0.0
411 };
412
413 let text_width = size.x() - gutter_width;
414 let text_offset = vec2f(-snapshot.font_descent(cx.font_cache), 0.);
415 let em_width = snapshot.em_width(font_cache);
416 let overscroll = vec2f(em_width, 0.);
417 let wrap_width = text_width - text_offset.x() - overscroll.x() - em_width;
418 let snapshot = self.update_view(cx.app, |view, cx| {
419 if view.set_wrap_width(wrap_width, cx) {
420 view.snapshot(cx)
421 } else {
422 snapshot
423 }
424 });
425
426 let scroll_height = (snapshot.max_point().row() + 1) as f32 * line_height;
427 if let EditorMode::AutoHeight { max_lines } = snapshot.mode {
428 size.set_y(
429 scroll_height
430 .min(constraint.max_along(Axis::Vertical))
431 .max(constraint.min_along(Axis::Vertical))
432 .min(line_height * max_lines as f32),
433 )
434 } else if size.y().is_infinite() {
435 size.set_y(scroll_height);
436 }
437 let gutter_size = vec2f(gutter_width, size.y());
438 let text_size = vec2f(text_width, size.y());
439
440 let (autoscroll_horizontally, mut snapshot) = self.update_view(cx.app, |view, cx| {
441 let autoscroll_horizontally = view.autoscroll_vertically(size.y(), line_height, cx);
442 let snapshot = view.snapshot(cx);
443 (autoscroll_horizontally, snapshot)
444 });
445
446 let scroll_position = snapshot.scroll_position();
447 let start_row = scroll_position.y() as u32;
448 let scroll_top = scroll_position.y() * line_height;
449 let end_row = ((scroll_top + size.y()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
450
451 let mut selections = HashMap::new();
452 let mut active_rows = BTreeMap::new();
453 self.update_view(cx.app, |view, cx| {
454 for selection_set_id in view.active_selection_sets(cx).collect::<Vec<_>>() {
455 let mut set = Vec::new();
456 for selection in view.selections_in_range(
457 selection_set_id,
458 DisplayPoint::new(start_row, 0)..DisplayPoint::new(end_row, 0),
459 cx,
460 ) {
461 set.push(selection.clone());
462 if selection_set_id == view.selection_set_id {
463 let is_empty = selection.start == selection.end;
464 let mut selection_start;
465 let mut selection_end;
466 if selection.start < selection.end {
467 selection_start = selection.start;
468 selection_end = selection.end;
469 } else {
470 selection_start = selection.end;
471 selection_end = selection.start;
472 };
473 selection_start = snapshot.prev_row_boundary(selection_start).0;
474 selection_end = snapshot.next_row_boundary(selection_end).0;
475 for row in cmp::max(selection_start.row(), start_row)
476 ..=cmp::min(selection_end.row(), end_row)
477 {
478 let contains_non_empty_selection =
479 active_rows.entry(row).or_insert(!is_empty);
480 *contains_non_empty_selection |= !is_empty;
481 }
482 }
483 }
484
485 selections.insert(selection_set_id.replica_id, set);
486 }
487 });
488
489 let line_number_layouts = if snapshot.mode == EditorMode::Full {
490 let settings = self
491 .view
492 .upgrade(cx.app)
493 .unwrap()
494 .read(cx.app)
495 .settings
496 .borrow();
497 match snapshot.layout_line_numbers(
498 start_row..end_row,
499 &active_rows,
500 cx.font_cache,
501 cx.text_layout_cache,
502 &settings.theme,
503 ) {
504 Err(error) => {
505 log::error!("error laying out line numbers: {}", error);
506 return (size, None);
507 }
508 Ok(layouts) => layouts,
509 }
510 } else {
511 Vec::new()
512 };
513
514 let mut max_visible_line_width = 0.0;
515 let line_layouts = match snapshot.layout_lines(
516 start_row..end_row,
517 &self.style,
518 font_cache,
519 layout_cache,
520 ) {
521 Err(error) => {
522 log::error!("error laying out lines: {}", error);
523 return (size, None);
524 }
525 Ok(layouts) => {
526 for line in &layouts {
527 if line.width() > max_visible_line_width {
528 max_visible_line_width = line.width();
529 }
530 }
531
532 layouts
533 }
534 };
535
536 let mut layout = LayoutState {
537 size,
538 gutter_size,
539 gutter_padding,
540 text_size,
541 overscroll,
542 text_offset,
543 snapshot,
544 active_rows,
545 line_layouts,
546 line_number_layouts,
547 line_height,
548 em_width,
549 selections,
550 max_visible_line_width,
551 };
552
553 self.update_view(cx.app, |view, cx| {
554 let clamped = view.clamp_scroll_left(layout.scroll_max(font_cache, layout_cache).x());
555 let autoscrolled;
556 if autoscroll_horizontally {
557 autoscrolled = view.autoscroll_horizontally(
558 start_row,
559 layout.text_size.x(),
560 layout.scroll_width(font_cache, layout_cache),
561 layout.snapshot.em_width(font_cache),
562 &layout.line_layouts,
563 cx,
564 );
565 } else {
566 autoscrolled = false;
567 }
568
569 if clamped || autoscrolled {
570 layout.snapshot = view.snapshot(cx);
571 }
572 });
573
574 (size, Some(layout))
575 }
576
577 fn paint(
578 &mut self,
579 bounds: RectF,
580 visible_bounds: RectF,
581 layout: &mut Self::LayoutState,
582 cx: &mut PaintContext,
583 ) -> Self::PaintState {
584 if let Some(layout) = layout {
585 cx.scene.push_layer(Some(bounds));
586
587 let gutter_bounds = RectF::new(bounds.origin(), layout.gutter_size);
588 let text_bounds = RectF::new(
589 bounds.origin() + vec2f(layout.gutter_size.x(), 0.0),
590 layout.text_size,
591 );
592
593 self.paint_background(gutter_bounds, text_bounds, layout, cx);
594 if layout.gutter_size.x() > 0. {
595 self.paint_gutter(gutter_bounds, visible_bounds, layout, cx);
596 }
597 self.paint_text(text_bounds, visible_bounds, layout, cx);
598
599 cx.scene.pop_layer();
600
601 Some(PaintState {
602 bounds,
603 text_bounds,
604 })
605 } else {
606 None
607 }
608 }
609
610 fn dispatch_event(
611 &mut self,
612 event: &Event,
613 _: RectF,
614 layout: &mut Self::LayoutState,
615 paint: &mut Self::PaintState,
616 cx: &mut EventContext,
617 ) -> bool {
618 if let (Some(layout), Some(paint)) = (layout, paint) {
619 match event {
620 Event::LeftMouseDown { position, cmd } => {
621 self.mouse_down(*position, *cmd, layout, paint, cx)
622 }
623 Event::LeftMouseUp { position } => self.mouse_up(*position, cx),
624 Event::LeftMouseDragged { position } => {
625 self.mouse_dragged(*position, layout, paint, cx)
626 }
627 Event::ScrollWheel {
628 position,
629 delta,
630 precise,
631 } => self.scroll(*position, *delta, *precise, layout, paint, cx),
632 Event::KeyDown { chars, .. } => self.key_down(chars, cx),
633 _ => false,
634 }
635 } else {
636 false
637 }
638 }
639
640 fn debug(
641 &self,
642 bounds: RectF,
643 _: &Self::LayoutState,
644 _: &Self::PaintState,
645 _: &gpui::DebugContext,
646 ) -> json::Value {
647 json!({
648 "type": "BufferElement",
649 "bounds": bounds.to_json()
650 })
651 }
652}
653
654pub struct LayoutState {
655 size: Vector2F,
656 gutter_size: Vector2F,
657 gutter_padding: f32,
658 text_size: Vector2F,
659 snapshot: Snapshot,
660 active_rows: BTreeMap<u32, bool>,
661 line_layouts: Vec<text_layout::Line>,
662 line_number_layouts: Vec<Option<text_layout::Line>>,
663 line_height: f32,
664 em_width: f32,
665 selections: HashMap<ReplicaId, Vec<Range<DisplayPoint>>>,
666 overscroll: Vector2F,
667 text_offset: Vector2F,
668 max_visible_line_width: f32,
669}
670
671impl LayoutState {
672 fn scroll_width(&self, font_cache: &FontCache, layout_cache: &TextLayoutCache) -> f32 {
673 let row = self.snapshot.longest_row();
674 let longest_line_width = self
675 .snapshot
676 .layout_line(row, font_cache, layout_cache)
677 .unwrap()
678 .width();
679 longest_line_width.max(self.max_visible_line_width) + self.overscroll.x()
680 }
681
682 fn scroll_max(&self, font_cache: &FontCache, layout_cache: &TextLayoutCache) -> Vector2F {
683 let text_width = self.text_size.x();
684 let scroll_width = self.scroll_width(font_cache, layout_cache);
685 let em_width = self.snapshot.em_width(font_cache);
686 let max_row = self.snapshot.max_point().row();
687
688 vec2f(
689 ((scroll_width - text_width) / em_width).max(0.0),
690 max_row.saturating_sub(1) as f32,
691 )
692 }
693}
694
695pub struct PaintState {
696 bounds: RectF,
697 text_bounds: RectF,
698}
699
700impl PaintState {
701 fn point_for_position(
702 &self,
703 snapshot: &Snapshot,
704 layout: &LayoutState,
705 position: Vector2F,
706 ) -> DisplayPoint {
707 let scroll_position = snapshot.scroll_position();
708 let position = position - self.text_bounds.origin();
709 let y = position.y().max(0.0).min(layout.size.y());
710 let row = ((y / layout.line_height) + scroll_position.y()) as u32;
711 let row = cmp::min(row, snapshot.max_point().row());
712 let line = &layout.line_layouts[(row - scroll_position.y() as u32) as usize];
713 let x = position.x() + (scroll_position.x() * layout.em_width);
714
715 let column = if x >= 0.0 {
716 line.index_for_x(x)
717 .map(|ix| ix as u32)
718 .unwrap_or(snapshot.line_len(row))
719 } else {
720 0
721 };
722
723 DisplayPoint::new(row, column)
724 }
725}
726
727struct Cursor {
728 origin: Vector2F,
729 line_height: f32,
730 color: Color,
731}
732
733impl Cursor {
734 fn paint(&self, cx: &mut PaintContext) {
735 cx.scene.push_quad(Quad {
736 bounds: RectF::new(self.origin, vec2f(2.0, self.line_height)),
737 background: Some(self.color),
738 border: Border::new(0., Color::black()),
739 corner_radius: 0.,
740 });
741 }
742}
743
744#[derive(Debug)]
745struct Selection {
746 start_y: f32,
747 line_height: f32,
748 lines: Vec<SelectionLine>,
749 color: Color,
750}
751
752#[derive(Debug)]
753struct SelectionLine {
754 start_x: f32,
755 end_x: f32,
756}
757
758impl Selection {
759 fn paint(&self, bounds: RectF, scene: &mut Scene) {
760 if self.lines.len() >= 2 && self.lines[0].start_x > self.lines[1].end_x {
761 self.paint_lines(self.start_y, &self.lines[0..1], bounds, scene);
762 self.paint_lines(
763 self.start_y + self.line_height,
764 &self.lines[1..],
765 bounds,
766 scene,
767 );
768 } else {
769 self.paint_lines(self.start_y, &self.lines, bounds, scene);
770 }
771 }
772
773 fn paint_lines(&self, start_y: f32, lines: &[SelectionLine], bounds: RectF, scene: &mut Scene) {
774 if lines.is_empty() {
775 return;
776 }
777
778 let mut path = PathBuilder::new();
779 let corner_radius = 0.15 * self.line_height;
780 let first_line = lines.first().unwrap();
781 let last_line = lines.last().unwrap();
782
783 let first_top_left = vec2f(first_line.start_x, start_y);
784 let first_top_right = vec2f(first_line.end_x, start_y);
785
786 let curve_height = vec2f(0., corner_radius);
787 let curve_width = |start_x: f32, end_x: f32| {
788 let max = (end_x - start_x) / 2.;
789 let width = if max < corner_radius {
790 max
791 } else {
792 corner_radius
793 };
794
795 vec2f(width, 0.)
796 };
797
798 let top_curve_width = curve_width(first_line.start_x, first_line.end_x);
799 path.reset(first_top_right - top_curve_width);
800 path.curve_to(first_top_right + curve_height, first_top_right);
801
802 let mut iter = lines.iter().enumerate().peekable();
803 while let Some((ix, line)) = iter.next() {
804 let bottom_right = vec2f(line.end_x, start_y + (ix + 1) as f32 * self.line_height);
805
806 if let Some((_, next_line)) = iter.peek() {
807 let next_top_right = vec2f(next_line.end_x, bottom_right.y());
808
809 match next_top_right.x().partial_cmp(&bottom_right.x()).unwrap() {
810 Ordering::Equal => {
811 path.line_to(bottom_right);
812 }
813 Ordering::Less => {
814 let curve_width = curve_width(next_top_right.x(), bottom_right.x());
815 path.line_to(bottom_right - curve_height);
816 path.curve_to(bottom_right - curve_width, bottom_right);
817 path.line_to(next_top_right + curve_width);
818 path.curve_to(next_top_right + curve_height, next_top_right);
819 }
820 Ordering::Greater => {
821 let curve_width = curve_width(bottom_right.x(), next_top_right.x());
822 path.line_to(bottom_right - curve_height);
823 path.curve_to(bottom_right + curve_width, bottom_right);
824 path.line_to(next_top_right - curve_width);
825 path.curve_to(next_top_right + curve_height, next_top_right);
826 }
827 }
828 } else {
829 let curve_width = curve_width(line.start_x, line.end_x);
830 path.line_to(bottom_right - curve_height);
831 path.curve_to(bottom_right - curve_width, bottom_right);
832
833 let bottom_left = vec2f(line.start_x, bottom_right.y());
834 path.line_to(bottom_left + curve_width);
835 path.curve_to(bottom_left - curve_height, bottom_left);
836 }
837 }
838
839 if first_line.start_x > last_line.start_x {
840 let curve_width = curve_width(last_line.start_x, first_line.start_x);
841 let second_top_left = vec2f(last_line.start_x, start_y + self.line_height);
842 path.line_to(second_top_left + curve_height);
843 path.curve_to(second_top_left + curve_width, second_top_left);
844 let first_bottom_left = vec2f(first_line.start_x, second_top_left.y());
845 path.line_to(first_bottom_left - curve_width);
846 path.curve_to(first_bottom_left - curve_height, first_bottom_left);
847 }
848
849 path.line_to(first_top_left + curve_height);
850 path.curve_to(first_top_left + top_curve_width, first_top_left);
851 path.line_to(first_top_right - top_curve_width);
852
853 scene.push_path(path.build(self.color, Some(bounds)));
854 }
855}
856
857fn scale_vertical_mouse_autoscroll_delta(delta: f32) -> f32 {
858 delta.powf(1.5) / 100.0
859}
860
861fn scale_horizontal_mouse_autoscroll_delta(delta: f32) -> f32 {
862 delta.powf(1.2) / 300.0
863}