TextEditor: Ask the user before closing a dirty (modified) document

It's a little unfortunate that we have two separate code paths that can
lead to asking the user about this. Longer-term we should find a way to
unify these things.

Fixes #491.
This commit is contained in:
Andreas Kling 2019-08-27 20:37:41 +02:00
parent c4b1456c88
commit b1763238d7
Notes: sideshowbarker 2024-07-19 12:29:08 +09:00
3 changed files with 19 additions and 4 deletions

View File

@ -55,7 +55,6 @@ TextEditorWidget::TextEditorWidget()
GMessageBox::Type::Information,
GMessageBox::InputType::OK, window());
}
});
m_find_previous_action = GAction::create("Find previous", { Mod_Ctrl | Mod_Shift, Key_G }, [&](auto&) {
auto needle = m_find_textbox->text();
@ -171,9 +170,10 @@ TextEditorWidget::TextEditorWidget()
app_menu->add_action(*m_save_action);
app_menu->add_action(*m_save_as_action);
app_menu->add_separator();
app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) {
app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [this](const GAction&) {
if (!request_close())
return;
GApplication::the().quit(0);
return;
}));
menubar->add_menu(move(app_menu));
@ -200,7 +200,6 @@ TextEditorWidget::TextEditorWidget()
});
menubar->add_menu(move(font_menu));
auto view_menu = make<GMenu>("View");
view_menu->add_action(*m_line_wrapping_setting_action);
menubar->add_menu(move(view_menu));
@ -265,3 +264,12 @@ void TextEditorWidget::open_sesame(const String& path)
m_editor->set_text(file.read_all());
set_path(FileSystemPath(path));
}
bool TextEditorWidget::request_close()
{
if (!m_document_dirty)
return true;
GMessageBox box("The document has been modified. Quit without saving?", "Quit without saving?", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel, window());
auto result = box.exec();
return result == GMessageBox::ExecOK;
}

View File

@ -16,6 +16,7 @@ public:
TextEditorWidget();
virtual ~TextEditorWidget() override;
void open_sesame(const String& path);
bool request_close();
private:
void set_path(const FileSystemPath& file);

View File

@ -12,6 +12,12 @@ int main(int argc, char** argv)
auto* text_widget = new TextEditorWidget();
window->set_main_widget(text_widget);
window->on_close_request = [&]() -> GWindow::CloseRequestDecision {
if (text_widget->request_close())
return GWindow::CloseRequestDecision::Close;
return GWindow::CloseRequestDecision::StayOpen;
};
if (argc >= 2)
text_widget->open_sesame(argv[1]);