Struct cosmic_text::Cursor
source · Expand description
Current cursor location
Fields§
§line: usizeText line the cursor is on
index: usizeIndex of glyph at cursor (will insert behind this glyph)
Implementations§
source§impl Cursor
impl Cursor
sourcepub const fn new(line: usize, index: usize) -> Self
pub const fn new(line: usize, index: usize) -> Self
Create a new cursor
Examples found in repository?
src/buffer.rs (line 451)
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
pub fn hit(&self, x: i32, y: i32) -> Option<Cursor> {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
let font_size = self.metrics.font_size;
let line_height = self.metrics.line_height;
let mut new_cursor_opt = None;
let mut runs = self.layout_runs().peekable();
let mut first_run = true;
while let Some(run) = runs.next() {
let line_y = run.line_y;
if first_run && y < line_y - font_size {
first_run = false;
let new_cursor = Cursor::new(run.line_i, 0);
new_cursor_opt = Some(new_cursor);
} else if y >= line_y - font_size
&& y < line_y - font_size + line_height
{
let mut new_cursor_glyph = run.glyphs.len();
let mut new_cursor_char = 0;
let mut first_glyph = true;
'hit: for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
if first_glyph {
first_glyph = false;
if (run.rtl && x > glyph.x as i32) || (!run.rtl && x < 0) {
new_cursor_glyph = 0;
new_cursor_char = 0;
}
}
if x >= glyph.x as i32
&& x <= (glyph.x + glyph.w) as i32
{
new_cursor_glyph = glyph_i;
let cluster = &run.text[glyph.start..glyph.end];
let total = cluster.grapheme_indices(true).count();
let mut egc_x = glyph.x;
let egc_w = glyph.w / (total as f32);
for (egc_i, egc) in cluster.grapheme_indices(true) {
if x >= egc_x as i32
&& x <= (egc_x + egc_w) as i32
{
new_cursor_char = egc_i;
let right_half = x >= (egc_x + egc_w / 2.0) as i32;
if right_half != glyph.level.is_rtl() {
// If clicking on last half of glyph, move cursor past glyph
new_cursor_char += egc.len();
}
break 'hit;
}
egc_x += egc_w;
}
let right_half = x >= (glyph.x + glyph.w / 2.0) as i32;
if right_half != glyph.level.is_rtl() {
// If clicking on last half of glyph, move cursor past glyph
new_cursor_char = cluster.len();
}
break 'hit;
}
}
let mut new_cursor = Cursor::new(run.line_i, 0);
match run.glyphs.get(new_cursor_glyph) {
Some(glyph) => {
// Position at glyph
new_cursor.index = glyph.start + new_cursor_char;
},
None => if let Some(glyph) = run.glyphs.last() {
// Position at end of line
new_cursor.index = glyph.end;
},
}
new_cursor_opt = Some(new_cursor);
break;
} else if runs.peek().is_none() && y > run.line_y {
let mut new_cursor = Cursor::new(run.line_i, 0);
if let Some(glyph) = run.glyphs.last() {
new_cursor.index = glyph.end;
}
new_cursor_opt = Some(new_cursor);
}
}
#[cfg(feature = "std")]
log::trace!("click({}, {}): {:?}", x, y, instant.elapsed());
new_cursor_opt
}