LibGUI+Userland: Remove Toolbar::try_add_{action,separator}()

These calls largely occur during initialization before there's
unsaved state worth preserving
This commit is contained in:
thankyouverycool 2023-08-15 04:36:28 -04:00 committed by Andreas Kling
parent eafdb06d87
commit 57f3b18109
Notes: sideshowbarker 2024-07-17 01:53:23 +09:00
10 changed files with 109 additions and 131 deletions

View file

@ -61,15 +61,15 @@ ErrorOr<NonnullRefPtr<CalendarWidget>> CalendarWidget::create(GUI::Window* paren
auto open_settings_action = TRY(widget->create_open_settings_action());
(void)TRY(toolbar->try_add_action(prev_date_action));
(void)TRY(toolbar->try_add_action(next_date_action));
TRY(toolbar->try_add_separator());
(void)TRY(toolbar->try_add_action(jump_to_action));
(void)TRY(toolbar->try_add_action(add_event_action));
TRY(toolbar->try_add_separator());
(void)TRY(toolbar->try_add_action(view_month_action));
(void)TRY(toolbar->try_add_action(view_year_action));
(void)TRY(toolbar->try_add_action(open_settings_action));
toolbar->add_action(prev_date_action);
toolbar->add_action(next_date_action);
toolbar->add_separator();
toolbar->add_action(jump_to_action);
toolbar->add_action(add_event_action);
toolbar->add_separator();
toolbar->add_action(view_month_action);
toolbar->add_action(view_year_action);
toolbar->add_action(open_settings_action);
widget->create_on_tile_doubleclick();

View file

@ -1079,31 +1079,31 @@ ErrorOr<int> run_in_windowed_mode(DeprecatedString const& initial_location, Depr
help_menu->add_action(GUI::CommonActions::make_command_palette_action(window));
help_menu->add_action(GUI::CommonActions::make_about_action("File Manager"sv, GUI::Icon::default_icon("app-file-manager"sv), window));
(void)TRY(main_toolbar.try_add_action(go_back_action));
(void)TRY(main_toolbar.try_add_action(go_forward_action));
(void)TRY(main_toolbar.try_add_action(open_parent_directory_action));
(void)TRY(main_toolbar.try_add_action(go_home_action));
main_toolbar.add_action(go_back_action);
main_toolbar.add_action(go_forward_action);
main_toolbar.add_action(open_parent_directory_action);
main_toolbar.add_action(go_home_action);
TRY(main_toolbar.try_add_separator());
(void)TRY(main_toolbar.try_add_action(directory_view->open_terminal_action()));
main_toolbar.add_separator();
main_toolbar.add_action(directory_view->open_terminal_action());
TRY(main_toolbar.try_add_separator());
(void)TRY(main_toolbar.try_add_action(mkdir_action));
(void)TRY(main_toolbar.try_add_action(touch_action));
TRY(main_toolbar.try_add_separator());
main_toolbar.add_separator();
main_toolbar.add_action(mkdir_action);
main_toolbar.add_action(touch_action);
main_toolbar.add_separator();
(void)TRY(main_toolbar.try_add_action(focus_dependent_delete_action));
(void)TRY(main_toolbar.try_add_action(directory_view->rename_action()));
main_toolbar.add_action(focus_dependent_delete_action);
main_toolbar.add_action(directory_view->rename_action());
TRY(main_toolbar.try_add_separator());
(void)TRY(main_toolbar.try_add_action(cut_action));
(void)TRY(main_toolbar.try_add_action(copy_action));
(void)TRY(main_toolbar.try_add_action(paste_action));
main_toolbar.add_separator();
main_toolbar.add_action(cut_action);
main_toolbar.add_action(copy_action);
main_toolbar.add_action(paste_action);
TRY(main_toolbar.try_add_separator());
(void)TRY(main_toolbar.try_add_action(directory_view->view_as_icons_action()));
(void)TRY(main_toolbar.try_add_action(directory_view->view_as_table_action()));
(void)TRY(main_toolbar.try_add_action(directory_view->view_as_columns_action()));
main_toolbar.add_separator();
main_toolbar.add_action(directory_view->view_as_icons_action());
main_toolbar.add_action(directory_view->view_as_table_action());
main_toolbar.add_action(directory_view->view_as_columns_action());
breadcrumbbar.on_path_change = [&](auto selected_path) {
if (FileSystem::is_directory(selected_path)) {

View file

@ -378,33 +378,33 @@ ErrorOr<void> MainWidget::create_actions()
ErrorOr<void> MainWidget::create_toolbars()
{
auto& toolbar = *find_descendant_of_type_named<GUI::Toolbar>("toolbar");
(void)TRY(toolbar.try_add_action(*m_new_action));
(void)TRY(toolbar.try_add_action(*m_open_action));
(void)TRY(toolbar.try_add_action(*m_save_action));
TRY(toolbar.try_add_separator());
(void)TRY(toolbar.try_add_action(*m_cut_action));
(void)TRY(toolbar.try_add_action(*m_copy_action));
(void)TRY(toolbar.try_add_action(*m_paste_action));
(void)TRY(toolbar.try_add_action(*m_delete_action));
TRY(toolbar.try_add_separator());
(void)TRY(toolbar.try_add_action(*m_undo_action));
(void)TRY(toolbar.try_add_action(*m_redo_action));
TRY(toolbar.try_add_separator());
(void)TRY(toolbar.try_add_action(*m_open_preview_action));
TRY(toolbar.try_add_separator());
(void)TRY(toolbar.try_add_action(*m_previous_glyph_action));
(void)TRY(toolbar.try_add_action(*m_next_glyph_action));
(void)TRY(toolbar.try_add_action(*m_go_to_glyph_action));
toolbar.add_action(*m_new_action);
toolbar.add_action(*m_open_action);
toolbar.add_action(*m_save_action);
toolbar.add_separator();
toolbar.add_action(*m_cut_action);
toolbar.add_action(*m_copy_action);
toolbar.add_action(*m_paste_action);
toolbar.add_action(*m_delete_action);
toolbar.add_separator();
toolbar.add_action(*m_undo_action);
toolbar.add_action(*m_redo_action);
toolbar.add_separator();
toolbar.add_action(*m_open_preview_action);
toolbar.add_separator();
toolbar.add_action(*m_previous_glyph_action);
toolbar.add_action(*m_next_glyph_action);
toolbar.add_action(*m_go_to_glyph_action);
auto& glyph_transform_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("glyph_transform_toolbar");
(void)TRY(glyph_transform_toolbar.try_add_action(*m_flip_horizontal_action));
(void)TRY(glyph_transform_toolbar.try_add_action(*m_flip_vertical_action));
(void)TRY(glyph_transform_toolbar.try_add_action(*m_rotate_counterclockwise_action));
(void)TRY(glyph_transform_toolbar.try_add_action(*m_rotate_clockwise_action));
glyph_transform_toolbar.add_action(*m_flip_horizontal_action);
glyph_transform_toolbar.add_action(*m_flip_vertical_action);
glyph_transform_toolbar.add_action(*m_rotate_counterclockwise_action);
glyph_transform_toolbar.add_action(*m_rotate_clockwise_action);
auto& glyph_mode_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("glyph_mode_toolbar");
(void)TRY(glyph_mode_toolbar.try_add_action(*m_paint_glyph_action));
(void)TRY(glyph_mode_toolbar.try_add_action(*m_move_glyph_action));
glyph_mode_toolbar.add_action(*m_paint_glyph_action);
glyph_mode_toolbar.add_action(*m_move_glyph_action);
return {};
}

View file

@ -200,9 +200,9 @@ ErrorOr<void> MainWidget::initialize_fallibles(GUI::Window& window)
open_page(help_index_path);
});
(void)TRY(m_toolbar->try_add_action(*m_go_back_action));
(void)TRY(m_toolbar->try_add_action(*m_go_forward_action));
(void)TRY(m_toolbar->try_add_action(*m_go_home_action));
m_toolbar->add_action(*m_go_back_action);
m_toolbar->add_action(*m_go_forward_action);
m_toolbar->add_action(*m_go_home_action);
auto file_menu = window.add_menu("&File"_string);
file_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {

View file

@ -294,17 +294,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
};
(void)TRY(main_toolbar->try_add_action(open_action));
(void)TRY(main_toolbar->try_add_action(delete_action));
(void)TRY(main_toolbar->try_add_separator());
(void)TRY(main_toolbar->try_add_action(go_first_action));
(void)TRY(main_toolbar->try_add_action(go_back_action));
(void)TRY(main_toolbar->try_add_action(go_forward_action));
(void)TRY(main_toolbar->try_add_action(go_last_action));
(void)TRY(main_toolbar->try_add_separator());
(void)TRY(main_toolbar->try_add_action(zoom_in_action));
(void)TRY(main_toolbar->try_add_action(reset_zoom_action));
(void)TRY(main_toolbar->try_add_action(zoom_out_action));
main_toolbar->add_action(open_action);
main_toolbar->add_action(delete_action);
main_toolbar->add_separator();
main_toolbar->add_action(go_first_action);
main_toolbar->add_action(go_back_action);
main_toolbar->add_action(go_forward_action);
main_toolbar->add_action(go_last_action);
main_toolbar->add_separator();
main_toolbar->add_action(zoom_in_action);
main_toolbar->add_action(reset_zoom_action);
main_toolbar->add_action(zoom_out_action);
auto file_menu = window->add_menu("&File"_string);
file_menu->add_action(open_action);

View file

@ -1178,19 +1178,19 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
});
auto& toolbar = *find_descendant_of_type_named<GUI::Toolbar>("toolbar");
(void)TRY(toolbar.try_add_action(*m_new_image_action));
(void)TRY(toolbar.try_add_action(*m_open_image_action));
(void)TRY(toolbar.try_add_action(*m_save_image_action));
TRY(toolbar.try_add_separator());
(void)TRY(toolbar.try_add_action(*m_cut_action));
(void)TRY(toolbar.try_add_action(*m_copy_action));
(void)TRY(toolbar.try_add_action(*m_paste_action));
(void)TRY(toolbar.try_add_action(*m_undo_action));
(void)TRY(toolbar.try_add_action(*m_redo_action));
TRY(toolbar.try_add_separator());
(void)TRY(toolbar.try_add_action(*m_zoom_in_action));
(void)TRY(toolbar.try_add_action(*m_zoom_out_action));
(void)TRY(toolbar.try_add_action(*m_reset_zoom_action));
toolbar.add_action(*m_new_image_action);
toolbar.add_action(*m_open_image_action);
toolbar.add_action(*m_save_image_action);
toolbar.add_separator();
toolbar.add_action(*m_cut_action);
toolbar.add_action(*m_copy_action);
toolbar.add_action(*m_paste_action);
toolbar.add_action(*m_undo_action);
toolbar.add_action(*m_redo_action);
toolbar.add_separator();
toolbar.add_action(*m_zoom_in_action);
toolbar.add_action(*m_zoom_out_action);
toolbar.add_action(*m_reset_zoom_action);
m_zoom_combobox = TRY(toolbar.try_add<GUI::ComboBox>());
m_zoom_combobox->set_max_width(75);
@ -1230,8 +1230,8 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
m_zoom_combobox->on_change(m_zoom_combobox->text(), GUI::ModelIndex());
};
TRY(toolbar.try_add_separator());
(void)TRY(toolbar.try_add_action(*m_levels_dialog_action));
toolbar.add_separator();
toolbar.add_action(*m_levels_dialog_action);
return {};
}

