2016-11-02 19:22:45 +00:00
|
|
|
#include "drawtypes/iconset.hpp"
|
|
|
|
|
2022-10-05 21:05:44 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
namespace drawtypes {
|
2018-12-23 21:59:10 +00:00
|
|
|
void iconset::add(string id, label_t&& icon) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_icons.emplace(id, forward<decltype(icon)>(icon));
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
bool iconset::has(const string& id) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return m_icons.find(id) != m_icons.end();
|
|
|
|
}
|
|
|
|
|
2018-12-23 21:59:10 +00:00
|
|
|
label_t iconset::get(const string& id, const string& fallback_id, bool fuzzy_match) {
|
2020-03-19 14:09:38 +00:00
|
|
|
// Try to match exactly first
|
|
|
|
auto icon = m_icons.find(id);
|
|
|
|
if (icon != m_icons.end()) {
|
|
|
|
return icon->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If fuzzy matching is turned on, try that first before returning the fallback.
|
2017-01-20 04:35:52 +00:00
|
|
|
if (fuzzy_match) {
|
2022-10-05 21:05:44 +00:00
|
|
|
// works by finding the *longest* matching icon to the given workspace id
|
|
|
|
size_t max_size = -1;
|
|
|
|
label_t max_label;
|
|
|
|
|
2020-03-19 14:09:38 +00:00
|
|
|
for (auto const& icon : m_icons) {
|
|
|
|
if (id.find(icon.first) != std::string::npos) {
|
2022-10-05 21:05:44 +00:00
|
|
|
if (icon.first.length() > max_size || !max_label) {
|
|
|
|
max_size = icon.first.length();
|
|
|
|
max_label = icon.second;
|
|
|
|
}
|
2017-01-20 04:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-05 21:05:44 +00:00
|
|
|
|
|
|
|
if (max_label) {
|
|
|
|
return max_label;
|
|
|
|
}
|
2017-01-20 04:35:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 14:09:38 +00:00
|
|
|
return m_icons.find(fallback_id)->second;
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iconset::operator bool() {
|
2016-11-25 12:55:15 +00:00
|
|
|
return !m_icons.empty();
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
2022-10-05 21:05:44 +00:00
|
|
|
} // namespace drawtypes
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|