polybar-dwm/include/drawtypes/ramp.hpp

76 lines
1.7 KiB
C++
Raw Normal View History

#pragma once
2016-05-19 14:41:06 +00:00
2016-06-15 03:32:35 +00:00
#include "common.hpp"
#include "components/config.hpp"
#include "drawtypes/label.hpp"
2016-10-25 05:10:03 +00:00
#include "utils/math.hpp"
2016-06-15 03:32:35 +00:00
#include "utils/mixins.hpp"
LEMONBUDDY_NS
namespace drawtypes {
class ramp : public non_copyable_mixin<ramp> {
public:
explicit ramp() = default;
explicit ramp(vector<icon_t>&& icons) : m_icons(forward<decltype(icons)>(icons)) {}
void add(icon_t&& icon) {
m_icons.emplace_back(forward<decltype(icon)>(icon));
}
2016-10-25 05:10:03 +00:00
icon_t get(size_t index) {
2016-06-15 03:32:35 +00:00
return m_icons[index];
}
icon_t get_by_percentage(float percentage) {
2016-10-25 05:10:03 +00:00
size_t index = percentage * (m_icons.size() - 1) / 100.0f + 0.5f;
return m_icons[math_util::cap<size_t>(index, 0, m_icons.size() - 1)];
2016-06-15 03:32:35 +00:00
}
operator bool() {
2016-10-25 05:10:03 +00:00
return !m_icons.empty();
2016-06-15 03:32:35 +00:00
}
protected:
vector<icon_t> m_icons;
2016-05-19 14:41:06 +00:00
};
2016-10-25 05:10:03 +00:00
using ramp_t = shared_ptr<ramp>;
2016-06-15 03:32:35 +00:00
2016-10-25 05:10:03 +00:00
/**
* Create a ramp by loading values
* from the configuration
*/
inline auto load_ramp(const config& conf, string section, string name, bool required = true) {
2016-06-15 03:32:35 +00:00
name = string_util::ltrim(string_util::rtrim(name, '>'), '<');
2016-10-25 05:10:03 +00:00
icon_t ramp_defaults;
try {
ramp_defaults = load_icon(conf, section, name);
} catch (const key_error&) {
}
vector<icon_t> vec;
2016-06-15 03:32:35 +00:00
vector<string> icons;
if (required)
icons = conf.get_list<string>(section, name);
else
icons = conf.get_list<string>(section, name, {});
2016-10-25 05:10:03 +00:00
for (size_t i = 0; i < icons.size(); i++) {
auto icon = load_optional_icon(conf, section, name + "-" + to_string(i), icons[i]);
if (ramp_defaults)
icon->copy_undefined(ramp_defaults);
vec.emplace_back(move(icon));
2016-06-15 03:32:35 +00:00
}
2016-10-25 05:10:03 +00:00
return ramp_t{new ramp_t::element_type(move(vec))};
2016-06-15 03:32:35 +00:00
}
2016-05-19 14:41:06 +00:00
}
2016-06-15 03:32:35 +00:00
LEMONBUDDY_NS_END