Skip to content

Commit

Permalink
convert CEF window to dialog. this should fix the window being opened…
Browse files Browse the repository at this point in the history
… in the background only
  • Loading branch information
Feuermagier committed Sep 19, 2024
1 parent c926caf commit a5f9ed4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class ArtemisSettings implements Configurable {

loginButton = new JButton("(Re-)Connect");
loginButton.addActionListener(a -> {
ArtemisSettingsState.getInstance().setArtemisAuthJWT(null);
ArtemisSettingsState.getInstance().setJwtExpiry(null);
this.apply();
PluginState.getInstance().connect();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ArtemisSettingsState implements PersistentStateComponent<ArtemisSet

private boolean useTokenLogin = true;
private String username = "";
private String artemisInstanceUrl = "https://artemis.praktomat.cs.kit.edu";
private String artemisInstanceUrl = "";
private AutograderOption autograderOption = AutograderOption.FROM_GITHUB;
private String autograderPath = null;
private String selectedGradingConfigPath;
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/edu/kit/kastel/login/CefDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package edu.kit.kastel.login;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.jcef.JBCefBrowser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import java.awt.GridLayout;

public class CefDialog extends DialogWrapper {
private final JBCefBrowser browser;

public CefDialog(JBCefBrowser browser) {
super((Project) null);
this.browser = browser;

this.setTitle("Artemis Login");
this.setModal(false);
this.init();
}

@Override
protected @Nullable JComponent createCenterPanel() {
JPanel browserContainer = new JPanel(new GridLayout(1, 1));
browserContainer.add(this.browser.getComponent());
return browserContainer;
}

@Override
protected Action @NotNull [] createActions() {
return new Action[0];
}
}
42 changes: 24 additions & 18 deletions src/main/java/edu/kit/kastel/login/CefUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.jcef.JBCefApp;
import com.intellij.ui.jcef.JBCefBrowser;
Expand Down Expand Up @@ -58,33 +60,35 @@ public static CompletableFuture<JBCefCookie> jcefBrowserLogin() {
// TODO the following code deletes the jwt cookie, which is needed for a "fresh" login
// TODO add this somewhere where it is useful
// CefApp.getInstance().onInitialization(state -> {
// browser.getJBCefCookieManager().deleteCookies(null, "jwt");
// browser.getJBCefCookieManager().deleteCookies(null, null);
// });

// add a handler to the Browser to be run if a page is loaded

// set focus handler because it gets invoked sometimes and causes NullPE otherwise
CefFocusHandler focusHandler = new CefWindowFocusHandler();
browserClient.addFocusHandler(focusHandler, browser.getCefBrowser());

// create window, display it and navigate to log in URL
JFrame window = createWindow(browser);

JwtRetriever jwtRetriever = new JwtRetriever(browser, window);
browserClient.addLoadHandler(jwtRetriever, browser.getCefBrowser());

var jwtFuture = new CompletableFuture<JBCefCookie>();

// Wait for CEF initialization
CefApp.getInstance().onInitialization(state -> {
jwtFuture.completeAsync(() -> {
try {
return jwtRetriever.getJwtCookie();
} catch (Exception ex) {
throw new CompletionException(ex);
}
SwingUtilities.invokeLater(() -> {
// create window, display it and navigate to log in URL
var window = new CefDialog(browser);
window.show();

JwtRetriever jwtRetriever = new JwtRetriever(browser, window);
browserClient.addLoadHandler(jwtRetriever, browser.getCefBrowser());

// Wait for CEF initialization
CefApp.getInstance().onInitialization(state -> {
jwtFuture.completeAsync(() -> {
try {
return jwtRetriever.getJwtCookie();
} catch (Exception ex) {
throw new CompletionException(ex);
}
});
});
});

return jwtFuture;
}

Expand All @@ -96,9 +100,11 @@ private static JFrame createWindow(@NotNull JBCefBrowser browserToAdd) {
(int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * BROWSER_WINDOW_SCALE_FACTOR),
(int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().getHeight() * BROWSER_WINDOW_SCALE_FACTOR));
JPanel browserContainer = new JPanel(new GridLayout(1, 1));
browserContainerWindow.add(browserContainer);
browserContainer.add(browserToAdd.getComponent());
browserContainerWindow.add(browserContainer);
browserContainerWindow.setAlwaysOnTop(true);
browserContainerWindow.setVisible(true);
new CefDialog(browserToAdd).show();
return browserContainerWindow;
}
}
7 changes: 4 additions & 3 deletions src/main/java/edu/kit/kastel/login/JwtRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public class JwtRetriever extends CefLoadHandlerAdapter {
private static final String JWT_COOKIE_KEY = "jwt";

private final JBCefBrowser browser;
private final JFrame window;
private final CefDialog window;

private volatile JBCefCookie jwtCookie;

public JwtRetriever(JBCefBrowser browser, JFrame window) {
public JwtRetriever(JBCefBrowser browser, CefDialog window) {
this.browser = browser;
this.window = window;
}
Expand All @@ -46,7 +46,8 @@ public JBCefCookie getJwtCookie() throws Exception {
// We may have been woken up because the cookie is available
if (jwtCookie != null) {
SwingUtilities.invokeLater(() -> {
window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
window.performOKAction();
// window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
this.browser.getCefBrowser().close(true);
});
return jwtCookie;
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/edu/kit/kastel/state/PluginState.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;

import com.intellij.openapi.diagnostic.Logger;
Expand Down Expand Up @@ -324,21 +325,24 @@ private void resetState() {
private CompletableFuture<String> retrieveJWT() {
var settings = ArtemisSettingsState.getInstance();

String previousJwt = settings.getArtemisAuthJWT();
if (previousJwt != null && !previousJwt.isBlank()) {
return CompletableFuture.completedFuture(previousJwt);
}
return CompletableFuture.supplyAsync(() -> {
String previousJwt = settings.getArtemisAuthJWT();
if (previousJwt != null && !previousJwt.isBlank()) {
return previousJwt;
}

if (!JBCefApp.isSupported()) {
return CompletableFuture.failedFuture(
new CompletionException(new IllegalStateException("JCEF unavailable")));
}
if (!JBCefApp.isSupported()) {
throw new CompletionException(new IllegalStateException("JCEF unavailable"));
}

var jwtCookieFuture = CefUtils.jcefBrowserLogin();
return jwtCookieFuture.thenApplyAsync(cookie -> {
settings.setArtemisAuthJWT(cookie.getValue());
settings.setJwtExpiry(cookie.getExpires());
return cookie.getValue();
try {
var cookie = CefUtils.jcefBrowserLogin().get();
settings.setArtemisAuthJWT(cookie.getValue());
settings.setJwtExpiry(cookie.getExpires());
return cookie.getValue();
} catch (ExecutionException | InterruptedException ex) {
throw new CompletionException(ex);
}
});
}

Expand Down

0 comments on commit a5f9ed4

Please sign in to comment.