cursor finally working properly and removed from render_text

This commit is contained in:
2025-09-15 20:30:26 -04:00
parent 9d659b6afd
commit 2700c31c13
15 changed files with 110 additions and 118 deletions

View File

@@ -1,5 +1,6 @@
use crate::prelude::*;
use cosmic_text::{Attrs, Cursor, Family, FontSystem, Metrics, Motion, Shaping};
use unicode_segmentation::UnicodeSegmentation;
pub struct TextEdit {
pub attrs: TextAttrs,
@@ -21,31 +22,60 @@ impl Widget for TextEdit {
let font_system = &mut painter.text_data().font_system;
self.buf.shape_until_scroll(font_system, false);
self.attrs.apply(font_system, &mut self.buf);
let (handle, offset) = painter.render_text(
&mut self.buf,
&self.attrs,
&match self.cursor {
None => VisualCursor::None,
Some(cursor) => VisualCursor::Select {
line: cursor.line as isize,
col: cursor.index as isize,
},
},
);
let (handle, tex_offset) = painter.render_text(&mut self.buf, &self.attrs);
let dims = handle.size();
self.size = offset.size(&handle);
let mut region = self.region();
region.top_left.offset += offset.top_left;
region.bot_right.offset = region.top_left.offset + dims;
painter.draw_texture_within(&handle, region);
self.size = tex_offset.size(&handle);
let region = self.region();
let mut tex_region = region;
tex_region.top_left.offset += tex_offset.top_left;
tex_region.bot_right.offset = tex_region.top_left.offset + dims;
painter.texture_within(&handle, tex_region);
if let Some(cursor) = &self.cursor
&& let Some(pos) = cursor_pos(cursor, &self.buf)
{
let size = vec2(1, self.attrs.line_height);
let offset = vec2(pos, cursor.line as f32 * self.attrs.line_height);
painter.primitive_within(
RectPrimitive::color(Color::WHITE),
UiRegion::from_size_align(size, Align::TopLeft)
.shifted(offset)
.within(&region),
);
}
}
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
let (handle, offset) = ctx.draw_text(&mut self.buf, &self.attrs, &VisualCursor::None);
let (handle, offset) = ctx.draw_text(&mut self.buf, &self.attrs);
offset.size(&handle)
}
}
/// copied & modified from fn found in Editor in cosmic_text
fn cursor_pos(cursor: &Cursor, buf: &TextBuffer) -> Option<f32> {
let run = buf.layout_runs().find(|r| r.line_i == cursor.line)?;
for glyph in run.glyphs.iter() {
if cursor.index == glyph.start {
return Some(glyph.x);
} else if cursor.index > glyph.start && cursor.index < glyph.end {
// Guess x offset based on characters
let mut before = 0;
let mut total = 0;
let cluster = &run.text[glyph.start..glyph.end];
for (i, _) in cluster.grapheme_indices(true) {
if glyph.start + i < cursor.index {
before += 1;
}
total += 1;
}
let offset = glyph.w * (before as f32) / (total as f32);
return Some(glyph.x + offset);
}
}
Some(run.line_w)
}
pub struct TextEditBuilder {
pub content: String,
pub attrs: TextAttrs,
@@ -112,26 +142,11 @@ impl<'a> TextEditCtx<'a> {
}
pub fn backspace(&mut self) {
if let Some(cursor) = &mut self.text.cursor {
if cursor.index == 0 {
if cursor.line == 0 {
return;
}
let add = self.text.buf.lines.remove(cursor.line).into_text();
cursor.line -= 1;
let line = &mut self.text.buf.lines[cursor.line];
let mut cur = line.text().to_string();
cursor.index = cur.len();
cur.push_str(&add);
line.set_text(cur, line.ending(), line.attrs_list().clone());
} else {
let line = &mut self.text.buf.lines[cursor.line];
let mut text = line.text().to_string();
let idx = text.floor_char_boundary(cursor.index - 1);
text.remove(idx);
line.set_text(text, line.ending(), line.attrs_list().clone());
cursor.index = idx;
}
if let Some(cursor) = &mut self.text.cursor
&& (cursor.index != 0 || cursor.line != 0)
{
self.motion(Motion::Left);
self.delete();
}
}
@@ -153,12 +168,6 @@ impl<'a> TextEditCtx<'a> {
line.set_text(text, line.ending(), line.attrs_list().clone());
}
}
if let Some(cursor) = self.text.cursor {
let line = &mut self.text.buf.lines[cursor.line];
let mut text = line.text().to_string();
text.remove(cursor.index);
line.set_text(text, line.ending(), line.attrs_list().clone());
}
}
pub fn select(&mut self, pos: Vec2, size: Vec2) {