mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-19 14:57:15 +03:00
Merge pull request #883 from rtfeldman/fast_selection
more efficient selection
This commit is contained in:
commit
2b91262073
@ -59,6 +59,7 @@ These are potentially inspirational resources for the editor's design.
|
||||
- Ability to link e.g. variable name in comments to actual variable name. Comment is automatically updated when variable name is changed.
|
||||
* Automatically create all "arms" when pattern matching after entering `when var is` based on the type.
|
||||
- All `when ... is` should be updated if the type is changed, e.g. adding Indigo to the Color type should add an arm everywhere where `when color is` is used.
|
||||
* When a function is called like `foo(false)`, the name of the boolean argument should be shown automatically; `foo(`*is_active:*`false)`. This should be done for booleans and numbers.
|
||||
|
||||
|
||||
### Non-Code Related Inspiration
|
||||
|
@ -3,10 +3,9 @@
|
||||
|
||||
use super::rect::Rect;
|
||||
use crate::graphics::colors::CODE_COLOR;
|
||||
use crate::graphics::style::CODE_FONT_SIZE;
|
||||
use crate::graphics::style::{CODE_FONT_SIZE, CODE_TXT_XY};
|
||||
use ab_glyph::{FontArc, Glyph, InvalidFont};
|
||||
use cgmath::{Vector2, Vector4};
|
||||
use itertools::Itertools;
|
||||
use wgpu_glyph::{ab_glyph, GlyphBrush, GlyphBrushBuilder, GlyphCruncher, Section};
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -37,7 +36,7 @@ impl Default for Text {
|
||||
// necessary to get dimensions for caret
|
||||
pub fn example_code_glyph_rect(glyph_brush: &mut GlyphBrush<()>) -> Rect {
|
||||
let code_text = Text {
|
||||
position: (30.0, 90.0).into(), //TODO 30.0 90.0 should be an arg
|
||||
position: CODE_TXT_XY.into(),
|
||||
area_bounds: (std::f32::INFINITY, std::f32::INFINITY).into(),
|
||||
color: CODE_COLOR.into(),
|
||||
text: "a".to_owned(),
|
||||
@ -83,39 +82,13 @@ fn section_from_text(
|
||||
)
|
||||
}
|
||||
|
||||
// returns bounding boxes for every glyph
|
||||
pub fn queue_text_draw(text: &Text, glyph_brush: &mut GlyphBrush<()>) -> Vec<Vec<Rect>> {
|
||||
// returns glyphs per line
|
||||
pub fn queue_text_draw(text: &Text, glyph_brush: &mut GlyphBrush<()>) {
|
||||
let layout = layout_from_text(text);
|
||||
|
||||
let section = section_from_text(text, layout);
|
||||
|
||||
glyph_brush.queue(section.clone());
|
||||
|
||||
let glyph_section_iter = glyph_brush.glyphs_custom_layout(section, &layout);
|
||||
|
||||
glyph_section_iter
|
||||
.map(glyph_to_rect)
|
||||
.group_by(|rect| rect.top_left_coords.y)
|
||||
.into_iter()
|
||||
.map(|(_y_coord, rect_group)| {
|
||||
let mut rects_vec = rect_group.collect::<Vec<Rect>>();
|
||||
let last_rect_opt = rects_vec.last().cloned();
|
||||
// add extra rect to make it easy to highlight the newline character
|
||||
if let Some(last_rect) = last_rect_opt {
|
||||
rects_vec.push(Rect {
|
||||
top_left_coords: [
|
||||
last_rect.top_left_coords.x + last_rect.width,
|
||||
last_rect.top_left_coords.y,
|
||||
]
|
||||
.into(),
|
||||
width: last_rect.width,
|
||||
height: last_rect.height,
|
||||
color: last_rect.color,
|
||||
});
|
||||
}
|
||||
rects_vec
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn glyph_to_rect(glyph: &wgpu_glyph::SectionGlyph) -> Rect {
|
||||
@ -140,7 +113,7 @@ pub fn glyph_top_y(glyph: &Glyph) -> f32 {
|
||||
}
|
||||
|
||||
pub fn glyph_width(glyph: &Glyph) -> f32 {
|
||||
glyph.scale.x * 0.5
|
||||
glyph.scale.x * 0.4765
|
||||
}
|
||||
|
||||
pub fn build_glyph_brush(
|
||||
|
@ -1 +1,2 @@
|
||||
pub const CODE_FONT_SIZE: f32 = 40.0;
|
||||
pub const CODE_TXT_XY: (f32, f32) = (30.0, 90.0);
|
||||
|
@ -24,12 +24,13 @@ use crate::graphics::primitives::text::{
|
||||
build_glyph_brush, example_code_glyph_rect, queue_text_draw, Text,
|
||||
};
|
||||
use crate::graphics::style::CODE_FONT_SIZE;
|
||||
use crate::graphics::style::CODE_TXT_XY;
|
||||
use crate::selection::create_selection_rects;
|
||||
use crate::tea::ed_model::EdModel;
|
||||
use crate::tea::{ed_model, update};
|
||||
use crate::vec_result::get_res;
|
||||
use bumpalo::collections::Vec as BumpVec;
|
||||
use bumpalo::Bump;
|
||||
use cgmath::Vector2;
|
||||
use ed_model::Position;
|
||||
use pipelines::RectResources;
|
||||
use std::error::Error;
|
||||
@ -215,12 +216,17 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||
.expect("Failed to acquire next SwapChainFrame")
|
||||
.output;
|
||||
|
||||
let glyph_bounds_rects =
|
||||
queue_all_text(&size, &ed_model.lines, ed_model.caret_pos, &mut glyph_brush);
|
||||
queue_all_text(
|
||||
&size,
|
||||
&ed_model.lines,
|
||||
ed_model.caret_pos,
|
||||
CODE_TXT_XY.into(),
|
||||
&mut glyph_brush,
|
||||
);
|
||||
|
||||
match draw_all_rects(
|
||||
&ed_model,
|
||||
&glyph_bounds_rects,
|
||||
&ed_model.glyph_dim_rect_opt,
|
||||
&arena,
|
||||
&mut encoder,
|
||||
&frame.view,
|
||||
@ -267,7 +273,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
fn draw_all_rects(
|
||||
ed_model: &EdModel,
|
||||
glyph_bounds_rects: &[Vec<Rect>],
|
||||
glyph_dim_rect_opt: &Option<Rect>,
|
||||
arena: &Bump,
|
||||
encoder: &mut CommandEncoder,
|
||||
texture_view: &TextureView,
|
||||
@ -276,17 +282,20 @@ fn draw_all_rects(
|
||||
) -> EdResult<()> {
|
||||
let mut all_rects: BumpVec<Rect> = BumpVec::new_in(arena);
|
||||
|
||||
let glyph_rect = if let Some(glyph_dim_rect) = glyph_dim_rect_opt {
|
||||
glyph_dim_rect
|
||||
} else {
|
||||
return Err(MissingGlyphDims {});
|
||||
};
|
||||
|
||||
if let Some(selection) = ed_model.selection_opt {
|
||||
let mut selection_rects = create_selection_rects(selection, glyph_bounds_rects, &arena)?;
|
||||
let mut selection_rects =
|
||||
create_selection_rects(selection, &ed_model.lines, glyph_rect, &arena)?;
|
||||
|
||||
all_rects.append(&mut selection_rects);
|
||||
}
|
||||
|
||||
all_rects.push(make_caret_rect(
|
||||
ed_model.caret_pos,
|
||||
glyph_bounds_rects,
|
||||
ed_model.glyph_dim_rect_opt,
|
||||
)?);
|
||||
all_rects.push(make_caret_rect(ed_model.caret_pos, glyph_rect)?);
|
||||
|
||||
let rect_buffers = create_rect_buffers(gpu_device, encoder, &all_rects);
|
||||
|
||||
@ -323,38 +332,17 @@ fn begin_render_pass<'a>(
|
||||
})
|
||||
}
|
||||
|
||||
fn make_caret_rect(
|
||||
caret_pos: Position,
|
||||
glyph_bound_rects: &[Vec<Rect>],
|
||||
glyph_dim_rect_opt: Option<Rect>,
|
||||
) -> EdResult<Rect> {
|
||||
let mut glyph_rect = if let Some(glyph_dim_rect) = glyph_dim_rect_opt {
|
||||
glyph_dim_rect
|
||||
} else {
|
||||
return Err(MissingGlyphDims {});
|
||||
};
|
||||
fn make_caret_rect(caret_pos: Position, glyph_dim_rect: &Rect) -> EdResult<Rect> {
|
||||
let caret_y =
|
||||
glyph_dim_rect.top_left_coords.y + (caret_pos.line as f32) * glyph_dim_rect.height;
|
||||
|
||||
let caret_y = glyph_rect.top_left_coords.y + (caret_pos.line as f32) * glyph_rect.height;
|
||||
|
||||
if caret_pos.column > 0 && glyph_bound_rects.len() > caret_pos.line {
|
||||
let indx = caret_pos.column - 1;
|
||||
let glyph_rect_line = get_res(caret_pos.line, glyph_bound_rects)?;
|
||||
|
||||
if glyph_rect_line.len() > indx {
|
||||
glyph_rect = *get_res(indx, glyph_rect_line)?;
|
||||
}
|
||||
};
|
||||
|
||||
let caret_x = if caret_pos.column == 0 {
|
||||
glyph_rect.top_left_coords.x
|
||||
} else {
|
||||
glyph_rect.top_left_coords.x + glyph_rect.width
|
||||
};
|
||||
let caret_x =
|
||||
glyph_dim_rect.top_left_coords.x + glyph_dim_rect.width * (caret_pos.column as f32);
|
||||
|
||||
Ok(Rect {
|
||||
top_left_coords: (caret_x, caret_y).into(),
|
||||
height: glyph_rect.height,
|
||||
width: 3.0,
|
||||
height: glyph_dim_rect.height,
|
||||
width: 2.0,
|
||||
color: CARET_COLOR,
|
||||
})
|
||||
}
|
||||
@ -364,8 +352,9 @@ fn queue_all_text(
|
||||
size: &PhysicalSize<u32>,
|
||||
lines: &[String],
|
||||
caret_pos: Position,
|
||||
code_coords: Vector2<f32>,
|
||||
glyph_brush: &mut GlyphBrush<()>,
|
||||
) -> Vec<Vec<Rect>> {
|
||||
) {
|
||||
let area_bounds = (size.width as f32, size.height as f32).into();
|
||||
|
||||
let main_label = Text {
|
||||
@ -378,7 +367,7 @@ fn queue_all_text(
|
||||
};
|
||||
|
||||
let code_text = Text {
|
||||
position: (30.0, 90.0).into(), //TODO 30 90 should be an arg
|
||||
position: code_coords,
|
||||
area_bounds,
|
||||
color: CODE_COLOR.into(),
|
||||
text: lines.join(""),
|
||||
@ -387,7 +376,7 @@ fn queue_all_text(
|
||||
};
|
||||
|
||||
let caret_pos_label = Text {
|
||||
position: (30.0, 530.0).into(),
|
||||
position: (30.0, (size.height as f32) - 45.0).into(),
|
||||
area_bounds,
|
||||
color: TXT_COLOR.into(),
|
||||
text: format!("Ln {}, Col {}", caret_pos.line, caret_pos.column),
|
||||
@ -399,5 +388,5 @@ fn queue_all_text(
|
||||
|
||||
queue_text_draw(&caret_pos_label, glyph_brush);
|
||||
|
||||
queue_text_draw(&code_text, glyph_brush)
|
||||
queue_text_draw(&code_text, glyph_brush);
|
||||
}
|
||||
|
@ -43,7 +43,8 @@ fn validate_selection(selection: RawSelection) -> EdResult<ValidSelection> {
|
||||
|
||||
pub fn create_selection_rects<'a>(
|
||||
raw_sel: RawSelection,
|
||||
glyph_bound_rects: &[Vec<Rect>],
|
||||
lines: &[String],
|
||||
glyph_dim_rect: &Rect,
|
||||
arena: &'a Bump,
|
||||
) -> EdResult<BumpVec<'a, Rect>> {
|
||||
let valid_sel = validate_selection(raw_sel)?;
|
||||
@ -51,25 +52,17 @@ pub fn create_selection_rects<'a>(
|
||||
|
||||
let mut all_rects: BumpVec<Rect> = BumpVec::new_in(arena);
|
||||
|
||||
let height = glyph_dim_rect.height;
|
||||
let start_y = glyph_dim_rect.top_left_coords.y + height * (start_pos.line as f32);
|
||||
let line_start_x = glyph_dim_rect.top_left_coords.x;
|
||||
|
||||
if start_pos.line == end_pos.line {
|
||||
let start_glyph_rect = get_res(
|
||||
start_pos.column,
|
||||
get_res(start_pos.line, glyph_bound_rects)?,
|
||||
)?;
|
||||
|
||||
let stop_glyph_rect = get_res(
|
||||
end_pos.column - 1,
|
||||
get_res(end_pos.line, glyph_bound_rects)?,
|
||||
)?;
|
||||
|
||||
let top_left_coords = start_glyph_rect.top_left_coords;
|
||||
|
||||
let height = start_glyph_rect.height;
|
||||
let width = (stop_glyph_rect.top_left_coords.x - start_glyph_rect.top_left_coords.x)
|
||||
+ stop_glyph_rect.width;
|
||||
let width = ((end_pos.column as f32) * glyph_dim_rect.width)
|
||||
- ((start_pos.column as f32) * glyph_dim_rect.width);
|
||||
let sel_rect_x = line_start_x + ((start_pos.column as f32) * glyph_dim_rect.width);
|
||||
|
||||
all_rects.push(Rect {
|
||||
top_left_coords,
|
||||
top_left_coords: (sel_rect_x, start_y).into(),
|
||||
width,
|
||||
height,
|
||||
color: colors::SELECT_COLOR,
|
||||
@ -78,21 +71,14 @@ pub fn create_selection_rects<'a>(
|
||||
Ok(all_rects)
|
||||
} else {
|
||||
// first line
|
||||
let start_line = get_res(start_pos.line, glyph_bound_rects)?;
|
||||
let end_col = get_res(start_pos.line, lines)?.len();
|
||||
let width = ((end_col as f32) * glyph_dim_rect.width)
|
||||
- ((start_pos.column as f32) * glyph_dim_rect.width);
|
||||
|
||||
let start_glyph_rect = get_res(start_pos.column, start_line)?;
|
||||
|
||||
let start_line_last_glyph_rect = get_res(start_line.len() - 1, start_line)?;
|
||||
|
||||
let top_left_coords = start_glyph_rect.top_left_coords;
|
||||
|
||||
let height = start_glyph_rect.height;
|
||||
let width = (start_line_last_glyph_rect.top_left_coords.x
|
||||
- start_glyph_rect.top_left_coords.x)
|
||||
+ start_line_last_glyph_rect.width;
|
||||
let sel_rect_x = line_start_x + ((start_pos.column as f32) * glyph_dim_rect.width);
|
||||
|
||||
all_rects.push(Rect {
|
||||
top_left_coords,
|
||||
top_left_coords: (sel_rect_x, start_y).into(),
|
||||
width,
|
||||
height,
|
||||
color: colors::SELECT_COLOR,
|
||||
@ -103,21 +89,14 @@ pub fn create_selection_rects<'a>(
|
||||
let first_mid_line = start_pos.line + 1;
|
||||
|
||||
for i in first_mid_line..(first_mid_line + nr_mid_lines) {
|
||||
let mid_line = get_res(i, glyph_bound_rects)?;
|
||||
let mid_line_len = get_res(i, lines)?.len();
|
||||
|
||||
let mid_line_first_glyph_rect = get_res(0, mid_line)?;
|
||||
let width = (mid_line_len as f32) * glyph_dim_rect.width;
|
||||
|
||||
let mid_line_last_glyph_rect = get_res(mid_line.len() - 1, mid_line)?;
|
||||
|
||||
let top_left_coords = mid_line_first_glyph_rect.top_left_coords;
|
||||
|
||||
let height = mid_line_first_glyph_rect.height;
|
||||
let width = (mid_line_last_glyph_rect.top_left_coords.x
|
||||
- mid_line_first_glyph_rect.top_left_coords.x)
|
||||
+ mid_line_last_glyph_rect.width;
|
||||
let sel_rect_y = start_y + ((i - start_pos.line) as f32) * glyph_dim_rect.height;
|
||||
|
||||
all_rects.push(Rect {
|
||||
top_left_coords,
|
||||
top_left_coords: (line_start_x, sel_rect_y).into(),
|
||||
width,
|
||||
height,
|
||||
color: colors::SELECT_COLOR,
|
||||
@ -126,21 +105,13 @@ pub fn create_selection_rects<'a>(
|
||||
|
||||
//last line
|
||||
if end_pos.column > 0 {
|
||||
let stop_line = get_res(end_pos.line, glyph_bound_rects)?;
|
||||
let sel_rect_y =
|
||||
start_y + ((end_pos.line - start_pos.line) as f32) * glyph_dim_rect.height;
|
||||
|
||||
let stop_line_first_glyph_rect = get_res(0, stop_line)?;
|
||||
|
||||
let stop_glyph_rect = get_res(end_pos.column - 1, stop_line)?;
|
||||
|
||||
let top_left_coords = stop_line_first_glyph_rect.top_left_coords;
|
||||
|
||||
let height = stop_glyph_rect.height;
|
||||
let width = (stop_glyph_rect.top_left_coords.x
|
||||
- stop_line_first_glyph_rect.top_left_coords.x)
|
||||
+ stop_glyph_rect.width;
|
||||
let width = (end_pos.column as f32) * glyph_dim_rect.width;
|
||||
|
||||
all_rects.push(Rect {
|
||||
top_left_coords,
|
||||
top_left_coords: (line_start_x, sel_rect_y).into(),
|
||||
width,
|
||||
height,
|
||||
color: colors::SELECT_COLOR,
|
||||
|
Loading…
Reference in New Issue
Block a user