initial text wrapping impl (resizing will break)

This commit is contained in:
2025-09-28 01:32:10 -04:00
parent b2950566af
commit 61df088cc7
8 changed files with 126 additions and 55 deletions

View File

@@ -44,11 +44,8 @@ impl Text {
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),
);
fn update_buf(&mut self, font_system: &mut FontSystem, width: Option<f32>) {
self.attrs.apply(font_system, &mut self.buf, width);
if self.content.changed {
self.content.changed = false;
self.buf.set_text(
@@ -63,8 +60,13 @@ impl Text {
impl Widget for Text {
fn draw(&mut self, painter: &mut Painter) {
let width = if self.attrs.wrap {
Some(painter.px_size().x)
} else {
None
};
let font_system = &mut painter.text_data().font_system;
self.update_buf(font_system);
self.update_buf(font_system, width);
let (handle, offset) = painter.render_text(&mut self.buf, &self.attrs);
let dims = handle.size();
self.size = offset.size(&handle);
@@ -75,7 +77,7 @@ impl Widget for Text {
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> UiVec2 {
self.update_buf(&mut ctx.text.font_system);
self.update_buf(&mut ctx.text.font_system, None);
let (handle, offset) = ctx.draw_text(&mut self.buf, &self.attrs);
UiVec2::abs(offset.size(&handle))
}
@@ -137,7 +139,7 @@ impl FnOnce<(&mut Ui,)> for TextBuilder {
};
text.content.changed = false;
self.attrs
.apply(&mut args.0.text.font_system, &mut text.buf);
.apply(&mut args.0.text.font_system, &mut text.buf, None);
text
}
}

View File

@@ -23,8 +23,13 @@ impl TextEdit {
impl Widget for TextEdit {
fn draw(&mut self, painter: &mut Painter) {
let width = if self.attrs.wrap {
Some(painter.px_size().x)
} else {
None
};
let font_system = &mut painter.text_data().font_system;
self.attrs.apply(font_system, &mut self.buf);
self.attrs.apply(font_system, &mut self.buf, width);
self.buf.shape_until_scroll(font_system, false);
let (handle, tex_offset) = painter.render_text(&mut self.buf, &self.attrs);
let dims = handle.size();
@@ -36,10 +41,9 @@ impl Widget for TextEdit {
painter.texture_within(&handle, tex_region);
if let Some(cursor) = &self.cursor
&& let Some(pos) = cursor_pos(cursor, &self.buf)
&& let Some(offset) = 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)
@@ -53,35 +57,51 @@ impl Widget for TextEdit {
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> UiVec2 {
let width = if self.attrs.wrap {
Some(ctx.px_size().x)
} else {
None
};
self.attrs
.apply(&mut ctx.text.font_system, &mut self.buf, width);
self.buf
.shape_until_scroll(&mut ctx.text.font_system, false);
let (handle, offset) = ctx.draw_text(&mut self.buf, &self.attrs);
UiVec2::abs(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;
fn cursor_pos(cursor: &Cursor, buf: &TextBuffer) -> Option<(f32, f32)> {
let mut prev = None;
for run in buf
.layout_runs()
.skip_while(|r| r.line_i < cursor.line)
.take_while(|r| r.line_i == cursor.line)
{
prev = Some((run.line_w, run.line_top));
for glyph in run.glyphs.iter() {
if cursor.index == glyph.start {
return Some((glyph.x, run.line_top));
} 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;
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;
}
total += 1;
}
let offset = glyph.w * (before as f32) / (total as f32);
return Some(glyph.x + offset);
let offset = glyph.w * (before as f32) / (total as f32);
return Some((glyph.x + offset, run.line_top));
}
}
}
Some(run.line_w)
prev
}
pub struct TextEditBuilder {
@@ -113,6 +133,10 @@ impl TextEditBuilder {
self.align = align;
self
}
pub fn wrap(mut self, wrap: bool) -> Self {
self.attrs.wrap = wrap;
self
}
}
pub struct TextEditCtx<'a> {
@@ -305,7 +329,7 @@ impl FnOnce<(&mut Ui,)> for TextEditBuilder {
Shaping::Advanced,
);
self.attrs
.apply(&mut args.0.text.font_system, &mut text.buf);
.apply(&mut args.0.text.font_system, &mut text.buf, None);
text
}
}