Skip to content

Commit

Permalink
CSSTUDIO-1997 Select most recently focused tab when closing a tab tha…
Browse files Browse the repository at this point in the history
…t has the focus.
  • Loading branch information
abrahamwolk committed Aug 8, 2023
1 parent 18ca8ef commit 4c9233a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions core/ui/src/main/java/org/phoebus/ui/docking/DockItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ public DockItem(final String label)
createContextMenu();

setOnClosed(event -> handleClosed());
setOnCloseRequest(event -> {
// Select the previously selected tab:
var dockPane = getDockPane();
var recentlyOpenedTabs = dockPane.tabsInOrderOfFocus;
recentlyOpenedTabs.remove(this);

if (recentlyOpenedTabs.size() > 0) {
var tab = recentlyOpenedTabs.getFirst();
var selectionModel = dockPane.getSelectionModel();
selectionModel.select(tab);
}
});
}

/** This tab should be in a DockPane, not a plain TabPane
Expand Down
15 changes: 14 additions & 1 deletion core/ui/src/main/java/org/phoebus/ui/docking/DockPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

import java.lang.ref.WeakReference;
import java.text.MessageFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.logging.Level;
Expand Down Expand Up @@ -219,8 +223,17 @@ public static void alwaysShowTabs(final boolean do_show_single_tabs)
getTabs().addListener((InvalidationListener) change -> handleTabChanges());

setOnContextMenuRequested(this::showContextMenu);

getSelectionModel().selectedItemProperty().addListener((observable, previous_item, new_item) -> {
// Keep track of the order of focus of tabs:
if (new_item != null) {
tabsInOrderOfFocus.remove(new_item);
tabsInOrderOfFocus.push((DockItem) new_item);
}
});
}

protected LinkedList<DockItem> tabsInOrderOfFocus = new LinkedList<>();

private void showContextMenu(final ContextMenuEvent event)
{
Expand Down

0 comments on commit 4c9233a

Please sign in to comment.