Skip to content

Commit d8b632f

Browse files
ctruedenclaude
andcommitted
Scripts declare their presentation, and menus take more than one source
Answering "how does a script say where it belongs, and what language it is written in": - `#@script(menu = "Process>Filters>Blur It", accelerator = "^B", label, weight, iconPath)` -- the same keys @menu uses. These directives were parsed already and consumed by nothing; now they mean something. - Where a script says nothing, the directory speaks for it: scripts/Process/Filters/Blur_It.groovy means the same thing, underscores read as spaces. That is how the scripts in the wild are arranged. - `#@script(language = "jython")` or a `#!` line settles which language is meant, for the ones sharing an extension -- .py being Jython or Python. Silence means the extension decides. Making that work needed the gap underneath filled. `MenuTree.of` took `Collection<CommandInfo>`, and a CommandInfo is class-and-index shaped; a script is not a class. So `ExecutableInfo` is now the question the menu layer asks -- menuPath, label, accelerator, iconPath, weight, visible -- and it extends Executable, so what a menu holds is the thing to run. `CommandInfo` implements it; `ExecutableInfo.of(executable, attrs)` makes one out of a script and its directives; a SciJava Common ModuleInfo will make one through the bridge, with no further change to the menu layer, the bindings or the shells. It is deliberately not called `MenuEntry`: a menu path is optional, and plenty of runnable things belong in no menu -- one invoked by name from a script, one reached only through the search bar, one that exists to be called by something else. MenuTree takes the subset with a path; CommandsTest pins that a pathless command is still labelled, still runnable, and simply absent from the tree. `ScriptFinder` reads a directory into scripts plus metadata maps -- not menu entries, since scijava-script3 knows nothing about menus. All three shells now build their menus from the annotation index *and* a directory of Groovy scripts, and nothing below them can tell which came from where. That is the AppLoader shape the fijifx notes sketched, now load-bearing rather than planned. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013gJpzGd3ksbME4CN6Tu2ni
1 parent ec8f4ee commit d8b632f

28 files changed

Lines changed: 974 additions & 74 deletions

File tree

‎docs/migration-plan.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,32 @@ widgets and watching its callbacks fire.
839839
which the header reader does directly. `ScriptService` has no equivalent -
840840
`Scripts.get().of(path)` is a line. `ScriptREPL`, `ScriptInterpreter`,
841841
`AutoCompleter` and `CodeGenerator` belong with the script editor.
842+
- **A script declares its presentation the way a command does.**
843+
`#@script(menu = "Process>Filters>Blur It", accelerator = "^B", label = ...,
844+
weight = ..., iconPath = ...)` carries the same keys `@Menu` does, and where
845+
a script says nothing, the directory it sits in speaks for it:
846+
`scripts/Process/Filters/Blur_It.groovy` means the same thing, underscores
847+
read as spaces. That is how the scripts already in the wild are arranged, so
848+
it had better keep working.
849+
- **A script names its own language** with `#@script(language = "jython")` or
850+
a `#!` line, which is what settles the languages sharing an extension -
851+
`.py` being Jython or Python depending on which is meant. Saying nothing
852+
means the extension decides, as before.
853+
- **`ExecutableInfo` is how an application takes more than one source.** A
854+
`CommandInfo` is one; `ExecutableInfo.of(executable, attrs)` makes one out
855+
of a script and its directives; a SciJava Common `ModuleInfo` will make one
856+
through the bridge. An application gathers what it can run from as many
857+
sources as it has, and nothing downstream learns there was more than one.
858+
**This is the `AppLoader` shape the `fijifx` notes sketched**, and it is now
859+
load-bearing rather than planned: all three shells build their menus from
860+
the annotation index *and* a directory of Groovy scripts, and nothing below
861+
them can tell which came from where.
862+
- It is **not** called `MenuEntry`, and the distinction matters: a menu path
863+
is optional. A command invoked by name from a script, one reached only
864+
through the search bar, one that exists to be called by something else -
865+
all are ordinary things to have, and all are perfectly runnable.
866+
`MenuTree` takes the subset with a path; the type is named for what it is
867+
rather than for the one place it is most often shown.
842868
- **Open**: whether a parameter with declared choices and no value should
843869
preselect the first. SciJava Common does; the model here leaves it unset,
844870
which is honest but shows an empty chooser. It is a one-line change in

