Compute grapheme index in find_cursor_position for TextInput
This commit is contained in:
parent
51844c5d0c
commit
26e902f7d8
2 changed files with 13 additions and 15 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
//! Draw and interact with text.
|
//! Draw and interact with text.
|
||||||
use crate::alignment;
|
use crate::alignment;
|
||||||
use crate::{Color, Point, Rectangle, Size, Vector};
|
use crate::{Color, Point, Rectangle, Size};
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
|
@ -34,10 +34,6 @@ pub struct Text<'a, Font> {
|
||||||
pub enum Hit {
|
pub enum Hit {
|
||||||
/// The point was within the bounds of the returned character index.
|
/// The point was within the bounds of the returned character index.
|
||||||
CharOffset(usize),
|
CharOffset(usize),
|
||||||
/// The provided point was not within the bounds of a glyph. The index
|
|
||||||
/// of the character with the closest centeroid position is returned,
|
|
||||||
/// as well as its delta.
|
|
||||||
NearestCharOffset(usize, Vector),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hit {
|
impl Hit {
|
||||||
|
|
@ -45,13 +41,6 @@ impl Hit {
|
||||||
pub fn cursor(self) -> usize {
|
pub fn cursor(self) -> usize {
|
||||||
match self {
|
match self {
|
||||||
Self::CharOffset(i) => i,
|
Self::CharOffset(i) => i,
|
||||||
Self::NearestCharOffset(i, delta) => {
|
|
||||||
if delta.x > f32::EPSILON {
|
|
||||||
i + 1
|
|
||||||
} else {
|
|
||||||
i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1193,17 +1193,26 @@ where
|
||||||
let size = size.unwrap_or_else(|| renderer.default_size());
|
let size = size.unwrap_or_else(|| renderer.default_size());
|
||||||
|
|
||||||
let offset = offset(renderer, text_bounds, font, size, value, state);
|
let offset = offset(renderer, text_bounds, font, size, value, state);
|
||||||
|
let value = value.to_string();
|
||||||
|
|
||||||
renderer
|
let char_offset = renderer
|
||||||
.hit_test(
|
.hit_test(
|
||||||
&value.to_string(),
|
&value,
|
||||||
size,
|
size,
|
||||||
font,
|
font,
|
||||||
Size::INFINITY,
|
Size::INFINITY,
|
||||||
Point::new(x + offset, text_bounds.height / 2.0),
|
Point::new(x + offset, text_bounds.height / 2.0),
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
.map(text::Hit::cursor)
|
.map(text::Hit::cursor)?;
|
||||||
|
|
||||||
|
Some(
|
||||||
|
unicode_segmentation::UnicodeSegmentation::graphemes(
|
||||||
|
&value[..char_offset],
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
.count(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const CURSOR_BLINK_INTERVAL_MILLIS: u128 = 500;
|
const CURSOR_BLINK_INTERVAL_MILLIS: u128 = 500;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue