mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 14:54:47 +00:00
refactoring for improved structure
This commit is contained in:
parent
bca451af3c
commit
f71d1a5c42
12 changed files with 115 additions and 229 deletions
|
@ -1,6 +1,6 @@
|
||||||
// Adapted from https://github.com/sotrh/learn-wgpu
|
// Adapted from https://github.com/sotrh/learn-wgpu
|
||||||
// by Benjamin Hansen, licensed under the MIT license
|
// by Benjamin Hansen, licensed under the MIT license
|
||||||
use crate::graphics::lowlevel::vertex::Vertex;
|
use super::vertex::Vertex;
|
||||||
use crate::graphics::primitives::rect::Rect;
|
use crate::graphics::primitives::rect::Rect;
|
||||||
use bumpalo::collections::Vec as BumpVec;
|
use bumpalo::collections::Vec as BumpVec;
|
||||||
use wgpu::util::{BufferInitDescriptor, DeviceExt};
|
use wgpu::util::{BufferInitDescriptor, DeviceExt};
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
pub mod buffer;
|
pub mod buffer;
|
||||||
pub mod ortho;
|
pub mod ortho;
|
||||||
|
pub mod pipelines;
|
||||||
pub mod vertex;
|
pub mod vertex;
|
||||||
|
|
|
@ -66,6 +66,7 @@ pub fn update_ortho_buffer(
|
||||||
cmd_queue.submit(Some(encoder.finish()));
|
cmd_queue.submit(Some(encoder.finish()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct OrthoResources {
|
pub struct OrthoResources {
|
||||||
pub buffer: Buffer,
|
pub buffer: Buffer,
|
||||||
pub bind_group_layout: BindGroupLayout,
|
pub bind_group_layout: BindGroupLayout,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
pub mod colors;
|
pub mod colors;
|
||||||
pub mod style;
|
|
||||||
pub mod lowlevel;
|
pub mod lowlevel;
|
||||||
pub mod primitives;
|
pub mod primitives;
|
||||||
|
pub mod style;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Adapted from https://github.com/sotrh/learn-wgpu
|
// Adapted from https://github.com/sotrh/learn-wgpu
|
||||||
// by Benjamin Hansen, licensed under the MIT license
|
// by Benjamin Hansen, licensed under the MIT license
|
||||||
|
|
||||||
use crate::graphics::primitives::rect::Rect;
|
use super::rect::Rect;
|
||||||
use crate::graphics::colors::CODE_COLOR;
|
use crate::graphics::colors::CODE_COLOR;
|
||||||
use crate::graphics::style::CODE_FONT_SIZE;
|
use crate::graphics::style::CODE_FONT_SIZE;
|
||||||
use ab_glyph::{FontArc, Glyph, InvalidFont};
|
use ab_glyph::{FontArc, Glyph, InvalidFont};
|
||||||
|
@ -66,7 +66,10 @@ fn layout_from_text(text: &Text) -> wgpu_glyph::Layout<wgpu_glyph::BuiltInLineBr
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn section_from_text(text: &Text, layout: wgpu_glyph::Layout<wgpu_glyph::BuiltInLineBreaker>) -> wgpu_glyph::Section {
|
fn section_from_text(
|
||||||
|
text: &Text,
|
||||||
|
layout: wgpu_glyph::Layout<wgpu_glyph::BuiltInLineBreaker>,
|
||||||
|
) -> wgpu_glyph::Section {
|
||||||
Section {
|
Section {
|
||||||
screen_position: text.position.into(),
|
screen_position: text.position.into(),
|
||||||
bounds: text.area_bounds.into(),
|
bounds: text.area_bounds.into(),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::tea::model::Model;
|
use crate::tea::ed_model::EdModel;
|
||||||
use crate::tea::update::{move_caret_down, move_caret_left, move_caret_right, move_caret_up};
|
use crate::tea::update::{move_caret_down, move_caret_left, move_caret_right, move_caret_up};
|
||||||
use winit::event::{ElementState, ModifiersState, VirtualKeyCode};
|
use winit::event::{ElementState, ModifiersState, VirtualKeyCode};
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ pub fn handle_keydown(
|
||||||
elem_state: ElementState,
|
elem_state: ElementState,
|
||||||
virtual_keycode: VirtualKeyCode,
|
virtual_keycode: VirtualKeyCode,
|
||||||
modifiers: ModifiersState,
|
modifiers: ModifiersState,
|
||||||
model: &mut Model,
|
model: &mut EdModel,
|
||||||
) {
|
) {
|
||||||
use winit::event::VirtualKeyCode::*;
|
use winit::event::VirtualKeyCode::*;
|
||||||
|
|
||||||
|
|
|
@ -8,27 +8,31 @@
|
||||||
|
|
||||||
// See this link to learn wgpu: https://sotrh.github.io/learn-wgpu/
|
// See this link to learn wgpu: https://sotrh.github.io/learn-wgpu/
|
||||||
|
|
||||||
use crate::error::{print_err, EdResult};
|
|
||||||
use crate::error::EdError::MissingGlyphDims;
|
use crate::error::EdError::MissingGlyphDims;
|
||||||
use crate::vec_result::{get_res};
|
use crate::error::{print_err, EdResult};
|
||||||
|
use crate::graphics::colors::{CARET_COLOR, CODE_COLOR, TXT_COLOR};
|
||||||
use crate::graphics::lowlevel::buffer::create_rect_buffers;
|
use crate::graphics::lowlevel::buffer::create_rect_buffers;
|
||||||
use crate::graphics::lowlevel::ortho::{init_ortho, update_ortho_buffer, OrthoResources};
|
use crate::graphics::lowlevel::ortho::update_ortho_buffer;
|
||||||
use crate::graphics::lowlevel::vertex::Vertex;
|
use crate::graphics::lowlevel::pipelines;
|
||||||
use crate::graphics::primitives::rect::Rect;
|
use crate::graphics::primitives::rect::Rect;
|
||||||
use crate::graphics::primitives::text::{build_glyph_brush, queue_text_draw, Text, example_code_glyph_rect};
|
use crate::graphics::primitives::text::{
|
||||||
use crate::graphics::colors::{TXT_COLOR, CODE_COLOR, CARET_COLOR};
|
build_glyph_brush, example_code_glyph_rect, queue_text_draw, Text,
|
||||||
use crate::graphics::style::{CODE_FONT_SIZE};
|
};
|
||||||
|
use crate::graphics::style::CODE_FONT_SIZE;
|
||||||
use crate::selection::create_selection_rects;
|
use crate::selection::create_selection_rects;
|
||||||
use crate::tea::{model, update};
|
use crate::tea::ed_model::EdModel;
|
||||||
use crate::tea::model::Model;
|
use crate::tea::{ed_model, update};
|
||||||
use crate::util::is_newline;
|
use crate::vec_result::get_res;
|
||||||
use model::{Position};
|
|
||||||
use bumpalo::Bump;
|
|
||||||
use bumpalo::collections::Vec as BumpVec;
|
use bumpalo::collections::Vec as BumpVec;
|
||||||
|
use bumpalo::Bump;
|
||||||
|
use ed_model::Position;
|
||||||
|
use pipelines::RectResources;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use wgpu::{CommandEncoder, RenderPass, TextureView};
|
use wgpu::{CommandEncoder, RenderPass, TextureView};
|
||||||
|
use wgpu_glyph::GlyphBrush;
|
||||||
|
use winit::dpi::PhysicalSize;
|
||||||
use winit::event;
|
use winit::event;
|
||||||
use winit::event::{Event, ModifiersState};
|
use winit::event::{Event, ModifiersState};
|
||||||
use winit::event_loop::ControlFlow;
|
use winit::event_loop::ControlFlow;
|
||||||
|
@ -109,12 +113,12 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
let mut swap_chain = gpu_device.create_swap_chain(&surface, &swap_chain_descr);
|
let mut swap_chain = gpu_device.create_swap_chain(&surface, &swap_chain_descr);
|
||||||
|
|
||||||
let (rect_pipeline, ortho) = make_rect_pipeline(&gpu_device, &swap_chain_descr);
|
let rect_resources = pipelines::make_rect_pipeline(&gpu_device, &swap_chain_descr);
|
||||||
|
|
||||||
let mut glyph_brush = build_glyph_brush(&gpu_device, render_format)?;
|
let mut glyph_brush = build_glyph_brush(&gpu_device, render_format)?;
|
||||||
|
|
||||||
let is_animating = true;
|
let is_animating = true;
|
||||||
let mut ed_model = model::init_model();
|
let mut ed_model = ed_model::init_model();
|
||||||
ed_model.glyph_dim_rect_opt = Some(example_code_glyph_rect(&mut glyph_brush));
|
ed_model.glyph_dim_rect_opt = Some(example_code_glyph_rect(&mut glyph_brush));
|
||||||
let mut keyboard_modifiers = ModifiersState::empty();
|
let mut keyboard_modifiers = ModifiersState::empty();
|
||||||
|
|
||||||
|
@ -161,7 +165,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
size.width,
|
size.width,
|
||||||
size.height,
|
size.height,
|
||||||
&gpu_device,
|
&gpu_device,
|
||||||
&ortho.buffer,
|
&rect_resources.ortho.buffer,
|
||||||
&cmd_queue,
|
&cmd_queue,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -170,7 +174,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
event: event::WindowEvent::ReceivedCharacter(ch),
|
event: event::WindowEvent::ReceivedCharacter(ch),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
update_text_state(&mut ed_model, &ch);
|
update::update_text_state(&mut ed_model, &ch);
|
||||||
}
|
}
|
||||||
//Keyboard Input
|
//Keyboard Input
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
|
@ -209,7 +213,6 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
let glyph_bounds_rects =
|
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);
|
||||||
|
|
||||||
// TODO too much args group them in Struct
|
|
||||||
match draw_all_rects(
|
match draw_all_rects(
|
||||||
&ed_model,
|
&ed_model,
|
||||||
&glyph_bounds_rects,
|
&glyph_bounds_rects,
|
||||||
|
@ -217,14 +220,13 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
&mut encoder,
|
&mut encoder,
|
||||||
&frame.view,
|
&frame.view,
|
||||||
&gpu_device,
|
&gpu_device,
|
||||||
&rect_pipeline,
|
&rect_resources,
|
||||||
&ortho
|
|
||||||
) {
|
) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
print_err(&e);
|
print_err(&e);
|
||||||
begin_render_pass(&mut encoder, &frame.view);
|
begin_render_pass(&mut encoder, &frame.view);
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw all text
|
// draw all text
|
||||||
|
@ -259,40 +261,35 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_all_rects(
|
fn draw_all_rects(
|
||||||
ed_model: &Model,
|
ed_model: &EdModel,
|
||||||
glyph_bounds_rects: &Vec<Vec<Rect>>,
|
glyph_bounds_rects: &[Vec<Rect>],
|
||||||
arena: &Bump,
|
arena: &Bump,
|
||||||
encoder: &mut CommandEncoder,
|
encoder: &mut CommandEncoder,
|
||||||
texture_view: &TextureView,
|
texture_view: &TextureView,
|
||||||
gpu_device: &wgpu::Device,
|
gpu_device: &wgpu::Device,
|
||||||
rect_pipeline: &wgpu::RenderPipeline,
|
rect_resources: &RectResources,
|
||||||
ortho: &OrthoResources
|
|
||||||
) -> EdResult<()> {
|
) -> EdResult<()> {
|
||||||
let mut all_rects: BumpVec<Rect> = BumpVec::new_in(arena);
|
let mut all_rects: BumpVec<Rect> = BumpVec::new_in(arena);
|
||||||
|
|
||||||
if let Some(selection) = ed_model.selection_opt {
|
if let Some(selection) = ed_model.selection_opt {
|
||||||
let mut selection_rects =
|
let mut selection_rects = create_selection_rects(selection, glyph_bounds_rects, &arena)?;
|
||||||
create_selection_rects(selection, glyph_bounds_rects, &arena)?;
|
|
||||||
|
|
||||||
all_rects.append(&mut selection_rects);
|
all_rects.append(&mut selection_rects);
|
||||||
}
|
}
|
||||||
|
|
||||||
all_rects.push(
|
all_rects.push(make_caret_rect(
|
||||||
make_caret_rect(ed_model.caret_pos, glyph_bounds_rects, ed_model.glyph_dim_rect_opt)?
|
ed_model.caret_pos,
|
||||||
);
|
glyph_bounds_rects,
|
||||||
|
ed_model.glyph_dim_rect_opt,
|
||||||
|
)?);
|
||||||
|
|
||||||
let rect_buffers = create_rect_buffers(
|
let rect_buffers = create_rect_buffers(gpu_device, encoder, &all_rects);
|
||||||
gpu_device,
|
|
||||||
encoder,
|
|
||||||
&all_rects,
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut render_pass = begin_render_pass(encoder, texture_view);
|
let mut render_pass = begin_render_pass(encoder, texture_view);
|
||||||
|
|
||||||
render_pass.set_pipeline(&rect_pipeline);
|
render_pass.set_pipeline(&rect_resources.pipeline);
|
||||||
render_pass.set_bind_group(0, &ortho.bind_group, &[]);
|
render_pass.set_bind_group(0, &rect_resources.ortho.bind_group, &[]);
|
||||||
render_pass
|
render_pass.set_vertex_buffer(0, rect_buffers.vertex_buffer.slice(..));
|
||||||
.set_vertex_buffer(0, rect_buffers.vertex_buffer.slice(..));
|
|
||||||
render_pass.set_index_buffer(rect_buffers.index_buffer.slice(..));
|
render_pass.set_index_buffer(rect_buffers.index_buffer.slice(..));
|
||||||
render_pass.draw_indexed(0..rect_buffers.num_rects, 0, 0..1);
|
render_pass.draw_indexed(0..rect_buffers.num_rects, 0, 0..1);
|
||||||
|
|
||||||
|
@ -324,7 +321,7 @@ fn begin_render_pass<'a>(
|
||||||
fn make_caret_rect(
|
fn make_caret_rect(
|
||||||
caret_pos: Position,
|
caret_pos: Position,
|
||||||
glyph_bound_rects: &[Vec<Rect>],
|
glyph_bound_rects: &[Vec<Rect>],
|
||||||
glyph_dim_rect_opt: Option<Rect>
|
glyph_dim_rect_opt: Option<Rect>,
|
||||||
) -> EdResult<Rect> {
|
) -> EdResult<Rect> {
|
||||||
let mut glyph_rect = if let Some(glyph_dim_rect) = glyph_dim_rect_opt {
|
let mut glyph_rect = if let Some(glyph_dim_rect) = glyph_dim_rect_opt {
|
||||||
glyph_dim_rect
|
glyph_dim_rect
|
||||||
|
@ -343,8 +340,7 @@ fn make_caret_rect(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let caret_x =
|
let caret_x = if caret_pos.column == 0 {
|
||||||
if caret_pos.column == 0 {
|
|
||||||
glyph_rect.top_left_coords.x
|
glyph_rect.top_left_coords.x
|
||||||
} else {
|
} else {
|
||||||
glyph_rect.top_left_coords.x + glyph_rect.width
|
glyph_rect.top_left_coords.x + glyph_rect.width
|
||||||
|
@ -354,80 +350,16 @@ fn make_caret_rect(
|
||||||
top_left_coords: (caret_x, caret_y).into(),
|
top_left_coords: (caret_x, caret_y).into(),
|
||||||
height: glyph_rect.height,
|
height: glyph_rect.height,
|
||||||
width: 3.0,
|
width: 3.0,
|
||||||
color: CARET_COLOR
|
color: CARET_COLOR,
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn make_rect_pipeline(
|
|
||||||
gpu_device: &wgpu::Device,
|
|
||||||
swap_chain_descr: &wgpu::SwapChainDescriptor,
|
|
||||||
) -> (wgpu::RenderPipeline, OrthoResources) {
|
|
||||||
let ortho = init_ortho(swap_chain_descr.width, swap_chain_descr.height, gpu_device);
|
|
||||||
|
|
||||||
let pipeline_layout = gpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
||||||
bind_group_layouts: &[&ortho.bind_group_layout],
|
|
||||||
push_constant_ranges: &[],
|
|
||||||
label: Some("Rectangle pipeline layout"),
|
|
||||||
});
|
|
||||||
let pipeline = create_render_pipeline(
|
|
||||||
&gpu_device,
|
|
||||||
&pipeline_layout,
|
|
||||||
swap_chain_descr.format,
|
|
||||||
&[Vertex::DESC],
|
|
||||||
wgpu::include_spirv!("graphics/shaders/rect.vert.spv"),
|
|
||||||
wgpu::include_spirv!("graphics/shaders/rect.frag.spv"),
|
|
||||||
);
|
|
||||||
|
|
||||||
(pipeline, ortho)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_render_pipeline(
|
|
||||||
device: &wgpu::Device,
|
|
||||||
layout: &wgpu::PipelineLayout,
|
|
||||||
color_format: wgpu::TextureFormat,
|
|
||||||
vertex_descs: &[wgpu::VertexBufferDescriptor],
|
|
||||||
vs_src: wgpu::ShaderModuleSource,
|
|
||||||
fs_src: wgpu::ShaderModuleSource,
|
|
||||||
) -> wgpu::RenderPipeline {
|
|
||||||
let vs_module = device.create_shader_module(vs_src);
|
|
||||||
let fs_module = device.create_shader_module(fs_src);
|
|
||||||
|
|
||||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
||||||
label: Some("Render pipeline"),
|
|
||||||
layout: Some(&layout),
|
|
||||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
|
||||||
module: &vs_module,
|
|
||||||
entry_point: "main",
|
|
||||||
},
|
|
||||||
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
|
||||||
module: &fs_module,
|
|
||||||
entry_point: "main",
|
|
||||||
}),
|
|
||||||
rasterization_state: None,
|
|
||||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
||||||
color_states: &[wgpu::ColorStateDescriptor {
|
|
||||||
format: color_format,
|
|
||||||
color_blend: wgpu::BlendDescriptor::REPLACE,
|
|
||||||
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
|
||||||
write_mask: wgpu::ColorWrite::ALL,
|
|
||||||
}],
|
|
||||||
depth_stencil_state: None,
|
|
||||||
sample_count: 1,
|
|
||||||
sample_mask: !0,
|
|
||||||
alpha_to_coverage_enabled: false,
|
|
||||||
vertex_state: wgpu::VertexStateDescriptor {
|
|
||||||
index_format: wgpu::IndexFormat::Uint32,
|
|
||||||
vertex_buffers: vertex_descs,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns bounding boxes for every glyph
|
// returns bounding boxes for every glyph
|
||||||
fn queue_all_text(
|
fn queue_all_text(
|
||||||
size: &winit::dpi::PhysicalSize<u32>,
|
size: &PhysicalSize<u32>,
|
||||||
lines: &[String],
|
lines: &[String],
|
||||||
caret_pos: Position,
|
caret_pos: Position,
|
||||||
glyph_brush: &mut wgpu_glyph::GlyphBrush<()>,
|
glyph_brush: &mut GlyphBrush<()>,
|
||||||
) -> Vec<Vec<Rect>> {
|
) -> Vec<Vec<Rect>> {
|
||||||
let area_bounds = (size.width as f32, size.height as f32).into();
|
let area_bounds = (size.width as f32, size.height as f32).into();
|
||||||
|
|
||||||
|
@ -464,53 +396,3 @@ fn queue_all_text(
|
||||||
|
|
||||||
queue_text_draw(&code_text, glyph_brush)
|
queue_text_draw(&code_text, glyph_brush)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_text_state(ed_model: &mut model::Model, received_char: &char) {
|
|
||||||
ed_model.selection_opt = None;
|
|
||||||
|
|
||||||
match received_char {
|
|
||||||
'\u{8}' | '\u{7f}' => {
|
|
||||||
// In Linux, we get a '\u{8}' when you press backspace,
|
|
||||||
// but in macOS we get '\u{7f}'.
|
|
||||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
|
||||||
if !last_line.is_empty() {
|
|
||||||
last_line.pop();
|
|
||||||
} else if ed_model.lines.len() > 1 {
|
|
||||||
ed_model.lines.pop();
|
|
||||||
}
|
|
||||||
ed_model.caret_pos =
|
|
||||||
update::move_caret_left(ed_model.caret_pos, None, false, &ed_model.lines).0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'\u{e000}'..='\u{f8ff}' | '\u{f0000}'..='\u{ffffd}' | '\u{100000}'..='\u{10fffd}' => {
|
|
||||||
// These are private use characters; ignore them.
|
|
||||||
// See http://www.unicode.org/faq/private_use.html
|
|
||||||
}
|
|
||||||
ch if is_newline(ch) => {
|
|
||||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
|
||||||
last_line.push(*received_char)
|
|
||||||
}
|
|
||||||
ed_model.lines.push(String::new());
|
|
||||||
ed_model.caret_pos = Position {
|
|
||||||
line: ed_model.caret_pos.line + 1,
|
|
||||||
column: 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
ed_model.selection_opt = None;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
let nr_lines = ed_model.lines.len();
|
|
||||||
|
|
||||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
|
||||||
last_line.push(*received_char);
|
|
||||||
|
|
||||||
ed_model.caret_pos = Position {
|
|
||||||
line: nr_lines - 1,
|
|
||||||
column: last_line.len(),
|
|
||||||
};
|
|
||||||
|
|
||||||
ed_model.selection_opt = None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::error::{EdResult, InvalidSelection};
|
use crate::error::{EdResult, InvalidSelection};
|
||||||
use crate::graphics::colors;
|
use crate::graphics::colors;
|
||||||
use crate::graphics::primitives::rect::Rect;
|
use crate::graphics::primitives::rect::Rect;
|
||||||
use crate::tea::model::RawSelection;
|
use crate::tea::ed_model::RawSelection;
|
||||||
use crate::vec_result::get_res;
|
use crate::vec_result::get_res;
|
||||||
use bumpalo::collections::Vec as BumpVec;
|
use bumpalo::collections::Vec as BumpVec;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
pub mod model;
|
pub mod ed_model;
|
||||||
pub mod update;
|
pub mod update;
|
||||||
|
|
|
@ -1,52 +0,0 @@
|
||||||
use std::cmp::Ordering;
|
|
||||||
use crate::graphics::primitives::rect::Rect;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Model {
|
|
||||||
pub lines: Vec<String>,
|
|
||||||
pub caret_pos: Position,
|
|
||||||
pub selection_opt: Option<RawSelection>,
|
|
||||||
pub glyph_dim_rect_opt: Option<Rect>
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_model() -> Model {
|
|
||||||
Model {
|
|
||||||
lines: vec![String::new()],
|
|
||||||
caret_pos: Position { line: 0, column: 0 },
|
|
||||||
selection_opt: None,
|
|
||||||
glyph_dim_rect_opt: None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Is model.rs the right place for these structs?
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
|
||||||
pub struct Position {
|
|
||||||
pub line: usize,
|
|
||||||
pub column: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for Position {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
(self.line, self.column).cmp(&(other.line, other.column))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for Position {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for Position {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
(self.line, self.column) == (other.line, other.column)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for Position {}
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
|
||||||
pub struct RawSelection {
|
|
||||||
pub start_pos: Position,
|
|
||||||
pub end_pos: Position,
|
|
||||||
}
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::tea::model::{Position, RawSelection};
|
use super::ed_model::EdModel;
|
||||||
|
use super::ed_model::{Position, RawSelection};
|
||||||
use crate::util::is_newline;
|
use crate::util::is_newline;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
|
@ -223,3 +224,53 @@ pub fn move_caret_down(
|
||||||
|
|
||||||
(new_caret_pos, new_selection_opt)
|
(new_caret_pos, new_selection_opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_text_state(ed_model: &mut EdModel, received_char: &char) {
|
||||||
|
ed_model.selection_opt = None;
|
||||||
|
|
||||||
|
match received_char {
|
||||||
|
'\u{8}' | '\u{7f}' => {
|
||||||
|
// In Linux, we get a '\u{8}' when you press backspace,
|
||||||
|
// but in macOS we get '\u{7f}'.
|
||||||
|
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||||
|
if !last_line.is_empty() {
|
||||||
|
last_line.pop();
|
||||||
|
} else if ed_model.lines.len() > 1 {
|
||||||
|
ed_model.lines.pop();
|
||||||
|
}
|
||||||
|
ed_model.caret_pos =
|
||||||
|
move_caret_left(ed_model.caret_pos, None, false, &ed_model.lines).0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'\u{e000}'..='\u{f8ff}' | '\u{f0000}'..='\u{ffffd}' | '\u{100000}'..='\u{10fffd}' => {
|
||||||
|
// These are private use characters; ignore them.
|
||||||
|
// See http://www.unicode.org/faq/private_use.html
|
||||||
|
}
|
||||||
|
ch if is_newline(ch) => {
|
||||||
|
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||||
|
last_line.push(*received_char)
|
||||||
|
}
|
||||||
|
ed_model.lines.push(String::new());
|
||||||
|
ed_model.caret_pos = Position {
|
||||||
|
line: ed_model.caret_pos.line + 1,
|
||||||
|
column: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
ed_model.selection_opt = None;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let nr_lines = ed_model.lines.len();
|
||||||
|
|
||||||
|
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||||
|
last_line.push(*received_char);
|
||||||
|
|
||||||
|
ed_model.caret_pos = Position {
|
||||||
|
line: nr_lines - 1,
|
||||||
|
column: last_line.len(),
|
||||||
|
};
|
||||||
|
|
||||||
|
ed_model.selection_opt = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue