From c8b9bc79bac08a6731719877ddb908700144dfb1 Mon Sep 17 00:00:00 2001 From: Lukasz Kozakiewicz Date: Mon, 9 Apr 2018 18:35:37 +0200 Subject: [PATCH] AlertWindow: ensure a native keyboard is hidden when alert window gets dismissed. --- .../widgets/juce_TextEditor.cpp | 21 ++++++++++++++----- .../juce_gui_basics/widgets/juce_TextEditor.h | 1 + .../windows/juce_AlertWindow.cpp | 10 +++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index 409991dbb4..20fa433239 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -1240,6 +1240,16 @@ void TextEditor::removeListener (Listener* l) { listeners.remove (l); } //============================================================================== void TextEditor::timerCallbackInt() +{ + checkFocus(); + + auto now = Time::getApproximateMillisecondCounter(); + + if (now > lastTransactionTime + 200) + newTransaction(); +} + +void TextEditor::checkFocus() { if (hasKeyboardFocus (false) && ! isCurrentlyBlockedByAnotherModalComponent()) { @@ -1249,11 +1259,6 @@ void TextEditor::timerCallbackInt() if (! isReadOnly()) peer->textInputRequired (peer->globalToLocal (getScreenPosition()), *this); } - - auto now = Time::getApproximateMillisecondCounter(); - - if (now > lastTransactionTime + 200) - newTransaction(); } void TextEditor::repaintText (Range range) @@ -2066,6 +2071,12 @@ void TextEditor::focusGained (FocusChangeType) moveCaretTo (getTotalNumChars(), true); } + // When caret position changes, we check focus automatically, to + // show any native keyboard if needed. If the position does not + // change though, we need to check focus manually. + if (getTotalNumChars() == 0) + checkFocus(); + repaint(); updateCaretPosition(); } diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.h b/modules/juce_gui_basics/widgets/juce_TextEditor.h index 60f348fdcf..517f188439 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.h +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.h @@ -777,6 +777,7 @@ private: float getWordWrapWidth() const; float getJustificationWidth() const; void timerCallbackInt(); + void checkFocus(); void repaintText (Range); void scrollByLines (int deltaLines); bool undoOrRedo (bool shouldUndo); diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp index 3426cb6625..a4a94194e2 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp @@ -58,6 +58,16 @@ AlertWindow::AlertWindow (const String& title, AlertWindow::~AlertWindow() { + // Ensure that the focus does not jump to another TextEditor while we + // remove children. + for (auto* t : textBoxes) + t->setWantsKeyboardFocus (false); + + // Giveaway focus before removing the editors, so that any TextEditor + // with focus has a chance to dismiss native keyboard if shown. + if (hasKeyboardFocus (true)) + Component::unfocusAllComponents(); + removeAllChildren(); }