Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle scrolling#130 #204

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
package org.correomqtt.gui.views.connections;

import javafx.animation.PauseTransition;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuButton;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.controlsfx.control.textfield.TextFields;
import org.correomqtt.core.CoreManager;
import org.correomqtt.core.connection.ConnectionStateChangedEvent;
import org.correomqtt.core.model.ControllerType;
import org.correomqtt.core.model.LabelType;
import org.correomqtt.core.model.MessageListViewConfig;
import org.correomqtt.core.model.MessageType;
import org.correomqtt.core.model.PublishStatus;
import org.correomqtt.core.model.*;
import org.correomqtt.di.Assisted;
import org.correomqtt.di.DefaultBean;
import org.correomqtt.di.Inject;
Expand All @@ -46,6 +38,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;
import java.util.function.Predicate;

import static org.correomqtt.core.connection.ConnectionState.CONNECTED;
Expand Down Expand Up @@ -87,11 +80,14 @@ public class MessageListViewController extends BaseConnectionController implemen
Button showDetailsButton;
@FXML
private VBox messagesVBox;
@FXML
protected ToggleButton automaticScrollButton;
private ObservableList<MessagePropertiesDTO> messages;
private FilteredList<MessagePropertiesDTO> filteredMessages;
private DetailViewController detailViewController;


private final PauseTransition pause = new PauseTransition(Duration.millis(200));
private boolean isUserScrolling = false;

