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 {
|
2022-10-20 19:34:56 -06:00
|
|
|
/// Text line the cursor is on
|
|
|
|
|
pub line: TextLineIndex,
|
2022-10-21 11:44:11 -06:00
|
|
|
/// Index of glyph at cursor (will insert behind this glyph)
|
|
|
|
|
pub index: usize,
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TextCursor {
|
2022-10-21 11:44:11 -06:00
|
|
|
pub const fn new(line: TextLineIndex, index: usize) -> Self {
|
|
|
|
|
Self { line, index }
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 09:02:15 -06:00
|
|
|
enum CursorScroll {
|
|
|
|
|
None,
|
|
|
|
|
Bottom,
|
|
|
|
|
Top,
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Index of a text line
|
2022-10-20 19:34:56 -06:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
|
2022-10-18 17:13:48 -06:00
|
|
|
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-19 17:48:11 -06:00
|
|
|
pub struct TextBufferLine<'a> {
|
|
|
|
|
text: String,
|
|
|
|
|
shape_opt: Option<FontShapeLine<'a>>,
|
|
|
|
|
layout_opt: Option<Vec<FontLayoutLine<'a>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> TextBufferLine<'a> {
|
|
|
|
|
pub fn new(text: String) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
text,
|
|
|
|
|
shape_opt: None,
|
|
|
|
|
layout_opt: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn text(&self) -> &str {
|
|
|
|
|
&self.text
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
|
self.shape_opt = None;
|
|
|
|
|
self.layout_opt = None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn shape(&mut self, font_matches: &'a FontMatches<'a>) -> &FontShapeLine<'a> {
|
2022-10-19 17:48:11 -06:00
|
|
|
if self.shape_opt.is_none() {
|
2022-10-20 19:34:56 -06:00
|
|
|
self.shape_opt = Some(font_matches.shape_line(&self.text));
|
2022-10-19 17:48:11 -06:00
|
|
|
self.layout_opt = None;
|
|
|
|
|
}
|
|
|
|
|
self.shape_opt.as_ref().unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
pub fn layout(&mut self, font_matches: &'a FontMatches<'a>, font_size: i32, width: i32) -> &[FontLayoutLine<'a>] {
|
2022-10-19 17:48:11 -06:00
|
|
|
if self.layout_opt.is_none() {
|
|
|
|
|
let mut layout = Vec::new();
|
2022-10-20 19:34:56 -06:00
|
|
|
let shape = self.shape(font_matches);
|
2022-10-19 17:48:11 -06:00
|
|
|
shape.layout(
|
|
|
|
|
font_size,
|
|
|
|
|
width,
|
|
|
|
|
&mut layout,
|
|
|
|
|
0,
|
|
|
|
|
);
|
|
|
|
|
self.layout_opt = Some(layout);
|
|
|
|
|
}
|
|
|
|
|
self.layout_opt.as_ref().unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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>,
|
2022-10-19 17:48:11 -06:00
|
|
|
lines: Vec<TextBufferLine<'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>,
|
2022-10-18 17:04:22 -06:00
|
|
|
metrics: TextMetrics,
|
2022-10-18 12:42:37 -06:00
|
|
|
) -> Self {
|
2022-10-19 17:48:11 -06:00
|
|
|
let mut buffer = Self {
|
2022-10-18 12:07:22 -06:00
|
|
|
font_matches,
|
2022-10-19 17:48:11 -06:00
|
|
|
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-19 17:48:11 -06:00
|
|
|
};
|
|
|
|
|
buffer.set_text("");
|
|
|
|
|
buffer
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
/// Pre-shape lines in the buffer, up to `lines`, return actual number of layout lines
|
|
|
|
|
pub fn shape_until(&mut self, lines: i32) -> i32 {
|
2022-10-18 12:07:22 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let mut reshaped = 0;
|
2022-10-20 19:34:56 -06:00
|
|
|
let mut total_layout = 0;
|
|
|
|
|
for line in self.lines.iter_mut() {
|
|
|
|
|
if total_layout >= lines {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if line.shape_opt.is_none() {
|
|
|
|
|
reshaped += 1;
|
|
|
|
|
}
|
|
|
|
|
let layout = line.layout(
|
|
|
|
|
self.font_matches,
|
|
|
|
|
self.metrics.font_size,
|
|
|
|
|
self.width
|
|
|
|
|
);
|
|
|
|
|
total_layout += layout.len() as i32;
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
if reshaped > 0 {
|
|
|
|
|
log::debug!("shape_until {}: {:?}", reshaped, duration);
|
2022-10-20 19:34:56 -06:00
|
|
|
self.redraw = true;
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
2022-10-20 19:34:56 -06:00
|
|
|
|
|
|
|
|
total_layout
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-21 09:02:15 -06:00
|
|
|
fn shape_until_cursor(&mut self, scroll: CursorScroll) {
|
|
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let mut reshaped = 0;
|
|
|
|
|
let mut layout_i = 0;
|
|
|
|
|
for (line_i, line) in self.lines.iter_mut().enumerate() {
|
|
|
|
|
if line_i > self.cursor.line.get() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if line.shape_opt.is_none() {
|
|
|
|
|
reshaped += 1;
|
|
|
|
|
}
|
|
|
|
|
let layout = line.layout(
|
|
|
|
|
self.font_matches,
|
|
|
|
|
self.metrics.font_size,
|
|
|
|
|
self.width
|
|
|
|
|
);
|
|
|
|
|
if line_i == self.cursor.line.get() {
|
|
|
|
|
for layout_line in layout {
|
|
|
|
|
let mut found = false;
|
|
|
|
|
for glyph in layout_line.glyphs.iter() {
|
2022-10-21 11:44:11 -06:00
|
|
|
if glyph.start <= self.cursor.index {
|
2022-10-21 09:02:15 -06:00
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if found {
|
|
|
|
|
layout_i += 1;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
layout_i += layout.len() as i32;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
if reshaped > 0 {
|
|
|
|
|
log::debug!("shape_until_cursor {}: {:?}", reshaped, duration);
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let lines = self.lines();
|
|
|
|
|
match scroll {
|
|
|
|
|
CursorScroll::None => (),
|
|
|
|
|
CursorScroll::Bottom => {
|
|
|
|
|
if layout_i < self.scroll
|
|
|
|
|
|| layout_i >= self.scroll + lines
|
|
|
|
|
{
|
|
|
|
|
self.scroll = layout_i - (lines - 1);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
CursorScroll::Top => {
|
|
|
|
|
if layout_i < self.scroll
|
|
|
|
|
|| layout_i >= self.scroll + lines
|
|
|
|
|
{
|
|
|
|
|
self.scroll = layout_i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2022-10-20 19:34:56 -06:00
|
|
|
let total_layout = self.shape_until(scroll_end);
|
2022-10-18 12:42:37 -06:00
|
|
|
|
|
|
|
|
self.scroll = cmp::max(
|
|
|
|
|
0,
|
|
|
|
|
cmp::min(
|
2022-10-20 19:34:56 -06:00
|
|
|
total_layout - (lines - 1),
|
2022-10-18 12:42:37 -06:00
|
|
|
self.scroll,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
fn relayout(&mut self) {
|
2022-10-18 12:07:22 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
for line in self.lines.iter_mut() {
|
|
|
|
|
if line.shape_opt.is_some() {
|
|
|
|
|
line.layout_opt = None;
|
|
|
|
|
line.layout(
|
|
|
|
|
self.font_matches,
|
|
|
|
|
self.metrics.font_size,
|
|
|
|
|
self.width
|
|
|
|
|
);
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
2022-10-20 19:34:56 -06:00
|
|
|
log::debug!("relayout: {:?}", duration);
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:15:07 -06:00
|
|
|
/// Set text of buffer
|
|
|
|
|
pub fn set_text(&mut self, text: &str) {
|
2022-10-19 17:48:11 -06:00
|
|
|
self.lines.clear();
|
|
|
|
|
for line in text.lines() {
|
|
|
|
|
self.lines.push(TextBufferLine::new(line.to_string()));
|
|
|
|
|
}
|
|
|
|
|
// Make sure there is always one line
|
|
|
|
|
if self.lines.is_empty() {
|
|
|
|
|
self.lines.push(TextBufferLine::new(String::new()));
|
2022-10-19 13:15:07 -06:00
|
|
|
}
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-19 13:15:07 -06:00
|
|
|
self.scroll = 0;
|
|
|
|
|
self.cursor = TextCursor::default();
|
|
|
|
|
self.select_opt = None;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-19 13:15:07 -06:00
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the lines of the original text
|
2022-10-19 17:48:11 -06:00
|
|
|
pub fn text_lines(&self) -> &[TextBufferLine<'a>] {
|
|
|
|
|
&self.lines
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
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 => {
|
2022-10-20 19:34:56 -06:00
|
|
|
todo!("left");
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Right => {
|
2022-10-20 19:34:56 -06:00
|
|
|
todo!("right");
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Up => {
|
2022-10-20 19:34:56 -06:00
|
|
|
todo!("up");
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Down => {
|
2022-10-20 19:34:56 -06:00
|
|
|
todo!("down");
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-19 11:08:15 -06:00
|
|
|
TextAction::Home => {
|
2022-10-20 19:34:56 -06:00
|
|
|
todo!("home");
|
2022-10-19 11:08:15 -06:00
|
|
|
},
|
|
|
|
|
TextAction::End => {
|
2022-10-20 19:34:56 -06:00
|
|
|
todo!("end");
|
2022-10-19 11:08:15 -06:00
|
|
|
}
|
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;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-18 12:42:37 -06:00
|
|
|
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;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-18 12:42:37 -06:00
|
|
|
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 {
|
2022-10-20 19:34:56 -06:00
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
line.reset();
|
2022-10-21 11:44:11 -06:00
|
|
|
line.text.insert(self.cursor.index, character);
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-21 11:44:11 -06:00
|
|
|
self.cursor.index += character.len_utf8();
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-21 09:02:15 -06:00
|
|
|
self.shape_until_cursor(CursorScroll::Bottom);
|
2022-10-19 11:33:35 -06:00
|
|
|
},
|
|
|
|
|
TextAction::Enter => {
|
2022-10-21 11:51:04 -06:00
|
|
|
let new_line = {
|
|
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
line.reset();
|
|
|
|
|
line.text.split_off(self.cursor.index)
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
let next_line = self.cursor.line.get() + 1;
|
2022-10-21 11:51:04 -06:00
|
|
|
self.lines.insert(next_line, TextBufferLine::new(new_line));
|
2022-10-19 11:33:35 -06:00
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
self.cursor.line = TextLineIndex::new(next_line);
|
2022-10-21 11:44:11 -06:00
|
|
|
self.cursor.index = 0;
|
2022-10-19 11:17:15 -06:00
|
|
|
|
2022-10-21 09:02:15 -06:00
|
|
|
self.shape_until_cursor(CursorScroll::Bottom);
|
2022-10-19 11:33:35 -06:00
|
|
|
},
|
|
|
|
|
TextAction::Backspace => {
|
2022-10-21 11:44:11 -06:00
|
|
|
if self.cursor.index > 0 {
|
2022-10-20 19:34:56 -06:00
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
line.reset();
|
|
|
|
|
|
2022-10-21 11:44:11 -06:00
|
|
|
// Find previous character index
|
|
|
|
|
let mut prev_index = 0;
|
2022-10-20 19:34:56 -06:00
|
|
|
for (i, _) in line.text.char_indices() {
|
2022-10-21 11:44:11 -06:00
|
|
|
if i < self.cursor.index {
|
|
|
|
|
prev_index = i;
|
2022-10-19 16:09:22 -06:00
|
|
|
} else {
|
2022-10-21 11:44:11 -06:00
|
|
|
break;
|
2022-10-19 16:09:22 -06:00
|
|
|
}
|
2022-10-20 19:34:56 -06:00
|
|
|
}
|
2022-10-19 11:51:17 -06:00
|
|
|
|
2022-10-21 11:44:11 -06:00
|
|
|
self.cursor.index = prev_index;
|
|
|
|
|
|
|
|
|
|
line.text.remove(self.cursor.index);
|
|
|
|
|
|
2022-10-21 09:02:15 -06:00
|
|
|
self.shape_until_cursor(CursorScroll::Top);
|
2022-10-20 19:34:56 -06:00
|
|
|
} else if self.cursor.line.get() > 0 {
|
|
|
|
|
let mut line_index = self.cursor.line.get();
|
|
|
|
|
let old_line = self.lines.remove(line_index);
|
|
|
|
|
line_index -= 1;
|
|
|
|
|
|
|
|
|
|
let line = &mut self.lines[line_index];
|
|
|
|
|
line.reset();
|
|
|
|
|
|
|
|
|
|
self.cursor.line = TextLineIndex::new(line_index);
|
2022-10-21 11:44:11 -06:00
|
|
|
self.cursor.index = line.text.len();
|
2022-10-19 11:51:17 -06:00
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
line.text.push_str(&old_line.text);
|
2022-10-19 11:51:17 -06:00
|
|
|
|
2022-10-21 09:02:15 -06:00
|
|
|
self.shape_until_cursor(CursorScroll::Top);
|
2022-10-19 11:33:35 -06:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
TextAction::Delete => {
|
2022-10-20 19:34:56 -06:00
|
|
|
todo!("delete");
|
2022-10-18 17:04:22 -06:00
|
|
|
},
|
|
|
|
|
TextAction::Click { x, y } => {
|
2022-10-19 10:12:52 -06:00
|
|
|
self.select_opt = None;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-19 10:12:52 -06:00
|
|
|
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-20 19:34:56 -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;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-18 13:20:13 -06:00
|
|
|
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 line_y = font_size;
|
2022-10-21 09:02:15 -06:00
|
|
|
let mut layout_i = 0;
|
|
|
|
|
for (line_i, line) in self.lines.iter().enumerate() {
|
|
|
|
|
let shape = match line.shape_opt.as_ref() {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => break,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let layout = match line.layout_opt.as_ref() {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => break,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for layout_line in layout {
|
|
|
|
|
let scrolled = layout_i < self.scroll;
|
|
|
|
|
layout_i += 1;
|
|
|
|
|
|
|
|
|
|
if scrolled {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if line_y > self.height {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if mouse_y >= line_y - font_size
|
|
|
|
|
&& mouse_y < line_y - font_size + line_height
|
|
|
|
|
{
|
|
|
|
|
let mut new_cursor_glyph = layout_line.glyphs.len();
|
|
|
|
|
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
|
|
|
|
|
if mouse_x >= glyph.x as i32
|
|
|
|
|
&& mouse_x <= (glyph.x + glyph.w) as i32
|
|
|
|
|
{
|
|
|
|
|
let right_half = mouse_x >= (glyph.x + glyph.w / 2.0) as i32;
|
|
|
|
|
if right_half == !shape.rtl {
|
|
|
|
|
// 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 09:31:01 -06:00
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
2022-10-21 09:02:15 -06:00
|
|
|
|
2022-10-21 11:44:11 -06:00
|
|
|
let mut new_cursor = TextCursor::new(TextLineIndex::new(line_i), 0);
|
2022-10-19 07:36:27 -06:00
|
|
|
|
2022-10-21 09:02:15 -06:00
|
|
|
match layout_line.glyphs.get(new_cursor_glyph) {
|
|
|
|
|
Some(glyph) => {
|
|
|
|
|
// Position at glyph
|
2022-10-21 11:44:11 -06:00
|
|
|
new_cursor.index = glyph.start;
|
2022-10-21 09:02:15 -06:00
|
|
|
},
|
|
|
|
|
None => match layout_line.glyphs.last() {
|
|
|
|
|
Some(glyph) => {
|
|
|
|
|
// Position at end of line
|
2022-10-21 11:44:11 -06:00
|
|
|
new_cursor.index = glyph.end;
|
2022-10-21 09:02:15 -06:00
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
// Keep at start of empty line
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
|
2022-10-21 09:02:15 -06:00
|
|
|
if new_cursor != self.cursor {
|
|
|
|
|
self.cursor = new_cursor;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
|
|
|
|
|
if let Some(glyph) = layout_line.glyphs.get(new_cursor_glyph) {
|
|
|
|
|
let text_glyph = &line.text[glyph.start..glyph.end];
|
|
|
|
|
log::debug!(
|
2022-10-21 11:44:11 -06:00
|
|
|
"{}, {}: '{}' ('{}'): '{}' ({:?})",
|
2022-10-21 09:02:15 -06:00
|
|
|
self.cursor.line.get(),
|
2022-10-21 11:44:11 -06:00
|
|
|
self.cursor.index,
|
2022-10-21 09:02:15 -06:00
|
|
|
glyph.font.info.family,
|
|
|
|
|
glyph.font.info.post_script_name,
|
|
|
|
|
text_glyph,
|
|
|
|
|
text_glyph
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line_y += line_height;
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
2022-10-19 13:29:50 -06:00
|
|
|
log::trace!("click({}, {}): {:?}", mouse_x, mouse_y, duration);
|
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;
|
2022-10-20 19:34:56 -06:00
|
|
|
let mut layout_i = 0;
|
2022-10-21 09:02:15 -06:00
|
|
|
for (line_i, line) in self.lines.iter().enumerate() {
|
|
|
|
|
let shape = match line.shape_opt.as_ref() {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => break,
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
let layout = match line.layout_opt.as_ref() {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => {
|
|
|
|
|
return
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for layout_line in layout {
|
|
|
|
|
let scrolled = layout_i < self.scroll;
|
|
|
|
|
layout_i += 1;
|
|
|
|
|
|
|
|
|
|
if scrolled {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if line_y > self.height {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 09:49:16 -06:00
|
|
|
let cursor_glyph_opt = |cursor: &TextCursor| -> Option<usize> {
|
2022-10-21 11:51:04 -06:00
|
|
|
if cursor.line.get() == line_i {
|
|
|
|
|
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
|
|
|
|
|
if cursor.index == glyph.start {
|
|
|
|
|
return Some(glyph_i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
match layout_line.glyphs.last() {
|
|
|
|
|
Some(glyph) => {
|
|
|
|
|
if cursor.index == glyph.end {
|
|
|
|
|
return Some(layout_line.glyphs.len());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
return Some(0);
|
|
|
|
|
}
|
2022-10-21 09:49:16 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-21 11:51:04 -06:00
|
|
|
None
|
2022-10-21 09:49:16 -06:00
|
|
|
};
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
// Highlight selection (TODO: HIGHLIGHT COLOR!)
|
|
|
|
|
if let Some(select) = self.select_opt {
|
|
|
|
|
let (start, end) = if select.line < self.cursor.line {
|
2022-10-19 10:12:52 -06:00
|
|
|
(select, self.cursor)
|
2022-10-20 19:34:56 -06:00
|
|
|
} else if select.line > self.cursor.line {
|
2022-10-19 10:12:52 -06:00
|
|
|
(self.cursor, select)
|
|
|
|
|
} else {
|
2022-10-20 19:34:56 -06:00
|
|
|
/* select.line == self.cursor.line */
|
2022-10-21 11:44:11 -06:00
|
|
|
if select.index < self.cursor.index {
|
2022-10-20 19:34:56 -06:00
|
|
|
(select, self.cursor)
|
|
|
|
|
} else {
|
2022-10-21 11:44:11 -06:00
|
|
|
/* select.index >= self.cursor.index */
|
2022-10-20 19:34:56 -06:00
|
|
|
(self.cursor, select)
|
|
|
|
|
}
|
2022-10-19 10:12:52 -06:00
|
|
|
};
|
|
|
|
|
|
2022-10-21 11:19:24 -06:00
|
|
|
// Check if this layout line is inside the selection
|
|
|
|
|
let mut inside = false;
|
|
|
|
|
if line_i > start.line.get() && line_i < end.line.get() {
|
|
|
|
|
// In between start and end lines, definitely inside selection
|
|
|
|
|
inside = true;
|
|
|
|
|
} else {
|
2022-10-21 11:27:21 -06:00
|
|
|
if line_i == start.line.get() && line_i == end.line.get() {
|
|
|
|
|
// On edge of start and end line, check if any contained glyphs are after start and before end
|
|
|
|
|
for glyph in layout_line.glyphs.iter() {
|
2022-10-21 11:44:11 -06:00
|
|
|
if glyph.start >= start.index && glyph.end <= end.index {
|
2022-10-21 11:27:21 -06:00
|
|
|
inside = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if line_i == start.line.get() {
|
2022-10-21 11:19:24 -06:00
|
|
|
// On edge of start line, check if any contained glyphs are after start
|
|
|
|
|
for glyph in layout_line.glyphs.iter() {
|
2022-10-21 11:44:11 -06:00
|
|
|
if glyph.start >= start.index {
|
2022-10-21 11:19:24 -06:00
|
|
|
inside = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-21 11:27:21 -06:00
|
|
|
} else if line_i == end.line.get() {
|
2022-10-21 11:19:24 -06:00
|
|
|
// On edge of end line, check if any contained glyphs are before end
|
|
|
|
|
for glyph in layout_line.glyphs.iter() {
|
2022-10-21 11:44:11 -06:00
|
|
|
if glyph.end <= end.index {
|
2022-10-21 11:19:24 -06:00
|
|
|
inside = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inside {
|
2022-10-21 09:49:16 -06:00
|
|
|
let start_glyph = if start.line.get() == line_i {
|
|
|
|
|
cursor_glyph_opt(&start).unwrap_or(0)
|
2022-10-20 19:34:56 -06:00
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
};
|
2022-10-19 10:12:52 -06:00
|
|
|
|
2022-10-21 09:49:16 -06:00
|
|
|
let end_glyph = if end.line.get() == line_i {
|
|
|
|
|
cursor_glyph_opt(&end).unwrap_or(layout_line.glyphs.len() + 1)
|
2022-10-19 10:44:21 -06:00
|
|
|
} else {
|
2022-10-21 09:49:16 -06:00
|
|
|
layout_line.glyphs.len() + 1
|
2022-10-19 10:44:21 -06:00
|
|
|
};
|
2022-10-19 10:20:22 -06:00
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
if end_glyph > start_glyph {
|
2022-10-21 09:49:16 -06:00
|
|
|
let (left_x, right_x) = if shape.rtl {
|
2022-10-20 19:34:56 -06:00
|
|
|
(
|
2022-10-21 09:49:16 -06:00
|
|
|
layout_line.glyphs.get(end_glyph - 1).map_or(0, |glyph| {
|
2022-10-20 19:34:56 -06:00
|
|
|
glyph.x as i32
|
|
|
|
|
}),
|
2022-10-21 09:49:16 -06:00
|
|
|
layout_line.glyphs.get(start_glyph).map_or(self.width, |glyph| {
|
2022-10-20 19:34:56 -06:00
|
|
|
(glyph.x + glyph.w) as i32
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(
|
2022-10-21 09:49:16 -06:00
|
|
|
layout_line.glyphs.get(start_glyph).map_or(0, |glyph| {
|
2022-10-20 19:34:56 -06:00
|
|
|
glyph.x as i32
|
|
|
|
|
}),
|
2022-10-21 09:49:16 -06:00
|
|
|
layout_line.glyphs.get(end_glyph - 1).map_or(self.width, |glyph| {
|
2022-10-20 19:34:56 -06:00
|
|
|
(glyph.x + glyph.w) as i32
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
f(
|
|
|
|
|
left_x,
|
|
|
|
|
line_y - font_size,
|
|
|
|
|
cmp::max(0, right_x - left_x) as u32,
|
|
|
|
|
line_height as u32,
|
|
|
|
|
0x33_00_00_00 | (color & 0xFF_FF_FF)
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-10-19 10:20:22 -06:00
|
|
|
}
|
2022-10-19 10:12:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
// Draw cursor
|
2022-10-21 11:51:04 -06:00
|
|
|
if let Some(cursor_glyph) = cursor_glyph_opt(&self.cursor) {
|
|
|
|
|
let x = match layout_line.glyphs.get(cursor_glyph) {
|
|
|
|
|
Some(glyph) => {
|
|
|
|
|
// Start of detected glyph
|
|
|
|
|
if shape.rtl {
|
|
|
|
|
(glyph.x + glyph.w) as i32
|
|
|
|
|
} else {
|
|
|
|
|
glyph.x as i32
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => match layout_line.glyphs.last() {
|
2022-10-21 09:49:16 -06:00
|
|
|
Some(glyph) => {
|
2022-10-21 11:51:04 -06:00
|
|
|
// End of last glyph
|
2022-10-21 09:49:16 -06:00
|
|
|
if shape.rtl {
|
|
|
|
|
glyph.x as i32
|
2022-10-21 11:51:04 -06:00
|
|
|
} else {
|
|
|
|
|
(glyph.x + glyph.w) as i32
|
2022-10-21 09:49:16 -06:00
|
|
|
}
|
|
|
|
|
},
|
2022-10-21 11:51:04 -06:00
|
|
|
None => {
|
|
|
|
|
// Start of empty line
|
|
|
|
|
0
|
2022-10-21 09:49:16 -06:00
|
|
|
}
|
2022-10-21 11:51:04 -06:00
|
|
|
}
|
|
|
|
|
};
|
2022-10-19 10:44:21 -06:00
|
|
|
|
2022-10-21 11:51:04 -06:00
|
|
|
println!("x: {}", x);
|
2022-10-21 09:02:15 -06:00
|
|
|
|
2022-10-21 11:51:04 -06:00
|
|
|
f(
|
|
|
|
|
x,
|
|
|
|
|
line_y - font_size,
|
|
|
|
|
1,
|
|
|
|
|
line_height as u32,
|
|
|
|
|
color,
|
|
|
|
|
);
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
layout_line.draw(color, |x, y, color| {
|
|
|
|
|
f(x, line_y + y, 1, 1, color);
|
|
|
|
|
});
|
2022-10-19 07:36:27 -06:00
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
line_y += line_height;
|
|
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|