Skip to content

Commit

Permalink
Merge pull request #2759 from ControlSystemStudio/CSSTUDIO-1997
Browse files Browse the repository at this point in the history
CSSTUDIO-1997 Add option to open most recently focused tab when closing a tab
  • Loading branch information
abrahamwolk authored Aug 10, 2023
2 parents 7be65b3 + 657aab9 commit 903f7a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
43 changes: 31 additions & 12 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,17 @@ public DockItem(final String label)
createContextMenu();

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

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

/** This tab should be in a DockPane, not a plain TabPane
Expand Down Expand Up @@ -593,20 +604,24 @@ public void select()
*/
public void addCloseCheck(final Supplier<Future<Boolean>> ok_to_close)
{
if (getOnCloseRequest() == null)
setOnCloseRequest(event ->
{
// For now, prevent closing
event.consume();
var alreadyExistingEventHandler = getOnCloseRequest();

// Invoke all the ok-to-close checks in background threads
// since those that save files might take time.
JobManager.schedule("Close " + getLabel(), monitor ->
{
if (prepareToClose())
Platform.runLater(() -> close());
});
setOnCloseRequest(event -> {
// For now, prevent closing
event.consume();

// Invoke all the ok-to-close checks in background threads
// since those that save files might take time.
JobManager.schedule("Close " + getLabel(), monitor ->
{
if (prepareToClose()) {
if (alreadyExistingEventHandler != null) {
alreadyExistingEventHandler.handle(event);
}
Platform.runLater(() -> close());
}
});
});

close_check.add(ok_to_close);
}
Expand Down Expand Up @@ -691,6 +706,10 @@ protected void handleClosed()
setContent(null);
// Remove "application" entry which otherwise holds on to application data model
getProperties().remove(KEY_APPLICATION);
var dockPane = getDockPane();
if (dockPane != null) {
dockPane.tabsInOrderOfFocus.remove(this);
}
}

/** Programmatically close this tab
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 903f7a6

Please sign in to comment.