🪲 The caret of a TextBox should not move when adding a newline at the start of a line that only exists due to word-wrap

0.8
Bruno Van de Velde 2017-10-07 15:56:40 +02:00
parent 495626ca38
commit c101b49cd5
2 changed files with 15 additions and 2 deletions

View File

@ -890,8 +890,12 @@ namespace tgui
m_text.insert(caretPosition, key); m_text.insert(caretPosition, key);
m_lines[m_selEnd.y].insert(m_selEnd.x, key); m_lines[m_selEnd.y].insert(m_selEnd.x, key);
// Increment the caret position, unless you type a newline at the start of a line while that line only existed due to word wrapping
if ((key != '\n') || (m_selEnd.x > 0) || (m_selEnd.y == 0) || m_lines[m_selEnd.y-1].isEmpty() || (m_text[caretPosition-1] == '\n'))
{
m_selStart.x++; m_selStart.x++;
m_selEnd.x++; m_selEnd.x++;
}
rearrangeText(true); rearrangeText(true);
}; };

View File

@ -301,6 +301,15 @@ TEST_CASE("[TextBox]")
textBox->keyPressed({sf::Keyboard::Key::BackSpace, false, false, false, false}); textBox->keyPressed({sf::Keyboard::Key::BackSpace, false, false, false, false});
REQUIRE(textBox->getText() == "AB\nFGHIJKLPQ\n\nTXYZ"); REQUIRE(textBox->getText() == "AB\nFGHIJKLPQ\n\nTXYZ");
// The caret should not move when adding a newline at the start of a line that only exists due to word-wrap
textBox->setText(""); // Make sure the scrollbar is gone
textBox->setText("ABCDEFGHIJKLMONPQRSTUVWXYZ");
for (unsigned int i = 0; i < 17; ++i)
textBox->keyPressed({sf::Keyboard::Key::Left, false, false, false, false});
textBox->keyPressed({sf::Keyboard::Key::Return, false, false, false, false});
textBox->keyPressed({sf::Keyboard::Key::Delete, false, false, false, false});
REQUIRE(textBox->getText() == "ABCDEFGHIJ\nLMONPQRSTUVWXYZ");
} }
SECTION("Copy and Paste") SECTION("Copy and Paste")