@Inject
public MessageListViewController(CoreManager coreManager,
Expand Down Expand Up @@ -153,8 +149,12 @@ private void initialize() {
listView.setItems(filteredMessages);
listView.setCellFactory(this::createCell);

addCustomScrollingListeners();

splitPane.widthProperty().addListener((observable, oldValue, newValue) -> Platform.runLater(() -> calculateDetailView(newValue)));

automaticScrollButton.setSelected(true);

messageSearchTextField.textProperty().addListener((observable, oldValue, newValue) -> searchInMessages(newValue));
}

Expand All @@ -163,6 +163,7 @@ private ListCell<MessagePropertiesDTO> createCell(ListView<MessagePropertiesDTO>
MessageListContextMenu contextMenu = messageListContextMenuFactory.create(this);
cell.setContextMenu(contextMenu);
cell.itemProperty().addListener((observable, oldValue, newValue) -> contextMenu.setObject(newValue));
cell.setOnMousePressed(event -> onCellPressed(cell));
cell.setOnMouseClicked(event -> onCellClicked(event, cell.getItem()));
cell.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (Boolean.TRUE.equals(newValue)) {
Expand All @@ -176,6 +177,11 @@ private ListCell<MessagePropertiesDTO> createCell(ListView<MessagePropertiesDTO>
return cell;
}

private void onCellPressed(MessageViewCell cell) {
listView.getFocusModel().focus(cell.getIndex());
disableAutomaticScrolling();
}

public void calculateDetailView(Number newValue) {
if (newValue.intValue() <= 670) {
closeDetailView();
Expand Down Expand Up @@ -349,6 +355,9 @@ void onNewMessage(MessagePropertiesDTO messageDTO) {
if (messageDTO.getMessageType().equals(MessageType.INCOMING)) {
addMessage(messageDTO);
}
if (listView.getFocusModel().getFocusedIndex() > 0 && !automaticScrollButton.isSelected()) {
listView.scrollTo(listView.getFocusModel().getFocusedIndex() + 1);
}
}

private void addMessage(MessagePropertiesDTO messageDTO) {
Expand Down Expand Up @@ -423,6 +432,25 @@ private void showDetailsOfMessage() {
detailViewControllerFactory.create(getSelectedMessage(), getConnectionId(), this, false).showAsDialog();
}

@FXML
private void toggleAutomaticScrolling() {
if (automaticScrollButton.isSelected()) {
enableAutomaticScrolling();
} else {
disableAutomaticScrolling();
}
}

private void enableAutomaticScrolling() {
automaticScrollButton.setSelected(true);
listView.getFocusModel().focus(-1);
listView.scrollTo(0);
}

private void disableAutomaticScrolling() {
automaticScrollButton.setSelected(false);
}

public void cleanUp() {
if (this.detailViewController != null) {
detailViewController.cleanUp();
Expand All @@ -438,4 +466,74 @@ public void showDetailsInSeparateWindow(MessagePropertiesDTO messageDTO) {
public void setUpToForm(MessagePropertiesDTO messageDTO) {
delegate.setUpToForm(messageDTO);
}

/**
Listeners to register scroll events in general that {@link #disableAutomaticScrolling()}.
Listeners to register scroll events to the top of the list that {@link #enableAutomaticScrolling()}.
*/
private void addCustomScrollingListeners() {
pause.setOnFinished(event -> isUserScrolling = false);
listView.skinProperty().addListener((observable, oldValue, newValue) -> {
Optional<ScrollBar> vScrollBar = findVerticalScrollBar(listView);
vScrollBar.ifPresent(scrollBar -> {
scrollBar.valueProperty().addListener((obs, oldVal, newVal) -> {
if (newVal.doubleValue() == scrollBar.getMin()) {
enableAutomaticScrolling();
}
});
});
});
listView.addEventFilter(ScrollEvent.SCROLL, event -> {
if (event.getDeltaY() > 0) { // Mouse wheel scrolling up
Optional<ScrollBar> vScrollBar = findVerticalScrollBar(listView);
vScrollBar.ifPresent(scrollBar -> {
if (scrollBar.getValue() == scrollBar.getMin()) {
enableAutomaticScrolling();
}
});
}
});

listView.addEventFilter(ScrollEvent.SCROLL, event -> {

if (!isUserScrolling) {
isUserScrolling = true;
// Reset the scrolling state after a delay
pause.playFromStart();
disableAutomaticScrolling();
}
});

listView.skinProperty().addListener((observable, oldValue, newValue) -> {
Optional<ScrollBar> vScrollBar = findVerticalScrollBar(listView);
vScrollBar.ifPresent(scrollBar -> {
scrollBar.valueProperty().addListener(new ScrollbarChangeListener());
});
});
}

private Optional<ScrollBar> findVerticalScrollBar(ListView<?> listView) {
for (var node : listView.lookupAll(".scroll-bar")) {
if (node instanceof ScrollBar) {
ScrollBar scrollBar = (ScrollBar) node;
if (scrollBar.getOrientation() == Orientation.VERTICAL) {
return Optional.of(scrollBar);
}
}
}
return Optional.empty();
}

private class ScrollbarChangeListener implements ChangeListener<Number> {
private boolean isScrolling = false;

@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if (!isUserScrolling) {
isUserScrolling = true;
pause.playFromStart();
disableAutomaticScrolling();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</HBox.margin>
</IconButton>
<IconButton fx:id="showDetailsButton" minHeight="25.0" minWidth="30.0" mnemonicParsing="false"
onAction="#showDetailsOfMessage" icon="mdi-open-in-new">
onAction="#showDetailsOfMessage" icon="mdi-open-in-new">
<tooltip>
<Tooltip text="%messageListViewShowDetailsTooltip"/>
</tooltip>
Expand All @@ -43,7 +43,7 @@


<IconButton fx:id="clearMessagesButton" minHeight="25.0" minWidth="30.0" mnemonicParsing="false"
onAction="#clearList" icon="mdi-trash-can">
onAction="#clearList" icon="mdi-trash-can">
<tooltip>
<Tooltip text="%messageListViewClearMessagesButton"/>
</tooltip>
Expand All @@ -58,12 +58,13 @@
</tooltip>
<items>
<IconCheckMenuItem fx:id="changeDisplayRetained" mnemonicParsing="false"
onAction="#changeRetainDisplay" styleClass="menuItem" text="%retainedMenuItem"/>
onAction="#changeRetainDisplay" styleClass="menuItem"
text="%retainedMenuItem"/>
<IconCheckMenuItem fx:id="changeDisplayQos" mnemonicParsing="false" onAction="#changeQosDisplay"
styleClass="menuItem" text="%qosMenuItem"/>
styleClass="menuItem" text="%qosMenuItem"/>
<IconCheckMenuItem fx:id="changeDisplayTimestamp" mnemonicParsing="false"
onAction="#changeTimestampDisplay" styleClass="menuItem"
text="%timestampMenuItem"/>
onAction="#changeTimestampDisplay" styleClass="menuItem"
text="%timestampMenuItem"/>
</items>
<HBox.margin>
<Insets left="5.0"/>
Expand All @@ -78,6 +79,15 @@
<Insets left="5.0"/>
</HBox.margin>
</IconToggleButton>
<IconToggleButton fx:id="automaticScrollButton" minHeight="25.0" minWidth="30.0" mnemonicParsing="false"
onAction="#toggleAutomaticScrolling" icon="mdi-mouse-scroll-wheel">
<tooltip>
<Tooltip text="%automaticScrollingTooltip"/>
</tooltip>
<HBox.margin>
<Insets left="5.0"/>
</HBox.margin>
</IconToggleButton>
</HBox>
<ListView fx:id="listView" styleClass="noBorder" VBox.vgrow="ALWAYS">
</ListView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,4 @@ scriptingUnsavedCheckDescription=Es gibt ungespeicherte Änderungen. Möchten Si
scriptingViewResetButtonTooltip=Änderungen verwerfen
scriptingViewClearExecutionsButtonTooltip=Ausführungslogs löschen
scriptingHelpLink=Learn how scripting works here.
automaticScrollingTooltip=Automatisches Scrolling aktivieren
3 changes: 2 additions & 1 deletion gui/src/main/resources/org/correomqtt/i18n_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,5 @@ scriptingUnsavedCheckTitle=Unsaved changes
scriptingUnsavedCheckDescription=There are unsaved changes. Do you really want to discard them?
scriptingViewResetButtonTooltip=Revert changes
scriptingViewClearExecutionsButtonTooltip=Remove Execution Logs
scriptingHelpLink=Learn how scripting works here.
scriptingHelpLink=Learn how scripting works here.
automaticScrollingTooltip=Toggle automatic scrolling
Loading