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 d030a866dd
commit 833bb137b5
No known key found for this signature in database
GPG Key ID: 0A48559CA32CB48F
4 changed files with 45 additions and 30 deletions

View File

@ -75,7 +75,7 @@ public class Settings {
private void supplementDefaults() { private void supplementDefaults() {
items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key.")); 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.")); 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. * @return the current on close mode.
* @since Envoy Client v0.3-alpha * @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. * 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 * @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 * @return the items

View File

@ -9,7 +9,6 @@ import javafx.stage.Stage;
import envoy.client.event.MessageCreationEvent; import envoy.client.event.MessageCreationEvent;
import envoy.data.Message; import envoy.data.Message;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.exception.EnvoyException;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -34,19 +33,21 @@ public class StatusTrayIcon {
*/ */
private boolean displayMessages = false; 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 * Creates a {@link StatusTrayIcon} with the Envoy logo, a tool tip and a pop-up
* menu. * menu.
* *
* @param stage the stage which focus determines if message * @param stage the stage which focus determines if message
* notifications are displayed * notifications are displayed
* @throws EnvoyException if the currently used OS does not support the System
* Tray API
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public StatusTrayIcon(Stage stage) throws EnvoyException { public StatusTrayIcon(Stage stage) {
if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported.");
trayIcon = new TrayIcon(IconUtil.loadAWTCompatible("/icons/envoy_logo.png"), "Envoy"); trayIcon = new TrayIcon(IconUtil.loadAWTCompatible("/icons/envoy_logo.png"), "Envoy");
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.");
@ -61,30 +62,36 @@ public class StatusTrayIcon {
// Only display messages if the stage is not focused // Only display messages if the stage is not focused
stage.focusedProperty().addListener((ov, onHidden, onShown) -> displayMessages = ov.getValue() == Boolean.FALSE); stage.focusedProperty().addListener((ov, onHidden, onShown) -> displayMessages = ov.getValue() == Boolean.FALSE);
// Show the window if the user clicks on the icon // Show the window if the user clicks on the icon
trayIcon.addActionListener(evt -> Platform.runLater(() -> { stage.setIconified(false); stage.toFront(); stage.requestFocus(); })); trayIcon.addActionListener(evt -> Platform.runLater(() -> { stage.setIconified(false); stage.toFront(); stage.requestFocus(); }));
// Start processing message events // Start processing message events
// TODO: Handle other message types EventBus.getInstance().register(MessageCreationEvent.class, evt -> {
EventBus.getInstance() if (displayMessages) trayIcon
.register(MessageCreationEvent.class, .displayMessage(
evt -> { if (displayMessages) trayIcon.displayMessage("New message received", evt.get().getText(), MessageType.INFO); }); 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 * @since Envoy Client v0.2-alpha
*/ */
public void show() throws EnvoyException { public void show() {
try { try {
SystemTray.getSystemTray().add(trayIcon); SystemTray.getSystemTray().add(trayIcon);
} catch (final AWTException e) { } catch (AWTException e) {}
throw new EnvoyException("Could not attach Envoy tray icon to system tray.", 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 Logger logger = EnvoyLog.getLogger(LoginScene.class);
private static final EventBus eventBus = EventBus.getInstance(); private static final EventBus eventBus = EventBus.getInstance();
private static final ClientConfig config = ClientConfig.getInstance(); private static final ClientConfig config = ClientConfig.getInstance();
private static final Settings settings = Settings.getInstance();
@FXML @FXML
private void initialize() { private void initialize() {
@ -209,15 +210,22 @@ public final class LoginScene {
sceneContext.load(SceneContext.SceneInfo.CHAT_SCENE); sceneContext.load(SceneContext.SceneInfo.CHAT_SCENE);
sceneContext.<ChatScene>getController().initializeData(sceneContext, localDB, client, writeProxy); sceneContext.<ChatScene>getController().initializeData(sceneContext, localDB, client, writeProxy);
try { if (StatusTrayIcon.isSupported()) {
new StatusTrayIcon(sceneContext.getStage()).show();
// Configure hide on close
sceneContext.getStage().setOnCloseRequest(e -> { sceneContext.getStage().setOnCloseRequest(e -> {
sceneContext.getStage().setIconified(true); if (settings.isHideOnClose()) {
e.consume(); sceneContext.getStage().setIconified(true);
e.consume();
}
});
// 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();
}); });
} catch (EnvoyException e) {
e.printStackTrace();
} }
} }
} }

View File

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