Skip to content

Commit d6b4c95

Browse files
committed
Fixed focus conflicts between find window and autocomplete
1 parent f522120 commit d6b4c95

2 files changed

Lines changed: 42 additions & 23 deletions

File tree

TextEditor.cpp

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -691,16 +691,7 @@ void TextEditor::handleKeyboardInputs() {
691691
#endif
692692

693693
// ignore specific keys when autocomplete is active, they will be handled later
694-
if (autocomplete.isActive()) {
695-
for (auto key : {ImGuiKey_Escape, ImGuiKey_Tab, ImGuiKey_Enter, ImGuiKey_KeypadEnter, ImGuiKey_UpArrow, ImGuiKey_DownArrow}) {
696-
if (ImGui::IsKeyPressed(key)) {
697-
return;
698-
}
699-
}
700-
}
701-
702-
// ignore escape key when find/replace window is visible, it will be handled later
703-
if (findReplaceVisible && ImGui::IsKeyPressed(ImGuiKey_Escape)) {
694+
if (autocomplete.isActive() && autocomplete.isSpecialKeyPressed()) {
704695
return;
705696
}
706697

@@ -756,7 +747,15 @@ void TextEditor::handleKeyboardInputs() {
756747
else if (!readOnly && language && isShortcut && ImGui::IsKeyPressed(ImGuiKey_Slash)) { toggleComments(); }
757748

758749
// find/replace support
759-
else if (isShortcut && ImGui::IsKeyPressed(ImGuiKey_F)) { openFindReplace(); }
750+
else if (isShortcut && ImGui::IsKeyPressed(ImGuiKey_F)) {
751+
if (autocomplete.isActive()) {
752+
autocomplete.cancel();
753+
findCancelledAutocomplete = true;
754+
}
755+
756+
openFindReplace();
757+
}
758+
760759
else if (isShiftShortcut && ImGui::IsKeyPressed(ImGuiKey_F)) { findAll(); }
761760
else if (isShortcut && ImGui::IsKeyPressed(ImGuiKey_G)) { findNext(); }
762761

@@ -797,13 +796,21 @@ void TextEditor::handleKeyboardInputs() {
797796
}
798797

799798
// handle escape key
800-
else if (ImGui::IsKeyPressed(ImGuiKey_Escape) && cursors.hasMultiple()) {
801-
cursors.clearAdditional();
799+
else if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
800+
if (autocomplete.isActive()) {
801+
autocomplete.cancel();
802+
803+
} else if (findReplaceVisible) {
804+
closeFindReplace();
805+
806+
} else if (cursors.hasMultiple()) {
807+
cursors.clearAdditional();
808+
}
802809
}
803810

804811
// handle regular text
805812
if (!io.InputQueueCharacters.empty()) {
806-
// ignore Ctrl inputs, but need to allow Alt+Ctrl as some keyboards (e.g. German) use AltGR (which _is_ Alt+Ctrl) to input certain characters
813+
// ignore Ctrl inputs, but need to allow Alt+Ctrl as some keyboards (e.g. German) use AltGR (which is Alt+Ctrl) to input certain characters
807814
if (!(io.KeyCtrl && !io.KeyAlt) && !readOnly) {
808815
for (auto i = 0; i < io.InputQueueCharacters.size(); i++) {
809816
auto character = io.InputQueueCharacters[i];
@@ -4142,6 +4149,10 @@ void TextEditor::renderFindReplace(ImVec2 pos, float width) {
41424149
if (focusOnFind) {
41434150
ImGui::SetKeyboardFocusHere();
41444151
focusOnFind = false;
4152+
4153+
} else if (findCancelledAutocomplete) {
4154+
ImGui::SetKeyboardFocusHere();
4155+
findCancelledAutocomplete = false;
41454156
}
41464157

41474158
if (inputString("###find", &findText, ImGuiInputTextFlags_AutoSelectAll)) {
@@ -4197,10 +4208,6 @@ void TextEditor::renderFindReplace(ImVec2 pos, float width) {
41974208
closeFindReplace();
41984209
}
41994210

4200-
if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
4201-
closeFindReplace();
4202-
}
4203-
42044211
if (!readOnly) {
42054212
ImGui::SetNextItemWidth(fieldWidth);
42064213
inputString("###replace", &replaceText);
@@ -4666,11 +4673,6 @@ bool TextEditor::Autocomplete::render(Document& document, Cursors& cursors, cons
46664673
}
46674674
}
46684675

4669-
// close autocomplete when user hits escape key
4670-
if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
4671-
requestDeactivation = true;
4672-
}
4673-
46744676
// open popup window
46754677
bool result = false;
46764678
auto cursorScreenPos = ImGui::GetCursorScreenPos();
@@ -4780,6 +4782,21 @@ void TextEditor::Autocomplete::setSuggestions(const std::vector<std::string>& su
47804782
}
47814783

47824784

4785+
//
4786+
// TextEditor::Autocomplete::isSpecialKeyPressed
4787+
//
4788+
4789+
bool TextEditor::Autocomplete::isSpecialKeyPressed() const {
4790+
for (auto key : {ImGuiKey_Tab, ImGuiKey_Enter, ImGuiKey_KeypadEnter, ImGuiKey_UpArrow, ImGuiKey_DownArrow}) {
4791+
if (ImGui::IsKeyPressed(key)) {
4792+
return true;
4793+
}
4794+
}
4795+
4796+
return false;
4797+
}
4798+
4799+
47834800
//
47844801
// TextEditor::Autocomplete::start
47854802
//

TextEditor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ class TextEditor {
10971097

10981098
// get information
10991099
inline bool isActive() const { return active; }
1100+
bool isSpecialKeyPressed() const;
11001101
inline ImGuiKeyChord getTriggerShortcut() const { return configuration.triggerShortcut; }
11011102
inline Coordinate getStart() const { return startLocation; }
11021103
inline std::string getReplacement() { return currentSelection < state.suggestions.size() ? state.suggestions[currentSelection] : ""; }
@@ -1297,6 +1298,7 @@ class TextEditor {
12971298
bool findReplaceVisible = false;
12981299
bool focusOnEditor = true;
12991300
bool focusOnFind = false;
1301+
bool findCancelledAutocomplete = false;
13001302
std::string findText;
13011303
std::string replaceText;
13021304
bool caseSensitiveFind = false;

0 commit comments

Comments
 (0)