Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Separated typed keys from pressed keys.

Authordjarvis <email>
Date2016-10-26 23:52:50 GMT-0700
Commit4d4b79bf3eca661379d56946ea971954fd3cfc15
Parent80d19c9
src/main/java/com/scrivendor/FileEditor.java
import javafx.scene.control.Tab;
import javafx.scene.control.Tooltip;
+import javafx.scene.input.InputEvent;
import javafx.scene.text.Text;
import org.fxmisc.undo.UndoManager;
import org.fxmisc.wellbehaved.event.EventPattern;
+import org.fxmisc.wellbehaved.event.InputMap;
/**
final Consumer<? super U> consumer ) {
getEditorPane().addEventListener( event, consumer );
+ }
+
+ public void addFallbackEventListener( final InputMap<InputEvent> map ) {
+ getEditorPane().addEventListener( map );
+ }
+
+ public void removeEventListener( final InputMap<InputEvent> map ) {
+ getEditorPane().removeEventListener( map );
}
src/main/java/com/scrivendor/FileEditorPane.java
import javafx.scene.control.TabPane;
import javafx.scene.control.TabPane.TabClosingPolicy;
+import javafx.scene.input.InputEvent;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import org.fxmisc.richtext.StyleClassedTextArea;
import org.fxmisc.richtext.StyledTextArea;
import org.fxmisc.richtext.model.TextEditingArea;
import org.fxmisc.wellbehaved.event.EventPattern;
+import org.fxmisc.wellbehaved.event.InputMap;
/**
final Consumer<? super U> consumer ) {
getActiveFileEditor().addEventListener( event, consumer );
+ }
+
+ /**
+ * Delegates to the active file editor pane, and, ultimately, to its text
+ * area.
+ *
+ * @param map The map of methods to events.
+ */
+ public void addFallbackEventListener( final InputMap<InputEvent> map ) {
+ getActiveFileEditor().addFallbackEventListener( map );
+ }
+
+ public void removeEventListener( final InputMap<InputEvent> map ) {
+ getActiveFileEditor().removeEventListener( map );
}
src/main/java/com/scrivendor/MainWindow.java
import javafx.beans.value.ObservableBooleanValue;
import javafx.event.Event;
-import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
+import javafx.scene.input.InputEvent;
import static javafx.scene.input.KeyCode.DIGIT2;
import static javafx.scene.input.KeyCode.ESCAPE;
import static org.fxmisc.wellbehaved.event.EventPattern.keyPressed;
import static org.fxmisc.wellbehaved.event.EventPattern.keyTyped;
+import org.fxmisc.wellbehaved.event.InputMap;
+import static org.fxmisc.wellbehaved.event.InputMap.consume;
+import static org.fxmisc.wellbehaved.event.InputMap.sequence;
/**
final Consumer<? super U> consumer ) {
getFileEditorPane().addEventListener( event, consumer );
+ }
+
+ /**
+ * Delegates to the file editor pane, and, ultimately, to its text area.
+ *
+ * @param map The map of methods to events.
+ */
+ private void addFallbackEventListener( final InputMap<InputEvent> map ) {
+ getFileEditorPane().addFallbackEventListener( map );
+ }
+
+ private void removeEventListener( final InputMap<InputEvent> map ) {
+ getFileEditorPane().removeEventListener( map );
}
/**
* The @ symbol is a short-cut to inserting a YAML variable reference.
*
* @param e
*/
private void atPressed( KeyEvent e ) {
- addEventListener( keyTyped(), this::variableKeyPressed );
- addEventListener( keyPressed(), this::variableKeyPressed );
+ startVariableMode();
}
/**
- * Receives key types and presses until the user completes the variable
- * selection.
- *
- * @param e The key that was pressed or typed.
+ * Receives typed keys until the user completes the variable selection.
+ *
+ * @param e The key that was typed.
*/
- private void variableKeyPressed( KeyEvent e ) {
+ private void variableKeyTyped( KeyEvent e ) {
System.out.println( "KEY: " + e.toString() );
+
+ if( e.getCode() == ESCAPE ) {
+ System.out.println( "STOP" );
+ stopVariableMode();
+ }
+
+ e.consume();
+ }
+
+ /**
+ * Receives key presses until the user completes the variable selection.
+ * This allows the arrow keys to be used for selecting variables.
+ *
+ * @param e The key that was pressed.
+ */
+ private void variableKeyPressed( KeyEvent e ) {
+ e.consume();
+ }
+
+ private InputMap<InputEvent> getKeyboardMap() {
+ return sequence(
+ consume( keyTyped(), this::variableKeyTyped ),
+ consume( keyPressed(), this::variableKeyPressed )
+ );
+ }
+
+ private void startVariableMode() {
+ addFallbackEventListener( getKeyboardMap() );
+ }
+
+ private void stopVariableMode() {
+ removeEventListener( getKeyboardMap() );
}
src/main/java/com/scrivendor/editor/MarkdownEditorPane.java
import javafx.scene.Node;
import javafx.scene.control.IndexRange;
+import javafx.scene.input.InputEvent;
import static javafx.scene.input.KeyCode.ENTER;
import javafx.scene.input.KeyEvent;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.StyleClassedTextArea;
import org.fxmisc.richtext.model.NavigationActions;
import org.fxmisc.undo.UndoManager;
import org.fxmisc.wellbehaved.event.EventPattern;
import static org.fxmisc.wellbehaved.event.EventPattern.keyPressed;
+import org.fxmisc.wellbehaved.event.InputMap;
import static org.fxmisc.wellbehaved.event.InputMap.consume;
import org.fxmisc.wellbehaved.event.Nodes;
private static final Pattern AUTO_INDENT_PATTERN = Pattern.compile(
"(\\s*[*+-]\\s+|\\s*[0-9]+\\.\\s+|\\s+)(.*)" );
+
+ /**
+ * Set when entering variable edit mode; retrieved upon exiting.
+ */
+ private InputMap<InputEvent> nodeMap;
private PegDownProcessor pegDownProcessor;
textArea.getStylesheets().add( STYLESHEET_EDITOR );
- addEventListener( keyPressed( ENTER ), this::enterPressed );
+ MarkdownEditorPane.this.addEventListener( keyPressed( ENTER ), this::enterPressed );
// addEventListener( keyPressed( X, SHORTCUT_DOWN ), this::cutLine );
final Consumer<? super U> consumer ) {
Nodes.addInputMap( getEditor(), consume( event, consumer ) );
+ }
+
+ /**
+ * This method adds listeners to editor events that can be removed without
+ * affecting the original listeners (i.e., the original lister is restored on
+ * a call to removeEventListener).
+ *
+ * @param map The map of methods to events.
+ */
+ public void addEventListener( final InputMap<InputEvent> map ) {
+ this.nodeMap = (InputMap<InputEvent>)getInputMap();
+ Nodes.addInputMap( getEditor(), map );
+ }
+
+ /**
+ * Returns the value for "org.fxmisc.wellbehaved.event.inputmap".
+ *
+ * @return An input map of input events.
+ */
+ private Object getInputMap() {
+ return getEditor().getProperties().get( getInputMapKey() );
}
+ private String getInputMapKey() {
+ return "org.fxmisc.wellbehaved.event.inputmap";
+ }
+
+ /**
+ * This method removes listeners to editor events and restores the default
+ * handler.
+ *
+ * @param map The map of methods to events.
+ */
+ public void removeEventListener( final InputMap<InputEvent> map ) {
+ Nodes.removeInputMap( getEditor(), map );
+ Nodes.addInputMap( getEditor(), this.nodeMap );
+ }
+
/**
* Add a listener to update the scrollY property.
/**
* Returns the scroll pane that contains the text area.
- *
- * @return
+ *
+ * @return
*/
public Node getNode() {
Delta126 lines added, 11 lines removed, 115-line increase