From fbbf81002012df7346d28924f59c8d37ea029284 Mon Sep 17 00:00:00 2001 From: Jared Van Bortel Date: Wed, 15 May 2024 14:09:32 -0400 Subject: [PATCH] chat: fix issues with the initial "New Chat" (#2330) * 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 --- gpt4all-chat/chatlistmodel.cpp | 9 +++++---- gpt4all-chat/chatlistmodel.h | 22 ++++++---------------- gpt4all-chat/qml/ChatDrawer.qml | 6 +++++- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/gpt4all-chat/chatlistmodel.cpp b/gpt4all-chat/chatlistmodel.cpp index 8ad02898..c7bee3e5 100644 --- a/gpt4all-chat/chatlistmodel.cpp +++ b/gpt4all-chat/chatlistmodel.cpp @@ -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 diff --git a/gpt4all-chat/chatlistmodel.h b/gpt4all-chat/chatlistmodel.h index 709391e0..89a10e7e 100644 --- a/gpt4all-chat/chatlistmodel.h +++ b/gpt4all-chat/chatlistmodel.h @@ -81,11 +81,15 @@ public: 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); @@ -114,20 +118,6 @@ public: 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); diff --git a/gpt4all-chat/qml/ChatDrawer.qml b/gpt4all-chat/qml/ChatDrawer.qml index 858f8323..893fbc73 100644 --- a/gpt4all-chat/qml/ChatDrawer.qml +++ b/gpt4all-chat/qml/ChatDrawer.qml @@ -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}) } } @@ -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