Skip to content

Commit

Permalink
Add detection of end for auto scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 committed Jan 10, 2024
1 parent 426a539 commit ac4880c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/faforever/client/chat/ChatMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.faforever.client.chat.emoticons.Emoticon;
import com.faforever.client.chat.emoticons.Reaction;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import lombok.EqualsAndHashCode;
Expand All @@ -28,6 +30,7 @@ public class ChatMessage {
@Getter
private final ChatMessage targetMessage;

private final BooleanProperty open = new SimpleBooleanProperty();
private final ObservableMap<Emoticon, ObservableMap<String, String>> reactions = FXCollections.synchronizedObservableMap(
FXCollections.observableHashMap());
private final ObservableMap<Emoticon, ObservableMap<String, String>> unmodifiableReactions = FXCollections.unmodifiableObservableMap(
Expand All @@ -52,6 +55,18 @@ public void removeReaction(Reaction reaction) {
}
}

public boolean isOpen() {
return open.get();

Check warning on line 59 in src/main/java/com/faforever/client/chat/ChatMessage.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/ChatMessage.java#L59

Added line #L59 was not covered by tests
}

public BooleanProperty openProperty() {
return open;
}

public void setOpen(boolean open) {
this.open.set(open);
}

Check warning on line 68 in src/main/java/com/faforever/client/chat/ChatMessage.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/ChatMessage.java#L67-L68

Added lines #L67 - L68 were not covered by tests

public enum Type {
MESSAGE, ACTION, PENDING
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ protected void onInitialize() {
avatarTooltip.setShowDuration(Duration.seconds(30));
Tooltip.install(avatarImageView, avatarTooltip);

chatMessage.subscribe((oldValue, newValue) -> {
chatMessage.when(attached).subscribe((oldValue, newValue) -> {
if (oldValue != null) {
oldValue.getReactions().removeListener(reactionChangeListener);
oldValue.openProperty().unbind();
oldValue.setOpen(false);

Check warning on line 169 in src/main/java/com/faforever/client/chat/ChatMessageController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/ChatMessageController.java#L167-L169

Added lines #L167 - L169 were not covered by tests
}

reactionNodeMap.clear();
Expand All @@ -173,6 +175,7 @@ protected void onInitialize() {
if (newValue != null) {
newValue.getReactions().forEach(this::addReaction);
newValue.getReactions().addListener(reactionChangeListener);
newValue.openProperty().bind(showing);
}
});

Expand All @@ -195,6 +198,15 @@ protected void onInitialize() {
.when(showing));
}

@Override
protected void onDetached() {
ChatMessage chatMessage = getChatMessage();

Check warning on line 203 in src/main/java/com/faforever/client/chat/ChatMessageController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/ChatMessageController.java#L203

Added line #L203 was not covered by tests
if (chatMessage != null) {
chatMessage.openProperty().unbind();
chatMessage.setOpen(false);

Check warning on line 206 in src/main/java/com/faforever/client/chat/ChatMessageController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/ChatMessageController.java#L205-L206

Added lines #L205 - L206 were not covered by tests
}
}

Check warning on line 208 in src/main/java/com/faforever/client/chat/ChatMessageController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/ChatMessageController.java#L208

Added line #L208 was not covered by tests

private void onReactionChange(
MapChangeListener.Change<? extends Emoticon, ? extends ObservableMap<String, String>> change) {
Emoticon reaction = change.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.faforever.client.util.PopupUtil;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanExpression;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
Expand Down Expand Up @@ -87,6 +88,9 @@ public class ChatMessageViewController extends NodeController<VBox> {
private final ObservableList<ChatMessage> rawMessages = FXCollections.synchronizedObservableList(
FXCollections.observableArrayList(chatMessage -> new Observable[]{chatMessage.getSender().categoryProperty()}));
private final FilteredList<ChatMessage> filteredMessages = new FilteredList<>(rawMessages);
private final BooleanExpression atEnd = BooleanExpression.booleanExpression(
Bindings.valueAt(filteredMessages, Bindings.size(filteredMessages).subtract(1))
.flatMap(ChatMessage::openProperty));

private Popup emoticonsPopup;

Expand Down Expand Up @@ -136,6 +140,7 @@ protected void onInitialize() {
ObservableList<ChatChannelUser> typingUsers = newValue.getTypingUsers();
setTypingLabel(typingUsers);
typingUsers.addListener(typingUserListChangeListener);
scrollToEnd();
}
}));

Expand All @@ -158,12 +163,16 @@ protected void onInitialize() {
.when(showing));

filteredMessages.subscribe(() -> {
if (showing.get()) {
fxApplicationThreadExecutor.execute(() -> messagesListView.scrollTo(filteredMessages.size()));
if (atEnd.get()) {
scrollToEnd();

Check warning on line 167 in src/main/java/com/faforever/client/chat/ChatMessageViewController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/ChatMessageViewController.java#L167

Added line #L167 was not covered by tests
}
});

Check warning on line 169 in src/main/java/com/faforever/client/chat/ChatMessageViewController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/ChatMessageViewController.java#L169

Added line #L169 was not covered by tests
}

private void scrollToEnd() {
fxApplicationThreadExecutor.execute(() -> messagesListView.scrollTo(filteredMessages.size()));
}

private AutoCompletionHelper createAutoCompletionHelper() {
return new AutoCompletionHelper(currentWord -> {
ObservableList<ChatChannelUser> users = this.users.getValue();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/faforever/client/fx/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public final void initialize() {
onDetached();
}
});
showing.subscribe(isSelected -> {
if (isSelected) {
showing.subscribe(isShowing -> {
if (isShowing) {
onShow();
} else {
shownSubscriptions.forEach(Subscription::unsubscribe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ public void testJoinChannel() {
instance.joinChannel(channelToJoin);

verify(spyClient).addChannel(channelToJoin);
verify(spyClient).sendRawLine("CHATHISTORY LATEST " + channelToJoin + " * " + (chatPrefs.getMaxMessages() + 50));
verify(spyClient).sendRawLine("CHATHISTORY LATEST " + channelToJoin + " * " + (chatPrefs.getMaxMessages() * 2));
verify(spyClient).sendRawLine("WHO " + channelToJoin);
}

Expand Down

0 comments on commit ac4880c

Please sign in to comment.