Support user-supplied tag mapper in ui::label

This commit is contained in:
Nikita Lisitsa 2022-05-28 20:09:17 +03:00
parent 2a2a97f0f8
commit b167e06898
3 changed files with 25 additions and 0 deletions

View file

@ -62,6 +62,8 @@ namespace psemek::ui
virtual struct image_provider * set_image_provider(struct image_provider * provider);
virtual struct image_provider * image_provider() const { return image_provider_; }
virtual void set_tag_mapper(tagged_text::mapper mapper);
using link_callback = std::function<void(std::string_view)>;
virtual void on_link_click(link_callback callback);
@ -95,6 +97,7 @@ namespace psemek::ui
bool skip_spaces_ = false;
struct image_provider * image_provider_ = nullptr;
tagged_text::mapper tag_mapper_ = nullptr;
link_callback link_callback_;

View file

@ -5,6 +5,7 @@
#include <variant>
#include <vector>
#include <stdexcept>
#include <functional>
namespace psemek::ui
{
@ -41,6 +42,8 @@ namespace psemek::ui
};
static tagged_text parse(std::string_view text);
using mapper = std::function<std::optional<std::vector<token>>(token const &)>;
};
}

View file

@ -65,6 +65,20 @@ namespace psemek::ui
auto parse_result = tagged_text::parse(text_);
if (tag_mapper_)
{
std::vector<tagged_text::token> mapped;
for (auto const & token : parse_result.tokens)
{
auto replace = tag_mapper_(token);
if (replace)
mapped.insert(mapped.end(), replace->begin(), replace->end());
else
mapped.push_back(token);
}
parse_result.tokens = std::move(mapped);
}
std::unordered_map<std::string, std::vector<std::optional<std::string>>> tags_stack;
for (auto const & token : parse_result.tokens)
@ -153,6 +167,11 @@ namespace psemek::ui
return provider;
}
void label::set_tag_mapper(tagged_text::mapper mapper)
{
tag_mapper_ = std::move(mapper);
}
void label::on_link_click(link_callback callback)
{
link_callback_ = std::move(callback);