cursor finally working properly and removed from render_text
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2354,6 +2354,7 @@ dependencies = [
|
||||
"cosmic-text",
|
||||
"image",
|
||||
"pollster",
|
||||
"unicode-segmentation",
|
||||
"wgpu",
|
||||
"winit",
|
||||
]
|
||||
|
||||
@@ -12,4 +12,5 @@ wgpu = "26.0.1"
|
||||
bytemuck = "1.23.1"
|
||||
image = "0.25.6"
|
||||
cosmic-text = "0.14.2"
|
||||
unicode-segmentation = "1.12.0"
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::ops::Range;
|
||||
use crate::{
|
||||
layout::{
|
||||
Active, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle, Textures, UiRegion,
|
||||
Vec2, VisualCursor, WidgetId, Widgets,
|
||||
Vec2, WidgetId, Widgets,
|
||||
},
|
||||
render::{Primitive, PrimitiveHandle, Primitives},
|
||||
util::{HashSet, Id},
|
||||
@@ -231,46 +231,53 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
|
||||
impl<'a, 'c> Painter<'a, 'c> {
|
||||
fn write_at<P: Primitive>(&mut self, data: P, region: UiRegion) {
|
||||
self.primitives
|
||||
.push(self.ctx.primitives.write(self.id.duplicate(), data, region));
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
self.primitives.push(
|
||||
self.ctx
|
||||
.primitives
|
||||
.write(self.id.duplicate(), primitive, region),
|
||||
);
|
||||
}
|
||||
|
||||
/// Writes a primitive to be rendered
|
||||
pub fn write<P: Primitive>(&mut self, data: P) {
|
||||
self.write_at(data, self.region)
|
||||
pub fn primitive<P: Primitive>(&mut self, primitive: P) {
|
||||
self.primitive_at(primitive, self.region)
|
||||
}
|
||||
|
||||
pub fn primitive_within<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
self.primitive_at(primitive, region.within(&self.region));
|
||||
}
|
||||
|
||||
/// Draws a widget within this widget's region.
|
||||
pub fn draw<W>(&mut self, id: &WidgetId<W>) {
|
||||
self.draw_at(id, self.region);
|
||||
pub fn widget<W>(&mut self, id: &WidgetId<W>) {
|
||||
self.widget_at(id, self.region);
|
||||
}
|
||||
|
||||
/// Draws a widget somewhere within this one.
|
||||
/// Useful for drawing child widgets in select areas.
|
||||
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||
self.draw_at(id, region.within(&self.region));
|
||||
pub fn widget_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||
self.widget_at(id, region.within(&self.region));
|
||||
}
|
||||
|
||||
fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||
fn widget_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||
self.children.push(id.id.duplicate());
|
||||
self.ctx
|
||||
.draw_inner(&id.id, region, Some(self.id.duplicate()), None);
|
||||
}
|
||||
|
||||
pub fn draw_texture_within(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
pub fn texture_within(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
self.textures.push(handle.clone());
|
||||
self.write_at(handle.primitive(), region.within(&self.region));
|
||||
self.primitive_at(handle.primitive(), region.within(&self.region));
|
||||
}
|
||||
|
||||
pub fn draw_texture(&mut self, handle: &TextureHandle) {
|
||||
pub fn texture(&mut self, handle: &TextureHandle) {
|
||||
self.textures.push(handle.clone());
|
||||
self.write(handle.primitive());
|
||||
self.primitive(handle.primitive());
|
||||
}
|
||||
|
||||
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
pub fn texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
self.textures.push(handle.clone());
|
||||
self.write_at(handle.primitive(), region);
|
||||
self.primitive_at(handle.primitive(), region);
|
||||
}
|
||||
|
||||
/// returns (handle, offset from top left)
|
||||
@@ -278,9 +285,8 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
&mut self,
|
||||
buffer: &mut TextBuffer,
|
||||
attrs: &TextAttrs,
|
||||
cursor: &VisualCursor,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
self.ctx.text.draw(buffer, attrs, cursor, self.ctx.textures)
|
||||
self.ctx.text.draw(buffer, attrs, self.ctx.textures)
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
@@ -327,9 +333,8 @@ impl SizeCtx<'_> {
|
||||
&mut self,
|
||||
buffer: &mut TextBuffer,
|
||||
attrs: &TextAttrs,
|
||||
cursor: &VisualCursor,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
self.text.draw(buffer, attrs, cursor, self.textures)
|
||||
self.text.draw(buffer, attrs, self.textures)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,16 +39,6 @@ impl TextAttrs {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Copy, Clone)]
|
||||
pub enum VisualCursor {
|
||||
#[default]
|
||||
None,
|
||||
Select {
|
||||
line: isize,
|
||||
col: isize,
|
||||
},
|
||||
}
|
||||
|
||||
pub type TextBuffer = Buffer;
|
||||
|
||||
impl Default for TextAttrs {
|
||||
@@ -68,7 +58,6 @@ impl TextData {
|
||||
&mut self,
|
||||
buffer: &mut TextBuffer,
|
||||
attrs: &TextAttrs,
|
||||
cursor: &VisualCursor,
|
||||
textures: &mut Textures,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
let mut pixels = HashMap::new();
|
||||
@@ -76,30 +65,20 @@ impl TextData {
|
||||
let mut min_y = 0;
|
||||
let mut max_x = 0;
|
||||
let mut max_y = 0;
|
||||
let c = attrs.color;
|
||||
let cosmic_color = {
|
||||
let c = attrs.color;
|
||||
cosmic_text::Color::rgba(c.r, c.g, c.b, c.a)
|
||||
};
|
||||
let mut max_width = 0.0f32;
|
||||
let mut cursor_x = 0;
|
||||
for (run_i, run) in buffer.layout_runs().enumerate() {
|
||||
if let VisualCursor::Select { line, .. } = cursor
|
||||
&& *line == run_i as isize
|
||||
{
|
||||
cursor_x = run.line_w as i32;
|
||||
}
|
||||
for (i, glyph) in run.glyphs.iter().enumerate() {
|
||||
for run in buffer.layout_runs() {
|
||||
for glyph in run.glyphs.iter() {
|
||||
let physical_glyph = glyph.physical((0., 0.), 1.0);
|
||||
|
||||
let glyph_color = match glyph.color_opt {
|
||||
Some(some) => some,
|
||||
None => cosmic_text::Color::rgba(c.r, c.g, c.b, c.a),
|
||||
None => cosmic_color,
|
||||
};
|
||||
|
||||
if let VisualCursor::Select { col: idx, line } = cursor
|
||||
&& *line == run_i as isize
|
||||
&& *idx == i as isize
|
||||
{
|
||||
cursor_x = physical_glyph.x;
|
||||
}
|
||||
|
||||
self.swash_cache.with_pixels(
|
||||
&mut self.font_system,
|
||||
physical_glyph.cache_key,
|
||||
@@ -117,11 +96,6 @@ impl TextData {
|
||||
}
|
||||
max_width = max_width.max(run.line_w);
|
||||
}
|
||||
if let &VisualCursor::Select { line, .. } = cursor {
|
||||
let y = (attrs.line_height * (line + 1) as f32) as i32 - 1;
|
||||
max_y = max_y.max(y);
|
||||
max_x = max_x.max(cursor_x);
|
||||
}
|
||||
let width = (max_x - min_x + 1) as u32;
|
||||
let height = (max_y - min_y + 1) as u32;
|
||||
let mut image = RgbaImage::new(width, height);
|
||||
@@ -130,15 +104,6 @@ impl TextData {
|
||||
let y = (y - min_y) as u32;
|
||||
image.put_pixel(x, y, color);
|
||||
}
|
||||
if let &VisualCursor::Select { line, .. } = cursor {
|
||||
let x = (cursor_x - min_x) as u32;
|
||||
for y in 0..attrs.line_height as u32 {
|
||||
// no clue if this is good or bad for non integer values
|
||||
// depends on how the layouting is actually done
|
||||
let y = (y as f32 + attrs.line_height * line as f32 - min_y as f32) as u32;
|
||||
image.put_pixel(x, y, Rgba(c.as_arr()));
|
||||
}
|
||||
}
|
||||
let lines = buffer.lines.len();
|
||||
let offset = TextOffset {
|
||||
top_left: Vec2::new(min_x as f32, min_y as f32),
|
||||
|
||||
@@ -11,8 +11,8 @@ pub struct Vec2 {
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
pub const fn vec2(x: f32, y: f32) -> Vec2 {
|
||||
Vec2::new(x, y)
|
||||
pub const fn vec2(x: impl const UiNum, y: impl const UiNum) -> Vec2 {
|
||||
Vec2::new(x.to_f32(), y.to_f32())
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
|
||||
@@ -212,6 +212,17 @@ pub struct RectPrimitive {
|
||||
pub inner_radius: f32,
|
||||
}
|
||||
|
||||
impl RectPrimitive {
|
||||
pub fn color(color: Color<u8>) -> Self {
|
||||
Self {
|
||||
color,
|
||||
radius: 0.0,
|
||||
thickness: 0.0,
|
||||
inner_radius: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct TexturePrimitive {
|
||||
|
||||
Reference in New Issue
Block a user