Skip to content

Manage the active UI in a better way #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 28 additions & 43 deletions src/main/java/org/scijava/ui/DefaultUIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public final class DefaultUIService extends AbstractService implements
/** The default user interface to use, if one is not explicitly specified. */
private UserInterface defaultUI;

/** The last UI used when performing UI operations via the service. */
private UserInterface activeUI;

/**
* When true, {@link #isHeadless()} will return true regardless of the value
* of the {@code java.awt.headless} system property. When false, {@link
Expand All @@ -143,18 +146,12 @@ public void addUI(final String name, final UserInterface ui) {
@Override
public void showUI() {
if (disposed) return;
final UserInterface ui = getDefaultUI();
if (ui == null) {
throw new IllegalStateException("No UIs available. " +
"Please add a component containing a UIPlugin " +
"(e.g., scijava-ui-swing) to your class-path.");
}
showUI(ui);
showUI(activeUI());
}

@Override
public void showUI(final String name) {
final UserInterface ui = uiMap().get(name);
final UserInterface ui = getUI(name);
if (ui == null) {
throw new IllegalArgumentException("No such user interface: " + name);
}
Expand All @@ -174,14 +171,12 @@ public void showUI(final UserInterface ui) {

@Override
public boolean isVisible() {
final UserInterface ui = getDefaultUI();
if (ui == null) return false;
return ui.isVisible();
return activeUI().isVisible();
}

@Override
public boolean isVisible(final String name) {
final UserInterface ui = uiMap().get(name);
final UserInterface ui = getUI(name);
return ui != null && ui.isVisible();
}

Expand All @@ -200,7 +195,7 @@ public boolean isHeadless() {
@Override
public UserInterface getDefaultUI() {
if (!initialized) discoverUIs();
if (isHeadless()) return uiMap().get(HeadlessUI.NAME);
if (isHeadless()) return getUI(HeadlessUI.NAME);
if (defaultUI != null) return defaultUI;
return uiList().isEmpty() ? null : uiList().get(0);
}
Expand Down Expand Up @@ -244,17 +239,17 @@ public List<PluginInfo<DisplayViewer<?>>> getViewerPlugins() {

@Override
public void show(final Object o) {
getVisibleUI(true).show(o);
activeUI().show(o);
}

@Override
public void show(final String name, final Object o) {
getVisibleUI(true).show(name, o);
activeUI().show(name, o);
}

@Override
public void show(final Display<?> display) {
getVisibleUI(true).show(display);
activeUI().show(display);
}

@Override
Expand Down Expand Up @@ -309,44 +304,38 @@ public DialogPrompt.Result showDialog(final String message,
final String title, final DialogPrompt.MessageType messageType,
final DialogPrompt.OptionType optionType)
{
UserInterface ui = getVisibleUI(false);
if (ui == null) return null;
final DialogPrompt dialogPrompt = ui.dialogPrompt(message, title, messageType, optionType);
final DialogPrompt dialogPrompt = //
activeUI().dialogPrompt(message, title, messageType, optionType);
return dialogPrompt == null ? null : dialogPrompt.prompt();
}

@Override
public File chooseFile(final File file, final String style) {
final UserInterface ui = getVisibleUI(true);
return ui == null ? null : ui.chooseFile(file, style);
return activeUI().chooseFile(file, style);
}

@Override
public File
chooseFile(final String title, final File file, final String style)
{
final UserInterface ui = getVisibleUI(true);
return ui == null ? null : ui.chooseFile(title, file, style);
return activeUI().chooseFile(title, file, style);
}

@Override
public File[] chooseFiles(File parent, File[] files, FileFilter filter, String style) {
final UserInterface ui = getVisibleUI(true);
return ui == null ? null : ui.chooseFiles(parent, files, filter, style);
return activeUI().chooseFiles(parent, files, filter, style);
}

@Override
public List<File> chooseFiles(File parent, List<File> fileList, FileFilter filter, String style) {
final UserInterface ui = getVisibleUI(true);
return ui == null ? null : ui.chooseFiles(parent, fileList, filter, style);
return activeUI().chooseFiles(parent, fileList, filter, style);
}

@Override
public void showContextMenu(final String menuRoot, final Display<?> display,
final int x, final int y)
{
final UserInterface ui = getVisibleUI(true);
if (ui != null) ui.showContextMenu(menuRoot, display, x, y);
activeUI().showContextMenu(menuRoot, display, x, y);
}

@Override
Expand Down Expand Up @@ -542,20 +531,16 @@ private String getTitle() {
return appService.getApp().getTitle();
}

private UserInterface getVisibleUI(final boolean forceShow) {
// finds the first (highest priority) VISIBLE UserInterface
// if none are visible, then we show default UI if the caller indicated so.
UserInterface defaultUI = getDefaultUI();
if (defaultUI == null) return null;
if (defaultUI.isVisible()) return defaultUI;
else if(getVisibleUIs().size() > 0) {
return getVisibleUIs().get(0);
}
/** Gets the UI to use when performing UI operations via the service. */
private UserInterface activeUI() {
// If a particular UI is already active and still visible, use that one.
if (activeUI != null && activeUI.isVisible()) return activeUI;

if (forceShow) {
showUI(defaultUI);
return defaultUI;
}
return null;
// If a UI is visible, use it.
final List<UserInterface> visibleUIs = getVisibleUIs();
if (visibleUIs.size() > 0) return activeUI = visibleUIs.get(0);

// No UI is visible, so use the default one.
return activeUI = getDefaultUI();
}
}