summaryrefslogtreecommitdiff
path: root/Userland/Services/Taskbar/ClockWidget.cpp
blob: 87a02825309fe301600060ab86567bccebe77d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ClockWidget.h"
#include <LibCore/Process.h>
#include <LibGUI/Painter.h>
#include <LibGUI/SeparatorWidget.h>
#include <LibGUI/Window.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>

namespace Taskbar {

ClockWidget::ClockWidget()
{
    set_frame_shape(Gfx::FrameShape::Box);
    set_frame_shadow(Gfx::FrameShadow::Sunken);
    set_frame_thickness(1);

    m_time_width = font().width("22:22:22");

    set_fixed_size(m_time_width + 20, 21);

    m_timer = add<Core::Timer>(1000, [this] {
        static time_t last_update_time;
        time_t now = time(nullptr);
        if (now != last_update_time) {
            tick_clock();
            last_update_time = now;
            set_tooltip(Core::DateTime::now().to_string("%Y-%m-%d"));
        }
    });

    m_calendar_window = add<GUI::Window>(window());
    m_calendar_window->resize(m_window_size.width(), m_window_size.height());
    m_calendar_window->set_frameless(true);
    m_calendar_window->set_resizable(false);
    m_calendar_window->set_minimizable(false);
    m_calendar_window->on_active_input_change = [this](bool is_active_input) {
        if (!is_active_input)
            close();
    };

    auto& root_container = m_calendar_window->set_main_widget<GUI::Frame>();
    root_container.set_fill_with_background_color(true);
    root_container.set_layout<GUI::VerticalBoxLayout>();
    root_container.layout()->set_margins({ 2, 0 });
    root_container.layout()->set_spacing(0);
    root_container.set_frame_shape(Gfx::FrameShape::Window);

    auto& navigation_container = root_container.add<GUI::Widget>();
    navigation_container.set_fixed_height(24);
    navigation_container.set_layout<GUI::HorizontalBoxLayout>();
    navigation_container.layout()->set_margins({ 2 });

    m_prev_date = navigation_container.add<GUI::Button>();
    m_prev_date->set_button_style(Gfx::ButtonStyle::Coolbar);
    m_prev_date->set_fixed_size(24, 24);
    m_prev_date->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-back.png").release_value_but_fixme_should_propagate_errors());
    m_prev_date->on_click = [&](auto) {
        unsigned view_month = m_calendar->view_month();
        unsigned view_year = m_calendar->view_year();
        if (m_calendar->mode() == GUI::Calendar::Month) {
            view_month--;
            if (m_calendar->view_month() == 1) {
                view_month = 12;
                view_year--;
            }
        } else {
            view_year--;
        }
        m_calendar->update_tiles(view_year, view_month);
        if (m_calendar->mode() == GUI::Calendar::Year)
            m_selected_calendar_button->set_text(m_calendar->formatted_date(GUI::Calendar::YearOnly));
        else
            m_selected_calendar_button->set_text(m_calendar->formatted_date());
    };

    m_selected_calendar_button = navigation_container.add<GUI::Button>();
    m_selected_calendar_button->set_button_style(Gfx::ButtonStyle::Coolbar);
    m_selected_calendar_button->set_fixed_height(24);
    m_selected_calendar_button->on_click = [&](auto) {
        m_calendar->toggle_mode();
        if (m_calendar->mode() == GUI::Calendar::Year)
            m_selected_calendar_button->set_text(m_calendar->formatted_date(GUI::Calendar::YearOnly));
        else
            m_selected_calendar_button->set_text(m_calendar->formatted_date());
    };

    m_next_date = navigation_container.add<GUI::Button>();
    m_next_date->set_button_style(Gfx::ButtonStyle::Coolbar);
    m_next_date->set_fixed_size(24, 24);
    m_next_date->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors());
    m_next_date->on_click = [&](auto) {
        unsigned view_month = m_calendar->view_month();
        unsigned view_year = m_calendar->view_year();
        if (m_calendar->mode() == GUI::Calendar::Month) {
            view_month++;
            if (m_calendar->view_month() == 12) {
                view_month = 1;
                view_year++;
            }
        } else {
            view_year++;
        }
        m_calendar->update_tiles(view_year, view_month);
        if (m_calendar->mode() == GUI::Calendar::Year)
            m_selected_calendar_button->set_text(m_calendar->formatted_date(GUI::Calendar::YearOnly));
        else
            m_selected_calendar_button->set_text(m_calendar->formatted_date());
    };

