Integrated the tray icon with the hide on close setting

This commit is contained in:
Kai S. K. Engelbart 2020-07-24 09:57:09 +02:00
parent 07fbe3438a
commit 59354c403d
4 changed files with 45 additions and 30 deletions

View File

@ -75,7 +75,7 @@ public class Settings {
private void supplementDefaults() {
items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key."));
items.putIfAbsent("onCloseMode", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed."));
items.putIfAbsent("hideOnClose", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed."));
items.putIfAbsent("currentTheme", new SettingsItem<>("dark", "Current Theme Name", "The name of the currently selected theme."));
}
@ -124,15 +124,15 @@ public class Settings {
* @return the current on close mode.
* @since Envoy Client v0.3-alpha
*/
public Boolean getCurrentOnCloseMode() { return (Boolean) items.get("onCloseMode").get(); }
public Boolean isHideOnClose() { return (Boolean) items.get("hideOnClose").get(); }
/**
* Sets the current on close mode.
*
* @param currentOnCloseMode the on close mode that should be set.
* @param hideOnClose the on close mode that should be set.
* @since Envoy Client v0.3-alpha
*/
public void setCurrentOnCloseMode(boolean currentOnCloseMode) { ((SettingsItem<Boolean>) items.get("onCloseMode")).set(currentOnCloseMode); }
public void setHideOnClose(boolean hideOnClose) { ((SettingsItem<Boolean>) items.get("hideOnClose")).set(hideOnClose); }
/**
* @return the items

View File

@ -9,7 +9,6 @@ import javafx.stage.Stage;
import envoy.client.event.MessageCreationEvent;
import envoy.data.Message;
import envoy.event.EventBus;
import envoy.exception.EnvoyException;
/**
* Project: <strong>envoy-client</strong><br>
@ -34,19 +33,21 @@ public class StatusTrayIcon {
*/
private boolean displayMessages = false;
/**
* @return true if the status tray icon is supported on this platform
* @since Envoy Client v0.2-beta
*/
public static boolean isSupported() { return SystemTray.isSupported(); }
/**
* Creates a {@link StatusTrayIcon} with the Envoy logo, a tool tip and a pop-up
* menu.
*
* @param stage the stage which focus determines if message
* notifications are displayed
* @throws EnvoyException if the currently used OS does not support the System
* Tray API
* @since Envoy Client v0.2-beta
*/
public StatusTrayIcon(Stage stage) throws EnvoyException {
if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported.");
public StatusTrayIcon(Stage stage) {
trayIcon = new TrayIcon(IconUtil.loadAWTCompatible("/icons/envoy_logo.png"), "Envoy");
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("You are notified if you have unread messages.");
@ -62,29 +63,35 @@ public class StatusTrayIcon {
// Only display messages if the stage is not focused
stage.focusedProperty().addListener((ov, onHidden, onShown) -> displayMessages = ov.getValue() == Boolean.FALSE);
// Show the window if the user clicks on the icon
trayIcon.addActionListener(evt -> Platform.runLater(() -> { stage.setIconified(false); stage.toFront(); stage.requestFocus(); }));
// Start processing message events
// TODO: Handle other message types
EventBus.getInstance()
.register(MessageCreationEvent.class,
evt -> { if (displayMessages) trayIcon.displayMessage("New message received", evt.get().getText(), MessageType.INFO); });
EventBus.getInstance().register(MessageCreationEvent.class, evt -> {
if (displayMessages) trayIcon
.displayMessage(
evt.get().hasAttachment() ? "New " + evt.get().getAttachment().getType().toString().toLowerCase() + " message received"
: "New message received",
evt.get().getText(),
MessageType.INFO);
});
}
/**
* Makes this {@link StatusTrayIcon} appear in the system tray.
* Makes the icon appear in the system tray.
*
* @throws EnvoyException if the status icon could not be attaches to the system
* tray for system-internal reasons
* @since Envoy Client v0.2-alpha
*/
public void show() throws EnvoyException {
public void show() {
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (final AWTException e) {
throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e);
}
} catch (AWTException e) {}
}
/**
* Removes the icon from the system tray.
*
* @since Envoy Client v0.2-beta
*/
public void hide() { SystemTray.getSystemTray().remove(trayIcon); }
}

View File

@ -62,6 +62,7 @@ public final class LoginScene {
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
private static final EventBus eventBus = EventBus.getInstance();
private static final ClientConfig config = ClientConfig.getInstance();
private static final Settings settings = Settings.getInstance();
@FXML
private void initialize() {
@ -209,15 +210,22 @@ public final class LoginScene {
sceneContext.load(SceneContext.SceneInfo.CHAT_SCENE);
sceneContext.<ChatScene>getController().initializeData(sceneContext, localDB, client, writeProxy);
try {
new StatusTrayIcon(sceneContext.getStage()).show();
if (StatusTrayIcon.isSupported()) {
// Configure hide on close
sceneContext.getStage().setOnCloseRequest(e -> {
if (settings.isHideOnClose()) {
sceneContext.getStage().setIconified(true);
e.consume();
}
});
} catch (EnvoyException e) {
e.printStackTrace();
}
// Initialize status tray icon
final var trayIcon = new StatusTrayIcon(sceneContext.getStage());
settings.getItems().get("hideOnClose").setChangeHandler(c -> {
if (((Boolean) c)) trayIcon.show();
else trayIcon.hide();
});
}
}
}

View File

@ -31,7 +31,7 @@ public class GeneralSettingsPane extends SettingsPane {
final var vbox = new VBox();
// TODO: Support other value types
List.of("onCloseMode", "enterToSend")
List.of("hideOnClose", "enterToSend")
.stream()
.map(settings.getItems()::get)
.map(i -> new SettingsCheckbox((SettingsItem<Boolean>) i))