From 3fefeb1082fef1091bf899c1ef2cddd5cc3e392b Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 15 Dec 2019 12:48:40 +0100 Subject: [PATCH] Autoscroll * Implemented functionality to automatically scroll down when user is on the bottom of the chat and then there are new messages added. * When chat is opened, the vertical scroll bar starts at the bottom. * When rereading messages, the chat doesn't scroll down if new messages are added. (Besides see first point) --- src/main/java/envoy/client/ui/ChatWindow.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 6968f5c..134734f 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -63,6 +63,8 @@ public class ChatWindow extends JFrame { // private JCheckBox jCbChangeMode; private PrimaryButton postButton = new PrimaryButton("Post"); private PrimaryButton settingsButton = new PrimaryButton("Settings"); + private int verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); + private boolean chatOpened = false; private static int space = 4; @@ -208,6 +210,7 @@ public class ChatWindow extends JFrame { textPane.setText(currentChat.getRecipient().getName()); messageList.setModel(currentChat.getModel()); + chatOpened = true; contentPane.revalidate(); } }); @@ -253,11 +256,8 @@ public class ChatWindow extends JFrame { // scrollPane scrollPane.setForeground(theme.getBackgroundColor()); scrollPane.setBackground(theme.getCellColor()); - // scrollPane.getVerticalScrollBar() - // .setBackground( - // new Color(theme.getBackgroundColor().getRed() + 50, - // theme.getBackgroundColor().getGreen() + 50, - // theme.getBackgroundColor().getBlue() + 50)); + + // Scroll Bar Styling scrollPane.getVerticalScrollBar().setBackground(theme.getCellColor()); scrollPane.getVerticalScrollBar() .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), @@ -267,10 +267,26 @@ public class ChatWindow extends JFrame { scrollPane.getHorizontalScrollBar() .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), new Color(theme.getInteractableBackgroundColor().getRGB() - 50), new Color(theme.getInteractableBackgroundColor().getRGB() + 170), false)); - // int currentVerticalScrollBarValue = - // scrollPane.getVerticalScrollBar().getValue() + - // scrollPane.getVerticalScrollBar().getVisibleAmount(); Work in Progress for - // autoscroll + + // Autoscroll + scrollPane.getVerticalScrollBar().addAdjustmentListener(e -> { + if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0) { + return; + } + + if (chatOpened == true) { + e.getAdjustable().setValue(e.getAdjustable().getMaximum()); + verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); + chatOpened = false; + return; + } + if (scrollPane.getVerticalScrollBar().getValue() + + scrollPane.getVerticalScrollBar().getVisibleAmount() + 100 >= scrollPane.getVerticalScrollBar().getMaximum()) { + e.getAdjustable().setValue(e.getAdjustable().getMaximum()); + verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum(); + } + }); + // messageEnterTextArea messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); messageEnterTextArea.setForeground(theme.getTypingMessageColor());