diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java
index 7b35158..6333a60 100644
--- a/src/main/java/envoy/client/ui/Startup.java
+++ b/src/main/java/envoy/client/ui/Startup.java
@@ -63,9 +63,9 @@ public class Startup {
EventQueue.invokeLater(() -> {
try {
- ChatWindow frame = new ChatWindow(client, localDB);
- new StatusTrayIcon().show();
- frame.setVisible(true);
+ ChatWindow chatWindow = new ChatWindow(client, localDB);
+ new StatusTrayIcon(chatWindow).show();
+ chatWindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java
index 94d30d0..827296d 100644
--- a/src/main/java/envoy/client/ui/StatusTrayIcon.java
+++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java
@@ -8,6 +8,8 @@ import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
import java.util.HashSet;
import java.util.Set;
@@ -16,6 +18,7 @@ import envoy.client.event.EventBus;
import envoy.client.event.EventHandler;
import envoy.client.event.MessageCreationEvent;
import envoy.exception.EnvoyException;
+import envoy.schema.Message;
/**
* Project: envoy-client
@@ -34,6 +37,12 @@ public class StatusTrayIcon implements EventHandler {
*/
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
* menu.
@@ -41,11 +50,11 @@ public class StatusTrayIcon implements EventHandler {
* @throws EnvoyException if the currently used OS does not support the System
* 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.");
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- Image img = Toolkit.getDefaultToolkit().createImage(loader.getResource("envoy_logo.png"));
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Image img = Toolkit.getDefaultToolkit().createImage(loader.getResource("envoy_logo.png"));
trayIcon = new TrayIcon(img, "Envoy Client");
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("You are notified if you have unread messages.");
@@ -57,6 +66,22 @@ public class StatusTrayIcon implements EventHandler {
popup.add(exitMenuItem);
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);
}
@@ -83,7 +108,9 @@ public class StatusTrayIcon implements EventHandler {
*/
@Override
public void handle(Event> event) {
- trayIcon.displayMessage("New message received", ((MessageCreationEvent) event).get().getContent().get(0).getText(), MessageType.INFO);
+ System.out.println("Message received. Displaying message: " + displayMessages);
+ if (displayMessages)
+ trayIcon.displayMessage("New message received", ((MessageCreationEvent) event).get().getContent().get(0).getText(), MessageType.INFO);
}
/**