Update logic for dealing with file dialogs.
This in preparation for an upcoming change that uses native file dialogs instead of our very basic ImGui file dialog. PiperOrigin-RevId: 861671041 Change-Id: I9b00c05efdda5e37ffe15048a2ccf66f6eca89d9
This commit is contained in:
committed by
Copybara-Service
parent
ad482a57bb
commit
75715ab966
@@ -37,6 +37,8 @@ target_compile_definitions(${MUJOCO_PLATFORM_TARGET_NAME}
|
||||
|
||||
target_sources(${MUJOCO_PLATFORM_TARGET_NAME}
|
||||
PUBLIC
|
||||
file_dialog.cc
|
||||
file_dialog.h
|
||||
gui.cc
|
||||
gui.h
|
||||
helpers.cc
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
// Copyright 2026 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "experimental/platform/file_dialog.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
namespace mujoco::platform {
|
||||
|
||||
static DialogResult ImGui_FileDialog(std::string_view path) {
|
||||
static constexpr int kBufferSize = 1024;
|
||||
char buf[kBufferSize];
|
||||
std::strncpy(buf, path.data(), sizeof(buf));
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
|
||||
DialogResult::Status status = DialogResult::kPending;
|
||||
|
||||
ImGui::Text("Filename");
|
||||
ImGui::SameLine();
|
||||
ImGui::SetNextItemWidth(600);
|
||||
if (ImGui::InputText("##Filename", buf, kBufferSize, ImGuiInputTextFlags_EnterReturnsTrue)) {
|
||||
status = DialogResult::kAccepted;
|
||||
}
|
||||
if (ImGui::Button("OK", ImVec2(120, 0)) || ImGui::IsKeyChordPressed(ImGuiKey_Enter)) {
|
||||
status = DialogResult::kAccepted;
|
||||
}
|
||||
ImGui::SetItemDefaultFocus();
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Cancel", ImVec2(120, 0)) || ImGui::IsKeyChordPressed(ImGuiKey_Escape)) {
|
||||
status = DialogResult::kCancelled;
|
||||
}
|
||||
|
||||
return DialogResult{.status = status, .path = std::string(buf)};
|
||||
}
|
||||
|
||||
DialogResult OpenFileDialog(std::string_view path,
|
||||
std::span<std::string_view> filters) {
|
||||
return ImGui_FileDialog(path);
|
||||
}
|
||||
|
||||
DialogResult SaveFileDialog(std::string_view path,
|
||||
std::span<std::string_view> filters) {
|
||||
return ImGui_FileDialog(path);
|
||||
}
|
||||
|
||||
DialogResult SelectPathDialog(std::string_view path) {
|
||||
return ImGui_FileDialog(path);
|
||||
}
|
||||
|
||||
} // namespace mujoco::platform
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright 2026 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef MUJOCO_SRC_EXPERIMENTAL_PLATFORM_FILE_DIALOG_H_
|
||||
#define MUJOCO_SRC_EXPERIMENTAL_PLATFORM_FILE_DIALOG_H_
|
||||
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace mujoco::platform {
|
||||
|
||||
// Result returned by various file dialog functions.
|
||||
struct DialogResult {
|
||||
// The status of the dialog.
|
||||
enum Status {
|
||||
// The dialog is still open.
|
||||
kPending,
|
||||
// A file/folder was selected.
|
||||
kAccepted,
|
||||
// The dialog was dismissed.
|
||||
kCancelled,
|
||||
// An error occurred.
|
||||
kError,
|
||||
};
|
||||
|
||||
// The status of the dialog.
|
||||
Status status = kPending;
|
||||
|
||||
// The path selected by the dialog if Status is kAccepted. If status is
|
||||
// kError, the path may contain an error message. Empty otherwise.
|
||||
std::string path;
|
||||
};
|
||||
|
||||
// Opens a file dialog used for selecting a file to open.
|
||||
DialogResult OpenFileDialog(std::string_view path,
|
||||
std::span<std::string_view> filters = {});
|
||||
|
||||
// Opens a file dialog used for selecting a file to save.
|
||||
DialogResult SaveFileDialog(std::string_view path,
|
||||
std::span<std::string_view> filters = {});
|
||||
|
||||
// Opens a file dialog used for selecting a folder.
|
||||
DialogResult SelectPathDialog(std::string_view path);
|
||||
|
||||
} // namespace mujoco::platform
|
||||
|
||||
#endif // MUJOCO_SRC_EXPERIMENTAL_PLATFORM_FILE_DIALOG_H_
|
||||
@@ -66,27 +66,6 @@ bool ImGui_Slider(const char* name, mjtNum* value, mjtNum min, mjtNum max) {
|
||||
return res;
|
||||
}
|
||||
|
||||
bool ImGui_FileDialog(char* buf, int len) {
|
||||
bool ok = false;
|
||||
ImGui::Text("Filename");
|
||||
ImGui::SameLine();
|
||||
ImGui::SetNextItemWidth(600);
|
||||
if (ImGui::InputText("##Filename", buf, len, ImGuiInputTextFlags_EnterReturnsTrue)) {
|
||||
ok = true;
|
||||
ImGui::CloseCurrentPopup();
|
||||
};
|
||||
if (ImGui::Button("OK", ImVec2(120, 0)) || ImGui_IsChordJustPressed(ImGuiKey_Enter)) {
|
||||
ok = true;
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
ImGui::SetItemDefaultFocus();
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Cancel", ImVec2(120, 0)) || ImGui_IsChordJustPressed(ImGuiKey_Escape)) {
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
void MaybeSaveToClipboard(const std::string& contents) {
|
||||
if (ImGui::GetIO().SetClipboardTextFn) {
|
||||
ImGui::GetIO().SetClipboardTextFn(nullptr, contents.c_str());
|
||||
|
||||
@@ -131,9 +131,6 @@ struct ScopedStyle {
|
||||
int num_vars = 0;
|
||||
};
|
||||
|
||||
// ImGui file dialog.
|
||||
bool ImGui_FileDialog(char* buf, int len);
|
||||
|
||||
// ImGui Slider that supports both float and double types.
|
||||
bool ImGui_Slider(const char* name, mjtNum* value, mjtNum min, mjtNum max);
|
||||
|
||||
|
||||
@@ -164,6 +164,13 @@ void Window::SetTitle(std::string_view title) {
|
||||
SDL_SetWindowTitle(sdl_window_, title.data());
|
||||
}
|
||||
|
||||
void Window::DisableWindowResizing() {
|
||||
SDL_SetWindowResizable(sdl_window_, SDL_FALSE);
|
||||
}
|
||||
void Window::EnableWindowResizing() {
|
||||
SDL_SetWindowResizable(sdl_window_, SDL_TRUE);
|
||||
}
|
||||
|
||||
std::string Window::GetDropFile() {
|
||||
std::string tmp;
|
||||
std::swap(tmp, drop_file_);
|
||||
|
||||
@@ -82,6 +82,12 @@ class Window {
|
||||
// the value will be cleared until the next time a file is dropped.
|
||||
std::string GetDropFile();
|
||||
|
||||
// Enables window resizing.
|
||||
void EnableWindowResizing();
|
||||
|
||||
// Disables window resizing.
|
||||
void DisableWindowResizing();
|
||||
|
||||
// Returns the handle to the underlying native window.
|
||||
void* GetNativeWindowHandle() { return native_window_; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user