From e762f75b6e03f09690191e9683f4f84767d3796d Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 8 Sep 2024 08:53:43 +0200 Subject: [PATCH] Some fixes and shift enter --- .../chaqt/chatwindow/ChatWindowWidget.java | 59 ++++++++++++++----- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/src/main/java/eu/m724/chaqt/chatwindow/ChatWindowWidget.java b/src/main/java/eu/m724/chaqt/chatwindow/ChatWindowWidget.java index d28f467..d47db6a 100644 --- a/src/main/java/eu/m724/chaqt/chatwindow/ChatWindowWidget.java +++ b/src/main/java/eu/m724/chaqt/chatwindow/ChatWindowWidget.java @@ -3,7 +3,11 @@ package eu.m724.chaqt.chatwindow; import eu.m724.chaqt.QMessage; import eu.m724.chatapi.chat.ChatEvent; import eu.m724.chatapi.chat.ChatMessage; +import io.qt.Nullable; +import io.qt.core.QEvent; +import io.qt.core.QObject; import io.qt.core.Qt; +import io.qt.gui.QKeyEvent; import io.qt.widgets.*; public class ChatWindowWidget extends QWidget { @@ -11,6 +15,7 @@ public class ChatWindowWidget extends QWidget { private QVBoxLayout layout; private QTextEdit textEdit; + private QScrollArea scrollArea; // TODO are both really needed private final Signal1 chatMessageSignal = new Signal1<>(); @@ -34,19 +39,40 @@ public class ChatWindowWidget extends QWidget { QWidget widget = new QWidget(); widget.setLayout(layout); - QScrollArea scrollArea = new QScrollArea(); + this.scrollArea = new QScrollArea(); scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff); scrollArea.setWidgetResizable(true); scrollArea.setWidget(widget); // + QPushButton sendButton = new QPushButton("Send"); + sendButton.setToolTip("Or Shift + Enter"); + sendButton.clicked.connect(this::onSend); + this.textEdit = new QTextEdit(); textEdit.setPlaceholderText("Write a message..."); - textEdit.textChanged.connect(this, "handleComposerType()"); + textEdit.textChanged.connect(this::handleComposerType); + textEdit.installEventFilter(new QObject() { + @Override + public boolean eventFilter(@Nullable QObject watched, @Nullable QEvent event) { + if (event == null) return false; - QPushButton sendButton = new QPushButton("Send"); - sendButton.clicked.connect(this::onSend); + if (event.type() == QEvent.Type.KeyPress) { + QKeyEvent keyEvent = (QKeyEvent) event; + if (keyEvent.key() == Qt.Key.Key_Return.value() && + keyEvent.modifiers().testFlag(Qt.KeyboardModifier.ShiftModifier)) { + System.out.println("Shift+Enter pressed"); + + //sendButton.click(); + sendButton.animateClick(); + + return true; + } + } + return false; + } + }); QHBoxLayout composerLayout = new QHBoxLayout(); composerLayout.addWidget(textEdit); @@ -70,6 +96,7 @@ public class ChatWindowWidget extends QWidget { }*/ textEdit.setFocus(); + resize(300, 500); } public void handleComposerType() { @@ -91,28 +118,23 @@ public class ChatWindowWidget extends QWidget { private void handleChatMessage(ChatMessage message) { System.out.printf("Widget received a message. Is response: %b\n", message.response()); - QMessage qMessage = new QMessage(false, message.content()); - layout.addWidget(qMessage); - // if the message is by assistant, disable composer. // I don't know if this is good, but it works if (message.response()) { + latestMessage = new QMessage(true, ""); + layout.addWidget(latestMessage); + textEdit.setEnabled(false); - textEdit.setPlaceholderText("Thinking..."); + textEdit.setPlaceholderText("Responding..."); + } else { + QMessage qMessage = new QMessage(false, message.content()); + layout.addWidget(qMessage); } } private void handleChatEvent(ChatEvent chatEvent) { String token = chatEvent.text(); - // usually when it's the first token - if (latestMessage == null) { - latestMessage = new QMessage(true, ""); - layout.addWidget(latestMessage); - - textEdit.setPlaceholderText("Responding..."); - } - if (token != null) { latestMessage.addText(token); } @@ -128,6 +150,11 @@ public class ChatWindowWidget extends QWidget { textEdit.setFocus(); } + QScrollBar scrollBar = scrollArea.verticalScrollBar(); + if (scrollBar != null) { + scrollBar.setValue(scrollBar.maximum()); + } + System.out.printf("Event: %s %s\n", chatEvent.finishReason(), chatEvent.text()); } // TODO that's messy so do something about that }