fix(text_input): RTL text cursor and highlight fixes

This commit is contained in:
Hojjat 2026-04-01 11:44:58 -06:00 committed by Michael Murphy
parent c33455e9ad
commit 2299fba69b
3 changed files with 365 additions and 157 deletions

View file

@ -132,11 +132,34 @@ impl Value {
graphemes: std::iter::repeat_n(String::from(""), self.graphemes.len()).collect(),
}
}
}
impl ToString for Value {
#[inline]
fn to_string(&self) -> String {
self.graphemes.concat()
/// Converts a grapheme index to a byte index in the underlying string.
#[must_use]
pub fn byte_index_at_grapheme(&self, grapheme_index: usize) -> usize {
self.graphemes[..grapheme_index.min(self.graphemes.len())]
.iter()
.map(|g| g.len())
.sum()
}
/// Converts a byte index to a grapheme index.
#[must_use]
pub fn grapheme_index_at_byte(&self, byte_index: usize) -> usize {
let mut bytes = 0;
for (i, g) in self.graphemes.iter().enumerate() {
if bytes >= byte_index {
return i;
}
bytes += g.len();
}
self.graphemes.len()
}
}
impl std::fmt::Display for Value {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.graphemes.concat())
}
}