progress on selection memory optimization

This commit is contained in:
Anton-4 2021-01-08 20:10:01 +01:00
parent 83b4bb9be8
commit 38a6b19b0f
4 changed files with 47 additions and 33 deletions

View file

@ -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

View file

@ -26,6 +26,10 @@ pub enum EdError {
},
#[snafu(display("MissingGlyphDims: glyph_dim_rect_opt was None for model. It needs to be set using the example_code_glyph_rect function."))]
MissingGlyphDims {},
#[snafu(display("EmptyGlyphsPerLine: glyphs_per_line was none, it is necessary for highlighting of the code."))]
EmptyGlyphsPerLine {
backtrace: Backtrace,
},
}
pub type EdResult<T, E = EdError> = std::result::Result<T, E>;

View file

@ -8,6 +8,8 @@ use ab_glyph::{FontArc, Glyph, InvalidFont};
use cgmath::{Vector2, Vector4};
use itertools::Itertools;
use wgpu_glyph::{ab_glyph, GlyphBrush, GlyphBrushBuilder, GlyphCruncher, Section};
use bumpalo::collections::Vec as BumpVec;
use bumpalo::Bump;
#[derive(Debug)]
pub struct Text {
@ -83,39 +85,41 @@ 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<'a>(text: &Text, glyph_brush: &mut GlyphBrush<()>, arena: &'a Bump, selectable: bool) -> Option<BumpVec<'a, usize>> {
let layout = layout_from_text(text);
let section = section_from_text(text, layout);
glyph_brush.queue(section.clone());
if selectable {
let mut glyphs_per_line: BumpVec<usize> = BumpVec::new_in(arena);
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,
});
let first_glyph_opt = glyph_section_iter.next();
if let Some(first_glyph) = first_glyph_opt {
let mut line_y_coord = first_glyph.glyph.scale.y;
let mut glyphs_on_line = 0;
for glyph in glyph_section_iter {
let curr_y_coord = glyph.glyph.scale.y;
if curr_y_coord != line_y_coord {
line_y_coord = curr_y_coord;
glyphs_per_line.push(glyphs_on_line);
glyphs_on_line = 0;
} else {
glyphs_on_line += 1;
}
}
}
Some(glyphs_per_line)
} else {
None
}
rects_vec
})
.collect()
}
fn glyph_to_rect(glyph: &wgpu_glyph::SectionGlyph) -> Rect {

View file

@ -13,7 +13,8 @@ extern crate pest;
#[macro_use]
extern crate pest_derive;
use crate::error::EdError::MissingGlyphDims;
use crate::error::EdError::{MissingGlyphDims};
use crate::error::{EmptyGlyphsPerLine};
use crate::error::{print_err, EdResult};
use crate::graphics::colors::{CARET_COLOR, CODE_COLOR, TXT_COLOR};
use crate::graphics::lowlevel::buffer::create_rect_buffers;
@ -41,6 +42,7 @@ use winit::dpi::PhysicalSize;
use winit::event;
use winit::event::{Event, ModifiersState};
use winit::event_loop::ControlFlow;
use snafu::ensure;
pub mod error;
pub mod graphics;
@ -216,7 +218,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
.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, &mut glyph_brush, &arena);
match draw_all_rects(
&ed_model,
@ -331,7 +333,7 @@ fn make_caret_rect(
let mut glyph_rect = if let Some(glyph_dim_rect) = glyph_dim_rect_opt {
glyph_dim_rect
} else {
return Err(MissingGlyphDims {});
return Err(MissingGlyphDims{});
};
let caret_y = glyph_rect.top_left_coords.y + (caret_pos.line as f32) * glyph_rect.height;
@ -360,12 +362,13 @@ fn make_caret_rect(
}
// returns bounding boxes for every glyph
fn queue_all_text(
fn queue_all_text<'a>(
size: &PhysicalSize<u32>,
lines: &[String],
caret_pos: Position,
glyph_brush: &mut GlyphBrush<()>,
) -> Vec<Vec<Rect>> {
arena: &'a Bump
) -> EdResult<BumpVec<'a, usize>> {
let area_bounds = (size.width as f32, size.height as f32).into();
let main_label = Text {
@ -395,9 +398,11 @@ fn queue_all_text(
..Default::default()
};
queue_text_draw(&main_label, glyph_brush);
queue_text_draw(&main_label, glyph_brush, arena, false);
queue_text_draw(&caret_pos_label, glyph_brush);
queue_text_draw(&caret_pos_label, glyph_brush, arena, false);
queue_text_draw(&code_text, glyph_brush)
let glyphs_per_line_opt = queue_text_draw(&code_text, glyph_brush, arena, true);
ensure!(glyphs_per_line_opt.is_some(), EmptyGlyphsPerLine{})
}