Skip to content

Commit 9a10183

Browse files
implicitfieldalimpfard
authored andcommitted
LibGUI+Calendar: Move date control logic to the calendar widget
1 parent c625ba3 commit 9a10183

File tree

4 files changed

+39
-48
lines changed

4 files changed

+39
-48
lines changed

Userland/Applications/Calendar/main.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
4949
auto calendar = main_widget->find_descendant_of_type_named<GUI::Calendar>("calendar");
5050

5151
auto prev_date_action = GUI::Action::create({}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"sv)), [&](const GUI::Action&) {
52-
unsigned view_month = calendar->view_month();
53-
unsigned view_year = calendar->view_year();
54-
if (calendar->mode() == GUI::Calendar::Month) {
55-
view_month--;
56-
if (calendar->view_month() == 1) {
57-
view_month = 12;
58-
view_year--;
59-
}
60-
} else {
61-
view_year--;
62-
}
63-
calendar->update_tiles(view_year, view_month);
52+
calendar->show_previous_date();
6453
});
6554

6655
auto next_date_action = GUI::Action::create({}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"sv)), [&](const GUI::Action&) {
67-
unsigned view_month = calendar->view_month();
68-
unsigned view_year = calendar->view_year();
69-
if (calendar->mode() == GUI::Calendar::Month) {
70-
view_month++;
71-
if (calendar->view_month() == 12) {
72-
view_month = 1;
73-
view_year++;
74-
}
75-
} else {
76-
view_year++;
77-
}
78-
calendar->update_tiles(view_year, view_month);
56+
calendar->show_next_date();
7957
});
8058

8159
auto add_event_action = GUI::Action::create("&Add Event", {}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/add-event.png"sv)), [&](const GUI::Action&) {

Userland/Libraries/LibGUI/Calendar.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,38 @@ void Calendar::toggle_mode()
8282
invalidate_layout();
8383
}
8484

85+
void Calendar::show_previous_date()
86+
{
87+
unsigned view_month = m_view_month;
88+
unsigned view_year = m_view_year;
89+
if (m_mode == GUI::Calendar::Month) {
90+
--view_month;
91+
if (view_month == 0) {
92+
view_month = 12;
93+
--view_year;
94+
}
95+
} else {
96+
--view_year;
97+
}
98+
update_tiles(view_year, view_month);
99+
}
100+
101+
void Calendar::show_next_date()
102+
{
103+
unsigned view_month = m_view_month;
104+
unsigned view_year = m_view_year;
105+
if (m_mode == GUI::Calendar::Month) {
106+
++view_month;
107+
if (view_month == 13) {
108+
view_month = 1;
109+
++view_year;
110+
}
111+
} else {
112+
++view_year;
113+
}
114+
update_tiles(view_year, view_month);
115+
}
116+
85117
void Calendar::resize_event(GUI::ResizeEvent& event)
86118
{
87119
m_event_size.set_width(event.size().width() - (frame_thickness() * 2));

Userland/Libraries/LibGUI/Calendar.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class Calendar final
6464
void set_show_days_of_the_week(bool b) { m_show_days = b; }
6565
bool is_showing_days_of_the_week() const { return m_show_days; }
6666

67+
void show_previous_date();
68+
void show_next_date();
69+
6770
Gfx::IntSize unadjusted_tile_size() const { return m_unadjusted_tile_size; }
6871
void set_unadjusted_tile_size(int width, int height)
6972
{

Userland/Services/Taskbar/ClockWidget.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,7 @@ ClockWidget::ClockWidget()
5353
m_prev_date->set_fixed_size(24, 24);
5454
m_prev_date->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"sv).release_value_but_fixme_should_propagate_errors());
5555
m_prev_date->on_click = [&](auto) {
56-
unsigned view_month = m_calendar->view_month();
57-
unsigned view_year = m_calendar->view_year();
58-
if (m_calendar->mode() == GUI::Calendar::Month) {
59-
view_month--;
60-
if (m_calendar->view_month() == 1) {
61-
view_month = 12;
62-
view_year--;
63-
}
64-
} else {
65-
view_year--;
66-
}
67-
m_calendar->update_tiles(view_year, view_month);
56+
m_calendar->show_previous_date();
6857
if (m_calendar->mode() == GUI::Calendar::Year)
6958
m_selected_calendar_button->set_text(m_calendar->formatted_date(GUI::Calendar::YearOnly).release_value_but_fixme_should_propagate_errors());
7059
else
@@ -87,18 +76,7 @@ ClockWidget::ClockWidget()
8776
m_next_date->set_fixed_size(24, 24);
8877
m_next_date->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"sv).release_value_but_fixme_should_propagate_errors());
8978
m_next_date->on_click = [&](auto) {
90-
unsigned view_month = m_calendar->view_month();
91-
unsigned view_year = m_calendar->view_year();
92-
if (m_calendar->mode() == GUI::Calendar::Month) {
93-
view_month++;
94-
if (m_calendar->view_month() == 12) {
95-
view_month = 1;
96-
view_year++;
97-
}
98-
} else {
99-
view_year++;
100-
}
101-
m_calendar->update_tiles(view_year, view_month);
79+
m_calendar->show_next_date();
10280
if (m_calendar->mode() == GUI::Calendar::Year)
10381
m_selected_calendar_button->set_text(m_calendar->formatted_date(GUI::Calendar::YearOnly).release_value_but_fixme_should_propagate_errors());
10482
else

0 commit comments

Comments
 (0)