dialog: Fix save file chooser with xdg portal

This correctly sets the xdg portal fields for targeting a specific
new filename or existing file.

"current_name" sets the dialogs placeholder name.
"current_file" targets an existing file.
"current_folder" for when the target is a folder.
This commit is contained in:
Colin Kinloch
2025-05-25 15:55:48 +01:00
committed by Sam Lantinga
parent 1af7dfb0a7
commit c79a18d0fa
2 changed files with 92 additions and 9 deletions

View File

@@ -12,6 +12,7 @@
/* Sample program: Create open and save dialogs. */
#include <SDL3/SDL.h>
#include <SDL3/SDL_iostream.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
@@ -23,6 +24,8 @@ const SDL_DialogFileFilter filters[] = {
};
static void SDLCALL callback(void *userdata, const char * const *files, int filter) {
char **saved_path = userdata;
if (files) {
const char* filter_name = "(filter fetching unsupported)";
@@ -36,6 +39,13 @@ static void SDLCALL callback(void *userdata, const char * const *files, int filt
SDL_Log("Filter used: '%s'", filter_name);
if (*files && saved_path) {
*saved_path = SDL_strdup(*files);
/* Create the file */
SDL_IOStream *stream = SDL_IOFromFile(*saved_path, "w");
SDL_CloseIO(stream);
}
while (*files) {
SDL_Log("'%s'", *files);
files++;
@@ -45,6 +55,23 @@ static void SDLCALL callback(void *userdata, const char * const *files, int filt
}
}
char *concat_strings(const char *a, const char *b)
{
char *out = NULL;
if (a != NULL && b != NULL) {
const size_t out_size = SDL_strlen(a) + SDL_strlen(b) + 1;
out = (char *)SDL_malloc(out_size);
if (out) {
*out = '\0';
SDL_strlcat(out, a, out_size);
SDL_strlcat(out, b, out_size);
}
}
return out;
}
int main(int argc, char *argv[])
{
SDL_Window *w;
@@ -54,7 +81,9 @@ int main(int argc, char *argv[])
const SDL_FRect save_file_rect = { 50, 290, 220, 140 };
const SDL_FRect open_folder_rect = { 370, 50, 220, 140 };
int i;
const char *default_filename = "Untitled.index";
const char *initial_path = NULL;
char *last_saved_path = NULL;
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, 0);
@@ -116,7 +145,14 @@ int main(int argc, char *argv[])
} else if (SDL_PointInRectFloat(&p, &open_folder_rect)) {
SDL_ShowOpenFolderDialog(callback, NULL, w, initial_path, 1);
} else if (SDL_PointInRectFloat(&p, &save_file_rect)) {
SDL_ShowSaveFileDialog(callback, NULL, w, filters, SDL_arraysize(filters), initial_path);
char *save_path = NULL;
if (last_saved_path) {
save_path = SDL_strdup(last_saved_path);
} else {
save_path = concat_strings(initial_path, default_filename);
}
SDL_ShowSaveFileDialog(callback, &last_saved_path, w, filters, SDL_arraysize(filters), save_path ? save_path : default_filename);
SDL_free(save_path);
}
}
}
@@ -145,6 +181,7 @@ int main(int argc, char *argv[])
SDL_RenderPresent(r);
}
SDL_free(last_saved_path);
SDLTest_CleanupTextDrawing();
SDL_DestroyRenderer(r);
SDL_DestroyWindow(w);