From 28bad49ed474c686b9caaa639f5fe7304f1aeed0 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 30 Dec 2020 23:46:38 +0000 Subject: [PATCH] DevTools: Add open, save as and quit actions to GML playground --- DevTools/Playground/main.cpp | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/DevTools/Playground/main.cpp b/DevTools/Playground/main.cpp index 4973e752ef..fa39dbcac8 100644 --- a/DevTools/Playground/main.cpp +++ b/DevTools/Playground/main.cpp @@ -24,14 +24,20 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include +#include #include #include #include +#include +#include +#include #include #include #include +#include class GMLAutocompleteProvider final : public virtual GUI::AutocompleteProvider { public: @@ -188,6 +194,44 @@ int main(int argc, char** argv) preview.load_from_gml(editor.text()); }; + auto menubar = GUI::MenuBar::construct(); + auto& app_menu = menubar->add_menu("GML Playground"); + + app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) { + Optional open_path = GUI::FilePicker::get_open_filepath(window); + + if (!open_path.has_value()) + return; + + auto file = Core::File::construct(open_path.value()); + if (!file->open(Core::IODevice::ReadOnly) && file->error() != ENOENT) { + GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: {}", open_path.value(), strerror(errno)), "Error", GUI::MessageBox::Type::Error); + return; + } + + editor.set_text(file->read_all()); + editor.set_focus(true); + })); + + app_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) { + Optional save_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "gml"); + if (!save_path.has_value()) + return; + + if (!editor.write_to_file(save_path.value())) { + GUI::MessageBox::show(window, "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error); + return; + } + })); + + app_menu.add_separator(); + + app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { + app->quit(); + })); + + app->set_menubar(move(menubar)); + window->show(); return app->exec(); }