View file

@ -279,18 +279,18 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
}));
help_menu->add_action(GUI::CommonActions::make_about_action("GML Playground", m_icon, &window));
(void)TRY(m_toolbar->try_add_action(open_action));
(void)TRY(m_toolbar->try_add_action(*m_save_action));
(void)TRY(m_toolbar->try_add_action(*m_save_as_action));
TRY(m_toolbar->try_add_separator());
(void)TRY(m_toolbar->try_add_action(m_editor->cut_action()));
(void)TRY(m_toolbar->try_add_action(m_editor->copy_action()));
(void)TRY(m_toolbar->try_add_action(m_editor->paste_action()));
TRY(m_toolbar->try_add_separator());
(void)TRY(m_toolbar->try_add_action(m_editor->undo_action()));
(void)TRY(m_toolbar->try_add_action(m_editor->redo_action()));
TRY(m_toolbar->try_add_separator());
(void)TRY(m_toolbar->try_add_action(format_gml_action));
m_toolbar->add_action(open_action);
m_toolbar->add_action(*m_save_action);
m_toolbar->add_action(*m_save_as_action);
m_toolbar->add_separator();
m_toolbar->add_action(m_editor->cut_action());
m_toolbar->add_action(m_editor->copy_action());
m_toolbar->add_action(m_editor->paste_action());
m_toolbar->add_separator();
m_toolbar->add_action(m_editor->undo_action());
m_toolbar->add_action(m_editor->redo_action());
m_toolbar->add_separator();
m_toolbar->add_action(format_gml_action);
return {};
}

