core: Fix double caret rendering in justified text

The condition

  visible_selection.start() >= *start && visible_selection.end() <= *end

was inaccurate, because the end of the selection is exclusive.
That caused the condition to be true for two adjacent boxes.
For instance:

  box 1: from 0 to 6,  "hello "
  box 2: from 6 to 11, "world"

The caret was rendered for both boxes when it was at position 6.

When applying a correct condition (i.e. treating the end as exclusive)
there is a problem with rendering the caret at the very end of the text,
because the condition will not be triggered for any box
(position 11 in the example above).

That is why a condition specific to this case is added, i.e.

  *end == text_len

When the box is the last box in the text, we are forcing
the caret to be rendered.
This commit is contained in:
Kamil Jarosz 2024-07-09 23:17:10 +02:00 committed by TÖRÖK Attila
parent eec5e908ad
commit ac9b39a652
1 changed files with 2 additions and 1 deletions

View File

@ -902,10 +902,11 @@ impl<'gc> EditText<'gc> {
let caret = if let LayoutContent::Text { start, end, .. } = &lbox.content() {
if let Some(visible_selection) = visible_selection {
let text_len = edit_text.text_spans.text().len();
if visible_selection.is_caret()
&& !edit_text.flags.contains(EditTextFlag::READ_ONLY)
&& visible_selection.start() >= *start
&& visible_selection.end() <= *end
&& (visible_selection.end() < *end || *end == text_len)
&& !visible_selection.blinks_now()
{
Some(visible_selection.start() - start)