diff --git a/src/main/java/stirling/software/SPDF/UI/impl/DesktopBrowser.java b/src/main/java/stirling/software/SPDF/UI/impl/DesktopBrowser.java index 403b25360..2f6593734 100644 --- a/src/main/java/stirling/software/SPDF/UI/impl/DesktopBrowser.java +++ b/src/main/java/stirling/software/SPDF/UI/impl/DesktopBrowser.java @@ -93,8 +93,21 @@ public class DesktopBrowser implements WebBrowser { setupMainFrame(); setupLoadHandler(); - // Show the frame immediately but transparent - frame.setVisible(true); + // Force initialize UI after 7 seconds if not already done + Timer timeoutTimer = + new Timer( + 2500, + e -> { + log.warn( + "Loading timeout reached. Forcing" + + " UI transition."); + if (!browserInitialized) { + // Force UI initialization + forceInitializeUI(); + } + }); + timeoutTimer.setRepeats(false); + timeoutTimer.start(); }); } catch (Exception e) { log.error("Error initializing JCEF browser: ", e); @@ -238,8 +251,8 @@ public class DesktopBrowser implements WebBrowser { boolean canGoBack, boolean canGoForward) { log.debug( - "Loading state change - isLoading: {}, canGoBack: {}, canGoForward: {}, " - + "browserInitialized: {}, Time elapsed: {}ms", + "Loading state change - isLoading: {}, canGoBack: {}, canGoForward:" + + " {}, browserInitialized: {}, Time elapsed: {}ms", isLoading, canGoBack, canGoForward, @@ -248,7 +261,8 @@ public class DesktopBrowser implements WebBrowser { if (!isLoading && !browserInitialized) { log.info( - "Browser finished loading, preparing to initialize UI components"); + "Browser finished loading, preparing to initialize UI" + + " components"); browserInitialized = true; SwingUtilities.invokeLater( () -> { @@ -289,10 +303,12 @@ public class DesktopBrowser implements WebBrowser { browser.getUIComponent() .requestFocus(); log.info( - "Browser component focused"); + "Browser component" + + " focused"); } catch (Exception ex) { log.error( - "Error focusing browser", + "Error focusing" + + " browser", ex); } }); @@ -415,4 +431,67 @@ public class DesktopBrowser implements WebBrowser { if (cefApp != null) cefApp.dispose(); if (loadingWindow != null) loadingWindow.dispose(); } + + public static void forceInitializeUI() { + try { + if (loadingWindow != null) { + log.info("Forcing start of UI initialization sequence"); + + // Close loading window first + loadingWindow.setVisible(false); + loadingWindow.dispose(); + loadingWindow = null; + log.info("Loading window disposed"); + + // Then setup the main frame + frame.setVisible(false); + frame.dispose(); + frame.setOpacity(1.0f); + frame.setUndecorated(false); + frame.pack(); + frame.setSize(UIScaling.scaleWidth(1280), UIScaling.scaleHeight(800)); + frame.setLocationRelativeTo(null); + log.debug("Frame reconfigured"); + + // Show the main frame + frame.setVisible(true); + frame.requestFocus(); + frame.toFront(); + log.info("Main frame displayed and focused"); + + // Focus the browser component if available + if (browser != null) { + Timer focusTimer = + new Timer( + 100, + e -> { + try { + browser.getUIComponent().requestFocus(); + log.info("Browser component focused"); + } catch (Exception ex) { + log.error( + "Error focusing browser during force ui" + + " initialization.", + ex); + } + }); + focusTimer.setRepeats(false); + focusTimer.start(); + } + } + } catch (Exception e) { + log.error("Error during Forced UI initialization.", e); + // Attempt cleanup on error + if (loadingWindow != null) { + loadingWindow.dispose(); + loadingWindow = null; + } + if (frame != null) { + frame.setVisible(true); + frame.setOpacity(1.0f); + frame.setUndecorated(false); + frame.requestFocus(); + } + } + } }