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:
parent
6dad4eda08
commit
2831b9a7a3
@ -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();
|
||||||
}
|
}
|
||||||
|
@ -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,11 +50,11 @@ 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();
|
||||||
Image img = Toolkit.getDefaultToolkit().createImage(loader.getResource("envoy_logo.png"));
|
Image img = Toolkit.getDefaultToolkit().createImage(loader.getResource("envoy_logo.png"));
|
||||||
trayIcon = new TrayIcon(img, "Envoy Client");
|
trayIcon = new TrayIcon(img, "Envoy Client");
|
||||||
trayIcon.setImageAutoSize(true);
|
trayIcon.setImageAutoSize(true);
|
||||||
trayIcon.setToolTip("You are notified if you have unread messages.");
|
trayIcon.setToolTip("You are notified if you have unread messages.");
|
||||||
@ -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,7 +108,9 @@ public class StatusTrayIcon implements EventHandler {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void handle(Event<?> event) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user