cursor finally working properly and removed from render_text
This commit is contained in:
@@ -8,7 +8,7 @@ pub struct Aligned {
|
||||
impl Widget for Aligned {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let region = UiRegion::from_size_align(painter.size(&self.inner), self.align);
|
||||
painter.draw_within(&self.inner, region);
|
||||
painter.widget_within(&self.inner, region);
|
||||
}
|
||||
|
||||
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||
|
||||
@@ -7,7 +7,7 @@ pub struct Padded {
|
||||
|
||||
impl Widget for Padded {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.draw_within(&self.inner, self.padding.region());
|
||||
painter.widget_within(&self.inner, self.padding.region());
|
||||
}
|
||||
|
||||
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||
|
||||
@@ -7,7 +7,7 @@ pub struct Image {
|
||||
|
||||
impl Widget for Image {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.draw_texture(&self.handle);
|
||||
painter.texture(&self.handle);
|
||||
}
|
||||
|
||||
fn get_size(&mut self, _: &mut SizeCtx) -> Vec2 {
|
||||
|
||||
@@ -29,7 +29,7 @@ impl Rect {
|
||||
|
||||
impl Widget for Rect {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.write(RectPrimitive {
|
||||
painter.primitive(RectPrimitive {
|
||||
color: self.color,
|
||||
radius: self.radius,
|
||||
thickness: self.thickness,
|
||||
|
||||
@@ -7,7 +7,7 @@ pub struct Sized {
|
||||
|
||||
impl Widget for Sized {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.draw(&self.inner);
|
||||
painter.widget(&self.inner);
|
||||
}
|
||||
|
||||
fn get_size(&mut self, _: &mut SizeCtx) -> Vec2 {
|
||||
|
||||
@@ -39,7 +39,7 @@ impl Widget for Span {
|
||||
if self.dir.sign == Sign::Neg {
|
||||
child_region.flip();
|
||||
}
|
||||
painter.draw_within(child, child_region);
|
||||
painter.widget_within(child, child_region);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ pub struct Stack {
|
||||
impl Widget for Stack {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
for child in &self.children {
|
||||
painter.draw(child);
|
||||
painter.widget(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,18 +62,18 @@ impl Widget for Text {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
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 (handle, 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);
|
||||
painter.texture_within(&handle, region);
|
||||
}
|
||||
|
||||
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||
self.update_buf(&mut ctx.text.font_system);
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(®ion),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user