From a5f9ed4a4d575af65d22e7ef27c76c2815effc98 Mon Sep 17 00:00:00 2001 From: "Florian (Feuermagier)" Date: Thu, 19 Sep 2024 16:53:57 +0200 Subject: [PATCH] convert CEF window to dialog. this should fix the window being opened in the background only --- .../extensions/settings/ArtemisSettings.java | 2 + .../settings/ArtemisSettingsState.java | 2 +- .../java/edu/kit/kastel/login/CefDialog.java | 37 ++++++++++++++++ .../java/edu/kit/kastel/login/CefUtils.java | 42 +++++++++++-------- .../edu/kit/kastel/login/JwtRetriever.java | 7 ++-- .../edu/kit/kastel/state/PluginState.java | 30 +++++++------ 6 files changed, 85 insertions(+), 35 deletions(-) create mode 100644 src/main/java/edu/kit/kastel/login/CefDialog.java diff --git a/src/main/java/edu/kit/kastel/extensions/settings/ArtemisSettings.java b/src/main/java/edu/kit/kastel/extensions/settings/ArtemisSettings.java index 4bd30c5..ebb46ae 100644 --- a/src/main/java/edu/kit/kastel/extensions/settings/ArtemisSettings.java +++ b/src/main/java/edu/kit/kastel/extensions/settings/ArtemisSettings.java @@ -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(); }); diff --git a/src/main/java/edu/kit/kastel/extensions/settings/ArtemisSettingsState.java b/src/main/java/edu/kit/kastel/extensions/settings/ArtemisSettingsState.java index cd50a6c..251d405 100644 --- a/src/main/java/edu/kit/kastel/extensions/settings/ArtemisSettingsState.java +++ b/src/main/java/edu/kit/kastel/extensions/settings/ArtemisSettingsState.java @@ -33,7 +33,7 @@ public class ArtemisSettingsState implements PersistentStateComponent 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(); - // 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; } @@ -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; } } diff --git a/src/main/java/edu/kit/kastel/login/JwtRetriever.java b/src/main/java/edu/kit/kastel/login/JwtRetriever.java index af86d8a..eb23d30 100644 --- a/src/main/java/edu/kit/kastel/login/JwtRetriever.java +++ b/src/main/java/edu/kit/kastel/login/JwtRetriever.java @@ -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; } @@ -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; diff --git a/src/main/java/edu/kit/kastel/state/PluginState.java b/src/main/java/edu/kit/kastel/state/PluginState.java index 6353c2a..e27638f 100644 --- a/src/main/java/edu/kit/kastel/state/PluginState.java +++ b/src/main/java/edu/kit/kastel/state/PluginState.java @@ -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; @@ -324,21 +325,24 @@ private void resetState() { private CompletableFuture 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); + } }); }