Some fixes and shift enter
This commit is contained in:
parent
1a7b2d567d
commit
e762f75b6e
1 changed files with 43 additions and 16 deletions
|
@ -3,7 +3,11 @@ package eu.m724.chaqt.chatwindow;
|
||||||
import eu.m724.chaqt.QMessage;
|
import eu.m724.chaqt.QMessage;
|
||||||
import eu.m724.chatapi.chat.ChatEvent;
|
import eu.m724.chatapi.chat.ChatEvent;
|
||||||
import eu.m724.chatapi.chat.ChatMessage;
|
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.core.Qt;
|
||||||
|
import io.qt.gui.QKeyEvent;
|
||||||
import io.qt.widgets.*;
|
import io.qt.widgets.*;
|
||||||
|
|
||||||
public class ChatWindowWidget extends QWidget {
|
public class ChatWindowWidget extends QWidget {
|
||||||
|
@ -11,6 +15,7 @@ public class ChatWindowWidget extends QWidget {
|
||||||
|
|
||||||
private QVBoxLayout layout;
|
private QVBoxLayout layout;
|
||||||
private QTextEdit textEdit;
|
private QTextEdit textEdit;
|
||||||
|
private QScrollArea scrollArea;
|
||||||
|
|
||||||
// TODO are both really needed
|
// TODO are both really needed
|
||||||
private final Signal1<ChatMessage> chatMessageSignal = new Signal1<>();
|
private final Signal1<ChatMessage> chatMessageSignal = new Signal1<>();
|
||||||
|
@ -34,19 +39,40 @@ public class ChatWindowWidget extends QWidget {
|
||||||
QWidget widget = new QWidget();
|
QWidget widget = new QWidget();
|
||||||
widget.setLayout(layout);
|
widget.setLayout(layout);
|
||||||
|
|
||||||
QScrollArea scrollArea = new QScrollArea();
|
this.scrollArea = new QScrollArea();
|
||||||
scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff);
|
scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff);
|
||||||
scrollArea.setWidgetResizable(true);
|
scrollArea.setWidgetResizable(true);
|
||||||
scrollArea.setWidget(widget);
|
scrollArea.setWidget(widget);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
|
QPushButton sendButton = new QPushButton("Send");
|
||||||
|
sendButton.setToolTip("Or Shift + Enter");
|
||||||
|
sendButton.clicked.connect(this::onSend);
|
||||||
|
|
||||||
this.textEdit = new QTextEdit();
|
this.textEdit = new QTextEdit();
|
||||||
textEdit.setPlaceholderText("Write a message...");
|
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");
|
if (event.type() == QEvent.Type.KeyPress) {
|
||||||
sendButton.clicked.connect(this::onSend);
|
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();
|
QHBoxLayout composerLayout = new QHBoxLayout();
|
||||||
composerLayout.addWidget(textEdit);
|
composerLayout.addWidget(textEdit);
|
||||||
|
@ -70,6 +96,7 @@ public class ChatWindowWidget extends QWidget {
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
textEdit.setFocus();
|
textEdit.setFocus();
|
||||||
|
resize(300, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleComposerType() {
|
public void handleComposerType() {
|
||||||
|
@ -91,28 +118,23 @@ public class ChatWindowWidget extends QWidget {
|
||||||
private void handleChatMessage(ChatMessage message) {
|
private void handleChatMessage(ChatMessage message) {
|
||||||
System.out.printf("Widget received a message. Is response: %b\n", message.response());
|
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.
|
// if the message is by assistant, disable composer.
|
||||||
// I don't know if this is good, but it works
|
// I don't know if this is good, but it works
|
||||||
if (message.response()) {
|
if (message.response()) {
|
||||||
|
latestMessage = new QMessage(true, "");
|
||||||
|
layout.addWidget(latestMessage);
|
||||||
|
|
||||||
textEdit.setEnabled(false);
|
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) {
|
private void handleChatEvent(ChatEvent chatEvent) {
|
||||||
String token = chatEvent.text();
|
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) {
|
if (token != null) {
|
||||||
latestMessage.addText(token);
|
latestMessage.addText(token);
|
||||||
}
|
}
|
||||||
|
@ -128,6 +150,11 @@ public class ChatWindowWidget extends QWidget {
|
||||||
textEdit.setFocus();
|
textEdit.setFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QScrollBar scrollBar = scrollArea.verticalScrollBar();
|
||||||
|
if (scrollBar != null) {
|
||||||
|
scrollBar.setValue(scrollBar.maximum());
|
||||||
|
}
|
||||||
|
|
||||||
System.out.printf("Event: %s %s\n", chatEvent.finishReason(), chatEvent.text());
|
System.out.printf("Event: %s %s\n", chatEvent.finishReason(), chatEvent.text());
|
||||||
} // TODO that's messy so do something about that
|
} // TODO that's messy so do something about that
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue