summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorPedro Pereira <pmh.pereira@gmail.com>2021-11-27 01:21:10 +0000
committerAndreas Kling <kling@serenityos.org>2021-11-27 17:18:44 +0100
commitee3b14fc38edd130e4ca0a66619c07eb60698d49 (patch)
treef121f6c6a792247711950cffa663aa28cdb6cbab /Userland
parent2f5f53d1cf951c76fd140b5a7367ae4374c121f8 (diff)
downloadserenity-ee3b14fc38edd130e4ca0a66619c07eb60698d49.zip
GameOfLife: TRY() all the things in serenity_main() :^)
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Games/GameOfLife/main.cpp114
1 files changed, 57 insertions, 57 deletions
diff --git a/Userland/Games/GameOfLife/main.cpp b/Userland/Games/GameOfLife/main.cpp
index 455623738f..ad249f201d 100644
--- a/Userland/Games/GameOfLife/main.cpp
+++ b/Userland/Games/GameOfLife/main.cpp
@@ -45,41 +45,41 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->set_double_buffering_enabled(false);
window->set_title("Game Of Life");
- auto& main_widget = window->set_main_widget<GUI::Widget>();
- main_widget.load_from_gml(game_of_life_gml);
- main_widget.set_fill_with_background_color(true);
+ auto main_widget = TRY(window->try_set_main_widget<GUI::Widget>());
+ main_widget->load_from_gml(game_of_life_gml);
+ main_widget->set_fill_with_background_color(true);
- auto& main_toolbar = *main_widget.find_descendant_of_type_named<GUI::Toolbar>("toolbar");
+ auto& main_toolbar = *main_widget->find_descendant_of_type_named<GUI::Toolbar>("toolbar");
main_toolbar.layout()->set_margins({ 0, 6 });
- auto& board_widget_container = *main_widget.find_descendant_of_type_named<GUI::Widget>("board_widget_container");
- auto& board_layout = board_widget_container.set_layout<GUI::VerticalBoxLayout>();
- board_layout.set_spacing(0);
- auto& board_widget = board_widget_container.add<BoardWidget>(board_rows, board_columns);
- board_widget.randomize_cells();
+ auto& board_widget_container = *main_widget->find_descendant_of_type_named<GUI::Widget>("board_widget_container");
+ auto board_layout = TRY(board_widget_container.try_set_layout<GUI::VerticalBoxLayout>());
+ board_layout->set_spacing(0);
+ auto board_widget = TRY(board_widget_container.try_add<BoardWidget>(board_rows, board_columns));
+ board_widget->randomize_cells();
- auto& statusbar = *main_widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar");
+ auto& statusbar = *main_widget->find_descendant_of_type_named<GUI::Statusbar>("statusbar");
statusbar.set_text(click_tip);
- auto& columns_spinbox = *main_widget.find_descendant_of_type_named<GUI::SpinBox>("columns_spinbox");
- auto& rows_spinbox = *main_widget.find_descendant_of_type_named<GUI::SpinBox>("rows_spinbox");
+ auto& columns_spinbox = *main_widget->find_descendant_of_type_named<GUI::SpinBox>("columns_spinbox");
+ auto& rows_spinbox = *main_widget->find_descendant_of_type_named<GUI::SpinBox>("rows_spinbox");
columns_spinbox.set_value(board_columns);
rows_spinbox.set_value(board_rows);
auto size_changed_function = [&] {
statusbar.set_text(click_tip);
- board_widget.resize_board(rows_spinbox.value(), columns_spinbox.value());
- board_widget.update();
+ board_widget->resize_board(rows_spinbox.value(), columns_spinbox.value());
+ board_widget->update();
};
rows_spinbox.on_change = [&](auto) { size_changed_function(); };
columns_spinbox.on_change = [&](auto) { size_changed_function(); };
- auto& interval_spinbox = *main_widget.find_descendant_of_type_named<GUI::SpinBox>("interval_spinbox");
+ auto& interval_spinbox = *main_widget->find_descendant_of_type_named<GUI::SpinBox>("interval_spinbox");
interval_spinbox.on_change = [&](auto value) {
- board_widget.set_running_timer_interval(value);
+ board_widget->set_running_timer_interval(value);
};
interval_spinbox.set_value(150);
@@ -88,88 +88,88 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto play_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/play.png").release_value_but_fixme_should_propagate_errors();
auto toggle_running_action = GUI::Action::create("&Toggle Running", { Mod_None, Key_Return }, *play_icon, [&](GUI::Action&) {
- board_widget.set_running(!board_widget.is_running());
+ board_widget->set_running(!board_widget->is_running());
});
toggle_running_action->set_checkable(true);
- auto& toggle_running_toolbar_button = main_toolbar.add_action(toggle_running_action);
+ auto toggle_running_toolbar_button = TRY(main_toolbar.try_add_action(toggle_running_action));
auto run_one_generation_action = GUI::Action::create("Run &Next Generation", { Mod_Ctrl, Key_Equal }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
statusbar.set_text(click_tip);
- board_widget.run_generation();
+ board_widget->run_generation();
});
- main_toolbar.add_action(run_one_generation_action);
+ TRY(main_toolbar.try_add_action(run_one_generation_action));
auto clear_board_action = GUI::Action::create("&Clear board", { Mod_Ctrl, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
statusbar.set_text(click_tip);
- board_widget.clear_cells();
- board_widget.update();
+ board_widget->clear_cells();
+ board_widget->update();
});
- main_toolbar.add_action(clear_board_action);
+ TRY(main_toolbar.try_add_action(clear_board_action));
auto randomize_cells_action = GUI::Action::create("&Randomize board", { Mod_Ctrl, Key_R }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/reload.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
statusbar.set_text(click_tip);
- board_widget.randomize_cells();
- board_widget.update();
+ board_widget->randomize_cells();
+ board_widget->update();
});
- main_toolbar.add_action(randomize_cells_action);
+ TRY(main_toolbar.try_add_action(randomize_cells_action));
auto rotate_pattern_action = GUI::Action::create("&Rotate pattern", { 0, Key_R }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/redo.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
- board_widget.selected_pattern()->rotate_clockwise();
+ board_widget->selected_pattern()->rotate_clockwise();
});
rotate_pattern_action->set_enabled(false);
- main_toolbar.add_action(rotate_pattern_action);
+ TRY(main_toolbar.try_add_action(rotate_pattern_action));
- auto& game_menu = window->add_menu("&Game");
+ auto game_menu = TRY(window->try_add_menu("&Game"));
- game_menu.add_action(clear_board_action);
- game_menu.add_action(randomize_cells_action);
- game_menu.add_separator();
- game_menu.add_action(toggle_running_action);
- game_menu.add_action(run_one_generation_action);
- game_menu.add_separator();
- game_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
+ TRY(game_menu->try_add_action(clear_board_action));
+ TRY(game_menu->try_add_action(randomize_cells_action));
+ TRY(game_menu->try_add_separator());
+ TRY(game_menu->try_add_action(toggle_running_action));
+ TRY(game_menu->try_add_action(run_one_generation_action));
+ TRY(game_menu->try_add_separator());
+ TRY(game_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
GUI::Application::the()->quit();
- }));
+ })));
- auto& help_menu = window->add_menu("&Help");
- help_menu.add_action(GUI::CommonActions::make_about_action("Game Of Life", app_icon, window));
+ auto help_menu = TRY(window->try_add_menu("&Help"));
+ TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Game Of Life", app_icon, window)));
- board_widget.on_running_state_change = [&]() {
- if (board_widget.is_running()) {
+ board_widget->on_running_state_change = [&]() {
+ if (board_widget->is_running()) {
statusbar.set_text("Running...");
- toggle_running_toolbar_button.set_icon(*paused_icon);
- main_widget.set_override_cursor(Gfx::StandardCursor::None);
+ toggle_running_toolbar_button->set_icon(*paused_icon);
+ main_widget->set_override_cursor(Gfx::StandardCursor::None);
} else {
statusbar.set_text(click_tip);
- toggle_running_toolbar_button.set_icon(*play_icon);
- main_widget.set_override_cursor(Gfx::StandardCursor::Drag);
+ toggle_running_toolbar_button->set_icon(*play_icon);
+ main_widget->set_override_cursor(Gfx::StandardCursor::Drag);
}
- interval_spinbox.set_value(board_widget.running_timer_interval());
+ interval_spinbox.set_value(board_widget->running_timer_interval());
- rows_spinbox.set_enabled(!board_widget.is_running());
- columns_spinbox.set_enabled(!board_widget.is_running());
- interval_spinbox.set_enabled(!board_widget.is_running());
+ rows_spinbox.set_enabled(!board_widget->is_running());
+ columns_spinbox.set_enabled(!board_widget->is_running());
+ interval_spinbox.set_enabled(!board_widget->is_running());
- run_one_generation_action->set_enabled(!board_widget.is_running());
- clear_board_action->set_enabled(!board_widget.is_running());
- randomize_cells_action->set_enabled(!board_widget.is_running());
+ run_one_generation_action->set_enabled(!board_widget->is_running());
+ clear_board_action->set_enabled(!board_widget->is_running());
+ randomize_cells_action->set_enabled(!board_widget->is_running());
- board_widget.update();
+ board_widget->update();
};
- board_widget.on_stall = [&] {
+ board_widget->on_stall = [&] {
toggle_running_action->activate();
statusbar.set_text("Stalled...");
};
- board_widget.on_cell_toggled = [&](auto, auto, auto) {
+ board_widget->on_cell_toggled = [&](auto, auto, auto) {
statusbar.set_text(click_tip);
};
- board_widget.on_pattern_selection_state_change = [&] {
- rotate_pattern_action->set_enabled(board_widget.selected_pattern() != nullptr);
+ board_widget->on_pattern_selection_state_change = [&] {
+ rotate_pattern_action->set_enabled(board_widget->selected_pattern() != nullptr);
};
window->resize(500, 420);