From f4df6d0d3b4f9142fd91fc04b6553de417724df5 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Fri, 9 Jul 2021 10:39:48 -0500 Subject: [PATCH] Typing auto-complete for usernames --- application/views/SendTextView.cpp | 51 +++++++++++++++++++++++++++++- application/views/SendTextView.h | 5 +++ protocols/Makefile | 2 +- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/application/views/SendTextView.cpp b/application/views/SendTextView.cpp index c2c1654..439a4c7 100644 --- a/application/views/SendTextView.cpp +++ b/application/views/SendTextView.cpp @@ -5,6 +5,7 @@ #include "SendTextView.h" +#include #include #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()); + } +} diff --git a/application/views/SendTextView.h b/application/views/SendTextView.h index 452d73e..6e12864 100644 --- a/application/views/SendTextView.h +++ b/application/views/SendTextView.h @@ -17,7 +17,12 @@ public: void KeyDown(const char* bytes, int32 numBytes); private: + void _AutoComplete(); + ConversationView* fConversationView; + + int32 fCurrentIndex; + BString fCurrentWord; }; diff --git a/protocols/Makefile b/protocols/Makefile index d2aee7d..1d912af 100644 --- a/protocols/Makefile +++ b/protocols/Makefile @@ -9,6 +9,6 @@ purple: xmpp: $(MAKE) -f protocols/xmpp/Makefile -all: irc xmpp purple +all: xmpp purple default: all