Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to set a custom title to the PickFolder dialog #63

Closed
wants to merge 10 commits into from
8 changes: 6 additions & 2 deletions src/include/nfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ nfdresult_t NFD_SaveDialogN(nfdnchar_t** outPath,
/* It is the caller's responsibility to free `outPath` via NFD_FreePathN() if this function returns
* NFD_OKAY */
/* If defaultPath is NULL, the operating system will decide */
nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath, const nfdnchar_t* defaultPath);
nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath,
const nfdnchar_t* defaultPath,
const nfdnchar_t* title);

/* Get last error -- set when nfdresult_t returns NFD_ERROR */
/* Returns the last error that was set, or NULL if there is no error. */
Expand Down Expand Up @@ -207,7 +209,9 @@ nfdresult_t NFD_SaveDialogU8(nfdu8char_t** outPath,
/* select folder dialog */
/* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function returns
* NFD_OKAY */
nfdresult_t NFD_PickFolderU8(nfdu8char_t** outPath, const nfdu8char_t* defaultPath);
nfdresult_t NFD_PickFolderU8(nfdu8char_t** outPath,
const nfdu8char_t* defaultPath,
const nfdu8char_t* title);

/* Get the UTF-8 path at offset index */
/* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function returns
Expand Down
20 changes: 12 additions & 8 deletions src/include/nfd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ inline nfdresult_t SaveDialog(nfdnchar_t*& outPath,
}

inline nfdresult_t PickFolder(nfdnchar_t*& outPath,
const nfdnchar_t* defaultPath = nullptr) noexcept {
return ::NFD_PickFolderN(&outPath, defaultPath);
const nfdnchar_t* defaultPath = nullptr,
const nfdnchar_t* title = nullptr) noexcept {
return ::NFD_PickFolderN(&outPath, defaultPath, title);
}

inline const char* GetError() noexcept {
Expand Down Expand Up @@ -120,8 +121,9 @@ inline nfdresult_t SaveDialog(nfdu8char_t*& outPath,
}

inline nfdresult_t PickFolder(nfdu8char_t*& outPath,
const nfdu8char_t* defaultPath = nullptr) noexcept {
return ::NFD_PickFolderU8(&outPath, defaultPath);
const nfdu8char_t* defaultPath = nullptr,
const nfdu8char_t* title = nullptr) noexcept {
return ::NFD_PickFolderU8(&outPath, defaultPath, title);
}

namespace PathSet {
Expand Down Expand Up @@ -220,9 +222,10 @@ inline nfdresult_t SaveDialog(UniquePathN& outPath,
}

inline nfdresult_t PickFolder(UniquePathN& outPath,
const nfdnchar_t* defaultPath = nullptr) noexcept {
const nfdnchar_t* defaultPath = nullptr,
const nfdnchar_t* title = nullptr) noexcept {
nfdnchar_t* out;
nfdresult_t res = PickFolder(out, defaultPath);
nfdresult_t res = PickFolder(out, defaultPath, title);
if (res == NFD_OKAY) {
outPath.reset(out);
}
Expand Down Expand Up @@ -268,9 +271,10 @@ inline nfdresult_t SaveDialog(UniquePathU8& outPath,
}

inline nfdresult_t PickFolder(UniquePathU8& outPath,
const nfdu8char_t* defaultPath = nullptr) noexcept {
const nfdu8char_t* defaultPath = nullptr,
const nfdu8char_t* title = nullptr) noexcept {
nfdu8char_t* out;
nfdresult_t res = PickFolder(out, defaultPath);
nfdresult_t res = PickFolder(out, defaultPath, title);
if (res == NFD_OKAY) {
outPath.reset(out);
}
Expand Down
8 changes: 7 additions & 1 deletion src/nfd_cocoa.m
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ nfdresult_t NFD_SaveDialogN(nfdnchar_t** outPath,
return result;
}

nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath, const nfdnchar_t* defaultPath) {
nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath,
const nfdnchar_t* defaultPath,
const nfdnchar_t* title) {
nfdresult_t result = NFD_CANCEL;
@autoreleasepool {
NSWindow* keyWindow = [[NSApplication sharedApplication] keyWindow];
Expand All @@ -247,6 +249,10 @@ nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath, const nfdnchar_t* defaultPath)
[dialog setCanCreateDirectories:YES];
[dialog setCanChooseFiles:NO];

if (title) {
[dialog setTitle:[NSString stringWithUTF8String:title]];
}

// Set the starting directory
SetDefaultPath(dialog, defaultPath);

Expand Down
31 changes: 22 additions & 9 deletions src/nfd_gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,28 @@ nfdresult_t NFD_SaveDialogN(nfdnchar_t** outPath,
}
}

nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath, const nfdnchar_t* defaultPath) {
GtkWidget* widget = gtk_file_chooser_dialog_new("Select folder",
nullptr,
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
"_Cancel",
GTK_RESPONSE_CANCEL,
"_Select",
GTK_RESPONSE_ACCEPT,
nullptr);
nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath,
const nfdnchar_t* defaultPath,
const nfdnchar_t* title) {
GtkWidget* widget;
if (title)
widget = gtk_file_chooser_dialog_new(title,
nullptr,
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
"_Cancel",
GTK_RESPONSE_CANCEL,
"_Select",
GTK_RESPONSE_ACCEPT,
nullptr);
else
widget = gtk_file_chooser_dialog_new("Select folder",
nullptr,
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
"_Cancel",
GTK_RESPONSE_CANCEL,
"_Select",
GTK_RESPONSE_ACCEPT,
nullptr);

// guard to destroy the widget when returning from this function
Widget_Guard widgetGuard(widget);
Expand Down
23 changes: 17 additions & 6 deletions src/nfd_portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ void AppendOpenFileQueryTitle<false, true>(DBusMessageIter& iter) {
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &STR_SELECT_FOLDER);
}

void AppendOpenFileQueryTitle(DBusMessageIter& iter, const char* title) {
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &title);
}

void AppendSaveFileQueryTitle(DBusMessageIter& iter) {
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &STR_SAVE_FILE);
}
Expand Down Expand Up @@ -529,13 +533,17 @@ template <bool Multiple, bool Directory>
void AppendOpenFileQueryParams(DBusMessage* query,
const char* handle_token,
const nfdnfilteritem_t* filterList,
nfdfiltersize_t filterCount) {
nfdfiltersize_t filterCount,
const char* title) {
DBusMessageIter iter;
dbus_message_iter_init_append(query, &iter);

dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &STR_EMPTY);

AppendOpenFileQueryTitle<Multiple, Directory>(iter);
if (title)
AppendOpenFileQueryTitle(iter, title);
else
AppendOpenFileQueryTitle<Multiple, Directory>(iter);

DBusMessageIter sub_iter;
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub_iter);
Expand Down Expand Up @@ -1028,7 +1036,8 @@ nfdresult_t AllocAndCopyFilePathWithExtn(const char* fileUri, const char* extn,
template <bool Multiple, bool Directory>
nfdresult_t NFD_DBus_OpenFile(DBusMessage*& outMsg,
const nfdnfilteritem_t* filterList,
nfdfiltersize_t filterCount) {
nfdfiltersize_t filterCount,
const char* title = nullptr) {
const char* handle_token_ptr;
char* handle_obj_path = MakeUniqueObjectPath(&handle_token_ptr);
Free_Guard<char> handle_obj_path_guard(handle_obj_path);
Expand All @@ -1051,7 +1060,7 @@ nfdresult_t NFD_DBus_OpenFile(DBusMessage*& outMsg,
"OpenFile");
DBusMessage_Guard query_guard(query);
AppendOpenFileQueryParams<Multiple, Directory>(
query, handle_token_ptr, filterList, filterCount);
query, handle_token_ptr, filterList, filterCount, title);

DBusMessage* reply =
dbus_connection_send_with_reply_and_block(dbus_conn, query, DBUS_TIMEOUT_INFINITE, &err);
Expand Down Expand Up @@ -1319,12 +1328,14 @@ nfdresult_t NFD_SaveDialogN(nfdnchar_t** outPath,
#endif
}

nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath, const nfdnchar_t* defaultPath) {
nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath,
const nfdnchar_t* defaultPath,
const nfdnchar_t* title) {
(void)defaultPath; // Default path not supported for portal backend

DBusMessage* msg;
{
const nfdresult_t res = NFD_DBus_OpenFile<false, true>(msg, nullptr, 0);
const nfdresult_t res = NFD_DBus_OpenFile<false, true>(msg, nullptr, 0, title);
if (res != NFD_OKAY) {
return res;
}
Expand Down
Loading