fix(date): Do not trigger updates when unchanged

This commit is contained in:
Michael Carlberg 2016-06-29 11:05:30 +02:00
parent 6329b5635c
commit f2999b7272
2 changed files with 11 additions and 7 deletions

View File

@ -16,7 +16,7 @@ namespace modules
std::string date_detailed;
char date_str[256] = {};
bool detailed = false;
concurrency::Atomic<bool> detailed { false };
public:
explicit DateModule(std::string name);

View File

@ -23,11 +23,18 @@ DateModule::DateModule(std::string name_)
bool DateModule::update()
{
if (!this->formatter->has(TAG_DATE))
return false;
auto date_format = this->detailed ? this->date_detailed : this->date;
auto time = std::time(nullptr);
char new_str[256] = {0,};
std::strftime(new_str, sizeof(this->date_str), date_format.c_str(), std::localtime(&time));
if (this->formatter->has(TAG_DATE))
std::strftime(this->date_str, sizeof(this->date_str), date_format.c_str(), std::localtime(&time));
if (std::strncmp(new_str, this->date_str, sizeof(new_str)) == 0)
return false;
else
std::memmove(this->date_str, new_str, sizeof(new_str));
return true;
}
@ -36,9 +43,7 @@ std::string DateModule::get_output()
{
if (!this->date_detailed.empty())
this->builder->cmd(Cmd::LEFT_CLICK, EVENT_TOGGLE);
this->builder->node(this->Module::get_output());
return this->builder->flush();
}
@ -53,8 +58,7 @@ bool DateModule::handle_command(std::string cmd)
{
if (cmd == EVENT_TOGGLE) {
this->detailed = !this->detailed;
this->broadcast();
this->wakeup();
}
return cmd == EVENT_TOGGLE;
}