Support user-supplied tag mapper in ui::label
This commit is contained in:
parent
2a2a97f0f8
commit
b167e06898
3 changed files with 25 additions and 0 deletions
|
|
@ -62,6 +62,8 @@ namespace psemek::ui
|
||||||
virtual struct image_provider * set_image_provider(struct image_provider * provider);
|
virtual struct image_provider * set_image_provider(struct image_provider * provider);
|
||||||
virtual struct image_provider * image_provider() const { return image_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)>;
|
using link_callback = std::function<void(std::string_view)>;
|
||||||
|
|
||||||
virtual void on_link_click(link_callback callback);
|
virtual void on_link_click(link_callback callback);
|
||||||
|
|
@ -95,6 +97,7 @@ namespace psemek::ui
|
||||||
bool skip_spaces_ = false;
|
bool skip_spaces_ = false;
|
||||||
|
|
||||||
struct image_provider * image_provider_ = nullptr;
|
struct image_provider * image_provider_ = nullptr;
|
||||||
|
tagged_text::mapper tag_mapper_ = nullptr;
|
||||||
|
|
||||||
link_callback link_callback_;
|
link_callback link_callback_;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace psemek::ui
|
namespace psemek::ui
|
||||||
{
|
{
|
||||||
|
|
@ -41,6 +42,8 @@ namespace psemek::ui
|
||||||
};
|
};
|
||||||
|
|
||||||
static tagged_text parse(std::string_view text);
|
static tagged_text parse(std::string_view text);
|
||||||
|
|
||||||
|
using mapper = std::function<std::optional<std::vector<token>>(token const &)>;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,20 @@ namespace psemek::ui
|
||||||
|
|
||||||
auto parse_result = tagged_text::parse(text_);
|
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;
|
std::unordered_map<std::string, std::vector<std::optional<std::string>>> tags_stack;
|
||||||
|
|
||||||
for (auto const & token : parse_result.tokens)
|
for (auto const & token : parse_result.tokens)
|
||||||
|
|
@ -153,6 +167,11 @@ namespace psemek::ui
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void label::set_tag_mapper(tagged_text::mapper mapper)
|
||||||
|
{
|
||||||
|
tag_mapper_ = std::move(mapper);
|
||||||
|
}
|
||||||
|
|
||||||
void label::on_link_click(link_callback callback)
|
void label::on_link_click(link_callback callback)
|
||||||
{
|
{
|
||||||
link_callback_ = std::move(callback);
|
link_callback_ = std::move(callback);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue