Calendar: Ask about unsaved changes when closing the window :^)

The Calendar now asks about unsaved changes in the calendar when
attempting to close the window.
This commit is contained in:
david072 2023-11-17 21:04:40 +01:00 committed by Andrew Kaster
parent 75faa9239a
commit b657fa6f95
3 changed files with 34 additions and 1 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, the SerenityOS developers.
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -93,10 +94,14 @@ ErrorOr<NonnullRefPtr<CalendarWidget>> CalendarWidget::create(GUI::Window* paren
file_menu->add_separator();
file_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
file_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
if (!widget->request_close())
return;
GUI::Application::the()->quit();
}));
widget->m_save_action = save_action;
auto event_menu = parent_window->add_menu("&Event"_string);
event_menu->add_action(add_event_action);
@ -114,6 +119,23 @@ ErrorOr<NonnullRefPtr<CalendarWidget>> CalendarWidget::create(GUI::Window* paren
return widget;
}
bool CalendarWidget::request_close()
{
if (!m_event_calendar->event_manager().is_dirty())
return true;
auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_event_calendar->event_manager().current_filename());
if (result == GUI::MessageBox::ExecResult::Yes) {
m_save_action->activate();
return !m_event_calendar->event_manager().is_dirty();
}
if (result == GUI::MessageBox::ExecResult::No)
return true;
return false;
}
void CalendarWidget::create_on_events_change()
{
m_event_calendar->event_manager().on_events_change = [&]() {

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, the SerenityOS developers.
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -24,6 +25,8 @@ public:
void update_window_title();
void load_file(FileSystemAccessClient::File file);
bool request_close();
private:
void create_on_tile_doubleclick();
@ -43,6 +46,7 @@ private:
ErrorOr<NonnullRefPtr<GUI::Action>> create_open_settings_action();
OwnPtr<GUI::ActionGroup> m_view_type_action_group;
RefPtr<GUI::Action> m_save_action;
RefPtr<EventCalendar> m_event_calendar;
};

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2019-2020, Ryan Grieb <ryan.m.grieb@gmail.com>
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -67,6 +68,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto calendar_widget = TRY(Calendar::CalendarWidget::create(window));
window->set_main_widget(calendar_widget);
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
if (calendar_widget->request_close())
return GUI::Window::CloseRequestDecision::Close;
return GUI::Window::CloseRequestDecision::StayOpen;
};
window->show();
if (!filename.is_empty()) {