Skip to content

Commit

Permalink
Merge pull request #97 from NuiCpp/devel
Browse files Browse the repository at this point in the history
Fixed File Dialog Bugs
  • Loading branch information
5cript authored Nov 22, 2023
2 parents b9994ce + 60c9499 commit 0c1694d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmake/dependencies/roar.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
option(NUI_FETCH_ROAR "Fetch roar" ON)
set(NUI_ROAR_REPOSITORY "https://github.com/5cript/roar.git" CACHE STRING "roar repository")
set(NUI_ROAR_TAG "7efcf0bd3d971426e0f6fe793927d4bc0ad49d85" CACHE STRING "roar tag")
set(NUI_ROAR_TAG "b144dd89e565f3ee8617d686a528529f88fd755e" CACHE STRING "roar tag")

if(NUI_FETCH_ROAR)
include(FetchContent)
Expand Down
3 changes: 3 additions & 0 deletions nui/include/nui/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ namespace Nui

/// WEBKIT ONLY (Linux & Mac)
std::optional<std::string> folderMappingScheme = std::string{"assets"};

// Called when a message from the view cannot be parsed or references an invalid function or has no id.
std::function<void(std::string_view)> onRpcError = {};
};
#else
struct WindowOptions
Expand Down
7 changes: 4 additions & 3 deletions nui/src/nui/backend/filesystem/file_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

namespace Nui::FileDialog
{
//#####################################################################################################################
// #####################################################################################################################
namespace
{
//---------------------------------------------------------------------------------------------------------------------
std::vector<std::string> flattenFilters(std::vector<Filter> const& filters)
{
std::vector<std::string> flattenedFilters;
if (flattenedFilters.empty())
if (filters.empty())
{
flattenedFilters.push_back("All files");
flattenedFilters.push_back("*");
Expand Down Expand Up @@ -76,6 +76,7 @@ namespace Nui::FileDialog
if (result.empty())
return std::nullopt;
std::vector<std::filesystem::path> paths;
paths.resize(result.size());
std::transform(result.begin(), result.end(), paths.begin(), [](std::string const& path) {
return std::filesystem::path(path);
});
Expand Down Expand Up @@ -115,5 +116,5 @@ namespace Nui::FileDialog
return std::nullopt;
return std::filesystem::path(result);
}
//#####################################################################################################################
// #####################################################################################################################
}
54 changes: 44 additions & 10 deletions nui/src/nui/backend/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <list>
#include <array>
#include <string>
#include <iostream>

#ifndef _WIN32
namespace Nui
Expand Down Expand Up @@ -98,9 +99,9 @@ namespace Nui
# include "mac_webview_config_from_window_options.ipp"
#elif defined(_WIN32)
# include <webview2_environment_options.hpp>
#ifndef _MSC_VER
# include <webview2_iids.h>
#endif
# ifndef _MSC_VER
# include <webview2_iids.h>
# endif
# include <wrl/event.h>
# include "environment_options_from_window_options.ipp"
constexpr static auto wakeUpMessage = WM_APP + 1;
Expand All @@ -113,24 +114,26 @@ namespace Nui
{
struct Window::Implementation : public std::enable_shared_from_this<Implementation>
{
std::recursive_mutex viewGuard;
boost::asio::thread_pool pool;
std::unique_ptr<webview::webview> view;
std::vector<std::filesystem::path> cleanupFiles;
std::unordered_map<std::string, std::function<void(nlohmann::json const&)>> callbacks;
boost::asio::thread_pool pool;
std::recursive_mutex viewGuard;
int width;
int height;
std::function<void(std::string_view)> onRpcError;

virtual void registerSchemeHandlers(WindowOptions const& options) = 0;

Implementation()
: view{}
: viewGuard{}
, pool{4}
, view{}
, cleanupFiles{}
, callbacks{}
, pool{4}
, viewGuard{}
, width{0}
, height{0}
, onRpcError{}
{}
virtual ~Implementation()
{
Expand Down Expand Up @@ -202,6 +205,15 @@ namespace Nui
: impl_{std::make_shared<WindowsImplementation>()}
#endif
{
if (!options.onRpcError)
{
impl_->onRpcError = [](std::string_view msg) {
std::cerr << "NUI RPC Error: " << msg << std::endl;
};
}
else
impl_->onRpcError = options.onRpcError;

#ifdef __APPLE__
impl_->initialize(
options.debug,
Expand All @@ -215,8 +227,27 @@ namespace Nui

impl_->view->install_message_hook([this](std::string const& msg) {
std::scoped_lock lock{impl_->viewGuard};
const auto obj = nlohmann::json::parse(msg);
impl_->callbacks[obj["id"].get<std::string>()](obj["args"]);
try
{
const auto obj = nlohmann::json::parse(msg);
if (!obj.contains("id"))
return impl_->onRpcError("Message does not contain a callback id!"), false;

const auto id = obj["id"].get<std::string>();
auto callbackIter = impl_->callbacks.find(id);
if (callbackIter == impl_->callbacks.end())
return impl_->onRpcError("Callback with id " + id + " does not exist!"), false;

if (!obj.contains("args"))
callbackIter->second(nlohmann::json{});
else
callbackIter->second(obj["args"]);
}
catch (std::exception const& exc)
{
impl_->onRpcError(
"Exception in webview message handler for message: " + msg + "\nException: " + exc.what());
}
return false;
});

Expand Down Expand Up @@ -481,6 +512,9 @@ namespace Nui
//---------------------------------------------------------------------------------------------------------------------
void Window::bind(std::string const& name, std::function<void(nlohmann::json const&)> const& callback)
{
if (!callback)
throw std::runtime_error("Callback must be valid.");

runInJavascriptThread([this, name, callback]() {
std::scoped_lock lock{impl_->viewGuard};
impl_->callbacks[name] = callback;
Expand Down
4 changes: 2 additions & 2 deletions nui/src/nui/frontend/filesystem/file_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ namespace Nui::FileDialog
opts.set("filters", Nui::val::array());
for (auto const& filter : options.filters)
{
Nui::val filterVal;
Nui::val filterVal = Nui::val::object();
filterVal.set("name", convertToVal(filter.name));
filterVal.set("masks", convertToVal(filter.masks));
opts["filter"].call<void>("push", filterVal);
opts["filters"].call<void>("push", filterVal);
}
opts.set("forcePath", convertToVal(options.forcePath));
}
Expand Down

0 comments on commit 0c1694d

Please sign in to comment.