Skip to content

Commit

Permalink
join discord button
Browse files Browse the repository at this point in the history
Rename to `DiscordEventHandler`
Add button to join Discord

Fixes GH-1933
  • Loading branch information
micheljung authored and Sheikah45 committed Nov 6, 2020
1 parent 188fbe1 commit 4c7e4f5
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ dependencies {
testCompile("com.natpryce.hamcrest:hamcrest-reflection:0.1-2")
testCompile("org.springframework.boot:spring-boot-starter-test")

optional("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")

codacy("com.github.codacy:codacy-coverage-reporter:-SNAPSHOT")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,7 @@ public static class Discord {
private String smallImageKey;
private String bigImageKey;
private String discordPrereleaseFeedbackChannelUrl;
/** URL to join the FAF Discord server. */
private String joinUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

@Slf4j
@Component
public class ClientDiscordEventHandler extends DiscordEventHandlers {
public class DiscordEventHandler extends DiscordEventHandlers {
private final ApplicationEventPublisher applicationEventPublisher;
private final NotificationService notificationService;
private final PreferencesService preferencesService;

public ClientDiscordEventHandler(ApplicationEventPublisher applicationEventPublisher, NotificationService notificationService, PreferencesService preferencesService) {
public DiscordEventHandler(ApplicationEventPublisher applicationEventPublisher, NotificationService notificationService) {
this.applicationEventPublisher = applicationEventPublisher;
this.notificationService = notificationService;
this.preferencesService = preferencesService;
ready = this::onDiscordReady;
disconnected = this::onDisconnected;
errored = this::onError;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.faforever.client.discord;

import lombok.Data;
import lombok.Value;

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DiscordRichPresenceService implements DisposableBean {
private final Timer timer;


public DiscordRichPresenceService(PlayerService playerService, ClientDiscordEventHandler discordEventHandler,
public DiscordRichPresenceService(PlayerService playerService, DiscordEventHandler discordEventHandler,
ClientProperties clientProperties, PreferencesService preferencesService) {
this.playerService = playerService;
this.clientProperties = clientProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.faforever.client.discord;

import lombok.Value;

/** Fired when the user wants to join the Discord Server. */
@Value
public class JoinDiscordEvent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.faforever.client.discord;

import com.faforever.client.config.ClientProperties;
import com.faforever.client.fx.PlatformService;
import com.sun.jna.Platform;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RequiredArgsConstructor
public class JoinDiscordEventHandler {

private static final String COMMAND_KEY = "Discord\\shell\\open\\command";

private final ClientProperties clientProperties;
private final PlatformService platformService;

@EventListener(value = JoinDiscordEvent.class)
public void onJoin() throws IOException {
String joinUrl = clientProperties.getDiscord().getJoinUrl();
if (canJoinViaDiscord()) {
joinViaDiscord(joinUrl);
return;
}

joinViaBrowser(joinUrl);
}

private boolean canJoinViaDiscord() {
return Platform.isWindows() && Advapi32Util.registryKeyExists(WinReg.HKEY_CLASSES_ROOT, COMMAND_KEY);
}

private void joinViaBrowser(String joinUrl) {
platformService.showDocument(joinUrl);
}

private void joinViaDiscord(String joinUrl) throws IOException {
String command = Advapi32Util.registryGetStringValue(WinReg.HKEY_CLASSES_ROOT, COMMAND_KEY, null)
.replace("%1", joinUrl);

Runtime.getRuntime().exec(String.format("%s > nul 2>&1", command));
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/faforever/client/main/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.micheljung.fxwindow.FxStage;
import com.faforever.client.chat.event.UnreadPrivateMessageEvent;
import com.faforever.client.config.ClientProperties;
import com.faforever.client.discord.JoinDiscordEvent;
import com.faforever.client.fx.AbstractViewController;
import com.faforever.client.fx.Controller;
import com.faforever.client.fx.JavaFxUtil;
Expand Down Expand Up @@ -570,7 +571,6 @@ public void onNavigateButtonClicked(ActionEvent event) {

@Subscribe
public void onNavigateEvent(NavigateEvent navigateEvent) {

NavigationItem item = navigateEvent.getItem();

AbstractViewController<?> controller = getView(item);
Expand Down Expand Up @@ -642,6 +642,10 @@ public void setFxStage(FxStage fxWindow) {
this.fxStage = fxWindow;
}

public void onDiscordButtonClicked() {
applicationEventPublisher.publishEvent(new JoinDiscordEvent());
}

public class ToastDisplayer implements InvalidationListener {
private final TransientNotificationsController transientNotificationsController;

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ faf-client:
linksTecHelpForum: https://forum.faforever.com/category/4/i-need-help
linksRules: ${faf-client.website.base-url}/rules
linksModerationReport: ${faf-client.website.base-url}/account/report
linksDiscord: https://discordapp.com/invite/hgvj6Af
linksFacebook: https://www.facebook.com/ForgedAllianceForever/
linksDevNews: https://faforeverdevblog.tumblr.com
linksYoutube: https://www.youtube.com/channel/UCkAWiUu4QE172kv-ZuyR42w
Expand Down Expand Up @@ -57,6 +56,7 @@ faf-client:
application_id: 464069837237518357
small_image_key: faf_logo_small
big_image_key: faf_logo_big
join-url: https://discord.gg/PfGcWas

spring:
profiles:
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -800,4 +800,5 @@ modVault.modManager=Manage active mods
modVault.manageMods=Manage active mods
theme.needsRestart.message=Changing the theme to ''{0}'' might require a restart.
theme.needsRestart.quit=Quit
discord.join=Join FAF on Discord
gameUpdate.error.gameNotWritableAllowMultiOn=The game could not be patched to the required version for this game launch. We detected that you turned on allowing to watch replays while in game/lobby. This is most likely the cause for us to be unable to start the game. You are most likely trying to watch a replay or open a game of a conflicting version to the one you have already open. We are reminding you that having multiple game instances open is an experimental feature.
4 changes: 4 additions & 0 deletions src/main/resources/theme/icons.css
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,8 @@

.delete-icon {
-fx-shape: "M17.114,3.923h-4.589V2.427c0-0.252-0.207-0.459-0.46-0.459H7.935c-0.252,0-0.459,0.207-0.459,0.459v1.496h-4.59c-0.252,0-0.459,0.205-0.459,0.459c0,0.252,0.207,0.459,0.459,0.459h1.51v12.732c0,0.252,0.207,0.459,0.459,0.459h10.29c0.254,0,0.459-0.207,0.459-0.459V4.841h1.511c0.252,0,0.459-0.207,0.459-0.459C17.573,4.127,17.366,3.923,17.114,3.923M8.394,2.886h3.214v0.918H8.394V2.886z M14.686,17.114H5.314V4.841h9.372V17.114z M12.525,7.306v7.344c0,0.252-0.207,0.459-0.46,0.459s-0.458-0.207-0.458-0.459V7.306c0-0.254,0.205-0.459,0.458-0.459S12.525,7.051,12.525,7.306M8.394,7.306v7.344c0,0.252-0.207,0.459-0.459,0.459s-0.459-0.207-0.459-0.459V7.306c0-0.254,0.207-0.459,0.459-0.459S8.394,7.051,8.394,7.306";
}

.discord-icon {
-fx-shape: "M104.4 103.9c-5.7 0-10.2 5-10.2 11.1s4.6 11.1 10.2 11.1c5.7 0 10.2-5 10.2-11.1.1-6.1-4.5-11.1-10.2-11.1zM140.9 103.9c-5.7 0-10.2 5-10.2 11.1s4.6 11.1 10.2 11.1c5.7 0 10.2-5 10.2-11.1s-4.5-11.1-10.2-11.1z M189.5 20h-134C44.2 20 35 29.2 35 40.6v135.2c0 11.4 9.2 20.6 20.5 20.6h113.4l-5.3-18.5 12.8 11.9 12.1 11.2 21.5 19V40.6c0-11.4-9.2-20.6-20.5-20.6zm-38.6 130.6s-3.6-4.3-6.6-8.1c13.1-3.7 18.1-11.9 18.1-11.9-4.1 2.7-8 4.6-11.5 5.9-5 2.1-9.8 3.5-14.5 4.3-9.6 1.8-18.4 1.3-25.9-.1-5.7-1.1-10.6-2.7-14.7-4.3-2.3-.9-4.8-2-7.3-3.4-.3-.2-.6-.3-.9-.5-.2-.1-.3-.2-.4-.3-1.8-1-2.8-1.7-2.8-1.7s4.8 8 17.5 11.8c-3 3.8-6.7 8.3-6.7 8.3-22.1-.7-30.5-15.2-30.5-15.2 0-32.2 14.4-58.3 14.4-58.3 14.4-10.8 28.1-10.5 28.1-10.5l1 1.2c-18 5.2-26.3 13.1-26.3 13.1s2.2-1.2 5.9-2.9c10.7-4.7 19.2-6 22.7-6.3.6-.1 1.1-.2 1.7-.2 6.1-.8 13-1 20.2-.2 9.5 1.1 19.7 3.9 30.1 9.6 0 0-7.9-7.5-24.9-12.7l1.4-1.6s13.7-.3 28.1 10.5c0 0 14.4 26.1 14.4 58.3 0 0-8.5 14.5-30.6 15.2z";
}
44 changes: 30 additions & 14 deletions src/main/resources/theme/main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
Expand All @@ -15,6 +14,7 @@
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.control.Tooltip?>
<StackPane fx:id="mainRoot" alignment="TOP_LEFT" minHeight="10.0" minWidth="10.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.faforever.client.main.MainController">
<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="10.0" minWidth="10.0" VBox.vgrow="ALWAYS">
<children>
Expand Down Expand Up @@ -55,19 +55,35 @@
<MenuButton fx:id="navigationDropdown" minHeight="-Infinity" minWidth="-Infinity" />
<Pane maxWidth="1.7976931348623157E308" HBox.hgrow="ALWAYS" />
<HBox fx:id="rightMenuPane" alignment="TOP_RIGHT" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" styleClass="right-menu">
<children>
<StackPane alignment="TOP_RIGHT" maxHeight="-Infinity">
<children>
<Button fx:id="notificationButton" contentDisplay="CENTER" onAction="#onNotificationsButtonClicked" styleClass="inbox-button">
<graphic>
<StackPane>
<Region styleClass="inbox-icon" />
<Region styleClass="badge" />
</StackPane>
</graphic>
</Button>
</children>
</StackPane>
<children>
<Button fx:id="discordButton" maxHeight="-Infinity" mnemonicParsing="false"
styleClass="discord-button" onAction="#onDiscordButtonClicked">
<graphic>
<Region styleClass="icon,discord-icon"/>
</graphic>
<tooltip>
<Tooltip text="%discord.join"/>
</tooltip>
<HBox.margin>
<Insets right="10.0"/>
</HBox.margin>
</Button>
<StackPane alignment="TOP_RIGHT" maxHeight="-Infinity">
<children>
<Button fx:id="notificationButton" contentDisplay="CENTER"
onAction="#onNotificationsButtonClicked" styleClass="inbox-button">
<graphic>
<StackPane>
<Region styleClass="inbox-icon"/>
<Region styleClass="badge"/>
</StackPane>
</graphic>
</Button>
</children>
<HBox.margin>
<Insets right="10.0"/>
</HBox.margin>
</StackPane>
<fx:include source="user_button.fxml" />
</children>
<HBox.margin>
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/theme/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,14 @@
-fx-spacing: 0.416667em;
}

/***************** Notification Button *****************/
/***************** Top right Menu Buttons *****************/

.inbox-button {
.inbox-button, .discord-button {
-fx-padding: 0;
-fx-background-color: null;
}

.inbox-icon {
.inbox-icon, .discord-icon {
-fx-background-color: -fx-light-text-color;
-fx-min-width: 2em;
-fx-max-width: 2em;
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/com/faforever/client/main/MainControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.micheljung.fxwindow.FxStage;
import com.faforever.client.chat.ChatController;
import com.faforever.client.config.ClientProperties;
import com.faforever.client.discord.JoinDiscordEvent;
import com.faforever.client.fx.PlatformService;
import com.faforever.client.game.GamePathHandler;
import com.faforever.client.game.GameService;
Expand Down Expand Up @@ -37,7 +38,6 @@
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
Expand All @@ -59,10 +59,7 @@
import java.util.function.Consumer;

import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -378,4 +375,10 @@ public void testVaultCheckerCalled() {
WaitForAsyncUtils.waitForFxEvents();
verify(vaultFileSystemLocationChecker).checkVaultFileSystemLocation();
}

@Test
public void testOnJoinDiscordButtonClicked() {
instance.onDiscordButtonClicked();
verify(applicationEventPublisher).publishEvent(any(JoinDiscordEvent.class));
}
}

0 comments on commit 4c7e4f5

Please sign in to comment.