    auto& separator1 = root_container.add<GUI::HorizontalSeparator>();
    separator1.set_fixed_height(2);

    auto& calendar_container = root_container.add<GUI::Widget>();
    calendar_container.set_layout<GUI::HorizontalBoxLayout>();
    calendar_container.layout()->set_margins({ 2 });

    m_calendar = calendar_container.add<GUI::Calendar>();
    m_selected_calendar_button->set_text(m_calendar->formatted_date());

    m_calendar->on_tile_click = [&] {
        m_selected_calendar_button->set_text(m_calendar->formatted_date());
    };

    m_calendar->on_month_click = [&] {
        m_selected_calendar_button->set_text(m_calendar->formatted_date());
    };

    auto& separator2 = root_container.add<GUI::HorizontalSeparator>();
    separator2.set_fixed_height(2);

    auto& settings_container = root_container.add<GUI::Widget>();
    settings_container.set_fixed_height(24);
    settings_container.set_layout<GUI::HorizontalBoxLayout>();
    settings_container.layout()->set_margins({ 2 });
    settings_container.layout()->add_spacer();

    m_jump_to_button = settings_container.add<GUI::Button>();
    m_jump_to_button->set_button_style(Gfx::ButtonStyle::Coolbar);
    m_jump_to_button->set_fixed_size(24, 24);
    m_jump_to_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/calendar-date.png").release_value_but_fixme_should_propagate_errors());
    m_jump_to_button->set_tooltip("Jump to today");
    m_jump_to_button->on_click = [this](auto) {
        jump_to_current_date();
    };

    m_calendar_launcher = settings_container.add<GUI::Button>();
    m_calendar_launcher->set_button_style(Gfx::ButtonStyle::Coolbar);
    m_calendar_launcher->set_fixed_size(24, 24);
    m_calendar_launcher->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-calendar.png").release_value_but_fixme_should_propagate_errors());
    m_calendar_launcher->set_tooltip("Calendar");
    m_calendar_launcher->on_click = [](auto) {
        Core::Process::spawn("/bin/Calendar"sv);
    };
}

void ClockWidget::paint_event(GUI::PaintEvent& event)
{
    GUI::Frame::paint_event(event);
    auto time_text = Core::DateTime::now().to_string("%T");
    GUI::Painter painter(*this);
    painter.add_clip_rect(frame_inner_rect());

    // Render string center-left aligned, but attempt to center the string based on a constant
    // "ideal" time string (i.e., the same one used to size this widget in the initializer).
    // This prevents the rest of the string from shifting around while seconds tick.
    Gfx::Font const& font = Gfx::FontDatabase::default_font();
    int const frame_width = frame_thickness();
    int const ideal_width = m_time_width;
    int const widget_width = max_width();
    int const translation_x = (widget_width - ideal_width) / 2 - frame_width;

    painter.draw_text(frame_inner_rect().translated(translation_x, frame_width), time_text, font, Gfx::TextAlignment::CenterLeft, palette().window_text());
}

void ClockWidget::mousedown_event(GUI::MouseEvent& event)
{
    if (event.button() != GUI::MouseButton::Primary) {
        return;
    } else {
        if (!m_calendar_window->is_visible())
            open();
        else
            close();
    }
}

void ClockWidget::open()
{
    jump_to_current_date();
    position_calendar_window();
    m_calendar_window->show();
}

void ClockWidget::close()
{
    m_calendar_window->hide();
}

void ClockWidget::position_calendar_window()
{
    constexpr auto taskbar_top_padding { 4 };
    m_calendar_window->set_rect(
        screen_relative_rect().right() - m_calendar_window->width() + 1,
        screen_relative_rect().top() - taskbar_top_padding - m_calendar_window->height(),
        m_window_size.width(),
        m_window_size.height());
}

void ClockWidget::jump_to_current_date()
{
    if (m_calendar->mode() == GUI::Calendar::Year)
        m_calendar->toggle_mode();
    m_calendar->set_selected_date(Core::DateTime::now());
    m_calendar->update_tiles(Core::DateTime::now().year(), Core::DateTime::now().month());
    m_selected_calendar_button->set_text(m_calendar->formatted_date());
}

}