MultiLine Text with Home and End keys

Hi folks

I think we found a bug with the multiline text box handling of home and end keys when on windows with screen resolution != 100%. I have a fix also which I’ll put in surge 1.3 but wanted to share here.

Basically the bug report we got was in a multiline text the home and end key were skipping up one line. So if you had multiline text with a caret at / like shown

this is
multi line / text
with three lines

on windows at some screen resolutions pressing home would jump you to before this rather than multi and end to after is as opposed to text

The reason for this seems to be as follows (and is fixed here).

bool TextEditor::moveCaretToStartOfLine (bool selecting)
    const auto caretPos = (getCaretRectangle() - getTextOffset()).toFloat();	    
    return moveCaretWithTransaction (indexAtPosition (0.0f, caretPos.getY()), selecting);	 

that implementation uses caretPos.getY() to locate the line. If you look at indexAtPos it does a linear search in float space. What happens on windows is at some screen zooms, the getY() pos fractional ends up tagging the forward iterative search one line early. For exact ints it is correct but this is all in float space at this point.

The fix I put in place is as follows. Replace the indexAtPosition to be

indexAtPosition (0.0f, caretPos.getY() + caretPos.getHeight() * 0.5)

and I applied the same fix in moveCaretToEndOfLine. This fixed the problem for me entirely.

Curious if others have seen this and what folks think of the fix or if you think it’s the wrong approach.


1 Like