Skip to content

Commit

Permalink
chat: fix issues with the initial "New Chat" (#2330)
Browse files Browse the repository at this point in the history
* select the existing new chat if there already is one when "New Chat" is clicked
* scroll to the new chat when "New Chat" is clicked
* fix the "New Chat" being scrolled past the top of the chat list

Signed-off-by: Jared Van Bortel <jared@nomic.ai>
  • Loading branch information
cebtenzzre committed May 15, 2024
1 parent 7e1e00f commit fbbf810
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
9 changes: 5 additions & 4 deletions gpt4all-chat/chatlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ ChatListModel *ChatListModel::globalInstance()
}

ChatListModel::ChatListModel()
: QAbstractListModel(nullptr)
: QAbstractListModel(nullptr) {}

void ChatListModel::loadChats()
{
addChat();

ChatsRestoreThread *thread = new ChatsRestoreThread;
connect(thread, &ChatsRestoreThread::chatRestored, this, &ChatListModel::restoreChat);
connect(thread, &ChatsRestoreThread::finished, this, &ChatListModel::chatsRestoredFinished);
connect(thread, &ChatsRestoreThread::chatRestored, this, &ChatListModel::restoreChat, Qt::QueuedConnection);
connect(thread, &ChatsRestoreThread::finished, this, &ChatListModel::chatsRestoredFinished, Qt::QueuedConnection);
connect(thread, &ChatsRestoreThread::finished, thread, &QObject::deleteLater);
thread->start();

connect(MySettings::globalInstance(), &MySettings::serverChatChanged, this, &ChatListModel::handleServerEnabledChanged);

}

void ChatListModel::removeChatFile(Chat *chat) const
Expand Down
22 changes: 6 additions & 16 deletions gpt4all-chat/chatlistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ class ChatListModel : public QAbstractListModel
bool shouldSaveChatGPTChats() const;
void setShouldSaveChatGPTChats(bool b);

Q_INVOKABLE void loadChats();

Q_INVOKABLE void addChat()
{
// Don't add a new chat if we already have one
if (m_newChat)
// Select the existing new chat if we already have one
if (m_newChat) {
setCurrentChat(m_newChat);
return;
}

// Create a new chat pointer and connect it to determine when it is populated
m_newChat = new Chat(this);
Expand Down Expand Up @@ -114,20 +118,6 @@ class ChatListModel : public QAbstractListModel
emit countChanged();
}

void setNewChat(Chat* chat)
{
// Don't add a new chat if we already have one
if (m_newChat)
return;

m_newChat = chat;
connect(m_newChat->chatModel(), &ChatModel::countChanged,
this, &ChatListModel::newChatCountChanged);
connect(m_newChat, &Chat::nameChanged,
this, &ChatListModel::nameChanged);
setCurrentChat(m_newChat);
}

Q_INVOKABLE void removeChat(Chat* chat)
{
Q_ASSERT(chat != m_serverChat);
Expand Down
6 changes: 5 additions & 1 deletion gpt4all-chat/qml/ChatDrawer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Rectangle {
text: qsTr("\uFF0B New chat")
Accessible.description: qsTr("Create a new chat")
onClicked: {
ChatListModel.addChat();
ChatListModel.addChat()
conversationList.positionViewAtIndex(0, ListView.Beginning)
Network.trackEvent("new_chat", {"number_of_chats": ChatListModel.count})
}
}
Expand All @@ -60,6 +61,9 @@ Rectangle {
anchors.fill: parent
anchors.rightMargin: 10
model: ChatListModel

Component.onCompleted: ChatListModel.loadChats()

ScrollBar.vertical: ScrollBar {
parent: conversationList.parent
anchors.top: conversationList.top
Expand Down

0 comments on commit fbbf810

Please sign in to comment.