diff --git a/Userland/Applications/Calendar/CalendarWidget.cpp b/Userland/Applications/Calendar/CalendarWidget.cpp index 68b7c6a48c..bf32d21066 100644 --- a/Userland/Applications/Calendar/CalendarWidget.cpp +++ b/Userland/Applications/Calendar/CalendarWidget.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2023, the SerenityOS developers. + * Copyright (c) 2023, David Ganz * * SPDX-License-Identifier: BSD-2-Clause */ @@ -93,10 +94,14 @@ ErrorOr> 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> 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 = [&]() { diff --git a/Userland/Applications/Calendar/CalendarWidget.h b/Userland/Applications/Calendar/CalendarWidget.h index 383542535e..9b67efafd6 100644 --- a/Userland/Applications/Calendar/CalendarWidget.h +++ b/Userland/Applications/Calendar/CalendarWidget.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2023, the SerenityOS developers. + * Copyright (c) 2023, David Ganz * * 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> create_open_settings_action(); OwnPtr m_view_type_action_group; + RefPtr m_save_action; RefPtr m_event_calendar; }; diff --git a/Userland/Applications/Calendar/main.cpp b/Userland/Applications/Calendar/main.cpp index 1039c0bb96..05ea47935b 100644 --- a/Userland/Applications/Calendar/main.cpp +++ b/Userland/Applications/Calendar/main.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2019-2020, Ryan Grieb + * Copyright (c) 2023, David Ganz * * SPDX-License-Identifier: BSD-2-Clause */ @@ -67,6 +68,12 @@ ErrorOr 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()) {