diff --git a/app.cpp b/app.cpp index 5e2dad8..e81e17f 100644 --- a/app.cpp +++ b/app.cpp @@ -22,6 +22,7 @@ void DrawMenu(); void DrawToolbar(ImVec2 buttonSize); +void DrawDroppedFilesPrompt(); #define DEFINE_APP_THEME_NAMES #include "app.h" @@ -37,6 +38,9 @@ AppTheme appTheme; ImFont* gFont = nullptr; +// Variable to store dropped file to load +std::string prompt_dropped_file = ""; + // Log a message to the terminal void Log(const char* format, ...) { va_list args; @@ -315,6 +319,43 @@ void MainInit(int argc, char** argv, int initial_width, int initial_height) { void MainCleanup() { } +// Validate that a file has the .otio extension +bool is_valid_file(const std::string& filepath) { + size_t last_dot = filepath.find_last_of('.'); + + // If no dot is found, it's not a valid file + if (last_dot == std::string::npos) { + return false; + } + + // Get and check the extension + std::string extension = filepath.substr(last_dot + 1); + return extension == "otio"; +} + +// Accept and open a file path +void FileDropCallback(int count, const char** filepaths) { + if (count > 1){ + Message("Cannot open multiple files."); + return; + } + + else if (count == 0) { + return; + } + + std::string file_path = filepaths[0]; + + if (!is_valid_file(file_path)){ + Message("Invalid file: %s", file_path.c_str()); + return; + } + + // Loading is done in DrawDroppedFilesPrompt() + prompt_dropped_file = file_path; + +} + // Make a button using the fancy icon font bool IconButton(const char* label, const ImVec2 size = ImVec2(0, 0)) { bool result = ImGui::Button(label, size); @@ -383,6 +424,7 @@ void MainGui() { exit(0); } + DrawDroppedFilesPrompt(); DrawMenu(); // ImGui::SameLine(ImGui::GetContentRegionAvailWidth() - button_size.x + @@ -785,6 +827,32 @@ void DrawToolbar(ImVec2 button_size) { #endif } +// Prompt the user to confirm file loading +void DrawDroppedFilesPrompt() { + if (prompt_dropped_file == "") { + return; + } + + ImGui::OpenPopup("Open File?"); + // Modal window for confirmation + if (ImGui::BeginPopupModal("Open File?", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::Text("Open file \n%s?", prompt_dropped_file.c_str()); + + if (ImGui::Button("Yes")) { + LoadFile(prompt_dropped_file); + prompt_dropped_file = ""; + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("No")) { + Message(""); // Reset last message + prompt_dropped_file = ""; + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } +} + void SelectObject( otio::SerializableObject* object, otio::SerializableObject* context) { diff --git a/main.h b/main.h index 269598b..f05143f 100644 --- a/main.h +++ b/main.h @@ -2,4 +2,4 @@ void MainInit(int argc, char** argv, int initial_width, int initial_height); void MainGui(); void MainCleanup(); - +void FileDropCallback(int count, const char** paths); diff --git a/main_macos.mm b/main_macos.mm index 8af61c1..7536b9f 100644 --- a/main_macos.mm +++ b/main_macos.mm @@ -18,6 +18,11 @@ #import #import +// Accept and open a file path +void file_drop_callback(GLFWwindow* window, int count, const char** paths) { + FileDropCallback(count, paths); +} + static void glfw_error_callback(int error, const char* description) { fprintf(stderr, "Glfw Error %d: %s\n", error, description); @@ -100,6 +105,9 @@ int main(int argc, char** argv) // Our state float clear_color[4] = {0.45f, 0.55f, 0.60f, 1.00f}; + // Set the drop callback + glfwSetDropCallback(window, file_drop_callback); + // Main loop while (!glfwWindowShouldClose(window)) {