feat(io): SwingIO.choose renders the menu as buttons

This commit is contained in:
2026-06-01 15:27:15 +02:00
parent 361fd0939c
commit 98b1561ef2

View File

@@ -10,7 +10,11 @@ import thb.jeanluc.adventure.io.text.StyledText;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
@@ -342,6 +346,51 @@ public class SwingIO implements GameIO {
// The GUI map panel is always visible; nothing extra to do on the 'map' command. // The GUI map panel is always visible; nothing extra to do on the 'map' command.
} }
@Override
public int choose(String title, List<String> options) {
// Same EDT↔worker handoff as readLine: the worker blocks on take(),
// the EDT (button click / window close) offers the chosen index.
LinkedBlockingQueue<Integer> picked = new LinkedBlockingQueue<>();
SwingUtilities.invokeLater(() -> {
JDialog dialog = new JDialog(frame, title, true);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
panel.add(new JLabel(title));
panel.add(Box.createVerticalStrut(8));
for (int i = 0; i < options.size(); i++) {
int idx = i;
JButton b = new JButton(options.get(i));
b.setAlignmentX(0f);
b.addActionListener(e -> {
dialog.dispose();
picked.offer(idx);
});
panel.add(b);
panel.add(Box.createVerticalStrut(4));
}
// Closing the dialog counts as the last (safe/cancel) option.
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
dialog.dispose();
picked.offer(options.size() - 1);
}
});
dialog.setContentPane(panel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
});
try {
return picked.take();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return options.size() - 1;
}
}
private void addList(List<Span> spans, List<String> xs, Style st) { private void addList(List<Span> spans, List<String> xs, Style st) {
for (int i = 0; i < xs.size(); i++) { for (int i = 0; i < xs.size(); i++) {
if (i > 0) { if (i > 0) {