View file

@ -104,33 +104,33 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
});
toggle_running_action->set_checkable(true);
auto toggle_running_toolbar_button = TRY(main_toolbar.try_add_action(toggle_running_action));
auto& toggle_running_toolbar_button = main_toolbar.add_action(toggle_running_action);
auto run_one_generation_action = GUI::Action::create("Run &Next Generation", { Mod_Ctrl, Key_Equal }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"sv)), [&](const GUI::Action&) {
statusbar.set_text(click_tip);
board_widget->run_generation();
});
(void)TRY(main_toolbar.try_add_action(run_one_generation_action));
main_toolbar.add_action(run_one_generation_action);
auto clear_board_action = GUI::Action::create("&Clear board", { Mod_Ctrl, Key_N }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"sv)), [&](auto&) {
statusbar.set_text(click_tip);
board_widget->clear_cells();
board_widget->update();
});
(void)TRY(main_toolbar.try_add_action(clear_board_action));
main_toolbar.add_action(clear_board_action);
auto randomize_cells_action = GUI::Action::create("&Randomize board", { Mod_Ctrl, Key_R }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"sv)), [&](auto&) {
statusbar.set_text(click_tip);
board_widget->randomize_cells();
board_widget->update();
});
(void)TRY(main_toolbar.try_add_action(randomize_cells_action));
main_toolbar.add_action(randomize_cells_action);
auto rotate_pattern_action = GUI::Action::create("&Rotate pattern", { 0, Key_R }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/redo.png"sv)), [&](auto&) {
board_widget->selected_pattern()->rotate_clockwise();
});
rotate_pattern_action->set_enabled(false);
(void)TRY(main_toolbar.try_add_action(rotate_pattern_action));
main_toolbar.add_action(rotate_pattern_action);
auto game_menu = window->add_menu("&Game"_string);
@ -154,11 +154,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
board_widget->on_running_state_change = [&]() {
if (board_widget->is_running()) {
statusbar.set_text("Running..."_string);
toggle_running_toolbar_button->set_icon(*paused_icon);
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);
toggle_running_toolbar_button.set_icon(*play_icon);
main_widget->set_override_cursor(Gfx::StandardCursor::Drag);
}

