diff options
author | Hüseyin ASLITÜRK <huseyin@asliturk.com> | 2019-12-27 21:17:19 +0300 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-12-27 22:47:31 +0100 |
commit | 3d4dddd1111affc8dff03524f5b0b83d7c1c4b0f (patch) | |
tree | 915ecd929a3cb7888f307f61aefcf4e5c225d9a0 /MenuApplets | |
parent | 0e49c842de67018d0c79e112669b1a52dc59efea (diff) | |
download | serenity-3d4dddd1111affc8dff03524f5b0b83d7c1c4b0f.zip |
MenuApplets: Add Clock applet, move code from WindowServer to the applet.
Diffstat (limited to 'MenuApplets')
-rwxr-xr-x | MenuApplets/Clock/Makefile | 7 | ||||
-rw-r--r-- | MenuApplets/Clock/main.cpp | 78 |
2 files changed, 85 insertions, 0 deletions
diff --git a/MenuApplets/Clock/Makefile b/MenuApplets/Clock/Makefile new file mode 100755 index 0000000000..f68290e9e6 --- /dev/null +++ b/MenuApplets/Clock/Makefile @@ -0,0 +1,7 @@ +OBJS = main.o + +PROGRAM = Clock.MenuApplet + +LIB_DEPS = GUI IPC Draw Core + +include ../../Makefile.common
\ No newline at end of file diff --git a/MenuApplets/Clock/main.cpp b/MenuApplets/Clock/main.cpp new file mode 100644 index 0000000000..3d74554e67 --- /dev/null +++ b/MenuApplets/Clock/main.cpp @@ -0,0 +1,78 @@ +#include <LibCore/CTimer.h> +#include <LibDraw/Palette.h> +#include <LibGUI/GApplication.h> +#include <LibGUI/GPainter.h> +#include <LibGUI/GWidget.h> +#include <LibGUI/GWindow.h> +#include <time.h> + +class ClockWidget final : public GWidget { + C_OBJECT(ClockWidget) +public: + ClockWidget() + : GWidget(nullptr) + { + m_time_width = Font::default_bold_font().width("2222-22-22 22:22:22"); + + m_timer = CTimer::construct(300, [this] { + static time_t last_update_time; + time_t now = time(nullptr); + if (now != last_update_time) { + tick_clock(); + last_update_time = now; + } + }); + } + + virtual ~ClockWidget() override {} + + int get_width() + { + return m_time_width + menubar_menu_margin(); + } + +private: + static int menubar_menu_margin() { return 2; } + + virtual void paint_event(GPaintEvent& event) override + { + time_t now = time(nullptr); + auto* tm = localtime(&now); + + auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u", + tm->tm_year + 1900, + tm->tm_mon + 1, + tm->tm_mday, + tm->tm_hour, + tm->tm_min, + tm->tm_sec); + + GPainter painter(*this); + painter.fill_rect(event.rect(), palette().window()); + painter.draw_text(event.rect(), time_text, Font::default_font(), TextAlignment::Center, palette().window_text()); + } + + void tick_clock() + { + update(); + } + + RefPtr<CTimer> m_timer; + int m_time_width; +}; + +int main(int argc, char** argv) +{ + GApplication app(argc, argv); + + auto window = GWindow::construct(); + window->set_window_type(GWindowType::MenuApplet); + + auto widget = ClockWidget::construct(); + + window->resize(widget->get_width(), 16); + window->set_main_widget(widget); + window->show(); + + return app.exec(); +} |