@@ -57,6 +57,9 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase {
57
57
58
58
private Runnable mShowSoftKeyboardRunnable ;
59
59
60
+ private boolean mShowSoftKeyboardIgnoreOnce ;
61
+ private boolean mShowSoftKeyboardWithDelayOnce ;
62
+
60
63
private static final String LOG_TAG = "TermuxTerminalViewClient" ;
61
64
62
65
public TermuxTerminalViewClient (TermuxActivity activity , TermuxTerminalSessionClient termuxTerminalSessionClient ) {
@@ -410,10 +413,19 @@ public void onToggleSoftKeyboardRequest() {
410
413
mActivity .getPreferences ().setSoftKeyboardEnabled (false );
411
414
KeyboardUtils .disableSoftKeyboard (mActivity , mActivity .getTerminalView ());
412
415
} else {
416
+ // Show with a delay, otherwise pressing keyboard toggle won't show the keyboard after
417
+ // switching back from another app if keyboard was previously disabled by user.
418
+ // Also request focus, since it wouldn't have been requested at startup by
419
+ // setSoftKeyboardState if keyboard was disabled. #2112
413
420
Logger .logVerbose (LOG_TAG , "Enabling soft keyboard on toggle" );
414
421
mActivity .getPreferences ().setSoftKeyboardEnabled (true );
415
422
KeyboardUtils .clearDisableSoftKeyboardFlags (mActivity );
416
- KeyboardUtils .showSoftKeyboard (mActivity , mActivity .getTerminalView ());
423
+ if (mShowSoftKeyboardWithDelayOnce ) {
424
+ mShowSoftKeyboardWithDelayOnce = false ;
425
+ mActivity .getTerminalView ().postDelayed (getShowSoftKeyboardRunnable (), 500 );
426
+ mActivity .getTerminalView ().requestFocus ();
427
+ } else
428
+ KeyboardUtils .showSoftKeyboard (mActivity , mActivity .getTerminalView ());
417
429
}
418
430
}
419
431
// If soft keyboard toggle behaviour is show/hide
@@ -431,12 +443,20 @@ public void onToggleSoftKeyboardRequest() {
431
443
}
432
444
433
445
public void setSoftKeyboardState (boolean isStartup , boolean isReloadTermuxProperties ) {
446
+ boolean noRequestFocus = false ;
447
+
434
448
// If soft keyboard is disabled by user for Termux (check function docs for Termux behaviour info)
435
449
if (KeyboardUtils .shouldSoftKeyboardBeDisabled (mActivity ,
436
- mActivity .getPreferences ().isSoftKeyboardEnabled (),
437
- mActivity .getPreferences ().isSoftKeyboardEnabledOnlyIfNoHardware ())) {
450
+ mActivity .getPreferences ().isSoftKeyboardEnabled (),
451
+ mActivity .getPreferences ().isSoftKeyboardEnabledOnlyIfNoHardware ())) {
438
452
Logger .logVerbose (LOG_TAG , "Maintaining disabled soft keyboard" );
439
453
KeyboardUtils .disableSoftKeyboard (mActivity , mActivity .getTerminalView ());
454
+ noRequestFocus = true ;
455
+ // Delay is only required if onCreate() is called like when Termux app is exited with
456
+ // double back press, not when Termux app is switched back from another app and keyboard
457
+ // toggle is pressed to enable keyboard
458
+ if (isStartup && mActivity .isOnResumeAfterOnCreate ())
459
+ mShowSoftKeyboardWithDelayOnce = true ;
440
460
} else {
441
461
// Set flag to automatically push up TerminalView when keyboard is opened instead of showing over it
442
462
KeyboardUtils .setResizeTerminalViewForSoftKeyboardFlags (mActivity );
@@ -450,34 +470,55 @@ public void setSoftKeyboardState(boolean isStartup, boolean isReloadTermuxProper
450
470
KeyboardUtils .hideSoftKeyboard (mActivity , mActivity .getTerminalView ());
451
471
// Required to keep keyboard hidden when Termux app is switched back from another app
452
472
KeyboardUtils .setSoftKeyboardAlwaysHiddenFlags (mActivity );
453
- } else {
454
- // Do not force show soft keyboard if termux-reload-settings command was run with hardware keyboard
455
- if (isReloadTermuxProperties )
456
- return ;
457
-
458
- if (mShowSoftKeyboardRunnable == null ) {
459
- mShowSoftKeyboardRunnable = () -> {
460
- Logger .logVerbose (LOG_TAG , "Showing soft keyboard on focus change" );
461
- KeyboardUtils .showSoftKeyboard (mActivity , mActivity .getTerminalView ());
462
- };
463
- }
473
+ noRequestFocus = true ;
474
+ // Required to keep keyboard hidden on app startup
475
+ mShowSoftKeyboardIgnoreOnce = true ;
476
+ }
477
+ }
464
478
465
- mActivity .getTerminalView ().setOnFocusChangeListener (new View .OnFocusChangeListener () {
466
- @ Override
467
- public void onFocusChange (View view , boolean hasFocus ) {
468
- // Force show soft keyboard if TerminalView or toolbar text input view has
469
- // focus and close it if they don't
470
- boolean textInputViewHasFocus = false ;
471
- final EditText textInputView = mActivity .findViewById (R .id .terminal_toolbar_text_input );
472
- if (textInputView != null ) textInputViewHasFocus = textInputView .hasFocus ();
473
- KeyboardUtils .setSoftKeyboardVisibility (mShowSoftKeyboardRunnable , mActivity , mActivity .getTerminalView (), hasFocus || textInputViewHasFocus );
479
+ mActivity .getTerminalView ().setOnFocusChangeListener (new View .OnFocusChangeListener () {
480
+ @ Override
481
+ public void onFocusChange (View view , boolean hasFocus ) {
482
+ // Force show soft keyboard if TerminalView or toolbar text input view has
483
+ // focus and close it if they don't
484
+ boolean textInputViewHasFocus = false ;
485
+ final EditText textInputView = mActivity .findViewById (R .id .terminal_toolbar_text_input );
486
+ if (textInputView != null ) textInputViewHasFocus = textInputView .hasFocus ();
487
+
488
+ if (hasFocus || textInputViewHasFocus ) {
489
+ if (mShowSoftKeyboardIgnoreOnce ) {
490
+ mShowSoftKeyboardIgnoreOnce = false ; return ;
474
491
}
475
- });
492
+ Logger .logVerbose (LOG_TAG , "Showing soft keyboard on focus change" );
493
+ } else {
494
+ Logger .logVerbose (LOG_TAG , "Hiding soft keyboard on focus change" );
495
+ }
476
496
477
- // Request focus for TerminalView
478
- mActivity .getTerminalView ().requestFocus ();
497
+ KeyboardUtils .setSoftKeyboardVisibility (getShowSoftKeyboardRunnable (), mActivity , mActivity .getTerminalView (), hasFocus || textInputViewHasFocus );
479
498
}
499
+ });
500
+
501
+ // Do not force show soft keyboard if termux-reload-settings command was run with hardware keyboard
502
+ // or soft keyboard is to be hidden or is disabled
503
+ if (!isReloadTermuxProperties && !noRequestFocus ) {
504
+ // Request focus for TerminalView
505
+ // Also show the keyboard, since onFocusChange will not be called if TerminalView already
506
+ // had focus on startup to show the keyboard, like when opening url with context menu
507
+ // "Select URL" long press and returning to Termux app with back button. This
508
+ // will also show keyboard even if it was closed before opening url. #2111
509
+ Logger .logVerbose (LOG_TAG , "Requesting TerminalView focus and showing soft keyboard" );
510
+ mActivity .getTerminalView ().requestFocus ();
511
+ mActivity .getTerminalView ().postDelayed (getShowSoftKeyboardRunnable (), 300 );
512
+ }
513
+ }
514
+
515
+ private Runnable getShowSoftKeyboardRunnable () {
516
+ if (mShowSoftKeyboardRunnable == null ) {
517
+ mShowSoftKeyboardRunnable = () -> {
518
+ KeyboardUtils .showSoftKeyboard (mActivity , mActivity .getTerminalView ());
519
+ };
480
520
}
521
+ return mShowSoftKeyboardRunnable ;
481
522
}
482
523
483
524
0 commit comments