mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +00:00
KeyboardMapper: Ask for unsaved changes on window close and file open
This commit is contained in:
parent
ae9e4c6f26
commit
b6510f8e76
3 changed files with 27 additions and 0 deletions
|
@ -21,6 +21,22 @@ KeyboardMapperWidget::KeyboardMapperWidget()
|
||||||
create_frame();
|
create_frame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool KeyboardMapperWidget::request_close()
|
||||||
|
{
|
||||||
|
if (!window()->is_modified())
|
||||||
|
return true;
|
||||||
|
auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_filename);
|
||||||
|
if (result == GUI::MessageBox::ExecYes) {
|
||||||
|
ErrorOr<void> error_or = save();
|
||||||
|
if (error_or.is_error())
|
||||||
|
show_error_to_user(error_or.error());
|
||||||
|
|
||||||
|
if (!window()->is_modified())
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return result == GUI::MessageBox::ExecNo;
|
||||||
|
}
|
||||||
|
|
||||||
void KeyboardMapperWidget::create_frame()
|
void KeyboardMapperWidget::create_frame()
|
||||||
{
|
{
|
||||||
set_fill_with_background_color(true);
|
set_fill_with_background_color(true);
|
||||||
|
|
|
@ -25,6 +25,8 @@ public:
|
||||||
void show_error_to_user(Error);
|
void show_error_to_user(Error);
|
||||||
void set_automatic_modifier(bool checked);
|
void set_automatic_modifier(bool checked);
|
||||||
|
|
||||||
|
bool request_close();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||||
virtual void keyup_event(GUI::KeyEvent&) override;
|
virtual void keyup_event(GUI::KeyEvent&) override;
|
||||||
|
|
|
@ -47,6 +47,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
auto open_action = GUI::CommonActions::make_open_action(
|
auto open_action = GUI::CommonActions::make_open_action(
|
||||||
[&](auto&) {
|
[&](auto&) {
|
||||||
|
if (!keyboard_mapper_widget->request_close())
|
||||||
|
return;
|
||||||
|
|
||||||
Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open", "/res/keymaps/");
|
Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open", "/res/keymaps/");
|
||||||
if (!path.has_value())
|
if (!path.has_value())
|
||||||
return;
|
return;
|
||||||
|
@ -99,6 +102,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
auto& help_menu = window->add_menu("&Help");
|
auto& help_menu = window->add_menu("&Help");
|
||||||
help_menu.add_action(GUI::CommonActions::make_about_action("Keyboard Mapper", app_icon, window));
|
help_menu.add_action(GUI::CommonActions::make_about_action("Keyboard Mapper", app_icon, window));
|
||||||
|
|
||||||
|
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
|
||||||
|
if (keyboard_mapper_widget->request_close())
|
||||||
|
return GUI::Window::CloseRequestDecision::Close;
|
||||||
|
return GUI::Window::CloseRequestDecision::StayOpen;
|
||||||
|
};
|
||||||
|
|
||||||
window->show();
|
window->show();
|
||||||
|
|
||||||
return app->exec();
|
return app->exec();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue