Skip to content

Commit

Permalink
Add drag & drop functionality for opening files (in macOS) (#69)
Browse files Browse the repository at this point in the history
Signed-off-by: Fernando Ortega <agetro06@gmail.com>
  • Loading branch information
agetroortega authored Sep 27, 2024
1 parent 72d57a7 commit d551b35
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
68 changes: 68 additions & 0 deletions app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

void DrawMenu();
void DrawToolbar(ImVec2 buttonSize);
void DrawDroppedFilesPrompt();

#define DEFINE_APP_THEME_NAMES
#include "app.h"
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -383,6 +424,7 @@ void MainGui() {
exit(0);
}

DrawDroppedFilesPrompt();
DrawMenu();

// ImGui::SameLine(ImGui::GetContentRegionAvailWidth() - button_size.x +
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
8 changes: 8 additions & 0 deletions main_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>

// 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);
Expand Down Expand Up @@ -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))
{
Expand Down

0 comments on commit d551b35

Please sign in to comment.