Skip to content

Commit

Permalink
UI: fixed file dialog input handling
Browse files Browse the repository at this point in the history
the issue was introduced in the context of  #517
  • Loading branch information
mgerhardy committed Jan 13, 2025
1 parent 1de103b commit a6fa616
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/modules/ui/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,9 @@ void FileDialog::onTextInput(void *windowHandle, const core::String &text) {
if (text.empty()) {
return;
}
int idx = 0;
if (!_acceptInput) {
return;
}

const core::TimeProviderPtr &timeProvider = _app->timeProvider();
if (!_scrollToText.isValid(timeProvider->tickNow())) {
Expand All @@ -770,6 +772,8 @@ void FileDialog::onTextInput(void *windowHandle, const core::String &text) {
} else {
_scrollToText.value().append(text);
}

int idx = 0;
for (const auto &entry : _filteredEntities) {
if (core::string::startsWith(entry->name, _scrollToText.value())) {
_selectedEntry = *entry;
Expand All @@ -782,18 +786,27 @@ void FileDialog::onTextInput(void *windowHandle, const core::String &text) {
}

bool FileDialog::showFileDialog(video::FileDialogOptions &options, core::String &entityPath, video::OpenFileMode type,
const io::FormatDescription **formatDesc) {
const io::FormatDescription **formatDesc, bool &showFileDialog) {
_acceptInput = false;
if (!showFileDialog) {
return false;
}
float width = core_min(100.0f * ImGui::GetFontSize(), ImGui::GetMainViewport()->Size.x * 0.95f);
const float itemHeight = (ImGui::GetFontSize() + ImGui::GetStyle().ItemSpacing.y);
ImGui::SetNextWindowSize(ImVec2(width, 0.0f));
const char *title = popupTitle(type);
if (!ImGui::IsPopupOpen(title)) {
ImGui::OpenPopup(title);
Log::error("Opened popup %s", title);
}

if (ImGui::BeginPopupModal(title)) {
_acceptInput = ImGui::GetTopMostPopupModal() == ImGui::GetCurrentWindow();
if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
showFileDialog = false;
return false;
}
currentPathPanel(type);
quickAccessPanel(type, _bookmarks->strVal(), 20 * itemHeight);
Expand Down
4 changes: 3 additions & 1 deletion src/modules/ui/FileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class FileDialog : public Panel {
// used when e.g. changing the directory
bool _needsSorting = false;
bool _scrollToSelection = false;
bool _acceptInput = false;
TimedString _scrollToText;

io::FilesystemEntry _newFolderName;
Expand Down Expand Up @@ -114,9 +115,10 @@ class FileDialog : public Panel {
* @param entityPath Output buffer for the full path of the selected entity
* @return @c true if user input was made - either an entity was selected, or the selection was cancelled.
* @return @c false if no user input was made yet and the dialog should still run
* @note If you want to close the dialog, set @c showFileDialog to @c false
*/
bool showFileDialog(video::FileDialogOptions &fileDialogOptions, core::String &entityPath, video::OpenFileMode type,
const io::FormatDescription **formatDesc = nullptr);
const io::FormatDescription **formatDesc, bool &showFileDialog);
};

}
2 changes: 1 addition & 1 deletion src/modules/ui/IMGUIApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ app::AppState IMGUIApp::onRunning() {

core::String buf;
const io::FormatDescription *formatDesc = nullptr;
if (_showFileDialog && _fileDialog.showFileDialog(_fileDialogOptions, buf, _fileDialogMode, &formatDesc)) {
if (_fileDialog.showFileDialog(_fileDialogOptions, buf, _fileDialogMode, &formatDesc, _showFileDialog)) {
if (buf[0] != '\0') {
_fileDialogCallback(buf, formatDesc);
}
Expand Down

0 comments on commit a6fa616

Please sign in to comment.