2022-10-18 12:42:37 -06:00
|
|
|
use std::{
|
|
|
|
|
cmp,
|
2022-10-18 17:04:22 -06:00
|
|
|
fmt,
|
2022-10-18 12:42:37 -06:00
|
|
|
time::Instant,
|
|
|
|
|
};
|
2022-10-18 12:07:22 -06:00
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
use crate::{FontLayoutLine, FontMatches, FontShapeLine};
|
2022-10-18 12:07:22 -06:00
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// An action to perform on a [TextBuffer]
|
2022-10-18 17:04:22 -06:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
2022-10-18 12:07:22 -06:00
|
|
|
pub enum TextAction {
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Move cursor left
|
2022-10-18 12:07:22 -06:00
|
|
|
Left,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Move cursor right
|
2022-10-18 12:07:22 -06:00
|
|
|
Right,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Move cursor up
|
2022-10-18 12:07:22 -06:00
|
|
|
Up,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Move cursor down
|
2022-10-18 12:07:22 -06:00
|
|
|
Down,
|
2022-10-19 11:08:15 -06:00
|
|
|
/// Move cursor to start of line
|
|
|
|
|
Home,
|
|
|
|
|
/// Move cursor to end of line
|
|
|
|
|
End,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Scroll up one page
|
2022-10-18 12:42:37 -06:00
|
|
|
PageUp,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Scroll down one page
|
2022-10-18 12:42:37 -06:00
|
|
|
PageDown,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Insert character at cursor
|
2022-10-18 12:07:22 -06:00
|
|
|
Insert(char),
|
2022-10-19 11:33:35 -06:00
|
|
|
/// Create new line
|
|
|
|
|
Enter,
|
|
|
|
|
/// Delete text behind cursor
|
|
|
|
|
Backspace,
|
|
|
|
|
/// Delete text in front of cursor
|
|
|
|
|
Delete,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Mouse click at specified position
|
2022-10-18 17:04:22 -06:00
|
|
|
Click { x: i32, y: i32 },
|
2022-10-19 10:12:52 -06:00
|
|
|
/// Mouse drag to specified position
|
|
|
|
|
Drag { x: i32, y: i32 },
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Scroll specified number of lines
|
|
|
|
|
Scroll { lines: i32 },
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Current cursor location
|
2022-10-18 17:04:22 -06:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
2022-10-18 12:07:22 -06:00
|
|
|
pub struct TextCursor {
|
|
|
|
|
pub line: usize,
|
|
|
|
|
pub glyph: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TextCursor {
|
2022-10-18 17:04:22 -06:00
|
|
|
pub const fn new(line: usize, glyph: usize) -> Self {
|
2022-10-18 12:07:22 -06:00
|
|
|
Self { line, glyph }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Index of a text line
|
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
|
|
|
|
|
pub struct TextLineIndex(usize);
|
|
|
|
|
|
|
|
|
|
impl TextLineIndex {
|
|
|
|
|
pub fn new(index: usize) -> Self {
|
|
|
|
|
Self(index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get(&self) -> usize {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Metrics of text
|
2022-10-18 17:04:22 -06:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
|
|
|
|
pub struct TextMetrics {
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Font size in pixels
|
2022-10-18 17:04:22 -06:00
|
|
|
pub font_size: i32,
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Line height in pixels
|
2022-10-18 17:04:22 -06:00
|
|
|
pub line_height: i32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TextMetrics {
|
|
|
|
|
pub const fn new(font_size: i32, line_height: i32) -> Self {
|
|
|
|
|
Self { font_size, line_height }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub const fn scale(self, scale: i32) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
font_size: self.font_size * scale,
|
|
|
|
|
line_height: self.line_height * scale,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for TextMetrics {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(f, "{}px / {}px", self.font_size, self.line_height)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// A buffer of text that is shaped and laid out
|
2022-10-18 12:07:22 -06:00
|
|
|
pub struct TextBuffer<'a> {
|
|
|
|
|
font_matches: &'a FontMatches<'a>,
|
|
|
|
|
text_lines: Vec<String>,
|
|
|
|
|
shape_lines: Vec<FontShapeLine<'a>>,
|
|
|
|
|
layout_lines: Vec<FontLayoutLine<'a>>,
|
2022-10-18 17:04:22 -06:00
|
|
|
metrics: TextMetrics,
|
2022-10-18 12:42:37 -06:00
|
|
|
width: i32,
|
|
|
|
|
height: i32,
|
2022-10-18 13:20:13 -06:00
|
|
|
scroll: i32,
|
2022-10-19 08:34:34 -06:00
|
|
|
cursor: TextCursor,
|
2022-10-19 10:12:52 -06:00
|
|
|
select_opt: Option<TextCursor>,
|
2022-10-18 12:07:22 -06:00
|
|
|
pub redraw: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> TextBuffer<'a> {
|
2022-10-18 12:42:37 -06:00
|
|
|
pub fn new(
|
|
|
|
|
font_matches: &'a FontMatches<'a>,
|
|
|
|
|
text: &str,
|
2022-10-18 17:04:22 -06:00
|
|
|
metrics: TextMetrics,
|
2022-10-18 12:42:37 -06:00
|
|
|
) -> Self {
|
2022-10-18 12:07:22 -06:00
|
|
|
let mut text_lines: Vec<String> = text.lines().map(String::from).collect();
|
|
|
|
|
if text_lines.is_empty() {
|
|
|
|
|
text_lines.push(String::new());
|
|
|
|
|
}
|
2022-10-18 17:04:22 -06:00
|
|
|
Self {
|
2022-10-18 12:07:22 -06:00
|
|
|
font_matches,
|
|
|
|
|
text_lines,
|
|
|
|
|
shape_lines: Vec::new(),
|
|
|
|
|
layout_lines: Vec::new(),
|
2022-10-18 17:04:22 -06:00
|
|
|
metrics,
|
|
|
|
|
width: 0,
|
|
|
|
|
height: 0,
|
|
|
|
|
scroll: 0,
|
2022-10-18 12:07:22 -06:00
|
|
|
cursor: TextCursor::default(),
|
2022-10-19 10:12:52 -06:00
|
|
|
select_opt: None,
|
2022-10-18 12:07:22 -06:00
|
|
|
redraw: false,
|
2022-10-18 17:04:22 -06:00
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Pre-shape lines in the buffer, up to `lines`
|
2022-10-18 12:07:22 -06:00
|
|
|
pub fn shape_until(&mut self, lines: i32) {
|
|
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let mut reshaped = 0;
|
|
|
|
|
while self.shape_lines.len() < self.text_lines.len()
|
|
|
|
|
&& (self.layout_lines.len() as i32) < lines
|
|
|
|
|
{
|
2022-10-18 17:13:48 -06:00
|
|
|
let line_i = TextLineIndex::new(self.shape_lines.len());
|
2022-10-18 12:07:22 -06:00
|
|
|
self.reshape_line(line_i);
|
|
|
|
|
reshaped += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
if reshaped > 0 {
|
|
|
|
|
log::debug!("shape_until {}: {:?}", reshaped, duration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
fn shape_until_scroll(&mut self) {
|
2022-10-18 13:05:36 -06:00
|
|
|
let lines = self.lines();
|
2022-10-18 12:42:37 -06:00
|
|
|
|
|
|
|
|
let scroll_end = self.scroll + lines;
|
|
|
|
|
self.shape_until(scroll_end);
|
|
|
|
|
|
|
|
|
|
self.scroll = cmp::max(
|
|
|
|
|
0,
|
|
|
|
|
cmp::min(
|
|
|
|
|
self.layout_lines().len() as i32 - (lines - 1),
|
|
|
|
|
self.scroll,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
fn reshape_line(&mut self, line_i: TextLineIndex) {
|
2022-10-18 12:07:22 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let shape_line = self
|
|
|
|
|
.font_matches
|
|
|
|
|
.shape_line(line_i, &self.text_lines[line_i.get()]);
|
|
|
|
|
if line_i.get() < self.shape_lines.len() {
|
|
|
|
|
self.shape_lines[line_i.get()] = shape_line;
|
|
|
|
|
} else {
|
|
|
|
|
self.shape_lines.insert(line_i.get(), shape_line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
log::debug!("reshape line {}: {:?}", line_i.get(), duration);
|
|
|
|
|
|
|
|
|
|
self.relayout_line(line_i);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
fn relayout(&mut self) {
|
2022-10-18 12:07:22 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
self.layout_lines.clear();
|
|
|
|
|
for line in self.shape_lines.iter() {
|
|
|
|
|
let layout_i = self.layout_lines.len();
|
|
|
|
|
line.layout(
|
2022-10-18 17:04:22 -06:00
|
|
|
self.metrics.font_size,
|
2022-10-18 12:42:37 -06:00
|
|
|
self.width,
|
2022-10-18 12:07:22 -06:00
|
|
|
&mut self.layout_lines,
|
|
|
|
|
layout_i,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
log::debug!("relayout: {:?}", duration);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
fn relayout_line(&mut self, line_i: TextLineIndex) {
|
2022-10-18 12:07:22 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let mut insert_opt = None;
|
|
|
|
|
let mut layout_i = 0;
|
|
|
|
|
while layout_i < self.layout_lines.len() {
|
|
|
|
|
let layout_line = &self.layout_lines[layout_i];
|
|
|
|
|
if layout_line.line_i == line_i {
|
|
|
|
|
if insert_opt.is_none() {
|
|
|
|
|
insert_opt = Some(layout_i);
|
|
|
|
|
}
|
|
|
|
|
self.layout_lines.remove(layout_i);
|
|
|
|
|
} else {
|
|
|
|
|
layout_i += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let insert_i = insert_opt.unwrap_or(self.layout_lines.len());
|
|
|
|
|
|
|
|
|
|
let shape_line = &self.shape_lines[line_i.get()];
|
|
|
|
|
shape_line.layout(
|
2022-10-18 17:04:22 -06:00
|
|
|
self.metrics.font_size,
|
2022-10-18 12:42:37 -06:00
|
|
|
self.width,
|
2022-10-18 12:07:22 -06:00
|
|
|
&mut self.layout_lines,
|
|
|
|
|
insert_i,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
log::debug!("relayout line {}: {:?}", line_i.get(), duration);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Get the current cursor position
|
|
|
|
|
pub fn cursor(&self) -> TextCursor {
|
|
|
|
|
self.cursor
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the current [TextMetrics]
|
|
|
|
|
pub fn metrics(&self) -> TextMetrics {
|
|
|
|
|
self.metrics
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Set the current [TextMetrics]
|
|
|
|
|
pub fn set_metrics(&mut self, metrics: TextMetrics) {
|
|
|
|
|
if metrics != self.metrics {
|
|
|
|
|
self.metrics = metrics;
|
2022-10-18 12:42:37 -06:00
|
|
|
self.relayout();
|
2022-10-18 13:20:13 -06:00
|
|
|
self.shape_until_scroll();
|
2022-10-18 12:42:37 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the current buffer dimensions (width, height)
|
|
|
|
|
pub fn size(&self) -> (i32, i32) {
|
|
|
|
|
(self.width, self.height)
|
2022-10-18 13:05:36 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Set the current buffer dimensions
|
2022-10-18 12:42:37 -06:00
|
|
|
pub fn set_size(&mut self, width: i32, height: i32) {
|
|
|
|
|
if width != self.width {
|
|
|
|
|
self.width = width;
|
|
|
|
|
self.relayout();
|
2022-10-18 13:20:13 -06:00
|
|
|
self.shape_until_scroll();
|
2022-10-18 12:42:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if height != self.height {
|
|
|
|
|
self.height = height;
|
|
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the current scroll location
|
|
|
|
|
pub fn scroll(&self) -> i32 {
|
|
|
|
|
self.scroll
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the number of lines that can be viewed in the buffer
|
|
|
|
|
pub fn lines(&self) -> i32 {
|
|
|
|
|
self.height / self.metrics.line_height
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the lines after layout for rendering
|
2022-10-18 12:07:22 -06:00
|
|
|
pub fn layout_lines(&self) -> &[FontLayoutLine] {
|
|
|
|
|
&self.layout_lines
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the lines of the original text
|
2022-10-18 12:07:22 -06:00
|
|
|
pub fn text_lines(&self) -> &[String] {
|
|
|
|
|
&self.text_lines
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Perform a [TextAction] on the buffer
|
2022-10-18 12:07:22 -06:00
|
|
|
pub fn action(&mut self, action: TextAction) {
|
|
|
|
|
match action {
|
|
|
|
|
TextAction::Left => {
|
|
|
|
|
let line = &self.layout_lines[self.cursor.line];
|
|
|
|
|
if self.cursor.glyph > line.glyphs.len() {
|
|
|
|
|
self.cursor.glyph = line.glyphs.len();
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
2022-10-19 10:44:21 -06:00
|
|
|
if line.rtl {
|
|
|
|
|
if self.cursor.glyph < line.glyphs.len() {
|
|
|
|
|
self.cursor.glyph += 1;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if self.cursor.glyph > 0 {
|
|
|
|
|
self.cursor.glyph -= 1;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Right => {
|
|
|
|
|
let line = &self.layout_lines[self.cursor.line];
|
|
|
|
|
if self.cursor.glyph > line.glyphs.len() {
|
|
|
|
|
self.cursor.glyph = line.glyphs.len();
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
2022-10-19 10:44:21 -06:00
|
|
|
if line.rtl {
|
|
|
|
|
if self.cursor.glyph > 0 {
|
|
|
|
|
self.cursor.glyph -= 1;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if self.cursor.glyph < line.glyphs.len() {
|
|
|
|
|
self.cursor.glyph += 1;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Up => {
|
|
|
|
|
if self.cursor.line > 0 {
|
|
|
|
|
self.cursor.line -= 1;
|
|
|
|
|
self.redraw = true;
|
2022-10-18 13:05:36 -06:00
|
|
|
|
|
|
|
|
let lines = self.lines();
|
|
|
|
|
if (self.cursor.line as i32) < self.scroll
|
|
|
|
|
|| (self.cursor.line as i32) >= self.scroll + lines
|
|
|
|
|
{
|
|
|
|
|
self.scroll = self.cursor.line as i32;
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Down => {
|
2022-10-18 13:05:36 -06:00
|
|
|
if self.cursor.line < self.layout_lines.len() {
|
2022-10-18 12:07:22 -06:00
|
|
|
self.cursor.line += 1;
|
|
|
|
|
self.redraw = true;
|
2022-10-18 13:05:36 -06:00
|
|
|
|
|
|
|
|
let lines = self.lines();
|
|
|
|
|
if (self.cursor.line as i32) < self.scroll
|
|
|
|
|
|| (self.cursor.line as i32) >= self.scroll + lines
|
|
|
|
|
{
|
|
|
|
|
self.scroll = self.cursor.line as i32 - (lines - 1);
|
|
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-19 11:08:15 -06:00
|
|
|
TextAction::Home => {
|
|
|
|
|
if self.cursor.glyph > 0 {
|
|
|
|
|
self.cursor.glyph = 0;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
TextAction::End => {
|
|
|
|
|
let line = &self.layout_lines[self.cursor.line];
|
|
|
|
|
if self.cursor.glyph < line.glyphs.len() {
|
|
|
|
|
self.cursor.glyph = line.glyphs.len();
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-18 12:42:37 -06:00
|
|
|
TextAction::PageUp => {
|
2022-10-18 13:05:36 -06:00
|
|
|
self.scroll -= self.lines();
|
2022-10-18 12:42:37 -06:00
|
|
|
self.redraw = true;
|
|
|
|
|
self.shape_until_scroll();
|
|
|
|
|
},
|
|
|
|
|
TextAction::PageDown => {
|
2022-10-18 13:05:36 -06:00
|
|
|
self.scroll += self.lines();
|
2022-10-18 12:42:37 -06:00
|
|
|
self.redraw = true;
|
|
|
|
|
self.shape_until_scroll();
|
|
|
|
|
},
|
2022-10-19 11:33:35 -06:00
|
|
|
TextAction::Insert(character) => if character.is_control() {
|
|
|
|
|
// Filter out special chars, use TextAction instead
|
|
|
|
|
log::debug!("Refusing to insert control character {:?}", character);
|
|
|
|
|
} else {
|
|
|
|
|
let line = &self.layout_lines[self.cursor.line];
|
|
|
|
|
let insert_i = if self.cursor.glyph >= line.glyphs.len() {
|
|
|
|
|
match line.glyphs.last() {
|
|
|
|
|
Some(glyph) => glyph.end,
|
|
|
|
|
None => self.text_lines[line.line_i.get()].len()
|
2022-10-19 11:12:34 -06:00
|
|
|
}
|
2022-10-19 11:33:35 -06:00
|
|
|
} else {
|
|
|
|
|
line.glyphs[self.cursor.glyph].start
|
|
|
|
|
};
|
2022-10-19 11:04:26 -06:00
|
|
|
|
2022-10-19 11:33:35 -06:00
|
|
|
let text_line = &mut self.text_lines[line.line_i.get()];
|
|
|
|
|
text_line.insert(insert_i, character);
|
|
|
|
|
self.cursor.glyph += 1;
|
|
|
|
|
self.reshape_line(line.line_i);
|
|
|
|
|
},
|
|
|
|
|
TextAction::Enter => {
|
|
|
|
|
{
|
2022-10-19 11:04:26 -06:00
|
|
|
let line = &self.layout_lines[self.cursor.line];
|
2022-10-19 11:33:35 -06:00
|
|
|
let new_line = if self.cursor.glyph >= line.glyphs.len() {
|
|
|
|
|
String::new()
|
2022-10-19 11:04:26 -06:00
|
|
|
} else {
|
2022-10-19 11:33:35 -06:00
|
|
|
let glyph = &line.glyphs[self.cursor.glyph];
|
|
|
|
|
self.text_lines[line.line_i.get()].split_off(glyph.start)
|
2022-10-19 11:17:15 -06:00
|
|
|
};
|
2022-10-19 11:33:35 -06:00
|
|
|
self.text_lines.insert(line.line_i.get() + 1, new_line);
|
|
|
|
|
|
|
|
|
|
// Reshape all lines after new line
|
|
|
|
|
//TODO: improve performance
|
|
|
|
|
self.shape_lines.truncate(line.line_i.get());
|
|
|
|
|
self.relayout();
|
|
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.cursor.glyph = 0;
|
|
|
|
|
self.cursor.line += 1;
|
2022-10-19 11:17:15 -06:00
|
|
|
|
2022-10-19 11:33:35 -06:00
|
|
|
let lines = self.lines();
|
|
|
|
|
if (self.cursor.line as i32) < self.scroll
|
|
|
|
|
|| (self.cursor.line as i32) >= self.scroll + lines
|
|
|
|
|
{
|
|
|
|
|
self.scroll = self.cursor.line as i32 - (lines - 1);
|
|
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
TextAction::Backspace => {
|
|
|
|
|
let line = &self.layout_lines[self.cursor.line];
|
|
|
|
|
if self.cursor.glyph > line.glyphs.len() {
|
|
|
|
|
self.cursor.glyph = line.glyphs.len();
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
if self.cursor.glyph > 0 {
|
|
|
|
|
self.cursor.glyph -= 1;
|
|
|
|
|
let glyph = &line.glyphs[self.cursor.glyph];
|
2022-10-19 11:17:15 -06:00
|
|
|
let text_line = &mut self.text_lines[line.line_i.get()];
|
2022-10-19 11:33:35 -06:00
|
|
|
text_line.remove(glyph.start);
|
|
|
|
|
self.reshape_line(line.line_i);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
TextAction::Delete => {
|
|
|
|
|
let line = &self.layout_lines[self.cursor.line];
|
|
|
|
|
if self.cursor.glyph < line.glyphs.len() {
|
|
|
|
|
let glyph = &line.glyphs[self.cursor.glyph];
|
|
|
|
|
let text_line = &mut self.text_lines[line.line_i.get()];
|
|
|
|
|
text_line.remove(glyph.start);
|
2022-10-19 11:17:15 -06:00
|
|
|
self.reshape_line(line.line_i);
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
2022-10-18 17:04:22 -06:00
|
|
|
},
|
|
|
|
|
TextAction::Click { x, y } => {
|
2022-10-19 10:12:52 -06:00
|
|
|
self.select_opt = None;
|
|
|
|
|
self.click(x, y);
|
|
|
|
|
},
|
|
|
|
|
TextAction::Drag { x, y } => {
|
|
|
|
|
if self.select_opt.is_none() {
|
|
|
|
|
self.select_opt = Some(self.cursor);
|
2022-10-19 10:21:29 -06:00
|
|
|
self.redraw = true;
|
2022-10-19 10:12:52 -06:00
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
self.click(x, y);
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-19 08:34:34 -06:00
|
|
|
TextAction::Scroll { lines } => {
|
2022-10-18 13:20:13 -06:00
|
|
|
self.scroll += lines;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
|
|
|
|
|
fn click(&mut self, mouse_x: i32, mouse_y: i32) {
|
|
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let font_size = self.metrics.font_size;
|
|
|
|
|
let line_height = self.metrics.line_height;
|
|
|
|
|
|
|
|
|
|
let mut new_cursor_opt = None;
|
|
|
|
|
|
|
|
|
|
let mut line_y = font_size;
|
|
|
|
|
for (line_i, line) in self.layout_lines.iter()
|
|
|
|
|
.skip(cmp::max(0, self.scroll()) as usize)
|
|
|
|
|
.take(cmp::max(0, self.lines()) as usize)
|
|
|
|
|
.enumerate()
|
|
|
|
|
{
|
|
|
|
|
if mouse_y >= line_y - font_size
|
|
|
|
|
&& mouse_y < line_y - font_size + line_height
|
|
|
|
|
{
|
|
|
|
|
let new_cursor_line = line_i + self.scroll() as usize;
|
|
|
|
|
let mut new_cursor_glyph = line.glyphs.len();
|
|
|
|
|
for (glyph_i, glyph) in line.glyphs.iter().enumerate() {
|
|
|
|
|
if mouse_x >= glyph.x as i32
|
|
|
|
|
&& mouse_x <= (glyph.x + glyph.w) as i32
|
|
|
|
|
{
|
2022-10-19 10:44:21 -06:00
|
|
|
let right_half = mouse_x >= (glyph.x + glyph.w / 2.0) as i32;
|
|
|
|
|
if right_half == !line.rtl {
|
2022-10-19 09:31:01 -06:00
|
|
|
// If clicking on last half of glyph, move cursor past glyph
|
|
|
|
|
new_cursor_glyph = glyph_i + 1;
|
|
|
|
|
} else {
|
|
|
|
|
new_cursor_glyph = glyph_i;
|
|
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
new_cursor_opt = Some(TextCursor::new(new_cursor_line, new_cursor_glyph));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line_y += line_height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(new_cursor) = new_cursor_opt {
|
|
|
|
|
if new_cursor != self.cursor {
|
|
|
|
|
self.cursor = new_cursor;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
log::debug!("click({}, {}): {:?}", mouse_x, mouse_y, duration);
|
2022-10-19 10:44:21 -06:00
|
|
|
|
|
|
|
|
println!("cursor {:?} select {:?}", self.cursor, self.select_opt);
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Draw the buffer
|
|
|
|
|
pub fn draw<F>(&self, color: u32, mut f: F)
|
|
|
|
|
where F: FnMut(i32, i32, u32, u32, u32)
|
|
|
|
|
{
|
|
|
|
|
let font_size = self.metrics.font_size;
|
|
|
|
|
let line_height = self.metrics.line_height;
|
|
|
|
|
|
|
|
|
|
let mut line_y = font_size;
|
|
|
|
|
for (line_i, line) in self.layout_lines.iter()
|
|
|
|
|
.skip(cmp::max(0, self.scroll()) as usize)
|
|
|
|
|
.take(cmp::max(0, self.lines()) as usize)
|
|
|
|
|
.enumerate()
|
|
|
|
|
{
|
2022-10-19 10:12:52 -06:00
|
|
|
let line_i_scrolled = line_i + cmp::max(0, self.scroll()) as usize;
|
|
|
|
|
|
|
|
|
|
// Highlight selection (TODO: HIGHLIGHT COLOR!)
|
|
|
|
|
if let Some(select) = self.select_opt {
|
|
|
|
|
let (start, end) = if select.line < self.cursor.line {
|
|
|
|
|
(select, self.cursor)
|
|
|
|
|
} else if select.line > self.cursor.line {
|
|
|
|
|
(self.cursor, select)
|
|
|
|
|
} else {
|
|
|
|
|
/* select.line == self.cursor.line */
|
|
|
|
|
if select.glyph < self.cursor.glyph {
|
|
|
|
|
(select, self.cursor)
|
|
|
|
|
} else {
|
|
|
|
|
/* select.glyph >= self.cursor.glyph */
|
|
|
|
|
(self.cursor, select)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if line_i_scrolled >= start.line && line_i_scrolled <= end.line {
|
|
|
|
|
let start_glyph = if start.line == line_i_scrolled {
|
|
|
|
|
start.glyph
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let end_glyph = if end.line == line_i_scrolled {
|
|
|
|
|
end.glyph
|
|
|
|
|
} else {
|
2022-10-19 10:20:22 -06:00
|
|
|
line.glyphs.len() + 1
|
2022-10-19 10:12:52 -06:00
|
|
|
};
|
|
|
|
|
|
2022-10-19 10:20:22 -06:00
|
|
|
if end_glyph > start_glyph {
|
2022-10-19 10:44:21 -06:00
|
|
|
let (left_x, right_x) = if line.rtl {
|
|
|
|
|
(
|
|
|
|
|
line.glyphs.get(end_glyph - 1).map_or(0, |glyph| {
|
|
|
|
|
glyph.x as i32
|
|
|
|
|
}),
|
|
|
|
|
line.glyphs.get(start_glyph).map_or(self.width, |glyph| {
|
|
|
|
|
(glyph.x + glyph.w) as i32
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(
|
|
|
|
|
line.glyphs.get(start_glyph).map_or(0, |glyph| {
|
|
|
|
|
glyph.x as i32
|
|
|
|
|
}),
|
|
|
|
|
line.glyphs.get(end_glyph - 1).map_or(self.width, |glyph| {
|
|
|
|
|
(glyph.x + glyph.w) as i32
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
};
|
2022-10-19 10:20:22 -06:00
|
|
|
|
|
|
|
|
f(
|
2022-10-19 10:44:21 -06:00
|
|
|
left_x,
|
2022-10-19 10:20:22 -06:00
|
|
|
line_y - font_size,
|
2022-10-19 10:44:21 -06:00
|
|
|
cmp::max(0, right_x - left_x) as u32,
|
2022-10-19 10:20:22 -06:00
|
|
|
line_height as u32,
|
|
|
|
|
0x33_00_00_00 | (color & 0xFF_FF_FF)
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-10-19 10:12:52 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw cursor
|
|
|
|
|
if self.cursor.line == line_i_scrolled {
|
2022-10-19 07:36:27 -06:00
|
|
|
if self.cursor.glyph >= line.glyphs.len() {
|
|
|
|
|
let x = match line.glyphs.last() {
|
|
|
|
|
Some(glyph) => glyph.x + glyph.w,
|
|
|
|
|
None => 0.0,
|
|
|
|
|
};
|
2022-10-19 10:44:21 -06:00
|
|
|
|
2022-10-19 07:36:27 -06:00
|
|
|
f(
|
|
|
|
|
x as i32,
|
|
|
|
|
line_y - font_size,
|
2022-10-19 09:26:43 -06:00
|
|
|
1,
|
2022-10-19 07:36:27 -06:00
|
|
|
line_height as u32,
|
2022-10-19 09:32:26 -06:00
|
|
|
color,
|
2022-10-19 07:36:27 -06:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
let glyph = &line.glyphs[self.cursor.glyph];
|
2022-10-19 10:44:21 -06:00
|
|
|
let x = if line.rtl {
|
|
|
|
|
(glyph.x + glyph.w) as i32
|
|
|
|
|
} else {
|
|
|
|
|
glyph.x as i32
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-19 07:36:27 -06:00
|
|
|
f(
|
2022-10-19 10:44:21 -06:00
|
|
|
x,
|
2022-10-19 07:36:27 -06:00
|
|
|
line_y - font_size,
|
2022-10-19 09:26:43 -06:00
|
|
|
1,
|
2022-10-19 07:36:27 -06:00
|
|
|
line_height as u32,
|
2022-10-19 09:32:26 -06:00
|
|
|
color,
|
2022-10-19 07:36:27 -06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line.draw(color, |x, y, color| {
|
|
|
|
|
f(x, line_y + y, 1, 1, color);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
line_y += line_height;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|