Typing auto-complete for usernames

This commit is contained in:
Jaidyn Ann 2021-07-09 10:39:48 -05:00
parent 8a3f22d5e9
commit f4df6d0d3b
3 changed files with 56 additions and 2 deletions

View File

@ -5,6 +5,7 @@
#include "SendTextView.h"
#include <StringList.h>
#include <Window.h>
#include "AppMessages.h"
@ -13,7 +14,8 @@
SendTextView::SendTextView(const char* name, ConversationView* convView)
:
BTextView(name),
fConversationView(convView)
fConversationView(convView),
fCurrentIndex(0)
{
}
@ -23,6 +25,14 @@ SendTextView::KeyDown(const char* bytes, int32 numBytes)
{
int32 modifiers = Window()->CurrentMessage()->GetInt32("modifiers", 0);
if (bytes[0] == B_TAB) {
_AutoComplete();
return;
}
// Reset auto-complete state if user typed/sent something
fCurrentIndex = 0;
fCurrentWord.SetTo("");
if ((bytes[0] == B_ENTER) && (modifiers & B_COMMAND_KEY))
Insert("\n");
else if (bytes[0] == B_ENTER)
@ -30,3 +40,42 @@ SendTextView::KeyDown(const char* bytes, int32 numBytes)
else
BTextView::KeyDown(bytes, numBytes);
}
void
SendTextView::_AutoComplete()
{
BStringList words;
BString text = Text();
text.Split(" ", true, words);
if (words.CountStrings() <= 0)
return;
BString lastWord = words.StringAt(words.CountStrings() - 1);
if (fCurrentWord.IsEmpty() == true)
fCurrentWord = lastWord;
// No command auto-completion (yet)
if (fCurrentWord.StartsWith("/") == true)
return;
BString user;
UserMap users = fConversationView->GetConversation()->Users();
for (int i, j = 0; i < users.CountItems(); i++)
if (users.KeyAt(i).StartsWith(fCurrentWord)) {
if (j == fCurrentIndex) {
user = users.KeyAt(i);
fCurrentIndex++;
break;
}
j++;
}
if (user.IsEmpty() == true)
fCurrentIndex = 0;
else {
text.ReplaceLast(lastWord.String(), user.String());
SetText(text.String());
Select(TextLength(), TextLength());
}
}

View File

@ -17,7 +17,12 @@ public:
void KeyDown(const char* bytes, int32 numBytes);
private:
void _AutoComplete();
ConversationView* fConversationView;
int32 fCurrentIndex;
BString fCurrentWord;
};

View File

@ -9,6 +9,6 @@ purple:
xmpp:
$(MAKE) -f protocols/xmpp/Makefile
all: irc xmpp purple
all: xmpp purple
default: all