Luna/apps/clock.cpp

44 lines
911 B
C++
Raw Normal View History

#include <os/Timer.h>
2023-10-09 20:05:30 +00:00
#include <time.h>
#include <ui/App.h>
#include <ui/Label.h>
ui::Label* g_label;
void update_time()
{
time_t t = time(NULL);
struct tm* tp = localtime(&t);
static char buf[2048];
strftime(buf, sizeof(buf), "%H:%M:%S", tp);
g_label->set_text(StringView { buf });
ui::App::the().main_window()->draw();
}
Result<int> luna_main(int, char**)
2023-10-09 20:05:30 +00:00
{
ui::App app;
TRY(app.init());
2023-10-09 20:05:30 +00:00
auto* window = TRY(ui::Window::create(ui::Rect { 500, 400, 100, 50 }));
app.set_main_window(window);
window->set_title("Clock");
window->set_background(ui::GRAY);
g_label = TRY(window->create_main_widget<ui::Label>());
g_label->set_text("00:00:00");
g_label->set_font(ui::Font::default_bold_font());
g_label->set_color(ui::BLACK);
2023-10-09 20:05:30 +00:00
update_time();
auto timer = TRY(os::Timer::create_repeating(1000, update_time));
2023-10-09 20:05:30 +00:00
return app.run();
}