[nostalgia/studio] Switch file dialog to NFDE and add save file dialog

This commit is contained in:
Gary Talent 2022-05-25 21:19:27 -05:00
parent a9d5272176
commit 0e8d2d7640
6 changed files with 53 additions and 86 deletions

View File

@ -12,8 +12,8 @@ add_executable(
target_link_libraries(
nostalgia-studio
OxClArgs
NostalgiaCore-Studio
NostalgiaStudio
NostalgiaCore-Studio
)
if (CMAKE_BUILD_TYPE STREQUAL "Release")

View File

@ -1,12 +1,3 @@
if(APPLE)
enable_language(OBJCXX)
endif()
if(NOT APPLE AND NOT WIN32)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
endif()
add_library(
NostalgiaStudio
configio.cpp
@ -16,8 +7,7 @@ add_library(
task.cpp
undostack.cpp
widget.cpp
filedialog_gtk.cpp
$<$<BOOL:${APPLE}>:filedialog_mac.mm>
filedialog_nfd.cpp
)
if(NOT MSVC)
@ -42,7 +32,7 @@ include_directories(
target_link_libraries(
NostalgiaStudio PUBLIC
${GTK3_LIBRARIES}
nfd
OxEvent
NostalgiaCore
)

View File

@ -6,6 +6,13 @@
namespace nostalgia::studio {
struct FDFilterItem {
ox::String name;
ox::String spec;
};
ox::Result<ox::String> saveFile(const ox::Vector<FDFilterItem> &exts) noexcept;
ox::Result<ox::String> chooseDirectory() noexcept;
}

View File

@ -1,41 +0,0 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <ox/std/defines.hpp>
#if !defined(OX_OS_Windows) && !defined(OX_OS_Darwin)
#include <gtk/gtk.h>
#include <ox/std/defer.hpp>
#include <ox/std/string.hpp>
namespace nostalgia::studio {
ox::Result<ox::String> chooseDirectory() noexcept {
ox::Result<ox::String> out;
const auto action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
gtk_init(nullptr, nullptr);
auto dialog = gtk_file_chooser_dialog_new("Open Project Directory", nullptr, action,
("_Cancel"), GTK_RESPONSE_CANCEL,
("_Open"), GTK_RESPONSE_ACCEPT,
nullptr);
const auto res = gtk_dialog_run(GTK_DIALOG(dialog));
if (res == GTK_RESPONSE_ACCEPT) {
auto chooser = GTK_FILE_CHOOSER(dialog);
auto path = gtk_file_chooser_get_filename(chooser);
out = ox::String(path);
g_free(path);
} else {
out = OxError(1);
}
gtk_widget_destroy(dialog);
// for some reason we need to 4 iterations through the main loop to destroy the dialog
for (int i = 0; i < 4; ++i) {
gtk_main_iteration_do(false);
}
return out;
}
}
#endif

View File

@ -1,32 +0,0 @@
/*
* Copyright 2016 - 2022 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#import <Cocoa/Cocoa.h>
#include <ox/std/string.hpp>
#include "filedialog.hpp"
namespace nostalgia::studio {
ox::Result<ox::String> chooseDirectory() noexcept {
auto panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:NO];
[panel setFloatingPanel:YES];
if ([panel runModal] == NSModalResponseOK) {
const auto url = [panel URL];
const auto urlStr = [url absoluteString];
const auto urlCStr = [urlStr UTF8String];
return ox::String(urlCStr).substr(7);
}
return OxError(1);
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <nfd.hpp>
#include <ox/std/error.hpp>
#include <ox/std/string.hpp>
#include <ox/std/trace.hpp>
#include "filedialog.hpp"
namespace nostalgia::studio {
static ox::Result<ox::String> toResult(nfdresult_t r, const NFD::UniquePathN &path) noexcept {
switch (r) {
case NFD_OKAY:
return ox::String(path.get());
case NFD_CANCEL:
return OxError(1, "Operation cancelled");
default:
return OxError(2, NFD::GetError());
}
}
ox::Result<ox::String> saveFile(const ox::Vector<FDFilterItem> &filters) noexcept {
NFD::Guard guard;
NFD::UniquePathN path;
ox::Vector<nfdnfilteritem_t, 5> filterItems(filters.size());
for (auto i = 0u; const auto &f : filters) {
filterItems[i] = {f.name.c_str(), f.spec.c_str()};
++i;
}
return toResult(NFD::SaveDialog(path, filterItems.data(), filterItems.size()), path);
}
ox::Result<ox::String> chooseDirectory() noexcept {
NFD::Guard guard;
NFD::UniquePathN path;
return toResult(NFD::PickFolder(path), path);
}
}