Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't load webViews in fxml #3071

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -163,8 +164,6 @@ protected void onInitialize() {
mentionPattern = Pattern.compile(
"(^|[^A-Za-z0-9-])" + Pattern.quote(loginService.getUsername()) + "([^A-Za-z0-9-]|$)", CASE_INSENSITIVE);

initChatView();

chatChannel.when(attached).subscribe(((oldValue, newValue) -> {
if (oldValue != null) {
oldValue.openProperty().unbind();
Expand Down Expand Up @@ -294,13 +293,12 @@ protected void onClosed(Event event) {

protected abstract TextInputControl messageTextField();

private void initChatView() {
WebView messagesWebView = getMessagesWebView();
webViewConfigurer.configureWebView(messagesWebView);
protected void configureWebView(WebView webView) {
webViewConfigurer.configureWebView(webView);

messagesWebView.zoomProperty().bindBidirectional(chatPrefs.zoomProperty());
webView.zoomProperty().bindBidirectional(chatPrefs.zoomProperty());

configureBrowser(messagesWebView);
configureBrowser(webView);
loadChatContainer();
}

Expand All @@ -323,10 +321,6 @@ private void loadChatContainer() {
private void configureBrowser(WebView messagesWebView) {
engine = messagesWebView.getEngine();

configureLoadListener();
}

private void configureLoadListener() {
engine.getLoadWorker()
.stateProperty()
.addListener((observable, oldValue, newValue) -> sendWaitingMessagesIfLoaded(newValue));
Expand All @@ -345,13 +339,17 @@ private void sendWaitingMessagesIfLoaded(State newValue) {
}
}

protected abstract WebView getMessagesWebView();
protected abstract CompletableFuture<WebView> getMessagesWebView();

protected JSObject getJsObject() {
return (JSObject) engine.executeScript("window");
}

protected void callJsMethod(String methodName, Object... args) {
if (engine == null) {
return;
}

try {
getJsObject().call(methodName, args);
} catch (Exception e) {
Expand Down Expand Up @@ -628,7 +626,7 @@ protected String convertUrlsToHyperlinks(String text) {
private void insertIntoContainer(String html, String containerId) {
((JSObject) engine.executeScript("document.getElementById('" + containerId + "')")).call("insertAdjacentHTML",
"beforeend", html);
getMessagesWebView().requestLayout();
getMessagesWebView().thenAcceptAsync(WebView::requestLayout, fxApplicationThreadExecutor);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/faforever/client/chat/ChannelTabController.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextFlow;
import javafx.scene.web.WebView;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -47,6 +48,7 @@
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static com.faforever.client.fx.PlatformService.URL_REGEX_PATTERN;
Expand All @@ -70,7 +72,7 @@ public class ChannelTabController extends AbstractChatTabController {
public HBox chatMessageSearchContainer;
public Button closeChatMessageSearchButton;
public TextField chatMessageSearchTextField;
public WebView messagesWebView;
public StackPane webViewContainer;
public TextField messageTextField;
public HBox topicPane;
public Label topicCharactersLimitLabel;
Expand All @@ -88,6 +90,8 @@ public class ChannelTabController extends AbstractChatTabController {
FXCollections.emptyObservableList());
private final ListChangeListener<ChatChannelUser> channelUserListChangeListener = this::updateChangedUsersStyles;

private CompletableFuture<WebView> webViewInitializationFuture;


public ChannelTabController(WebViewConfigurer webViewConfigurer, LoginService loginService, ChatService chatService,
PlayerService playerService,
Expand Down Expand Up @@ -167,6 +171,13 @@ protected void onInitialize() {

AutoCompletionHelper autoCompletionHelper = getAutoCompletionHelper();
autoCompletionHelper.bindTo(messageTextField());

webViewInitializationFuture = CompletableFuture.supplyAsync(() -> {
WebView webView = new WebView();
webViewContainer.getChildren().add(webView);
configureWebView(webView);
return webView;
}, fxApplicationThreadExecutor);
}

@Override
Expand Down Expand Up @@ -397,7 +408,7 @@ protected TextInputControl messageTextField() {
}

@Override
protected WebView getMessagesWebView() {
return messagesWebView;
protected CompletableFuture<WebView> getMessagesWebView() {
return webViewInitializationFuture;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputControl;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextFlow;
import javafx.scene.web.WebView;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
Expand All @@ -31,15 +32,16 @@
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class MatchmakingChatController extends AbstractChatTabController {

private final JoinDiscordEventHandler joinDiscordEventHandler;

public StackPane webViewContainer;
public Tab matchmakingChatTabRoot;
public WebView messagesWebView;
public TextField messageTextField;
public TextFlow topicText;
public Hyperlink discordLink;
Expand All @@ -54,6 +56,8 @@
}
};

private CompletableFuture<WebView> webViewInitializationFuture;

// TODO cut dependencies
public MatchmakingChatController(LoginService loginService,
PlayerService playerService, TimeService timeService, I18n i18n,
Expand Down Expand Up @@ -96,6 +100,13 @@
}).toList();
topicText.getChildren().setAll(labels);
topicText.getChildren().add(discordLink);

webViewInitializationFuture = CompletableFuture.supplyAsync(() -> {
WebView webView = new WebView();
webViewContainer.getChildren().add(webView);
configureWebView(webView);
return webView;
}, fxApplicationThreadExecutor);
}

@Override
Expand Down Expand Up @@ -123,8 +134,8 @@
}

@Override
protected WebView getMessagesWebView() {
return messagesWebView;
protected CompletableFuture<WebView> getMessagesWebView() {
return webViewInitializationFuture;

Check warning on line 138 in src/main/java/com/faforever/client/chat/MatchmakingChatController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/MatchmakingChatController.java#L138

Added line #L138 was not covered by tests
}

public void onDiscordButtonClicked() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javafx.scene.control.TextInputControl;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
Expand All @@ -36,6 +37,7 @@

import java.time.Instant;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static com.faforever.client.player.SocialStatus.FOE;

Expand All @@ -48,14 +50,15 @@
public Tab privateChatTabRoot;
public ImageView avatarImageView;
public Region defaultIconImageView;
public WebView messagesWebView;
public TextField messageTextField;
public PrivatePlayerInfoController privatePlayerInfoController;
public ScrollPane gameDetailScrollPane;
public StackPane webViewContainer;

private final ListChangeListener<ChatChannelUser> usersChangeListener = this::handlerPlayerChange;

private boolean userOffline;
private CompletableFuture<WebView> webViewInitializationFuture;

@Autowired
// TODO cut dependencies
Expand Down Expand Up @@ -104,6 +107,13 @@
newValue.addUsersListeners(usersChangeListener);
}
}));

webViewInitializationFuture = CompletableFuture.supplyAsync(() -> {
WebView webView = new WebView();
webViewContainer.getChildren().add(webView);
configureWebView(webView);
return webView;
}, fxApplicationThreadExecutor);
}

@Override
Expand Down Expand Up @@ -136,8 +146,8 @@
}

@Override
protected WebView getMessagesWebView() {
return messagesWebView;
protected CompletableFuture<WebView> getMessagesWebView() {
return webViewInitializationFuture;

Check warning on line 150 in src/main/java/com/faforever/client/chat/PrivateChatTabController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/PrivateChatTabController.java#L150

Added line #L150 was not covered by tests
}

@Override
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/com/faforever/client/coop/CoopController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.faforever.client.util.TimeService;
import com.faforever.commons.lobby.GameStatus;
import com.faforever.commons.lobby.GameType;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.google.common.base.Strings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand All @@ -49,6 +48,7 @@
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebView;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -63,6 +63,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -97,7 +98,7 @@

public GridPane coopRoot;
public ComboBox<CoopMissionBean> missionComboBox;
public WebView descriptionWebView;
public StackPane webViewContainer;
public Pane gameViewContainer;
public TextField titleTextField;
public Button playButton;
Expand All @@ -116,6 +117,8 @@
public TableColumn<CoopResultBean, String> replayColumn;
public GamesTableController gamesTableController;

private CompletableFuture<WebView> webViewInitializationFuture;

@Override
protected void onInitialize() {
missionComboBox.setCellFactory(param -> missionListCell());
Expand Down Expand Up @@ -190,9 +193,14 @@
tooltip.setShowDuration(javafx.util.Duration.seconds(30));
Tooltip.install(leaderboardInfoIcon, tooltip);

// Without this and no coop missions, the WebView is empty and the transparent background can't be applied to <html>
descriptionWebView.getEngine().loadContent("<html></html>");
webViewConfigurer.configureWebView(descriptionWebView);
webViewInitializationFuture = CompletableFuture.supplyAsync(() -> {
WebView webView = new WebView();

// Without this and no coop missions, the WebView is empty and the transparent background can't be applied to <html>
webView.getEngine().loadContent("<html></html>");
webViewConfigurer.configureWebView(webView);
return webView;
}, fxApplicationThreadExecutor);

FilteredList<GameBean> filteredItems = new FilteredList<>(gameService.getGames());
filteredItems.setPredicate(OPEN_COOP_GAMES_PREDICATE);
Expand Down Expand Up @@ -310,7 +318,8 @@
fxApplicationThreadExecutor.execute(() -> {
String description = mission.getDescription();
if (description != null) {
descriptionWebView.getEngine().loadContent(description);
webViewInitializationFuture.thenAcceptAsync(webView -> webView.getEngine().loadContent(description),

Check warning on line 321 in src/main/java/com/faforever/client/coop/CoopController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/coop/CoopController.java#L321

Added line #L321 was not covered by tests
fxApplicationThreadExecutor);
}
});
loadLeaderboard();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.base.Strings;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.web.WebView;
Expand All @@ -12,27 +13,42 @@
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequiredArgsConstructor
public class DualStringListCellController extends NodeController<Node> {
private final ThemeService themeService;
private final FxApplicationThreadExecutor fxApplicationThreadExecutor;

public HBox root;
public Label left;
public Label right;
public WebView webViewToolTip;
public Tooltip tooltip;

private CompletableFuture<WebView> webViewInitializeFuture;

@Override
protected void onInitialize() {
webViewInitializeFuture = CompletableFuture.supplyAsync(() -> {
WebView webView = new WebView();
themeService.registerWebView(webView);
tooltip.setGraphic(webView);
return webView;

Check warning on line 38 in src/main/java/com/faforever/client/fx/DualStringListCellController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/fx/DualStringListCellController.java#L34-L38

Added lines #L34 - L38 were not covered by tests
}, fxApplicationThreadExecutor);
}

Check warning on line 40 in src/main/java/com/faforever/client/fx/DualStringListCellController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/fx/DualStringListCellController.java#L40

Added line #L40 was not covered by tests

public void setLeftText(String apply) {
left.setText(apply);
}

public void setRightText(String apply) { right.setText(apply); }

public void setWebViewToolTip(String apply) {
if (!Strings.isNullOrEmpty(apply)) {
themeService.registerWebView(webViewToolTip);
webViewToolTip.getEngine().loadContent(apply);
public void setWebViewToolTip(String content) {
if (!Strings.isNullOrEmpty(content)) {
webViewInitializeFuture.thenAcceptAsync(webView -> webView.getEngine().loadContent(content),

Check warning on line 50 in src/main/java/com/faforever/client/fx/DualStringListCellController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/fx/DualStringListCellController.java#L50

Added line #L50 was not covered by tests
fxApplicationThreadExecutor);
}
}

Expand Down
Loading
Loading