Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git
M src/main/java/com/scrivenvar/FileEditorTab.java
3030
import com.scrivenvar.service.events.Notification;
3131
import com.scrivenvar.service.events.Notifier;
32
import javafx.application.Platform;
3332
import javafx.beans.binding.Bindings;
3433
import javafx.beans.property.BooleanProperty;
...
5958
import static java.nio.charset.StandardCharsets.UTF_8;
6059
import static java.util.Locale.ENGLISH;
60
import static javafx.application.Platform.runLater;
6161
6262
/**
...
8989
    setOnSelectionChanged( e -> {
9090
      if( isSelected() ) {
91
        Platform.runLater( this::activated );
91
        runLater( this::activated );
92
        requestFocus();
9293
      }
9394
    } );
...
137138
    }
138139
139
    // Switch to the tab without loading if the contents are already in memory.
140
    if( getContent() != null ) {
141
      getEditorPane().requestFocus();
142
      return;
140
    // If the tab is devoid of content, load it.
141
    if( getContent() == null ) {
142
      readFile();
143
      initLayout();
144
      initUndoManager();
143145
    }
144
145
    // Load the text and update the preview before the undo manager.
146
    load();
147
148
    // Track undo requests -- can only be called *after* load.
149
    initUndoManager();
150
    initLayout();
151
    initFocus();
152146
  }
153147
154148
  private void initLayout() {
155149
    setContent( getScrollPane() );
156
  }
157
158
  private void initFocus() {
159
    getEditorPane().requestFocus();
160150
  }
161151
152
  /**
153
   * Tracks undo requests, but can only be called <em>after</em> load.
154
   */
162155
  private void initUndoManager() {
163156
    final UndoManager<?> undoManager = getUndoManager();
164157
    undoManager.forgetHistory();
165158
166159
    // Bind the editor undo manager to the properties.
167160
    mModified.bind( Bindings.not( undoManager.atMarkedPositionProperty() ) );
168161
    canUndo.bind( undoManager.undoAvailableProperty() );
169162
    canRedo.bind( undoManager.redoAvailableProperty() );
163
  }
164
165
  private void requestFocus() {
166
    getEditorPane().requestFocus();
170167
  }
171168
...
242239
   * Reads the entire file contents from the path associated with this tab.
243240
   */
244
  private void load() {
241
  private void readFile() {
245242
    final Path path = getPath();
246243
    final File file = path.toFile();
M src/main/java/com/scrivenvar/FileEditorTabPane.java
229229
230230
  /**
231
   * Called to add a new {@link FileEditorTab} to the tab pane. The return
232
   * value is used when initializing the application, but isn't required.
233
   *
234
   * @return The new {@link FileEditorTab} instance created.
231
   * Called to add a new {@link FileEditorTab} to the tab pane.
235232
   */
236
  FileEditorTab newEditor() {
233
  void newEditor() {
237234
    final FileEditorTab tab = createFileEditor( getDefaultPath() );
238235
239236
    getTabs().add( tab );
240237
    getSelectionModel().select( tab );
241
    return tab;
242238
  }
243239
M src/main/java/com/scrivenvar/editors/EditorPane.java
6363
  @Override
6464
  public void requestFocus() {
65
    runLater( () -> getEditor().requestFocus() );
65
    requestFocus( 3 );
66
  }
67
68
  /**
69
   * There's a race-condition between displaying the {@link EditorPane}
70
   * and giving the {@link #mEditor} focus. Try to focus up to {@code max}
71
   * times before giving up.
72
   *
73
   * @param max The number of attempts to try to request focus.
74
   */
75
  private void requestFocus( final int max ) {
76
    if( max > 0 ) {
77
      runLater(
78
          () -> {
79
            final var editor = getEditor();
80
81
            if( !editor.isFocused() ) {
82
              editor.requestFocus();
83
              requestFocus( max - 1 );
84
            }
85
          }
86
      );
87
    }
6688
  }
6789