‎docs/migration.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ A **dropped** row is a successful outcome, not a gap.
123123
| `convert` | `org.scijava.convert3`: `Converter` asks one question - can you turn *this value* into *that type* - where SciJava Common had eight `canConvert` overloads and a `ConversionRequest` to carry the combinations. `ConvertService` becomes `Converters`, backed by plain `ServiceLoader` so that conversion sits *below* the container: parameter binding converts, and a script or command line supplies strings for everything. `DefaultConverter`'s pile of special cases becomes one converter per concern (cast, number, string, to-string, array, collection, constructor, file/path), each contributable and orderable by priority. `Converter.of(String.class, Foo.class, Foo::parse)` covers the common case in a line | ported |
124124
| `module`, `module.process` | `org.scijava.execute`: `@Parameter` on fields, `Executables` for the plain case, and `Runner` for the processor chain. `ModuleService.run` becomes `Runner.run`, still returning a future, but of an `ExecutionResult` that reports a declined run with its reason and its author rather than through `ModulePreprocessor.isCanceled()` | ported |
125125
| `command` | `org.scijava.command`: `Command` is a `Runnable` whose `@Parameter` fields are its struct; `@Menu` (repeatable) carries the presentation metadata, read from the annotation index without loading the class. `CommandInfo` replaces `ModuleInfo` and needs no `Context`; `MenuTree` replaces `ShadowMenu`. There is no `DynamicCommand`: see `choicesFrom` and `@Group(membersFrom)` | ported |
126-
| `script`, `script.process` | `org.scijava.script3`: `ScriptLanguage`/`ScriptSession` are the SPI, with `JsrLanguage` making every JSR-223 engine on the classpath available for free; GraalVM polyglot and Appose are further languages, not replacements. `ScriptInfo`/`ScriptModule` become `ScriptExecutable`, an ordinary `Executable`, so a script runs through the same `Runner`, preprocessors, dialogs and menus as a Java command. `ScriptService` has no equivalent: `Scripts.get().of(path)` is a line. The `#@` header syntax is unchanged, and declaration lines are blanked rather than removed so the language's error messages keep pointing at the right line. The processor chain (`ScriptProcessor` and friends) is not ported: it existed to rewrite scripts before evaluation, and parameters plus a shebang are all it was actually used for. `ScriptREPL`, `ScriptInterpreter`, `AutoCompleter` and `CodeGenerator` await the script editor | ported |
126+
| `script`, `script.process` | `org.scijava.script3`: `ScriptLanguage`/`ScriptSession` are the SPI, with `JsrLanguage` making every JSR-223 engine on the classpath available for free; GraalVM polyglot and Appose are further languages, not replacements. `ScriptInfo`/`ScriptModule` become `ScriptExecutable`, an ordinary `Executable`, so a script runs through the same `Runner`, preprocessors, dialogs and menus as a Java command. `ScriptService` has no equivalent: `Scripts.get().of(path)` is a line. The `#@` header syntax is unchanged, and declaration lines are blanked rather than removed so the language's error messages keep pointing at the right line. The processor chain (`ScriptProcessor` and friends) is not ported: it existed to rewrite scripts before evaluation, and parameters plus a shebang are all it was actually used for. `ScriptREPL`, `ScriptInterpreter`, `AutoCompleter` and `CodeGenerator` await the script editor. `ScriptFinder` is ported: it reads `#@script` directives and falls back to the directory hierarchy for a menu path, handing an application metadata rather than menus, since this component knows nothing about menus | ported |
127127
| `widget` (`InputWidget`, `InputPanel`, `InputHarvester`, `WidgetModel`, `WidgetStyle`) | `org.scijava.ui3`: `Widget` is a view of a `ParameterNode`, `WidgetFactory` a discoverable `@Plugin`, `WidgetPanel` a widget holding widgets (so a group nests with no special case), and `WidgetPanels` walks the tree for every toolkit. `WidgetModel` has no equivalent - a widget reads the `ParameterModel` and the node directly, and `WidgetStyle`'s parsing is `Widgets.isStyle`/`styleValue`. `InputHarvester` is a `Preprocessor` that declines the run when the user dismisses the dialog, rather than setting a `canceled` flag on itself | ported |
128128
| `ui.awt` (satellite repo `scijava-ui-awt`) | `org.scijava.awt`: the widget set in pure `java.awt` - no spinner, so the number widget is a field and a scroll bar - plus `Edt`, the first of the AWT platform layer that the Swing binding shares. `AWTColors`, `AWTWindows`, `AWTClipboard`, `AWTInputEventDispatcher` and the drag-and-drop dispatchers belong there too, when the `input`/`tool` layer arrives; the `AWTUI`/`AWTApplicationFrame`/`AWTStatusBar` shell classes await the application layer | ported |
129129
| `ui.javafx` (satellite repo `scijava-ui-javafx`) | `org.scijava.javafx`: the same widget set as the Swing binding, plus `FxThread` for JavaFX's application thread. `JavaFXKeys`'s key-code mapping is worth keeping when the `input`/`tool` layer arrives; `JavaFXMenuCreator` is superseded by `FxMenus`, some 40 lines against `MenuCreator` | ported |
130130
| `ui.swing.widget` | `org.scijava.swing`: number (spinner, with an optional slider or scroll bar and labelled ticks), text (field, password, area, and `BigInteger`/`BigDecimal`, which a spinner cannot step), toggle, choice, file, message, and the panel and dialog. `SpinnerBigDecimalModel`, `SpinnerBigIntegerModel`, `SpinnerTypedNumberModel` and `SpinnerNumberModelFactory` have no equivalent: `SpinnerNumberModel` already steps in the type of the value it holds, and arbitrary precision goes to the text widget. MigLayout is gone, in favor of `GridBagLayout`: one fewer dependency, and one fewer automatic module. The button, color, date, file-list and radio-choice widgets are not ported yet - each earns its place when something asks for it | ported |
131-
| `menu` (`MenuCreator`, `AbstractMenuCreator`, `MenuService`, `ShadowMenu`) | `org.scijava.command`: `MenuTree` is the model and `MenuCreator`/`Menus` the toolkit-free walk, with `SwingMenus` the first implementation. `MenuService` has no equivalent: a menu bar is `MenuTree.of(Commands.discover(context))`, which is a line rather than a service | ported |
131+
| `menu` (`MenuCreator`, `AbstractMenuCreator`, `MenuService`, `ShadowMenu`) | `org.scijava.command`: `ExecutableInfo` is anything an application can run (a menu path being optional), `MenuTree` arranges the subset that has one, and `MenuCreator`/`Menus` are the toolkit-free walk, with `SwingMenus`, `FxMenus` and `AwtMenus` implementing it. `MenuService` has no equivalent: a menu bar is `MenuTree.of(Commands.discover(context))`, which is a line rather than a service | ported |
132132
| `ui`, `menu` | `scijava-ui3` (toolkit-agnostic contracts) and `scijava-menu` (the menu tree), with `ApplicationFrame`/`Desktop`/`StatusBar`/`ToolBar` as contracts there and implementations in the toolkit bindings. Dropped from the *core*, not from the stack — see the layering section of [migration-plan.md](migration-plan.md) | planned |
133133
| `input`, `tool` | UI-agnostic facades on `scijava-events` (Phase 4) | planned |
134134
| `display` | overhauled, not ported (Phase 4) | open |

