56 lines
1.2 KiB
C++
56 lines
1.2 KiB
C++
#include "Date.hpp"
|
|
|
|
REGISTER_WIDGET(frame::widgets::Date, "Date");
|
|
|
|
#include "../../font/FontRegistry.hpp"
|
|
|
|
#include <ctime>
|
|
#include <fmt/chrono.h>
|
|
#include <fmt/format.h>
|
|
|
|
namespace frame::widgets
|
|
{
|
|
|
|
Widget::shared_ptr Date::Create()
|
|
{
|
|
return std::make_shared<Date>();
|
|
}
|
|
|
|
void Date::Update()
|
|
{
|
|
auto const now = std::chrono::system_clock::now();
|
|
auto const min = std::chrono::floor<std::chrono::minutes>(now);
|
|
|
|
if(last_time != min)
|
|
{
|
|
setDirty();
|
|
last_time = min;
|
|
}
|
|
}
|
|
|
|
void Date::Render(render::RenderTarget& rt)
|
|
{
|
|
std::time_t t = std::chrono::system_clock::to_time_t(last_time);
|
|
|
|
auto const& size = getSize();
|
|
Rect draw{0, 0, size.x, size.y};
|
|
|
|
auto text = fmt::format("{:%d.%m.%Y}", fmt::localtime(t));
|
|
|
|
auto font = font::GetFont("Fira Code");
|
|
|
|
auto font_size = font->getOptimalSize(text, draw);
|
|
|
|
rt.setInvert(true);
|
|
rt.DrawText(draw,
|
|
text,
|
|
*font,
|
|
font_size,
|
|
AlignHorizontal::CENTER,
|
|
AlignVertical::CENTER);
|
|
rt.setInvert(false);
|
|
|
|
setClear();
|
|
}
|
|
|
|
} // namespace frame::widgets
|