TaskBar: Cleanup TaskBar string generation

This commit is contained in:
Bananymous 2024-11-08 02:57:35 +02:00
parent da8170c5b6
commit cf21eb4b39
1 changed files with 14 additions and 6 deletions

View File

@ -3,6 +3,17 @@
#include <time.h> #include <time.h>
static BAN::String get_task_bar_string()
{
BAN::String result;
const time_t current_time = time(nullptr);
if (!result.append(ctime(&current_time)).is_error())
result.pop_back();
return result;
}
int main() int main()
{ {
constexpr uint32_t padding = 3; constexpr uint32_t padding = 3;
@ -30,21 +41,18 @@ int main()
bool is_running = true; bool is_running = true;
time_t last_update;
const auto update_time_string = const auto update_time_string =
[&]() [&]()
{ {
last_update = time(nullptr); const auto text = get_task_bar_string();
BAN::StringView time_sv = ctime(&last_update);
time_sv = time_sv.substring(0, time_sv.size() - 1); // new line
const uint32_t text_w = time_sv.size() * font.width(); const uint32_t text_w = text.size() * font.width();
const uint32_t text_h = font.height(); const uint32_t text_h = font.height();
const uint32_t text_x = window->width() - text_w - padding; const uint32_t text_x = window->width() - text_w - padding;
const uint32_t text_y = padding; const uint32_t text_y = padding;
window->fill_rect(text_x, text_y, text_w, text_h, bg_color); window->fill_rect(text_x, text_y, text_w, text_h, bg_color);
window->draw_text(time_sv, font, text_x, text_y, fg_color); window->draw_text(text, font, text_x, text_y, fg_color);
if (!window->invalidate(text_x, text_y, text_w, text_h)) if (!window->invalidate(text_x, text_y, text_w, text_h))
is_running = false; is_running = false;
}; };