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(() -> {
|
||||
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();
|
||||
}
|
||||
|
@ -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: <strong>envoy-client</strong><br>
|
||||
@ -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,7 +50,7 @@ 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();
|
||||
@ -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,6 +108,8 @@ public class StatusTrayIcon implements EventHandler {
|
||||
*/
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user