Compare commits
No commits in common. "e762f75b6e03f09690191e9683f4f84767d3796d" and "f5f20dd0e926b5c08294c0d788034d653759ce36" have entirely different histories.
e762f75b6e
...
f5f20dd0e9
2 changed files with 17 additions and 44 deletions
|
@ -18,7 +18,7 @@ public class QMessage extends QWidget {
|
||||||
layout.addWidget(label);
|
layout.addWidget(label);
|
||||||
|
|
||||||
if (assistant) {
|
if (assistant) {
|
||||||
label.setContentsMargins(5,0, 0, 0);
|
layout.setContentsMargins(5,0,0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
setLayout(layout);
|
setLayout(layout);
|
||||||
|
|
|
@ -3,11 +3,7 @@ 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 {
|
||||||
|
@ -15,7 +11,6 @@ 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<>();
|
||||||
|
@ -39,40 +34,19 @@ public class ChatWindowWidget extends QWidget {
|
||||||
QWidget widget = new QWidget();
|
QWidget widget = new QWidget();
|
||||||
widget.setLayout(layout);
|
widget.setLayout(layout);
|
||||||
|
|
||||||
this.scrollArea = new QScrollArea();
|
QScrollArea 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;
|
|
||||||
|
|
||||||
if (event.type() == QEvent.Type.KeyPress) {
|
QPushButton sendButton = new QPushButton("Send");
|
||||||
QKeyEvent keyEvent = (QKeyEvent) event;
|
sendButton.clicked.connect(this::onSend);
|
||||||
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);
|
||||||
|
@ -96,7 +70,6 @@ public class ChatWindowWidget extends QWidget {
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
textEdit.setFocus();
|
textEdit.setFocus();
|
||||||
resize(300, 500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleComposerType() {
|
public void handleComposerType() {
|
||||||
|
@ -118,23 +91,28 @@ 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("Responding...");
|
textEdit.setPlaceholderText("Thinking...");
|
||||||
} 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);
|
||||||
}
|
}
|
||||||
|
@ -150,11 +128,6 @@ 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