mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:27:42 +00:00
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:
parent
c4b1456c88
commit
b1763238d7
3 changed files with 19 additions and 4 deletions
|
@ -55,7 +55,6 @@ TextEditorWidget::TextEditorWidget()
|
||||||
GMessageBox::Type::Information,
|
GMessageBox::Type::Information,
|
||||||
GMessageBox::InputType::OK, window());
|
GMessageBox::InputType::OK, window());
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
m_find_previous_action = GAction::create("Find previous", { Mod_Ctrl | Mod_Shift, Key_G }, [&](auto&) {
|
m_find_previous_action = GAction::create("Find previous", { Mod_Ctrl | Mod_Shift, Key_G }, [&](auto&) {
|
||||||
auto needle = m_find_textbox->text();
|
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_action);
|
||||||
app_menu->add_action(*m_save_as_action);
|
app_menu->add_action(*m_save_as_action);
|
||||||
app_menu->add_separator();
|
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);
|
GApplication::the().quit(0);
|
||||||
return;
|
|
||||||
}));
|
}));
|
||||||
menubar->add_menu(move(app_menu));
|
menubar->add_menu(move(app_menu));
|
||||||
|
|
||||||
|
@ -200,7 +200,6 @@ TextEditorWidget::TextEditorWidget()
|
||||||
});
|
});
|
||||||
menubar->add_menu(move(font_menu));
|
menubar->add_menu(move(font_menu));
|
||||||
|
|
||||||
|
|
||||||
auto view_menu = make<GMenu>("View");
|
auto view_menu = make<GMenu>("View");
|
||||||
view_menu->add_action(*m_line_wrapping_setting_action);
|
view_menu->add_action(*m_line_wrapping_setting_action);
|
||||||
menubar->add_menu(move(view_menu));
|
menubar->add_menu(move(view_menu));
|
||||||
|
@ -265,3 +264,12 @@ void TextEditorWidget::open_sesame(const String& path)
|
||||||
m_editor->set_text(file.read_all());
|
m_editor->set_text(file.read_all());
|
||||||
set_path(FileSystemPath(path));
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ public:
|
||||||
TextEditorWidget();
|
TextEditorWidget();
|
||||||
virtual ~TextEditorWidget() override;
|
virtual ~TextEditorWidget() override;
|
||||||
void open_sesame(const String& path);
|
void open_sesame(const String& path);
|
||||||
|
bool request_close();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void set_path(const FileSystemPath& file);
|
void set_path(const FileSystemPath& file);
|
||||||
|
|
|
@ -12,6 +12,12 @@ int main(int argc, char** argv)
|
||||||
auto* text_widget = new TextEditorWidget();
|
auto* text_widget = new TextEditorWidget();
|
||||||
window->set_main_widget(text_widget);
|
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)
|
if (argc >= 2)
|
||||||
text_widget->open_sesame(argv[1]);
|
text_widget->open_sesame(argv[1]);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue