mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 22:31:14 +00:00
Move the text_input_byte_offset_for_position from the FontMetrics to the Window
In preparation of having mutiple-lines TextInput, we will need to give more data to this function so it no longer belong in FontMetrics Also remove the unused FontMetric::line_height()
This commit is contained in:
parent
e1be599bc0
commit
c1fc242a9a
7 changed files with 77 additions and 64 deletions
|
@ -556,6 +556,40 @@ impl PlatformWindow for GraphicsWindow {
|
|||
))
|
||||
}
|
||||
|
||||
fn text_input_byte_offset_for_position(
|
||||
&self,
|
||||
text_input: Pin<&sixtyfps_corelib::items::TextInput>,
|
||||
pos: Point,
|
||||
) -> usize {
|
||||
let scale_factor = self.scale_factor();
|
||||
let cache_data = text_input
|
||||
.cached_rendering_data
|
||||
.get_or_update(&self.graphics_cache, || {
|
||||
Some(crate::ItemGraphicsCacheEntry::Font(crate::fonts::FONT_CACHE.with(|cache| {
|
||||
cache.borrow_mut().font(
|
||||
text_input
|
||||
.unresolved_font_request()
|
||||
.merge(&self.default_font_properties.as_ref().get()),
|
||||
scale_factor,
|
||||
&text_input.text(),
|
||||
)
|
||||
})))
|
||||
})
|
||||
.unwrap();
|
||||
let font = cache_data.as_font();
|
||||
let x = pos.x * scale_factor;
|
||||
let text = text_input.text();
|
||||
let metrics = font.measure(text_input.letter_spacing(), &text);
|
||||
let mut current_x = 0.;
|
||||
for glyph in metrics.glyphs {
|
||||
if current_x + glyph.advance_x / 2. >= x {
|
||||
return glyph.byte_index;
|
||||
}
|
||||
current_x += glyph.advance_x;
|
||||
}
|
||||
text.len()
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue