diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index d1e4a640ce..de2d7973a8 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -944,14 +944,10 @@ TextEditor::TextEditor (const String& name, juce_wchar passwordChar) setWantsKeyboardFocus (true); recreateCaret(); - - juce::Desktop::getInstance().addGlobalMouseListener (this); } TextEditor::~TextEditor() { - juce::Desktop::getInstance().removeGlobalMouseListener (this); - textValue.removeListener (textHolder); textValue.referTo (Value()); @@ -1046,7 +1042,7 @@ bool TextEditor::isReadOnly() const noexcept bool TextEditor::isTextInputActive() const { - return ! isReadOnly() && (! clicksOutsideDismissVirtualKeyboard || mouseDownInEditor); + return ! isReadOnly() && (! clicksOutsideDismissVirtualKeyboard || globalMouseListener.lastMouseDownInEditor()); } void TextEditor::setReturnKeyStartsNewLine (bool shouldStartNewLine) @@ -1851,11 +1847,6 @@ void TextEditor::performPopupMenuAction (const int menuItemID) //============================================================================== void TextEditor::mouseDown (const MouseEvent& e) { - mouseDownInEditor = e.originalComponent == this; - - if (! mouseDownInEditor) - return; - beginDragAutoRepeat (100); newTransaction(); @@ -1893,9 +1884,6 @@ void TextEditor::mouseDown (const MouseEvent& e) void TextEditor::mouseDrag (const MouseEvent& e) { - if (! mouseDownInEditor) - return; - if (wasFocused || ! selectAllTextWhenFocused) if (! (popupMenuEnabled && e.mods.isPopupMenu())) moveCaretTo (getTextIndexAt (e.getPosition()), true); @@ -1903,9 +1891,6 @@ void TextEditor::mouseDrag (const MouseEvent& e) void TextEditor::mouseUp (const MouseEvent& e) { - if (! mouseDownInEditor) - return; - newTransaction(); textHolder->restartTimer(); @@ -1918,9 +1903,6 @@ void TextEditor::mouseUp (const MouseEvent& e) void TextEditor::mouseDoubleClick (const MouseEvent& e) { - if (! mouseDownInEditor) - return; - int tokenEnd = getTextIndexAt (e.getPosition()); int tokenStart = 0; @@ -1987,9 +1969,6 @@ void TextEditor::mouseDoubleClick (const MouseEvent& e) void TextEditor::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel) { - if (! mouseDownInEditor) - return; - if (! viewport->useMouseWheelMoveIfNeeded (e, wheel)) Component::mouseWheelMove (e, wheel); } diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.h b/modules/juce_gui_basics/widgets/juce_TextEditor.h index d93e6af1c7..c48425d48b 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.h +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.h @@ -771,10 +771,26 @@ private: struct RemoveAction; class EditorAccessibilityHandler; + class GlobalMouseListener : private MouseListener + { + public: + explicit GlobalMouseListener (Component& e) : editor (e) { Desktop::getInstance().addGlobalMouseListener (this); } + ~GlobalMouseListener() override { Desktop::getInstance().removeGlobalMouseListener (this); } + + bool lastMouseDownInEditor() const { return mouseDownInEditor; } + + private: + void mouseDown (const MouseEvent& event) override { mouseDownInEditor = event.originalComponent == &editor; } + + Component& editor; + bool mouseDownInEditor = false; + }; + std::unique_ptr viewport; TextHolderComponent* textHolder; BorderSize borderSize { 1, 1, 1, 3 }; Justification justification { Justification::topLeft }; + const GlobalMouseListener globalMouseListener { *this }; bool readOnly = false; bool caretVisible = true; @@ -791,7 +807,6 @@ private: bool valueTextNeedsUpdating = false; bool consumeEscAndReturnKeys = true; bool underlineWhitespace = true; - bool mouseDownInEditor = false; bool clicksOutsideDismissVirtualKeyboard = false; UndoManager undoManager;