View file

@ -97,44 +97,25 @@ private:
}
};
ErrorOr<NonnullRefPtr<GUI::Button>> Toolbar::try_add_action(Action& action)
Button& Toolbar::add_action(Action& action)
{
auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
auto item = make<Item>();
item->type = Item::Type::Action;
item->action = action;
// NOTE: Grow the m_items capacity before potentially adding a child widget.
// This avoids having to untangle the child widget in case of allocation failure.
TRY(m_items.try_ensure_capacity(m_items.size() + 1));
item->widget = TRY(try_add<ToolbarButton>(action));
item->widget = add<ToolbarButton>(action);
item->widget->set_fixed_size(m_button_size, m_button_size);
m_items.unchecked_append(move(item));
m_items.append(move(item));
return *static_cast<Button*>(m_items.last()->widget.ptr());
}
GUI::Button& Toolbar::add_action(Action& action)
{
auto button = MUST(try_add_action(action));
return *button;
}
ErrorOr<void> Toolbar::try_add_separator()
{
// NOTE: Grow the m_items capacity before potentially adding a child widget.
TRY(m_items.try_ensure_capacity(m_items.size() + 1));
auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
item->type = Item::Type::Separator;
item->widget = TRY(try_add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal));
m_items.unchecked_append(move(item));
return {};
}
void Toolbar::add_separator()
{
MUST(try_add_separator());
auto item = make<Item>();
item->type = Item::Type::Separator;
item->widget = add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal);
m_items.append(move(item));
}
void Toolbar::paint_event(PaintEvent& event)
@ -176,7 +157,7 @@ ErrorOr<void> Toolbar::create_overflow_objects()
add_spacer();
m_overflow_button = TRY(try_add_action(*m_overflow_action));
m_overflow_button = add_action(*m_overflow_action);
m_overflow_button->set_visible(false);
return {};

View file

@ -18,9 +18,6 @@ class Toolbar : public Widget {
public:
virtual ~Toolbar() override = default;
ErrorOr<NonnullRefPtr<GUI::Button>> try_add_action(GUI::Action&);
ErrorOr<void> try_add_separator();
GUI::Button& add_action(GUI::Action&);
void add_separator();