File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -188,6 +188,8 @@ pub struct HitPoint {
188
188
pub line : usize ,
189
189
/// First-byte-index of glyph at cursor (will insert behind this glyph)
190
190
pub index : usize ,
191
+ /// Code-point index of glyph at cursor (use with String::chars())
192
+ pub char_index : usize ,
191
193
/// Whether or not the point was inside the bounds of the layout object.
192
194
///
193
195
/// A click outside the layout object will still resolve to a position in the
@@ -354,15 +356,32 @@ impl TextLayout {
354
356
if let Some ( cursor) = self . hit ( point. x as f32 , point. y as f32 ) {
355
357
let size = self . size ( ) ;
356
358
let is_inside = point. x <= size. width && point. y <= size. height ;
359
+
360
+ // FIXME: It seems that there is no API to get the char-point index of the text directly
361
+ // So it have to calculate it manually.
362
+ let char_index = {
363
+ let mut byte_index = 0 ;
364
+ self . buffer
365
+ . lines
366
+ . iter ( )
367
+ . flat_map ( |x| x. text ( ) . chars ( ) )
368
+ . take_while ( |x| {
369
+ byte_index += x. len_utf8 ( ) ;
370
+ byte_index <= cursor. index
371
+ } )
372
+ . count ( )
373
+ } ;
357
374
HitPoint {
358
375
line : cursor. line ,
359
376
index : cursor. index ,
377
+ char_index,
360
378
is_inside,
361
379
}
362
380
} else {
363
381
HitPoint {
364
382
line : 0 ,
365
383
index : 0 ,
384
+ char_index : 0 ,
366
385
is_inside : false ,
367
386
}
368
387
}
Original file line number Diff line number Diff line change @@ -356,10 +356,12 @@ impl TextInput {
356
356
}
357
357
self . cursor_x = cursor_x;
358
358
359
- let clip_start = virt_text. hit_point ( Point :: new ( clip_start_x, 0.0 ) ) . index ;
359
+ let clip_start = virt_text
360
+ . hit_point ( Point :: new ( clip_start_x, 0.0 ) )
361
+ . char_index ;
360
362
let clip_end = virt_text
361
363
. hit_point ( Point :: new ( clip_start_x + node_width, 0.0 ) )
362
- . index ;
364
+ . char_index ;
363
365
364
366
let new_text = self
365
367
. buffer
You can’t perform that action at this time.
0 commit comments