‎scijava-awt/pom.xml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<scijava-index.version>1.0.0-SNAPSHOT</scijava-index.version>
9393
<scijava-ui3.version>3.0.0-SNAPSHOT</scijava-ui3.version>
9494
<scijava-ui3-test.version>3.0.0-SNAPSHOT</scijava-ui3-test.version>
95+
<scijava-script3.version>3.0.0-SNAPSHOT</scijava-script3.version>
9596
<scijava-common3.version>1.0.1-SNAPSHOT</scijava-common3.version>
9697
<scijava-execute.version>1.0.0-SNAPSHOT</scijava-execute.version>
9798
<scijava-harvest.version>1.0.0-SNAPSHOT</scijava-harvest.version>
@@ -158,6 +159,23 @@
158159
</dependency>
159160

160161
<!-- Test scope dependencies -->
162+
<!-- NB: so the demo shell can put scripts in its menus, as Fiji does. -->
163+
<dependency>
164+
<groupId>org.scijava</groupId>
165+
<artifactId>scijava-script3</artifactId>
166+
<version>${scijava-script3.version}</version>
167+
<scope>test</scope>
168+
</dependency>
169+
<dependency>
170+
<groupId>org.apache.groovy</groupId>
171+
<artifactId>groovy</artifactId>
172+
<scope>test</scope>
173+
</dependency>
174+
<dependency>
175+
<groupId>org.apache.groovy</groupId>
176+
<artifactId>groovy-jsr223</artifactId>
177+
<scope>test</scope>
178+
</dependency>
161179
<dependency>
162180
<groupId>org.scijava</groupId>
163181
<artifactId>scijava-ui3-test</artifactId>

