1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/include/modules/meta/base.inl
Patrick Ziegler ce93188a4a
Add units support (POINT, PIXEL, SPACE) (#2578)
* add units support (POINT, PIXEL, SPACE) for polybar

- add a size_with_unit struct
- add a geometry_format_values struct
- move dpi initialisation from renderer.cpp to bar.cpp
- add a string to size_with_unit converter
- add point support (with pt)
- add pixel support (with px)

* Fix unit test compilation

* clang-format

* Better names

The old names didn't really capture the purpose of the structs and
function.

space_type -> spacing_type
space_size -> spacing_val

size_type -> extent_type
geometry -> extent_val

geometry_format_values -> percentage_with_offset

* Remove parse_size_with_unit

No longer needed. The convert<spacing_val> function in config.cpp
already does all the work for us and always setting the type to pixel
was wrong.

In addition, line-size should not be of type spacing_val but extent_val.

* Cleanup

I tried to address most of my comments on the old PR

* Fix renderer width calculation

We can't just blindly add the x difference to the width because for
example the width should increase if x < width and the increase keeps
x < width.

Similarly, we can't just add the offset to the width.

* Rename geom_format_to_pixels to percentage_with_offset_to_pixel

* Cleanup

* Apply suggested changes from Patrick on GitHub

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>

* Update src/components/bar.cpp

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>

* Update src/components/config.cpp

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>

* Update src/components/builder.cpp

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>

* Update src/components/builder.cpp

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>

* config: Use stod for parsing percentage

* Use stof instead of strtof

* units: Fix test edge cases

* Remove unnecessary clang-format toggle

* Use percentage_with_offset for margin-{top,bottom}

* Support negative extent values

* Rename unit to units and create a cpp file

* Move percentage_with_offset_to_pixel unit test to units

* Add unit tests for units_utils

* Clarify when and how negative spacing/extent is allowed

Negative spacing is never allowed and produces a config error.

Extents allow negative values in theory, but only a few use-cases accept
it.
Only the extent value used for the `%{O}` tag and the offset value in
percentage_with_offset can be negative. Everything else is capped below
at 0.

The final pixel value of percentage_with_offset also caps below at 0.

* Fix parsing errors not being caught in config

* Print a proper error message for uncaught exceptions

* Cleanup module::get_output

All changes preserve the existing semantics

* Stop using remove_trailing_space in module::get_output

Instead, we first check if the current tag is built, and only if it is,
the spacing is prepended.

* Remove unused imports

* Restore old behavior

If there are two tags and the second one isn't built (module::build
returns false), the space in between them is removed.
For example in the mpd module:

format-online = <toggle> <label-song> foo

If mpd is not running, the mpd module will return false when trying to
build the `<label-song>` tag. If we don't remove the space between
`<toggle>` and `<label-song>`, we end up with two spaces between
`<toggle>` and `foo`.

This change is to match the old behavior where at least one trailing
space character was removed from the builder.

* Add changelog entry

* Remove unused setting

* Use percentage with offset for tray-offset

Co-authored-by: Jérôme BOULMIER <jerome.boulmier@outlook.fr>
Co-authored-by: Joe Groocock <github@frebib.net>
2022-02-20 21:08:57 +01:00

313 lines
8 KiB
C++

