Creating message notifications only if ChatWindow has lost focus

StatusTray injects a WindowFocusListener into ChatWindow in its
constructor and does only react to received messages if ChatWindow has
currently lost focus.
This commit is contained in:
Kai S. K. Engelbart 2019-12-05 15:42:20 +01:00
parent 6dad4eda08
commit 2831b9a7a3
2 changed files with 34 additions and 7 deletions

View File

@ -63,9 +63,9 @@ public class Startup {
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {
try { try {
ChatWindow frame = new ChatWindow(client, localDB); ChatWindow chatWindow = new ChatWindow(client, localDB);
new StatusTrayIcon().show(); new StatusTrayIcon(chatWindow).show();
frame.setVisible(true); chatWindow.setVisible(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -8,6 +8,8 @@ import java.awt.SystemTray;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.TrayIcon; import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType; import java.awt.TrayIcon.MessageType;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -16,6 +18,7 @@ import envoy.client.event.EventBus;
import envoy.client.event.EventHandler; import envoy.client.event.EventHandler;
import envoy.client.event.MessageCreationEvent; import envoy.client.event.MessageCreationEvent;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.schema.Message;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -34,6 +37,12 @@ public class StatusTrayIcon implements EventHandler {
*/ */
private TrayIcon trayIcon; private TrayIcon trayIcon;
/**
* A received {@link Message} is only displayed as a system tray notification if
* this variable is set to {@code true}.
*/
private boolean displayMessages = false;
/** /**
* Creates a {@link StatusTrayIcon} with the Envoy logo, a tool tip and a pop-up * Creates a {@link StatusTrayIcon} with the Envoy logo, a tool tip and a pop-up
* menu. * menu.
@ -41,7 +50,7 @@ public class StatusTrayIcon implements EventHandler {
* @throws EnvoyException if the currently used OS does not support the System * @throws EnvoyException if the currently used OS does not support the System
* Tray API * Tray API
*/ */
public StatusTrayIcon() throws EnvoyException { public StatusTrayIcon(ChatWindow chatWindow) throws EnvoyException {
if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported."); if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported.");
ClassLoader loader = Thread.currentThread().getContextClassLoader(); ClassLoader loader = Thread.currentThread().getContextClassLoader();
@ -57,6 +66,22 @@ public class StatusTrayIcon implements EventHandler {
popup.add(exitMenuItem); popup.add(exitMenuItem);
trayIcon.setPopupMenu(popup); trayIcon.setPopupMenu(popup);
// Only display messages if the chat window is not focused
chatWindow.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowGainedFocus(WindowEvent e) {
displayMessages = false;
}
@Override
public void windowLostFocus(WindowEvent e) {
displayMessages = true;
}
});
// Start processing message events
EventBus.getInstance().register(this); EventBus.getInstance().register(this);
} }
@ -83,6 +108,8 @@ public class StatusTrayIcon implements EventHandler {
*/ */
@Override @Override
public void handle(Event<?> event) { public void handle(Event<?> event) {
System.out.println("Message received. Displaying message: " + displayMessages);
if (displayMessages)
trayIcon.displayMessage("New message received", ((MessageCreationEvent) event).get().getContent().get(0).getText(), MessageType.INFO); trayIcon.displayMessage("New message received", ((MessageCreationEvent) event).get().getContent().get(0).getText(), MessageType.INFO);
} }