Skip to content

Commit

Permalink
Made several small improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Jun 17, 2024
1 parent 0501f22 commit 0c36c85
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nui/include/nui/frontend/elements/switch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace Nui::Elements
Nui::ElementRenderer renderer;
return !((cases.value == observed.value() ? (renderer = cases.renderer, true) : false) || ...)
? [](auto&, auto const&) -> std::shared_ptr<Dom::Element> {
Console::log("Nui::switch_ error! No case matched and no default case was provided!");
Console::warn("Nui::switch_ error! No case matched and no default case was provided!");
return nullptr;
} : renderer;
});
Expand Down
20 changes: 20 additions & 0 deletions nui/include/nui/frontend/rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,31 @@ namespace Nui

struct AutoUnregister : public OnDestroy
{
AutoUnregister()
: OnDestroy{[]() {}}
{}
AutoUnregister(std::string name)
: OnDestroy{[name = std::move(name)]() {
unregisterFunction(name);
}}
{}
~AutoUnregister() = default;

AutoUnregister(AutoUnregister const&) = delete;
AutoUnregister(AutoUnregister&& other)
: OnDestroy{std::move(other)}
{}
AutoUnregister& operator=(AutoUnregister const&) = delete;
AutoUnregister& operator=(AutoUnregister&& other)
{
OnDestroy::operator=(std::move(other));
return *this;
}

void reset()
{
trigger();
}
};

template <typename FunctionT>
Expand Down
8 changes: 8 additions & 0 deletions nui/include/nui/shared/on_destroy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ namespace Nui
if (!wasMoved_ && onDestroy_)
onDestroy_();
}
void trigger()
{
if (!wasMoved_ && onDestroy_)
{
onDestroy_();
onDestroy_ = nullptr;
}
}

private:
bool wasMoved_;
Expand Down
32 changes: 32 additions & 0 deletions nui/include/nui/utility/move_detector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

namespace Nui
{
/**
* @brief Utility class to detect if an object was moved.
*/
class MoveDetector
{
public:
MoveDetector() = default;
MoveDetector(MoveDetector const&) = default;
MoveDetector(MoveDetector&& other) noexcept
{
other.wasMoved_ = true;
}
MoveDetector& operator=(MoveDetector const&) = default;
MoveDetector& operator=(MoveDetector&& other) noexcept
{
other.wasMoved_ = true;
return *this;
}

bool wasMoved() const noexcept
{
return wasMoved_;
}

private:
bool wasMoved_{false};
};
}
1 change: 0 additions & 1 deletion nui/include/nui/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ namespace Nui
void setConsoleOutput(bool active);
#endif

private:
void runInJavascriptThread(std::function<void()>&& func);

public:
Expand Down
1 change: 0 additions & 1 deletion nui/src/nui/frontend/filesystem/file_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ namespace Nui::FileDialog
Nui::val opts = Nui::val::object();
convertOptions(opts, options);
const auto id = RpcClient::registerFunctionOnce([onResult = std::move(onResult)](Nui::val const& param) {
Console::log(param);
if (param.typeOf().as<std::string>() == "null")
onResult(std::nullopt);
else
Expand Down

0 comments on commit 0c36c85

Please sign in to comment.