Some fixes and shift enter

This commit is contained in:
Minecon724 2024-09-08 08:53:43 +02:00
parent 1a7b2d567d
commit e762f75b6e
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8

View file

@ -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<ChatMessage> 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
}