Skip to content

Commit

Permalink
Merge branch 'devel' of github.com:NuiCpp/Nui into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Aug 30, 2023
2 parents dad7131 + b574f89 commit a0626fd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
20 changes: 16 additions & 4 deletions nui/include/nui/data_structures/selectables_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,31 @@ namespace Nui
return result;
}

std::optional<T>* select(IdType id)
struct SelectionResult
{
std::optional<T>* item;
bool found;
bool alreadySelected;
};
SelectionResult select(IdType id)
{
const auto iter = findItem(id);
if (iter == std::end(items_) || !iter->item.has_value())
return nullptr;
if (iter == std::end(items_))
return {nullptr, false, false};
if (!iter->item.has_value())
return {nullptr, true, true};

--itemCount_;

const auto selectedIter = selected_.insert(std::move(*iter)).first;
iter->item.reset();
// having modifying access to the optional<T> does not mess with the set ordering. const casting is fine
// here.
return &(const_cast<ItemWithId&>(*selectedIter).item);
return {
.item = &(const_cast<ItemWithId&>(*selectedIter).item),
.found = true,
.alreadySelected = false,
};
}

void deselectAll(std::invocable<ItemWithId const&> auto callback)
Expand Down
4 changes: 4 additions & 0 deletions nui/include/nui/frontend/dom/childless_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ namespace Nui::Dom
else
element_.call<Nui::val>("setAttribute", Nui::val{std::string{key}}, Nui::val{value});
}
void setAttribute(std::string_view key, std::string_view value)
{
setAttribute(key, std::string{value});
}
void setAttribute(std::string_view key, std::invocable<Nui::val> auto&& value)
{
element_.set(Nui::val{std::string{key}}, Nui::bind(value, std::placeholders::_1));
Expand Down
2 changes: 1 addition & 1 deletion nui/include/nui/frontend/event_system/event_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace Nui
{
return impl_->eventRegistry().registerEvent(std::move(event));
}
auto* activateEvent(EventIdType id)
auto activateEvent(EventIdType id)
{
return impl_->eventRegistry().activateEvent(id);
}
Expand Down
7 changes: 4 additions & 3 deletions nui/include/nui/frontend/event_system/event_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Nui
class EventRegistry
{
public:
using RegistryType = SelectablesRegistry<Event>;
using EventIdType = SelectablesRegistry<Event>::IdType;
constexpr static EventIdType invalidEventId = std::numeric_limits<EventIdType>::max();

Expand All @@ -36,7 +37,7 @@ namespace Nui
* @param id
* @return auto*
*/
auto* activateEvent(EventIdType id)
RegistryType::SelectionResult activateEvent(EventIdType id)
{
return registry_.select(id);
}
Expand Down Expand Up @@ -70,7 +71,7 @@ namespace Nui
}

private:
SelectablesRegistry<Event> registry_;
SelectablesRegistry<Event> afterEffects_;
RegistryType registry_;
RegistryType afterEffects_;
};
}
5 changes: 2 additions & 3 deletions nui/include/nui/frontend/event_system/observed_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include <deque>
#include <string>

#include <iostream>

namespace Nui
{
class ObservedBase
Expand Down Expand Up @@ -88,7 +86,8 @@ namespace Nui
{
for (auto& event : attachedEvents_)
{
if (globalEventContext.activateEvent(event) == nullptr)
auto activationResult = globalEventContext.activateEvent(event);
if (activationResult.found == false)
event = EventRegistry::invalidEventId;
}
for (auto& event : attachedOneshotEvents_)
Expand Down

0 comments on commit a0626fd

Please sign in to comment.