From 2c68554629e83679cc935e3b4eae31173441a6cc Mon Sep 17 00:00:00 2001 From: david072 Date: Fri, 17 Nov 2023 19:33:01 +0100 Subject: [PATCH] Calendar/AddEventDialog: Use the DatePicker to pick dates --- .../Applications/Calendar/AddEventDialog.cpp | 78 ++++++------------- .../Applications/Calendar/AddEventDialog.gml | 44 ++++------- 2 files changed, 39 insertions(+), 83 deletions(-) diff --git a/Userland/Applications/Calendar/AddEventDialog.cpp b/Userland/Applications/Calendar/AddEventDialog.cpp index 0bf1244ea0..3f17f19dc0 100644 --- a/Userland/Applications/Calendar/AddEventDialog.cpp +++ b/Userland/Applications/Calendar/AddEventDialog.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2019-2020, Ryan Grieb * Copyright (c) 2022-2023, the SerenityOS developers. + * Copyright (c) 2023, David Ganz * * SPDX-License-Identifier: BSD-2-Clause */ @@ -11,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -44,15 +46,19 @@ AddEventDialog::AddEventDialog(Core::DateTime date_time, EventManager& event_man auto event_title_textbox = widget->find_descendant_of_type_named("event_title_textbox"); event_title_textbox->set_focus(true); - auto starting_month_input = widget->find_descendant_of_type_named("start_month"); - starting_month_input->set_model(GUI::MonthListModel::create()); - starting_month_input->set_selected_index(m_start_date_time.month() - 1); + auto start_date_box = widget->find_descendant_of_type_named("start_date"); + start_date_box->set_text(MUST(m_start_date_time.to_string("%Y-%m-%d"sv))); - auto starting_day_input = widget->find_descendant_of_type_named("start_day"); - starting_day_input->set_value(m_start_date_time.day()); + auto calendar_date_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/calendar-date.png"sv).release_value_but_fixme_should_propagate_errors(); - auto starting_year_input = widget->find_descendant_of_type_named("start_year"); - starting_year_input->set_value(m_start_date_time.year()); + auto& pick_start_date_button = *widget->find_descendant_of_type_named("pick_start_date"); + pick_start_date_button.set_icon(calendar_date_icon); + pick_start_date_button.on_click = [&](auto) { + if (auto new_date = GUI::DatePicker::show(this, "Pick Start Date"_string, m_start_date_time); new_date.has_value()) { + m_start_date_time.set_date(new_date.release_value()); + start_date_box->set_text(MUST(m_start_date_time.to_string("%Y-%m-%d"sv))); + } + }; auto starting_hour_input = widget->find_descendant_of_type_named("start_hour"); starting_hour_input->set_value(m_start_date_time.hour()); @@ -64,15 +70,17 @@ AddEventDialog::AddEventDialog(Core::DateTime date_time, EventManager& event_man starting_meridiem_input->set_model(MeridiemListModel::create()); starting_meridiem_input->set_selected_index(0); - auto ending_month_input = widget->find_descendant_of_type_named("end_month"); - ending_month_input->set_model(GUI::MonthListModel::create()); - ending_month_input->set_selected_index(m_end_date_time.month() - 1); + auto end_date_box = widget->find_descendant_of_type_named("end_date"); + end_date_box->set_text(MUST(m_start_date_time.to_string("%Y-%m-%d"sv))); - auto ending_day_input = widget->find_descendant_of_type_named("end_day"); - ending_day_input->set_value(m_end_date_time.day()); - - auto ending_year_input = widget->find_descendant_of_type_named("end_year"); - ending_year_input->set_value(m_end_date_time.year()); + auto& pick_end_date_button = *widget->find_descendant_of_type_named("pick_end_date"); + pick_end_date_button.set_icon(calendar_date_icon); + pick_end_date_button.on_click = [&](auto) { + if (auto new_date = GUI::DatePicker::show(this, "Pick End Date"_string, m_end_date_time); new_date.has_value()) { + m_end_date_time.set_date(new_date.release_value()); + end_date_box->set_text(MUST(m_end_date_time.to_string("%Y-%m-%d"sv))); + } + }; auto ending_hour_input = widget->find_descendant_of_type_named("end_hour"); ending_hour_input->set_value(m_end_date_time.hour()); @@ -91,57 +99,21 @@ AddEventDialog::AddEventDialog(Core::DateTime date_time, EventManager& event_man done(ExecResult::OK); }; - auto update_starting_day_range = [=]() { - auto year = starting_year_input->value(); - auto month = starting_month_input->selected_index(); - - starting_day_input->set_range(1, days_in_month(year, month + 1)); - }; - auto update_ending_day_range = [=]() { - auto year = ending_year_input->value(); - auto month = ending_month_input->selected_index(); - - ending_day_input->set_range(1, days_in_month(year, month + 1)); - }; auto update_starting_input_values = [=, this]() { - auto year = starting_year_input->value(); - auto month = starting_month_input->selected_index() + 1; - auto day = starting_day_input->value(); auto hour = starting_hour_input->value(); auto minute = starting_minute_input->value(); - m_start_date_time = Core::DateTime::create(year, month, day, hour, minute); + m_start_date_time.set_time_only(hour, minute); }; auto update_ending_input_values = [=, this]() { - auto year = ending_year_input->value(); - auto month = ending_month_input->selected_index() + 1; - auto day = ending_day_input->value(); auto hour = ending_hour_input->value(); auto minute = ending_minute_input->value(); - m_end_date_time = Core::DateTime::create(year, month, day, hour, minute); + m_end_date_time.set_time_only(hour, minute); }; - starting_year_input->on_change = [update_starting_input_values, update_starting_day_range](auto) { - update_starting_input_values(); - update_starting_day_range(); - }; - starting_month_input->on_change = [update_starting_input_values, update_starting_day_range](auto, auto) { - update_starting_input_values(); - update_starting_day_range(); - }; - starting_day_input->on_change = [update_starting_input_values](auto) { update_starting_input_values(); }; starting_hour_input->on_change = [update_starting_input_values](auto) { update_starting_input_values(); }; starting_minute_input->on_change = [update_starting_input_values](auto) { update_starting_input_values(); }; - ending_year_input->on_change = [update_ending_input_values, update_ending_day_range](auto) { - update_ending_input_values(); - update_ending_day_range(); - }; - ending_month_input->on_change = [update_ending_input_values, update_ending_day_range](auto, auto) { - update_ending_input_values(); - update_ending_day_range(); - }; - ending_day_input->on_change = [update_ending_input_values](auto) { update_ending_input_values(); }; ending_hour_input->on_change = [update_ending_input_values](auto) { update_ending_input_values(); }; ending_minute_input->on_change = [update_ending_input_values](auto) { update_ending_input_values(); }; } diff --git a/Userland/Applications/Calendar/AddEventDialog.gml b/Userland/Applications/Calendar/AddEventDialog.gml index 939acbb6e5..0f560aaa29 100644 --- a/Userland/Applications/Calendar/AddEventDialog.gml +++ b/Userland/Applications/Calendar/AddEventDialog.gml @@ -36,23 +36,15 @@ font_weight: "Bold" } - @GUI::ComboBox { - name: "start_month" - model_only: true - fixed_size: [50, 20] + @GUI::TextBox { + name: "start_date" + mode: "ReadOnly" + fixed_width: 80 } - @GUI::SpinBox { - name: "start_day" - fixed_size: [40, 20] - min: 1 - } - - @GUI::SpinBox { - name: "start_year" - fixed_size: [55, 20] - min: 0 - max: 9999 + @GUI::Button { + name: "pick_start_date" + fixed_width: 20 } @GUI::SpinBox { @@ -90,23 +82,15 @@ font_weight: "Bold" } - @GUI::ComboBox { - name: "end_month" - model_only: true - fixed_size: [50, 20] + @GUI::TextBox { + name: "end_date" + mode: "ReadOnly" + fixed_width: 80 } - @GUI::SpinBox { - name: "end_day" - fixed_size: [40, 20] - min: 1 - } - - @GUI::SpinBox { - name: "end_year" - fixed_size: [55, 20] - min: 0 - max: 9999 + @GUI::Button { + name: "pick_end_date" + fixed_width: 20 } @GUI::SpinBox {