Skip to content

Commit

Permalink
Themes support
Browse files Browse the repository at this point in the history
Fixes #1792
  • Loading branch information
1-alex98 committed Jun 20, 2020
1 parent 2f98cdd commit a3c3055
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,14 @@ public void initialize() {
});

currentThemeChangeListener = (observable, oldValue, newValue) -> themeComboBox.getSelectionModel().select(newValue);
selectedThemeChangeListener = (observable, oldValue, newValue) -> uiService.setTheme(newValue);
selectedThemeChangeListener = (observable, oldValue, newValue) -> {
uiService.setTheme(newValue);
if (newValue.isNeedsRestart()) {
notificationService.addNotification(new PersistentNotification(i18n.get("theme.needsRestart.message", newValue.getDisplayName()), Severity.WARN,
Collections.singletonList(new Action(i18n.get("theme.needsRestart.quit"), event -> Platform.exit()))));
// FIXME reload application (stage & application context) https://github.com/FAForever/downlords-faf-client/issues/1794
}
};

JavaFxUtil.addListener(preferences.getNotification().toastPositionProperty(), (observable, oldValue, newValue) -> setSelectedToastPosition(newValue));
setSelectedToastPosition(preferences.getNotification().getToastPosition());
Expand Down
46 changes: 30 additions & 16 deletions src/main/java/com/faforever/client/theme/Theme.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.faforever.client.theme;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
Expand All @@ -13,21 +15,20 @@ public class Theme {
private static final String AUTHOR = "author";
private static final String COMPATIBILITY_VERSION = "compatibilityVersion";
private static final String THEME_VERSION = "themeVersion";
private static final String NEEDS_RESTART = "needsRestart";

private StringProperty displayName;
private StringProperty author;
private SimpleObjectProperty<Integer> compatibilityVersion;
private StringProperty themeVersion;
private final StringProperty displayName;
private final StringProperty author;
private final SimpleObjectProperty<Integer> compatibilityVersion;
private final StringProperty themeVersion;
private final BooleanProperty needsRestart;

public Theme() {
this(null, null, null, null);
}

public Theme(String displayName, String author, Integer compatibilityVersion, String themeVersion) {
public Theme(String displayName, String author, Integer compatibilityVersion, String themeVersion, boolean needsRestart) {
this.displayName = new SimpleStringProperty(displayName);
this.author = new SimpleStringProperty(author);
this.compatibilityVersion = new SimpleObjectProperty<>(compatibilityVersion);
this.themeVersion = new SimpleStringProperty(themeVersion);
this.needsRestart = new SimpleBooleanProperty(needsRestart);
}

public String getDisplayName() {
Expand Down Expand Up @@ -78,6 +79,24 @@ public StringProperty themeVersionProperty() {
return themeVersion;
}

public static Theme fromProperties(Properties properties) {
return new Theme(
properties.getProperty(DISPLAY_NAME),
properties.getProperty(AUTHOR),
Integer.valueOf(properties.getProperty(COMPATIBILITY_VERSION)),
properties.getProperty(THEME_VERSION),
Boolean.parseBoolean(properties.getProperty(NEEDS_RESTART))
);
}

public boolean isNeedsRestart() {
return needsRestart.get();
}

public void setNeedsRestart(boolean needsRestart) {
this.needsRestart.set(needsRestart);
}

@Override
public int hashCode() {
return Objects.hash(displayName, themeVersion);
Expand Down Expand Up @@ -105,12 +124,7 @@ public Properties toProperties() {
return properties;
}

public static Theme fromProperties(Properties properties) {
return new Theme(
properties.getProperty(DISPLAY_NAME),
properties.getProperty(AUTHOR),
Integer.valueOf(properties.getProperty(COMPATIBILITY_VERSION)),
properties.getProperty(THEME_VERSION)
);
public BooleanProperty needsRestartProperty() {
return needsRestart;
}
}
11 changes: 2 additions & 9 deletions src/main/java/com/faforever/client/theme/UiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,7 @@ public class UiService implements InitializingBean, DisposableBean {
public static final String CHAT_LIST_STATUS_LOBBYING = "theme/images/player_status/lobby.png";
public static final String CHAT_LIST_STATUS_PLAYING = "theme/images/player_status/playing.png";

public static Theme DEFAULT_THEME = new Theme() {
{
setAuthor("Downlord");
setCompatibilityVersion(1);
setDisplayName("Default");
setThemeVersion("1.0");
}
};
public static Theme DEFAULT_THEME = new Theme("Default", "Downlord", 1, "1", false);

private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
/**
Expand Down Expand Up @@ -329,9 +322,9 @@ public void setTheme(Theme theme) {
preferencesService.getPreferences().setThemeName(getThemeDirectory(theme).getFileName().toString());
}
preferencesService.storeInBackground();
reloadStylesheet();
currentTheme.set(theme);
cacheManager.getCache(CacheNames.THEME_IMAGES).clear();
reloadStylesheet();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -764,4 +764,6 @@ startTab.message=Here you can select the tab your client starts in. You can chan
startTab.title=Select start tab
startTab.save=Save
startTab.wantToConfigure=Select the tab to start at:
startTab.configure=Configure
startTab.configure=Configure
theme.needsRestart.message=Changing the theme to {} might require a restart.
theme.needsRestart.quit=Quit
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.faforever.client.preferences.TimeInfo;
import com.faforever.client.settings.LanguageItemController;
import com.faforever.client.test.AbstractPlainJavaFxTest;
import com.faforever.client.theme.Theme;
import com.faforever.client.theme.UiService;
import com.faforever.client.update.ClientUpdateService;
import com.faforever.client.user.UserService;
Expand All @@ -22,20 +23,26 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.testfx.util.WaitForAsyncUtils;

import java.util.Arrays;
import java.util.Locale;
import java.util.Set;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class SettingsControllerTest extends AbstractPlainJavaFxTest {
private static final Theme DEFAULT_THEME = new Theme("Default", "none", 1, "1", false);
private static final Theme SECOND_THEME = new Theme("Second", "none", 1, "1", true);

private SettingsController instance;
@Mock
Expand Down Expand Up @@ -67,6 +74,13 @@ public void setUp() throws Exception {
preferences = new Preferences();
when(preferenceService.getPreferences()).thenReturn(preferences);
when(uiService.currentThemeProperty()).thenReturn(new SimpleObjectProperty<>());
when(uiService.getCurrentTheme())
.thenReturn(DEFAULT_THEME);
when(uiService.getAvailableThemes())
.thenReturn(Arrays.asList(
DEFAULT_THEME,
SECOND_THEME
));
when(uiService.loadFxml("theme/settings/auto_join_channels.fxml")).thenReturn(autoJoinChannelsController);
when(autoJoinChannelsController.getRoot()).thenReturn(new Pane());

Expand All @@ -77,6 +91,29 @@ public void setUp() throws Exception {
loadFxml("theme/settings/settings.fxml", param -> instance);
}

@Test
public void testThemesDisplayed() {
assertThat(instance.themeComboBox.getSelectionModel().getSelectedItem(), is(DEFAULT_THEME));
assertThat(instance.themeComboBox.getItems(), hasItem(DEFAULT_THEME));
assertThat(instance.themeComboBox.getItems(), hasItem(SECOND_THEME));
}

@Test
public void testSelectingSecondThemeCausesReloadAndRestartPrompt() {
instance.themeComboBox.getSelectionModel().select(SECOND_THEME);
verify(uiService).setTheme(SECOND_THEME);
verify(notificationService).addNotification(any(PersistentNotification.class));
}

@Test
public void testSelectingDefaultThemeDoesNotCausesRestartPrompt() {
instance.themeComboBox.getSelectionModel().select(SECOND_THEME);
WaitForAsyncUtils.waitForFxEvents();
instance.themeComboBox.getSelectionModel().select(DEFAULT_THEME);
verify(notificationService, times(1)).addNotification(any(PersistentNotification.class));
verify(uiService).setTheme(DEFAULT_THEME);
}

@Test
public void testSearchForBetaUpdateIfOptionIsTurnedOn() {
instance.prereleaseToggleButton.setSelected(true);
Expand Down

0 comments on commit a3c3055

Please sign in to comment.