Dave Jarvis' Repositories

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

Fix focus behaviour upon opening new file

AuthorDaveJarvis <email>
Date2020-07-11 14:40:00 GMT-0700
Commitfb30595f03cc2bf1a3a9969b847d96b9240af780
Parent7b0c71b
Delta23 lines added, 1 line removed, 22-line increase
src/main/java/com/scrivenvar/editors/EditorPane.java
@Override
public void requestFocus() {
- runLater( () -> getEditor().requestFocus() );
+ requestFocus( 3 );
+ }
+
+ /**
+ * There's a race-condition between displaying the {@link EditorPane}
+ * and giving the {@link #mEditor} focus. Try to focus up to {@code max}
+ * times before giving up.
+ *
+ * @param max The number of attempts to try to request focus.
+ */
+ private void requestFocus( final int max ) {
+ if( max > 0 ) {
+ runLater(
+ () -> {
+ final var editor = getEditor();
+
+ if( !editor.isFocused() ) {
+ editor.requestFocus();
+ requestFocus( max - 1 );
+ }
+ }
+ );
+ }
}