Chat history in send textbox

History of sent messages is now saved― if the user presses the up or
down arrows, history will be traversed in the textbox.
This commit is contained in:
Jaidyn Ann 2021-07-31 18:50:04 -05:00
parent 8855464d02
commit 2e3742d4e1
2 changed files with 61 additions and 8 deletions

View File

@ -13,12 +13,11 @@
SendTextView::SendTextView(const char* name, ConversationView* convView) SendTextView::SendTextView(const char* name, ConversationView* convView)
: :
EnterTextView(name), BTextView(name),
fConversationView(convView), fConversationView(convView),
fCurrentIndex(0) fCurrentIndex(0),
fHistoryIndex(0)
{ {
SetMessage(BMessage(APP_CHAT));
SetTarget(convView);
} }
@ -31,14 +30,27 @@ SendTextView::KeyDown(const char* bytes, int32 numBytes)
_AutoComplete(); _AutoComplete();
return; return;
} }
// Reset auto-complete state if user typed/sent something // Reset auto-complete state if user typed/sent something other than tab
fCurrentIndex = 0; fCurrentIndex = 0;
fCurrentWord.SetTo(""); fCurrentWord.SetTo("");
if ((bytes[0] == B_UP_ARROW) && (modifiers == 0)) {
_UpHistory();
return;
}
else if ((bytes[0] == B_DOWN_ARROW) && (modifiers == 0)) {
_DownHistory();
return;
}
if ((bytes[0] == B_ENTER) && (modifiers & B_COMMAND_KEY)) if ((bytes[0] == B_ENTER) && (modifiers & B_COMMAND_KEY))
Insert("\n"); Insert("\n");
else if ((bytes[0] == B_ENTER) && (modifiers == 0)) {
_AppendHistory();
fConversationView->MessageReceived(new BMessage(APP_CHAT));
}
else else
EnterTextView::KeyDown(bytes, numBytes); BTextView::KeyDown(bytes, numBytes);
} }
@ -79,3 +91,36 @@ SendTextView::_AutoComplete()
Select(TextLength(), TextLength()); Select(TextLength(), TextLength());
} }
} }
void
SendTextView::_AppendHistory()
{
fHistoryIndex = 0;
fHistory.Add(BString(Text()), 0);
if (fHistory.CountStrings() == 21)
fHistory.Remove(20);
}
void
SendTextView::_UpHistory()
{
if (fHistoryIndex == 0 && TextLength() > 0)
_AppendHistory();
if (fHistoryIndex < fHistory.CountStrings()) {
fHistoryIndex++;
SetText(fHistory.StringAt(fHistoryIndex - 1));
}
}
void
SendTextView::_DownHistory()
{
if (fHistoryIndex > 1) {
fHistoryIndex--;
SetText(fHistory.StringAt(fHistoryIndex - 1));
}
}

View File

@ -5,12 +5,13 @@
#ifndef _SEND_TEXT_VIEW_H #ifndef _SEND_TEXT_VIEW_H
#define _SEND_TEXT_VIEW_H #define _SEND_TEXT_VIEW_H
#include <libinterface/EnterTextView.h> #include <StringList.h>
#include <TextView.h>
#include "ConversationView.h" #include "ConversationView.h"
class SendTextView : public EnterTextView { class SendTextView : public BTextView {
public: public:
SendTextView(const char* name, ConversationView* convView); SendTextView(const char* name, ConversationView* convView);
@ -19,10 +20,17 @@ public:
private: private:
void _AutoComplete(); void _AutoComplete();
void _AppendHistory();
void _UpHistory();
void _DownHistory();
ConversationView* fConversationView; ConversationView* fConversationView;
int32 fCurrentIndex; int32 fCurrentIndex;
BString fCurrentWord; BString fCurrentWord;
BStringList fHistory;
int32 fHistoryIndex;
}; };
#endif // _SEND_TEXT_VIEW_H #endif // _SEND_TEXT_VIEW_H