‎scijava-awt/src/main/java/org/scijava/awt/AwtMenus.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import java.util.function.Consumer;
3939

4040
import org.scijava.command.Accelerator;
41-
import org.scijava.command.CommandInfo;
41+
import org.scijava.command.ExecutableInfo;
4242
import org.scijava.command.MenuCreator;
4343
import org.scijava.command.MenuTree;
4444
import org.scijava.command.Menus;
@@ -54,15 +54,15 @@ public class AwtMenus implements MenuCreator<MenuBar, Menu> {
5454

5555
private static final Logger log = LoggerFactory.getLogger(AwtMenus.class);
5656

57-
private final Consumer<CommandInfo> onSelect;
57+
private final Consumer<ExecutableInfo> onSelect;
5858

59-
public AwtMenus(final Consumer<CommandInfo> onSelect) {
59+
public AwtMenus(final Consumer<ExecutableInfo> onSelect) {
6060
this.onSelect = onSelect;
6161
}
6262

6363
/** Builds a menu bar holding the given menu tree. */
6464
public static MenuBar create(final MenuTree root,
65-
final Consumer<CommandInfo> onSelect)
65+
final Consumer<ExecutableInfo> onSelect)
6666
{
6767
return Menus.build(root, new MenuBar(), new AwtMenus(onSelect));
6868
}
@@ -83,7 +83,7 @@ public Menu subMenu(final MenuTree node, final Menu parent) {
8383

8484
@Override
8585
public void item(final MenuTree leaf, final Menu parent) {
86-
final CommandInfo command = leaf.command().orElseThrow();
86+
final ExecutableInfo command = leaf.entry().orElseThrow();
8787
final MenuItem item = new MenuItem(leaf.label());
8888
shortcut(command.accelerator().orElse(null)).ifPresent(item::setShortcut);
8989
// NB: AWT menu items have no icon at all, so iconPath is ignored here.

‎scijava-awt/src/test/java/org/scijava/awt/AwtShell.java‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
import java.util.concurrent.Future;
4343
import java.util.stream.Collectors;
4444

45-
import org.scijava.command.CommandInfo;
4645
import org.scijava.command.Commands;
46+
import org.scijava.command.ExecutableInfo;
4747
import org.scijava.command.MenuTree;
4848
import org.scijava.context.Context;
4949
import org.scijava.execute.ExecutionResult;
@@ -72,16 +72,15 @@
7272
public class AwtShell {
7373

7474
private final Context context = Context.create();
75-
private final java.util.List<CommandInfo> commands = Commands.discover(
76-
context);
75+
private final java.util.List<ExecutableInfo> commands = entries();
7776
private final Runner runner;
7877

7978
private final Frame frame = new Frame("SciJava");
8079
private final Label status = new Label("Ready");
8180
private final TextField search = new TextField(24);
8281
private final List matches = new List(8);
8382

84-
private java.util.List<CommandInfo> shown = java.util.List.of();
83+
private java.util.List<ExecutableInfo> shown = java.util.List.of();
8584

8685
public AwtShell() {
8786
runner = Runner.of(java.util.List.of(AwtInputHarvester.of(context)),
@@ -118,6 +117,24 @@ public static void main(final String... args) {
118117

119118
// -- Helper methods --
120119

120+
/** Gathers what this application can run: commands, and scripts. */
121+
private java.util.List<ExecutableInfo> entries() {
122+
final java.util.List<ExecutableInfo> entries = new java.util.ArrayList<>(
123+
Commands.discover(context));
124+
final java.net.URL url = AwtShell.class.getResource("/scripts");
125+
if (url != null) {
126+
try {
127+
new org.scijava.script3.ScriptFinder().find(java.nio.file.Paths.get(url
128+
.toURI())).forEach(found -> entries.add(ExecutableInfo.of(found.script(),
129+
found.metadata())));
130+
}
131+
catch (final java.net.URISyntaxException exc) {
132+
// NB: no scripts to add, which is not a reason to fail to start.
133+
}
134+
}
135+
return entries;
136+
}
137+
121138
/** Narrows the list to the commands whose name contains what was typed. */
122139
private void filter() {
123140
final String text = search.getText().trim().toLowerCase(Locale.ROOT);
@@ -136,16 +153,16 @@ private void runSelected() {
136153
}
137154

138155
/** Runs a command, and says what came back. */
139-
private void run(final CommandInfo command) {
156+
private void run(final ExecutableInfo command) {
140157
status.setText("Running " + command.label() + "...");
141158
final Future<ExecutionResult> future = runner.run(command, Map.of());
142159
new Thread(() -> {
143160
final String message = describe(future);
144161
Edt.later(() -> status.setText(message));
145-
}, "shell-" + command.className()).start();
162+
}, "shell-" + command.name()).start();
146163
}
147164

148-
private static String describe(final CommandInfo command) {
165+
private static String describe(final ExecutableInfo command) {
149166
return command.menuPath().map(path -> path.replace(">", " ▸ ")) //
150167
.orElseGet(() -> command.label() + " (not in any menu)");
151168
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#@ double(label = "Radius", min = 0, max = 20, style = "slider,ticks:4") sigma
2+
#@output String result
3+
result = "Blurred with sigma " + sigma
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#@script(menu = "Help>Say Hello From A Script", accelerator = "^H", weight = 50)
2+
#@ String(value = "world") name
3+
#@output String greeting
4+
greeting = "hello " + name

‎scijava-command/src/main/java/org/scijava/command/CommandInfo.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
*
5858
* @author Curtis Rueden
5959
*/
60-
public class CommandInfo implements Executable {
60+
public class CommandInfo implements ExecutableInfo {
6161

6262
private final Discovery<Command> discovery;
6363
private final Map<String, String> menu;
@@ -78,34 +78,40 @@ public String className() {
7878
}
7979

8080
/** Gets where this command sits in the menus, if anywhere. */
81+
@Override
8182
public Optional<String> menuPath() {
8283
return attr("path");
8384
}
8485

8586
/** Gets the label to display, defaulting to the last menu path element. */
87+
@Override
8688
public String label() {
8789
return menuPath() //
8890
.map(path -> path.substring(path.lastIndexOf('>') + 1)) //
8991
.orElseGet(this::name);
9092
}
9193

9294
/** Gets this command's keyboard shortcut, if it has one. */
95+
@Override
9396
public Optional<String> accelerator() {
9497
return attr("accelerator");
9598
}
9699

97100
/** Gets this command's icon resource, if it has one. */
101+
@Override
98102
public Optional<String> iconPath() {
99103
return attr("iconPath");
100104
}
101105

102106
/** Gets how this command sorts among its menu siblings. */
107+
@Override
103108
public double weight() {
104109
return attr("weight").map(Double::parseDouble) //
105110
.orElse(Double.POSITIVE_INFINITY);
106111
}
107112

108113
/** Gets whether this command should appear in menus. */
114+
@Override
109115
public boolean isVisible() {
110116
return !"false".equals(menu.get("visible"));
111117
}

0 commit comments

Comments
 (0)