Skip to content

Commit

Permalink
fix: properly handle ctrl + backspace (mehah#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekiro authored Oct 12, 2024
1 parent 54421bb commit 98bf9d2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/framework/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ namespace Fw
KeyBar = 124, // |
KeyRightCurly = 125, // }
KeyTilde = 126, // ~
KeyDel = 127, // DEL (Ctrl + Backspace)
KeyF1 = 128,
KeyF2 = 129,
KeyF3 = 130,
Expand Down
46 changes: 40 additions & 6 deletions src/framework/ui/uitextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,24 @@ void UITextEdit::blinkCursor()
repaint();
}

void UITextEdit::deleteSelection()
{
if (!hasSelection()) {
return;
}

std::string tmp = m_text;
tmp.erase(m_selectionStart, m_selectionEnd - m_selectionStart);

setCursorPos(m_selectionStart);
clearSelection();
setText(tmp);
}

void UITextEdit::del(bool right)
{
if (hasSelection()) {
std::string tmp = m_text;
tmp.erase(m_selectionStart, m_selectionEnd - m_selectionStart);

setCursorPos(m_selectionStart);
clearSelection();
setText(tmp);
deleteSelection();
} else
removeCharacter(right);
}
Expand Down Expand Up @@ -779,6 +788,7 @@ bool UITextEdit::onKeyPress(uint8_t keyCode, int keyboardModifiers, int autoRepe
paste(g_window.getClipboardText());
return true;
}

if (keyCode == Fw::KeyX && getProp(PropEditable) && getProp(PropSelectable)) {
if (hasSelection()) {
cut();
Expand All @@ -794,6 +804,25 @@ bool UITextEdit::onKeyPress(uint8_t keyCode, int keyboardModifiers, int autoRepe
selectAll();
return true;
}
} else if (keyCode == Fw::KeyBackspace) {
if (hasSelection()) {
deleteSelection();
} else if (m_text.length() > 0) {
// delete last word
std::string tmp = m_text;
if (m_cursorPos == 0) {
tmp.erase(tmp.begin());
} else {
int pos = m_cursorPos;
while (pos > 0 && tmp[pos - 1] == ' ')
--pos;
while (pos > 0 && tmp[pos - 1] != ' ')
--pos;
tmp.erase(tmp.begin() + pos, tmp.begin() + m_cursorPos);
}
setText(tmp);
return true;
}
}
} else if (keyboardModifiers == Fw::KeyboardShiftModifier) {
if (keyCode == Fw::KeyTab && !getProp(PropShiftNavigation)) {
Expand Down Expand Up @@ -838,6 +867,11 @@ bool UITextEdit::onKeyPress(uint8_t keyCode, int keyboardModifiers, int autoRepe

bool UITextEdit::onKeyText(const std::string_view keyText)
{
// ctrl + backspace inserts a special ASCII character
if (keyText.length() == 1 && keyText.front() == Fw::KeyDel) {
return false;
}

if (getProp(PropEditable)) {
appendText(keyText.data());
return true;
Expand Down
1 change: 1 addition & 0 deletions src/framework/ui/uitextedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class UITextEdit : public UIWidget
void removeCharacter(bool right);
void blinkCursor();

void deleteSelection();
void del(bool right = false);
void paste(const std::string_view text);
std::string copy();
Expand Down

0 comments on commit 98bf9d2

Please sign in to comment.