tag + basic layout for rooms

This commit is contained in:
Clément
2025-03-12 22:55:31 +01:00
parent 39a65e1ca6
commit 4bb45cc3db
2 changed files with 29 additions and 11 deletions

View File

@@ -15,6 +15,7 @@ import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -49,6 +50,7 @@ public void initialize() throws SocketException {
client = new Client(new InetSocketAddress("localhost", 6665), this, UsernameSingleton.getInstance().getUsername()); client = new Client(new InetSocketAddress("localhost", 6665), this, UsernameSingleton.getInstance().getUsername());
Platform.runLater(() -> { Platform.runLater(() -> {
Stage stage = (Stage) vueContainer.getScene().getWindow(); Stage stage = (Stage) vueContainer.getScene().getWindow();
stage.setResizable(true); // Fixed a bug that made the close button disappear under my Wayland setup (don't know if it's the same for x11)
stage.setOnCloseRequest(event -> { stage.setOnCloseRequest(event -> {
connected = false; connected = false;
client.close(); client.close();
@@ -88,24 +90,46 @@ public void initialize() throws SocketException {
} }
@Override @Override
public void handleChatMessage(Instant time, String chatter, String content) { public void handleChatMessage(Instant time, String chatter, String content) {
String untagged = untag(content);
String messageColor = Objects.equals(content, untagged) ? "lightblue" : "lightgreen";
Platform.runLater(() -> { Platform.runLater(() -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm") DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm")
.withZone(ZoneId.systemDefault()); .withZone(ZoneId.systemDefault());
String timeString = formatter.format(time); String timeString = formatter.format(time);
Text chatterText = new Text(chatter + ": "); Text chatterText = new Text(chatter + ": ");
chatterText.setStyle("-fx-fill: black; -fx-font-weight: bold;"); chatterText.setStyle("-fx-fill: black; -fx-font-weight: bold;");
TextFlow messageText = formatMessage(chatter, content); TextFlow messageText = formatMessage(chatter, untagged);
TextFlow wholeMessage = new TextFlow(chatterText, messageText); TextFlow wholeMessage = new TextFlow(chatterText, messageText);
Text timeText = new Text(" " + timeString); Text timeText = new Text(" " + timeString);
timeText.setStyle("-fx-fill: gray; -fx-font-size: 10px;"); timeText.setStyle("-fx-fill: gray; -fx-font-size: 10px;");
HBox messageContainer = new HBox(5, wholeMessage, timeText); HBox messageContainer = new HBox(5, wholeMessage, timeText);
messageContainer.setStyle("-fx-background-color: lightblue; -fx-padding: 8; -fx-background-radius: 5;"); messageContainer.setStyle("-fx-background-color: " + messageColor + "; -fx-padding: 8; -fx-background-radius: 5;");
messageContainer.setMaxWidth(Double.MAX_VALUE); messageContainer.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(messageText, Priority.ALWAYS); HBox.setHgrow(messageText, Priority.ALWAYS);
chatList.getChildren().add(messageContainer); chatList.getChildren().add(messageContainer);
}); });
} }
/**
* Remove the tag from the message (handle ANSI characters encoded by the server)
* @param message the message to untag
* @return the untagged message
*/
public String untag(String message) {
Pattern pattern = Pattern.compile("\u001B\\[44;30m(.*?)\u001B\\[49;39m");
Matcher matcher = pattern.matcher(message);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
String taggedName = matcher.group(1);
matcher.appendReplacement(result, taggedName);
}
matcher.appendTail(result);
return result.toString();
}
@Override @Override
public void handleRoomList(List<String> roomNames) { public void handleRoomList(List<String> roomNames) {
Platform.runLater(() -> { Platform.runLater(() -> {

View File

@@ -25,20 +25,14 @@
</HBox> </HBox>
</top> </top>
<left> <left>
<ScrollPane prefWidth="200.0" styleClass="rooms-pane" fx:id="roomsPane"> <ScrollPane prefWidth="200.0" styleClass="rooms-pane" fx:id="roomsPane" fitToWidth="true">
<VBox fx:id="rooms" spacing="10.0" alignment="CENTER"> <VBox fx:id="rooms" spacing="10.0" alignment="CENTER" VBox.vgrow="ALWAYS">
<Label text="Rooms" styleClass="rooms-label"/> <Label text="Rooms" styleClass="rooms-label"/>
<VBox fx:id="roomList" styleClass="rooms-list" spacing="5.0"/> <VBox fx:id="roomList" styleClass="rooms-list" spacing="5.0" VBox.vgrow="ALWAYS" alignment="CENTER"/>
</VBox> </VBox>
</ScrollPane> </ScrollPane>
</left> </left>
<center> <center>
<!-- <ScrollPane styleClass="chat-pane" fx:id="chatPane">-->
<!-- <VBox fx:id="chat" spacing="10.0" prefWidth="Infinity">-->
<!-- <VBox fx:id="chatList" styleClass="chat-list" spacing="5.0" VBox.vgrow="ALWAYS"/>-->
<!-- <HBox fx:id="chatInput" styleClass="chat-input" spacing="5.0" />-->
<!-- </VBox>-->
<!-- </ScrollPane>-->
<BorderPane> <BorderPane>
<center> <center>
<ScrollPane styleClass="chat-pane" fx:id="chatPane" fitToWidth="true"> <ScrollPane styleClass="chat-pane" fx:id="chatPane" fitToWidth="true">