feat: Add rtl support to text input

This commit is contained in:
Hojjat 2026-03-31 14:24:39 -06:00 committed by Ashley Wulber
parent de2982b37e
commit 9b2857083e
6 changed files with 411 additions and 134 deletions

View file

@ -278,16 +278,34 @@ impl Hash for LineHeight {
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Hit {
/// The point was within the bounds of the returned character index.
CharOffset(usize),
CharOffset(usize, Affinity),
}
impl Hit {
/// Computes the cursor position of the [`Hit`] .
pub fn cursor(self) -> usize {
match self {
Self::CharOffset(i) => i,
Self::CharOffset(i, _) => i,
}
}
/// Returns the cursor [`Affinity`] of the [`Hit`].
pub fn affinity(&self) -> Affinity {
match self {
Self::CharOffset(_, a) => *a,
}
}
}
/// Cursor affinity for BiDi text. At the boundary between runs of different
/// directions, the same byte index can map to different visual positions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Affinity {
/// Associate with the run before the cursor index.
#[default]
Before,
/// Associate with the run after the cursor index.
After,
}
/// The difference detected in some text.

View file

@ -1,7 +1,8 @@
//! Draw paragraphs.
use crate::alignment;
use crate::text::{
Alignment, Difference, Hit, LineHeight, Shaping, Span, Text, Wrapping,
Affinity, Alignment, Difference, Hit, LineHeight, Shaping, Span, Text,
Wrapping,
};
use crate::{Pixels, Point, Rectangle, Size};
@ -71,6 +72,32 @@ pub trait Paragraph: Sized + Default {
/// Returns the distance to the given grapheme index in the [`Paragraph`].
fn grapheme_position(&self, line: usize, index: usize) -> Option<Point>;
/// Returns the visual position of a cursor at the given byte index and [`Affinity`].
fn cursor_position(
&self,
_line: usize,
_byte_index: usize,
_affinity: Affinity,
) -> Option<Point> {
None
}
/// Returns highlight rectangles for a text selection. For mixed BiDi text,
/// this may return multiple rectangles.
fn highlight(
&self,
_line: usize,
_start: (usize, Affinity),
_end: (usize, Affinity),
) -> Vec<Rectangle> {
Vec::new()
}
/// Returns `true` if the line is RTL, `false` for LTR.
fn is_rtl(&self, _line: usize) -> Option<bool> {
None
}
/// Returns the minimum width that can fit the contents of the [`Paragraph`].
fn min_width(&self) -> f32 {
self.min_bounds().width
@ -81,7 +108,6 @@ pub trait Paragraph: Sized + Default {
self.min_bounds().height
}
/// Returns the [`Ellipsize`] strategy of the [`Paragraph`]>
fn ellipsize(&self) -> Ellipsize;
}
@ -180,7 +206,7 @@ impl<P: Paragraph> Plain<P> {
align_y: self.raw.align_y(),
shaping: self.raw.shaping(),
wrapping: self.raw.wrapping(),
ellipsize: self.raw.ellipsize()
ellipsize: self.raw.ellipsize(),
}
}
}