From 2e3742d4e13fa3b2e6361ee1e37fff94b77fa81d Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sat, 31 Jul 2021 18:50:04 -0500 Subject: [PATCH] Chat history in send textbox MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit History of sent messages is now saved― if the user presses the up or down arrows, history will be traversed in the textbox. --- application/views/SendTextView.cpp | 57 ++++++++++++++++++++++++++---- application/views/SendTextView.h | 12 +++++-- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/application/views/SendTextView.cpp b/application/views/SendTextView.cpp index 65bc091..abc10ae 100644 --- a/application/views/SendTextView.cpp +++ b/application/views/SendTextView.cpp @@ -13,12 +13,11 @@ SendTextView::SendTextView(const char* name, ConversationView* convView) : - EnterTextView(name), + BTextView(name), 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(); return; } - // Reset auto-complete state if user typed/sent something + // Reset auto-complete state if user typed/sent something other than tab fCurrentIndex = 0; 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)) Insert("\n"); + else if ((bytes[0] == B_ENTER) && (modifiers == 0)) { + _AppendHistory(); + fConversationView->MessageReceived(new BMessage(APP_CHAT)); + } else - EnterTextView::KeyDown(bytes, numBytes); + BTextView::KeyDown(bytes, numBytes); } @@ -79,3 +91,36 @@ SendTextView::_AutoComplete() 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)); + } +} diff --git a/application/views/SendTextView.h b/application/views/SendTextView.h index 21f12ea..7d9524f 100644 --- a/application/views/SendTextView.h +++ b/application/views/SendTextView.h @@ -5,12 +5,13 @@ #ifndef _SEND_TEXT_VIEW_H #define _SEND_TEXT_VIEW_H -#include +#include +#include #include "ConversationView.h" -class SendTextView : public EnterTextView { +class SendTextView : public BTextView { public: SendTextView(const char* name, ConversationView* convView); @@ -19,10 +20,17 @@ public: private: void _AutoComplete(); + void _AppendHistory(); + void _UpHistory(); + void _DownHistory(); + ConversationView* fConversationView; int32 fCurrentIndex; BString fCurrentWord; + + BStringList fHistory; + int32 fHistoryIndex; }; #endif // _SEND_TEXT_VIEW_H