#include <cassert>
#include "components/builder.hpp"
#include "components/config.hpp"
#include "components/logger.hpp"
#include "events/signal.hpp"
#include "events/signal_emitter.hpp"
#include "modules/meta/base.hpp"
#include "utils/action_router.hpp"
POLYBAR_NS
namespace modules {
// module<Impl> public {{{
template <typename Impl>
module<Impl>::module(const bar_settings bar, string name)
: m_sig(signal_emitter::make())
, m_bar(bar)
, m_log(logger::make())
, m_conf(config::make())
, m_router(make_unique<action_router>())
, m_name("module/" + name)
, m_name_raw(name)
, m_builder(make_unique<builder>(bar))
, m_formatter(make_unique<module_formatter>(m_conf, m_name))
, m_handle_events(m_conf.get(m_name, "handle-events", true))
, m_visible(!m_conf.get(m_name, "hidden", false)) {
m_router->register_action(EVENT_MODULE_TOGGLE, [this]() { action_module_toggle(); });
m_router->register_action(EVENT_MODULE_SHOW, [this]() { action_module_show(); });
m_router->register_action(EVENT_MODULE_HIDE, [this]() { action_module_hide(); });
}
template <typename Impl>
module<Impl>::~module() noexcept {
m_log.trace("%s: Deconstructing", name());
if (running()) {
/*
* We can't stop in the destructor because we have to call the subclasses which at this point already have been
* destructed.
*/
m_log.err("%s: Module was not stopped before deconstructing.", name());
}
}
template <typename Impl>
string module<Impl>::name() const {
return m_name;
}
template <typename Impl>
string module<Impl>::name_raw() const {
return m_name_raw;
}
template <typename Impl>
string module<Impl>::type() const {
return string(Impl::TYPE);
}
template <typename Impl>
bool module<Impl>::running() const {
return static_cast<bool>(m_enabled);
}
template <class Impl>
void module<Impl>::start() {
m_enabled = true;
}
template <class Impl>
void module<Impl>::join() {
for (auto&& thread_ : m_threads) {
if (thread_.joinable()) {
thread_.join();
}
}
if (m_mainthread.joinable()) {
m_mainthread.join();
}
}
template <typename Impl>
bool module<Impl>::visible() const {
return static_cast<bool>(m_visible);
}
template <typename Impl>
void module<Impl>::stop() {
if (!static_cast<bool>(m_enabled)) {
return;
}
m_log.info("%s: Stopping", name());
m_enabled = false;
std::lock(m_buildlock, m_updatelock);
std::lock_guard<std::mutex> guard_a(m_buildlock, std::adopt_lock);
std::lock_guard<std::mutex> guard_b(m_updatelock, std::adopt_lock);
{
CAST_MOD(Impl)->wakeup();
CAST_MOD(Impl)->teardown();
m_sig.emit(signals::eventqueue::check_state{});
}
}
template <typename Impl>
void module<Impl>::halt(string error_message) {
m_log.err("%s: %s", name(), error_message);
m_log.notice("Stopping '%s'...", name());
stop();
}
template <typename Impl>
void module<Impl>::teardown() {}
template <typename Impl>
string module<Impl>::contents() {
if (m_changed) {
m_log.info("%s: Rebuilding cache", name());
m_cache = CAST_MOD(Impl)->get_output();
// Make sure builder is really empty
m_builder->flush();
if (!m_cache.empty()) {
// Add a reset tag after the module
m_builder->control(tags::controltag::R);
m_cache += m_builder->flush();
}
m_changed = false;
}
return m_cache;
}
template <typename Impl>
bool module<Impl>::input(const string& name, const string& data) {
if (!m_router->has_action(name)) {
return false;
}
try {
m_router->invoke(name, data);
} catch (const exception& err) {
m_log.err("%s: Failed to handle command '%s' with data '%s' (%s)", this->name(), name, data, err.what());
}
return true;
}
// }}}
// module<Impl> protected {{{
template <typename Impl>
void module<Impl>::broadcast() {
m_changed = true;
m_sig.emit(signals::eventqueue::notify_change{});
}
template <typename Impl>
void module<Impl>::idle() {
if (running()) {
CAST_MOD(Impl)->sleep(25ms);
}
}
template <typename Impl>
void module<Impl>::sleep(chrono::duration<double> sleep_duration) {
if (running()) {
std::unique_lock<std::mutex> lck(m_sleeplock);
m_sleephandler.wait_for(lck, sleep_duration);
}
}
template <typename Impl>
template <class Clock, class Duration>
void module<Impl>::sleep_until(chrono::time_point<Clock, Duration> point) {
if (running()) {
std::unique_lock<std::mutex> lck(m_sleeplock);
m_sleephandler.wait_until(lck, point);
}
}
template <typename Impl>
void module<Impl>::wakeup() {
m_log.trace("%s: Release sleep lock", name());
m_sleephandler.notify_all();
}
template <typename Impl>
string module<Impl>::get_format() const {
return DEFAULT_FORMAT;
}
template <typename Impl>
string module<Impl>::get_output() {
std::lock_guard<std::mutex> guard(m_buildlock);
auto format_name = CONST_MOD(Impl).get_format();
auto format = m_formatter->get(format_name);
/*
* Builder for building individual tags isolated, so that we can
*/
builder tag_builder(m_bar);
// Whether any tags have been processed yet
bool has_tags = false;
// Cursor pointing into 'value'
size_t cursor = 0;
const string& value{format->value};
/*
* Search for all tags in the format definition. A tag is enclosed in '<' and '>'.
* Each tag is given to the module to produce some output for it. All other text is added as-is.
*/
while (cursor < value.size()) {
// Check if there are any tags left
// Start index of next tag
size_t start = value.find('<', cursor);
if (start == string::npos) {
break;
}
// End index (inclusive) of next tag
size_t end = value.find('>', start + 1);
if (end == string::npos) {
break;
}
// Potential regular text that appears before the tag.
string non_tag;
// There is some non-tag text
if (start > cursor) {
/*
* Produce anything between the previous and current tag as regular text.
*/
non_tag = value.substr(cursor, start - cursor);
if (!has_tags) {
/*
* If no module tag has been built we do not want to add
* whitespace defined between the format tags, but we do still
* want to output other non-tag content
*/
non_tag = string_util::ltrim(move(non_tag), ' ');
}
}
string tag = value.substr(start, end - start + 1);
bool tag_built = CONST_MOD(Impl).build(&tag_builder, tag);
string tag_content = tag_builder.flush();
/*
* Remove exactly one space between two tags if the second tag was not built.
*/
if (!tag_built && has_tags && !format->spacing) {
if (!non_tag.empty() && non_tag.back() == ' ') {
non_tag.erase(non_tag.size() - 1);
}
}
m_builder->node(non_tag);
if (tag_built) {
if (has_tags) {
// format-spacing is added between all tags
m_builder->spacing(format->spacing);
}
m_builder->append(tag_content);
has_tags = true;
}
cursor = end + 1;
}
if (cursor < value.size()) {
m_builder->append(value.substr(cursor));
}
return format->decorate(&*m_builder, m_builder->flush());
}
template <typename Impl>
void module<Impl>::set_visible(bool value) {
m_log.notice("%s: Visibility changed (state=%s)", m_name, value ? "shown" : "hidden");
m_visible = value;
broadcast();
}
template <typename Impl>
void module<Impl>::action_module_toggle() {
set_visible(!m_visible);
}
template <typename Impl>
void module<Impl>::action_module_show() {
set_visible(true);
}
template <typename Impl>
void module<Impl>::action_module_hide() {
set_visible(false);
}
// }}}
} // namespace modules
POLYBAR_NS_END