Skip to content

Commit

Permalink
working on window handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 19, 2023
1 parent 4cf6b43 commit aa71c9c
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/main/java/org/htmlunit/WebClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1030,12 +1030,17 @@ private void fireWindowOpened(final WebWindowEvent event) {
}

private void fireWindowClosed(final WebWindowEvent event) {
if (currentWindowTracker_ != null) {
currentWindowTracker_.webWindowClosed(event);
}

for (final WebWindowListener listener : new ArrayList<>(webWindowListeners_)) {
listener.webWindowClosed(event);
}

// to open a new top level window if all others are gone
if (currentWindowTracker_ != null) {
currentWindowTracker_.webWindowClosed(event);
currentWindowTracker_.afterWebWindowClosedListenersProcessed(event);
}
}

Expand Down Expand Up @@ -2197,12 +2202,7 @@ public void webWindowClosed(final WebWindowEvent event) {
if (window instanceof TopLevelWindow) {
webClient_.topLevelWindows_.remove(window);
if (window == webClient_.getCurrentWindow()) {
if (webClient_.topLevelWindows_.isEmpty()) {
// Must always have at least window, and there are no top-level windows left; must create one.
final TopLevelWindow newWindow = new TopLevelWindow("", webClient_);
webClient_.setCurrentWindow(newWindow);
}
else {
if (!webClient_.topLevelWindows_.isEmpty()) {
// The current window is now the previous top-level window.
webClient_.setCurrentWindow(
webClient_.topLevelWindows_.get(webClient_.topLevelWindows_.size() - 1));
Expand All @@ -2221,6 +2221,17 @@ else if (window == webClient_.getCurrentWindow()) {
}
}

/**
* Postprocessing to make sure we have always one top level window open.
*/
public void afterWebWindowClosedListenersProcessed(final WebWindowEvent event) {
if (webClient_.topLevelWindows_.isEmpty()) {
// Must always have at least window, and there are no top-level windows left; must create one.
final TopLevelWindow newWindow = new TopLevelWindow("", webClient_);
webClient_.setCurrentWindow(newWindow);
}
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit aa71c9c

Please sign in to comment.