From 4bb45cc3db40e92cde1de3aeb7006cd4b9741de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 12 Mar 2025 22:55:31 +0100 Subject: [PATCH] tag + basic layout for rooms --- .../main/java/client/ClientGuiController.java | 28 +++++++++++++++++-- .../src/main/resources/client/clientVue.fxml | 12 ++------ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/ChatApp/app/src/main/java/client/ClientGuiController.java b/ChatApp/app/src/main/java/client/ClientGuiController.java index 6bd72f2..776e3b5 100644 --- a/ChatApp/app/src/main/java/client/ClientGuiController.java +++ b/ChatApp/app/src/main/java/client/ClientGuiController.java @@ -15,6 +15,7 @@ import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.regex.Matcher; 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()); Platform.runLater(() -> { 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 -> { connected = false; client.close(); @@ -88,24 +90,46 @@ public void initialize() throws SocketException { } @Override public void handleChatMessage(Instant time, String chatter, String content) { + String untagged = untag(content); + String messageColor = Objects.equals(content, untagged) ? "lightblue" : "lightgreen"; Platform.runLater(() -> { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm") .withZone(ZoneId.systemDefault()); String timeString = formatter.format(time); Text chatterText = new Text(chatter + ": "); 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); Text timeText = new Text(" " + timeString); timeText.setStyle("-fx-fill: gray; -fx-font-size: 10px;"); 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); HBox.setHgrow(messageText, Priority.ALWAYS); 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 public void handleRoomList(List roomNames) { Platform.runLater(() -> { diff --git a/ChatApp/app/src/main/resources/client/clientVue.fxml b/ChatApp/app/src/main/resources/client/clientVue.fxml index 252ee02..c43dd35 100644 --- a/ChatApp/app/src/main/resources/client/clientVue.fxml +++ b/ChatApp/app/src/main/resources/client/clientVue.fxml @@ -25,20 +25,14 @@ - - + +
- - - - - -