mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:17:35 +00:00
Playground: Convert to try_create_default_icon
and sprinkle in some more TRY() statements
This commit is contained in:
parent
64a5f990b1
commit
8513aff1cd
1 changed files with 40 additions and 39 deletions
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Julius Heijmen <julius.heijmen@gmail.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -75,21 +76,21 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
args_parser.add_positional_argument(path, "GML file to edit", "file", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(path, "GML file to edit", "file", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
auto app_icon = GUI::Icon::default_icon("app-playground");
|
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-playground"));
|
||||||
auto window = TRY(GUI::Window::try_create());
|
auto window = TRY(GUI::Window::try_create());
|
||||||
window->set_title("GML Playground");
|
window->set_title("GML Playground");
|
||||||
window->set_icon(app_icon.bitmap_for_size(16));
|
window->set_icon(app_icon.bitmap_for_size(16));
|
||||||
window->resize(800, 600);
|
window->resize(800, 600);
|
||||||
|
|
||||||
auto& splitter = window->set_main_widget<GUI::HorizontalSplitter>();
|
auto splitter = TRY(window->try_set_main_widget<GUI::HorizontalSplitter>());
|
||||||
|
|
||||||
auto& editor = splitter.add<GUI::TextEditor>();
|
auto editor = TRY(splitter->try_add<GUI::TextEditor>());
|
||||||
auto& preview = splitter.add<GUI::Frame>();
|
auto preview = TRY(splitter->try_add<GUI::Frame>());
|
||||||
|
|
||||||
editor.set_syntax_highlighter(make<GUI::GMLSyntaxHighlighter>());
|
editor->set_syntax_highlighter(make<GUI::GMLSyntaxHighlighter>());
|
||||||
editor.set_autocomplete_provider(make<GUI::GMLAutocompleteProvider>());
|
editor->set_autocomplete_provider(make<GUI::GMLAutocompleteProvider>());
|
||||||
editor.set_should_autocomplete_automatically(true);
|
editor->set_should_autocomplete_automatically(true);
|
||||||
editor.set_automatic_indentation_enabled(true);
|
editor->set_automatic_indentation_enabled(true);
|
||||||
|
|
||||||
String file_path;
|
String file_path;
|
||||||
auto update_title = [&] {
|
auto update_title = [&] {
|
||||||
|
@ -107,14 +108,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
};
|
};
|
||||||
|
|
||||||
if (String(path).is_empty()) {
|
if (String(path).is_empty()) {
|
||||||
editor.set_text(R"~~~(@GUI::Frame {
|
editor->set_text(R"~~~(@GUI::Frame {
|
||||||
layout: @GUI::VerticalBoxLayout {
|
layout: @GUI::VerticalBoxLayout {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now add some widgets!
|
// Now add some widgets!
|
||||||
}
|
}
|
||||||
)~~~");
|
)~~~");
|
||||||
editor.set_cursor(4, 28); // after "...widgets!"
|
editor->set_cursor(4, 28); // after "...widgets!"
|
||||||
update_title();
|
update_title();
|
||||||
} else {
|
} else {
|
||||||
auto file = Core::File::construct(path);
|
auto file = Core::File::construct(path);
|
||||||
|
@ -127,30 +128,30 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
file_path = path;
|
file_path = path;
|
||||||
editor.set_text(file->read_all());
|
editor->set_text(file->read_all());
|
||||||
update_title();
|
update_title();
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.on_change = [&] {
|
editor->on_change = [&] {
|
||||||
preview.remove_all_children();
|
preview->remove_all_children();
|
||||||
preview.load_from_gml(editor.text(), [](const String& class_name) -> RefPtr<Core::Object> {
|
preview->load_from_gml(editor->text(), [](const String& class_name) -> RefPtr<Core::Object> {
|
||||||
return UnregisteredWidget::construct(class_name);
|
return UnregisteredWidget::construct(class_name);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
editor.on_modified_change = [&](bool modified) {
|
editor->on_modified_change = [&](bool modified) {
|
||||||
window->set_modified(modified);
|
window->set_modified(modified);
|
||||||
update_title();
|
update_title();
|
||||||
};
|
};
|
||||||
|
|
||||||
auto& file_menu = window->add_menu("&File");
|
auto file_menu = TRY(window->try_add_menu("&File"));
|
||||||
|
|
||||||
auto save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
|
auto save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
|
||||||
Optional<String> new_save_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "gml");
|
Optional<String> new_save_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "gml");
|
||||||
if (!new_save_path.has_value())
|
if (!new_save_path.has_value())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!editor.write_to_file(new_save_path.value())) {
|
if (!editor->write_to_file(new_save_path.value())) {
|
||||||
GUI::MessageBox::show(window, "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
GUI::MessageBox::show(window, "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -160,7 +161,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
auto save_action = GUI::CommonActions::make_save_action([&](auto&) {
|
auto save_action = GUI::CommonActions::make_save_action([&](auto&) {
|
||||||
if (!file_path.is_empty()) {
|
if (!file_path.is_empty()) {
|
||||||
if (!editor.write_to_file(file_path)) {
|
if (!editor->write_to_file(file_path)) {
|
||||||
GUI::MessageBox::show(window, "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
GUI::MessageBox::show(window, "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -171,7 +172,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
save_as_action->activate();
|
save_as_action->activate();
|
||||||
});
|
});
|
||||||
|
|
||||||
file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
|
TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
|
||||||
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window);
|
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window);
|
||||||
|
|
||||||
if (!open_path.has_value())
|
if (!open_path.has_value())
|
||||||
|
@ -196,23 +197,23 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
file_path = open_path.value();
|
file_path = open_path.value();
|
||||||
editor.set_text(file->read_all());
|
editor->set_text(file->read_all());
|
||||||
editor.set_focus(true);
|
editor->set_focus(true);
|
||||||
update_title();
|
update_title();
|
||||||
}));
|
})));
|
||||||
|
|
||||||
file_menu.add_action(save_action);
|
TRY(file_menu->try_add_action(save_action));
|
||||||
file_menu.add_action(save_as_action);
|
TRY(file_menu->try_add_action(save_as_action));
|
||||||
file_menu.add_separator();
|
TRY(file_menu->try_add_separator());
|
||||||
|
|
||||||
file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
|
TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) {
|
||||||
if (window->on_close_request() == GUI::Window::CloseRequestDecision::Close)
|
if (window->on_close_request() == GUI::Window::CloseRequestDecision::Close)
|
||||||
app->quit();
|
app->quit();
|
||||||
}));
|
})));
|
||||||
|
|
||||||
auto& edit_menu = window->add_menu("&Edit");
|
auto edit_menu = TRY(window->try_add_menu("&Edit"));
|
||||||
edit_menu.add_action(GUI::Action::create("&Format GML", { Mod_Ctrl | Mod_Shift, Key_I }, [&](auto&) {
|
TRY(edit_menu->try_add_action(GUI::Action::create("&Format GML", { Mod_Ctrl | Mod_Shift, Key_I }, [&](auto&) {
|
||||||
auto source = editor.text();
|
auto source = editor->text();
|
||||||
GUI::GMLLexer lexer(source);
|
GUI::GMLLexer lexer(source);
|
||||||
for (auto& token : lexer.lex()) {
|
for (auto& token : lexer.lex()) {
|
||||||
if (token.m_type == GUI::GMLToken::Type::Comment) {
|
if (token.m_type == GUI::GMLToken::Type::Comment) {
|
||||||
|
@ -229,7 +230,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
}
|
}
|
||||||
auto formatted_gml = GUI::format_gml(source);
|
auto formatted_gml = GUI::format_gml(source);
|
||||||
if (!formatted_gml.is_null()) {
|
if (!formatted_gml.is_null()) {
|
||||||
editor.set_text(formatted_gml);
|
editor->set_text(formatted_gml);
|
||||||
} else {
|
} else {
|
||||||
GUI::MessageBox::show(
|
GUI::MessageBox::show(
|
||||||
window,
|
window,
|
||||||
|
@ -237,22 +238,22 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
"Error",
|
"Error",
|
||||||
GUI::MessageBox::Type::Error);
|
GUI::MessageBox::Type::Error);
|
||||||
}
|
}
|
||||||
}));
|
})));
|
||||||
|
|
||||||
auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [&](auto& action) {
|
auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [&](auto& action) {
|
||||||
if (action.is_checked())
|
if (action.is_checked())
|
||||||
editor.set_editing_engine(make<GUI::VimEditingEngine>());
|
editor->set_editing_engine(make<GUI::VimEditingEngine>());
|
||||||
else
|
else
|
||||||
editor.set_editing_engine(make<GUI::RegularEditingEngine>());
|
editor->set_editing_engine(make<GUI::RegularEditingEngine>());
|
||||||
});
|
});
|
||||||
vim_emulation_setting_action->set_checked(false);
|
vim_emulation_setting_action->set_checked(false);
|
||||||
edit_menu.add_action(vim_emulation_setting_action);
|
TRY(edit_menu->try_add_action(vim_emulation_setting_action));
|
||||||
|
|
||||||
auto& help_menu = window->add_menu("&Help");
|
auto help_menu = TRY(window->try_add_menu("&Help"));
|
||||||
help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
|
TRY(help_menu->try_add_action(GUI::CommonActions::make_help_action([](auto&) {
|
||||||
Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/Playground.md"), "/bin/Help");
|
Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/Playground.md"), "/bin/Help");
|
||||||
}));
|
})));
|
||||||
help_menu.add_action(GUI::CommonActions::make_about_action("GML Playground", app_icon, window));
|
TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("GML Playground", app_icon, window)));
|
||||||
|
|
||||||
window->on_close_request = [&] {
|
window->on_close_request = [&] {
|
||||||
if (!window->is_modified())
|
if (!window->is_modified())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue