From aa63841e2c80ca8130adf41d25e5d731409b92f4 Mon Sep 17 00:00:00 2001 From: Tom Date: Sat, 21 Aug 2021 10:31:26 -0700 Subject: Implement textual hit testing --- native/src/widget/text_input.rs | 70 +++++------------------------------------ 1 file changed, 7 insertions(+), 63 deletions(-) (limited to 'native/src/widget/text_input.rs') diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index bb64d5b7..f1a7a1a0 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -707,15 +707,15 @@ pub trait Renderer: text::Renderer + Sized { let offset = self.offset(text_bounds, font, size, &value, &state); - find_cursor_position( - self, - &value, + self.hit_test( + &value.to_string(), + size.into(), font, - size, - x + offset, - 0, - value.len(), + Size::INFINITY, + Point::new(x + offset, text_bounds.height / 2.0), + true, ) + .cursor() } } @@ -803,62 +803,6 @@ impl State { } } -// TODO: Reduce allocations -fn find_cursor_position( - renderer: &Renderer, - value: &Value, - font: Renderer::Font, - size: u16, - target: f32, - start: usize, - end: usize, -) -> usize { - if start >= end { - if start == 0 { - return 0; - } - - let prev = value.until(start - 1); - let next = value.until(start); - - let prev_width = renderer.measure_value(&prev.to_string(), size, font); - let next_width = renderer.measure_value(&next.to_string(), size, font); - - if next_width - target > target - prev_width { - return start - 1; - } else { - return start; - } - } - - let index = (end - start) / 2; - let subvalue = value.until(start + index); - - let width = renderer.measure_value(&subvalue.to_string(), size, font); - - if width > target { - find_cursor_position( - renderer, - value, - font, - size, - target, - start, - start + index, - ) - } else { - find_cursor_position( - renderer, - value, - font, - size, - target, - start + index + 1, - end, - ) - } -} - mod platform { use crate::keyboard; -- cgit