actually use the text library for text editing (fully working I think but code isn't cleanest)
This commit is contained in:
137
src/core/text.rs
137
src/core/text.rs
@@ -1,4 +1,4 @@
|
||||
use cosmic_text::{Family, Metrics};
|
||||
use cosmic_text::{Attrs, Family, FontSystem, Metrics, Shaping};
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
@@ -7,8 +7,7 @@ pub struct Text {
|
||||
pub attrs: TextAttrs,
|
||||
/// inner alignment of text region (within where its drawn)
|
||||
pub align: Align,
|
||||
buf: TextBuffer,
|
||||
cursor: Cursor,
|
||||
pub(super) buf: TextBuffer,
|
||||
size: Vec2,
|
||||
}
|
||||
|
||||
@@ -37,129 +36,33 @@ impl Text {
|
||||
buf: TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height)),
|
||||
attrs,
|
||||
align: Align::Center,
|
||||
cursor: Cursor::None,
|
||||
size: Vec2::ZERO,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&mut self, pos: Vec2, size: Vec2) {
|
||||
let pos = pos - self.region().top_left.to_size(size);
|
||||
let Some(cursor) = self.buf.hit(pos.x, pos.y) else {
|
||||
return;
|
||||
};
|
||||
self.cursor = Cursor::Select {
|
||||
line: cursor.line as isize,
|
||||
col: cursor.index as isize,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deselect(&mut self) {
|
||||
self.cursor = Cursor::None;
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, text: &str) {
|
||||
let i = self.update_cursor();
|
||||
self.content.insert_str(i, text);
|
||||
|
||||
match &mut self.cursor {
|
||||
Cursor::None => (),
|
||||
Cursor::Select { col, .. } => {
|
||||
*col += 1;
|
||||
}
|
||||
}
|
||||
self.update_cursor();
|
||||
}
|
||||
|
||||
pub fn backspace(&mut self) {
|
||||
if let Some(i) = self
|
||||
.update_cursor()
|
||||
.checked_sub(1)
|
||||
.map(|i| self.content.floor_char_boundary(i))
|
||||
{
|
||||
self.content.remove(i);
|
||||
match &mut self.cursor {
|
||||
Cursor::None => (),
|
||||
Cursor::Select { col, .. } => {
|
||||
*col -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete(&mut self) {
|
||||
let i = self.update_cursor();
|
||||
if i != self.content.len() {
|
||||
self.content.remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_cursor(&mut self, dir: Dir) {
|
||||
self.update_cursor();
|
||||
if let Cursor::Select { line, col } = &mut self.cursor {
|
||||
match dir {
|
||||
Dir::LEFT => *col -= 1,
|
||||
Dir::RIGHT => *col += 1,
|
||||
Dir::UP => *line -= 1,
|
||||
Dir::DOWN => *line += 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_cursor(&mut self) -> usize {
|
||||
match &mut self.cursor {
|
||||
Cursor::None => 0,
|
||||
Cursor::Select { line, col } => {
|
||||
if *col < 0 {
|
||||
*line -= 1;
|
||||
}
|
||||
if *line < 0 {
|
||||
*line = 0;
|
||||
*col = 0;
|
||||
}
|
||||
let mut idx = self.content.len();
|
||||
let mut l = 0;
|
||||
let mut c = 0;
|
||||
let mut cur_len = 0;
|
||||
for (i, ch) in self.content.char_indices() {
|
||||
if ch == '\n' {
|
||||
l += 1;
|
||||
c = 0;
|
||||
} else {
|
||||
if l == *line {
|
||||
cur_len = c + 1;
|
||||
if c == *col {
|
||||
idx = i;
|
||||
}
|
||||
}
|
||||
c += 1;
|
||||
}
|
||||
}
|
||||
if *col < 0 {
|
||||
*col = cur_len;
|
||||
}
|
||||
if *col > cur_len {
|
||||
*col = 0;
|
||||
*line += 1;
|
||||
}
|
||||
if *line > l {
|
||||
*line = l;
|
||||
*col = cur_len;
|
||||
}
|
||||
idx
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
UiRegion::from_size_align(self.size, self.align)
|
||||
}
|
||||
|
||||
fn update_buf(&mut self, font_system: &mut FontSystem) {
|
||||
self.buf.set_metrics(
|
||||
font_system,
|
||||
Metrics::new(self.attrs.font_size, self.attrs.line_height),
|
||||
);
|
||||
self.buf.set_text(
|
||||
font_system,
|
||||
&self.content,
|
||||
&Attrs::new().family(self.attrs.family),
|
||||
Shaping::Advanced,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Text {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
self.update_cursor();
|
||||
let (handle, offset) =
|
||||
painter.render_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
|
||||
let font_system = &mut painter.text_data().font_system;
|
||||
self.update_buf(font_system);
|
||||
let (handle, offset) = painter.render_text(&mut self.buf, &self.attrs, &VisualCursor::None);
|
||||
let dims = handle.size();
|
||||
self.size = offset.size(&handle);
|
||||
let mut region = self.region();
|
||||
@@ -169,8 +72,8 @@ impl Widget for Text {
|
||||
}
|
||||
|
||||
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||
let (handle, offset) =
|
||||
ctx.draw_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
|
||||
self.update_buf(&mut ctx.text.font_system);
|
||||
let (handle, offset) = ctx.draw_text(&mut self.buf, &self.attrs, &VisualCursor::None);
|
||||
offset.size(&handle)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user