Support whole-word caret movement in ui::edit

This commit is contained in:
Nikita Lisitsa 2022-05-10 12:01:46 +03:00
parent 922daee6d2
commit 8ca5ab3f25

View file

@ -195,6 +195,16 @@ namespace psemek::ui
if (e.down && editing_) if (e.down && editing_)
{ {
if (e.key == SDLK_LEFT) if (e.key == SDLK_LEFT)
{
if (ctrl_down_)
{
std::size_t start = caret_;
while (start > 0 && std::isspace(text_[start - 1])) --start;
while (start > 0 && !std::isspace(text_[start - 1])) --start;
caret_ = start;
reset_caret();
}
else
{ {
if (caret_ > 0) if (caret_ > 0)
{ {
@ -202,7 +212,18 @@ namespace psemek::ui
reset_caret(); reset_caret();
} }
} }
}
else if (e.key == SDLK_RIGHT) else if (e.key == SDLK_RIGHT)
{
if (ctrl_down_)
{
std::size_t end = caret_;
while (end < text_.size() && !std::isspace(text_[end])) ++end;
while (end < text_.size() && std::isspace(text_[end])) ++end;
caret_ = end;
reset_caret();
}
else
{ {
if (caret_ < text_.size()) if (caret_ < text_.size())
{ {
@ -210,6 +231,7 @@ namespace psemek::ui
reset_caret(); reset_caret();
} }
} }
}
else if (e.key == SDLK_HOME) else if (e.key == SDLK_HOME)
{ {
caret_ = 0; caret_ = 0;