Skip to content

Commit

Permalink
Allow the escape key to close file choosers
Browse files Browse the repository at this point in the history
This adds an event handler to the file chooser that closes the chooser
when the user presses the escape key. The chooser acts as if the user
didn't select a file.

Fix: #14
  • Loading branch information
io7m committed Apr 10, 2021
1 parent e587ea6 commit 5587639
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
23 changes: 16 additions & 7 deletions README-CHANGES.xml
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<c:changelog project="com.io7m.jwheatsheaf" xmlns:c="urn:com.io7m.changelog:4.0">
<c:releases>
<c:release date="2020-03-28T00:00:00+00:00" ticket-system="com.github.io7m.jwheatsheaf" version="1.0.0">
<c:release date="2020-03-28T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.jwheatsheaf" version="1.0.0">
<c:changes>
<c:change date="2020-03-28T00:00:00+00:00" summary="Initial public release"/>
</c:changes>
</c:release>
<c:release date="2020-03-28T00:00:00+00:00" ticket-system="com.github.io7m.jwheatsheaf" version="1.0.1">
<c:release date="2020-03-28T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.jwheatsheaf" version="1.0.1">
<c:changes>
<c:change date="2020-03-28T00:00:00+00:00" summary="Add missing package exports to module descriptors"/>
</c:changes>
</c:release>
<c:release date="2020-03-28T00:00:00+00:00" ticket-system="com.github.io7m.jwheatsheaf" version="1.0.2">
<c:release date="2020-03-28T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.jwheatsheaf" version="1.0.2">
<c:changes>
<c:change date="2020-03-28T00:00:00+00:00" summary="Ensure CSS stylesheets are passed to any dialogs opened by the chooser">
<c:tickets>
Expand All @@ -20,7 +20,7 @@
</c:change>
</c:changes>
</c:release>
<c:release date="2020-03-29T00:00:00+00:00" ticket-system="com.github.io7m.jwheatsheaf" version="1.0.3">
<c:release date="2020-03-29T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.jwheatsheaf" version="1.0.3">
<c:changes>
<c:change date="2020-03-29T00:00:00+00:00" summary="Show the full paths of files/directories in tooltips">
<c:tickets>
Expand All @@ -29,14 +29,23 @@
</c:change>
</c:changes>
</c:release>
<c:release date="2020-04-04T00:00:00+00:00" ticket-system="com.github.io7m.jwheatsheaf" version="2.0.0">
<c:release date="2020-04-04T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.jwheatsheaf" version="2.0.0">
<c:changes>
<c:change compatible="false" date="2020-04-04T00:00:00+00:00" summary="Move implementation classes in the ui module into an internal non-exported package"/>
</c:changes>
</c:release>
<c:release date="2020-04-10T14:35:31+00:00" ticket-system="com.github.io7m.jwheatsheaf" version="2.0.1">
<c:release date="2020-04-10T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.jwheatsheaf" version="2.0.1">
<c:changes>
<c:change date="2020-04-10T14:35:31+00:00" summary="Enable CheckStyle to enforce code style"/>
<c:change date="2020-04-10T00:00:00+00:00" summary="Enable CheckStyle to enforce code style"/>
</c:changes>
</c:release>
<c:release date="2021-04-10T20:20:02+00:00" is-open="true" ticket-system="com.github.io7m.jwheatsheaf" version="3.0.0">
<c:changes>
<c:change date="2021-04-10T20:20:02+00:00" summary="Allow the escape key to close file choosers">
<c:tickets>
<c:ticket id="14"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
Expand Down
Expand Up @@ -469,4 +469,31 @@ public void testDirectoryUp(
);
Assertions.assertEquals(0, this.events.size());
}

/**
* Pressing escape closes the dialog without selecting a file.
*
* @param robot The FX test robot
*/

@Test
public void testEscapeCloses(
final FxRobot robot,
final TestInfo info)
{
JWFileWindowTitles.setTitle(this.chooser, info);

final var rootItem =
robot.lookup(".fileChooserSourceList")
.query();

robot.clickOn(rootItem);
robot.type(KeyCode.ESCAPE);

Assertions.assertEquals(
List.of(),
this.chooser.result()
);
Assertions.assertEquals(0, this.events.size());
}
}
Expand Up @@ -26,6 +26,8 @@
import com.io7m.jwheatsheaf.ui.internal.JWStrings;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
Expand Down Expand Up @@ -186,6 +188,16 @@ public JWFileChooserType create(
break;
}

/*
* Close the dialog when escape is pressed.
*/

dialog.addEventHandler(KeyEvent.KEY_RELEASED, (KeyEvent event) -> {
if (KeyCode.ESCAPE == event.getCode()) {
dialog.close();
}
});

return new JWFileChooser(dialog, viewController);
} catch (final IOException e) {
throw new UncheckedIOException(e);
Expand Down

0 comments on commit 5587639

Please sign in to comment.