Dave Jarvis' Repositories

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

Fix editor focus behaviour

AuthorDaveJarvis <email>
Date2020-07-11 13:43:03 GMT-0700
Commit7b0c71bdf4bc89e5c96292dd08b3129db38b6d2a
Parentcecd18a
Delta18 lines added, 24 lines removed, 6-line decrease
src/main/java/com/scrivenvar/FileEditorTab.java
import com.scrivenvar.service.events.Notification;
import com.scrivenvar.service.events.Notifier;
-import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Locale.ENGLISH;
+import static javafx.application.Platform.runLater;
/**
setOnSelectionChanged( e -> {
if( isSelected() ) {
- Platform.runLater( this::activated );
+ runLater( this::activated );
}
} );
}
- // Switch to the tab without loading if the contents are already in memory.
- if( getContent() != null ) {
- getEditorPane().requestFocus();
- return;
+ // If the tab is devoid of content, load it.
+ if( getContent() == null ) {
+ readFile();
+ initLayout();
+ initUndoManager();
}
-
- // Load the text and update the preview before the undo manager.
- load();
- // Track undo requests -- can only be called *after* load.
- initUndoManager();
- initLayout();
- initFocus();
+ requestFocus();
}
private void initLayout() {
setContent( getScrollPane() );
- }
-
- private void initFocus() {
- getEditorPane().requestFocus();
}
+ /**
+ * Tracks undo requests, but can only be called <em>after</em> load.
+ */
private void initUndoManager() {
final UndoManager<?> undoManager = getUndoManager();
undoManager.forgetHistory();
// Bind the editor undo manager to the properties.
mModified.bind( Bindings.not( undoManager.atMarkedPositionProperty() ) );
canUndo.bind( undoManager.undoAvailableProperty() );
canRedo.bind( undoManager.redoAvailableProperty() );
+ }
+
+ private void requestFocus() {
+ runLater( () -> getEditorPane().requestFocus() );
}
* Reads the entire file contents from the path associated with this tab.
*/
- private void load() {
+ private void readFile() {
final Path path = getPath();
final File file = path.toFile();
src/main/java/com/scrivenvar/FileEditorTabPane.java
/**
- * Called to add a new {@link FileEditorTab} to the tab pane. The return
- * value is used when initializing the application, but isn't required.
- *
- * @return The new {@link FileEditorTab} instance created.
+ * Called to add a new {@link FileEditorTab} to the tab pane.
*/
- FileEditorTab newEditor() {
+ void newEditor() {
final FileEditorTab tab = createFileEditor( getDefaultPath() );
getTabs().add( tab );
getSelectionModel().select( tab );
- return tab;
}