Minesweeper: Simplify resizing game window

Instead of propagating field size changes to main and manually
calculating window size, use auto shrink to automatically
resize the window after changes to the board.
This commit is contained in:
thankyouverycool 2023-04-16 18:35:29 -04:00 committed by Andreas Kling
parent 76d17e6a8e
commit 5bb9af8297
3 changed files with 7 additions and 17 deletions

View file

@ -108,9 +108,9 @@ private:
bool m_chord { false };
};
ErrorOr<NonnullRefPtr<Field>> Field::create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed)
ErrorOr<NonnullRefPtr<Field>> Field::create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button)
{
auto field = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Field(flag_label, time_label, face_button, move(on_size_changed))));
auto field = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Field(flag_label, time_label, face_button)));
field->m_mine_bitmap = TRY(Gfx::Bitmap::load_from_file("/res/icons/minesweeper/mine.png"sv));
field->m_flag_bitmap = TRY(Gfx::Bitmap::load_from_file("/res/icons/minesweeper/flag.png"sv));
field->m_badflag_bitmap = TRY(Gfx::Bitmap::load_from_file("/res/icons/minesweeper/badflag.png"sv));
@ -124,12 +124,11 @@ ErrorOr<NonnullRefPtr<Field>> Field::create(GUI::Label& flag_label, GUI::Label&
return field;
}
Field::Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed)
Field::Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button)
: m_mine_palette(GUI::Application::the()->palette().impl().clone())
, m_face_button(face_button)
, m_flag_label(flag_label)
, m_time_label(time_label)
, m_on_size_changed(move(on_size_changed))
{
}
@ -568,9 +567,6 @@ void Field::set_field_size(Difficulty difficulty, size_t rows, size_t columns, s
m_mine_count = mine_count;
set_fixed_size(frame_thickness() * 2 + m_columns * square_size(), frame_thickness() * 2 + m_rows * square_size());
reset();
deferred_invoke([this] {
m_on_size_changed(Gfx::IntSize(min_size()));
});
}
void Field::set_single_chording(bool enabled)

View file

@ -44,7 +44,7 @@ class Field final : public GUI::Frame {
friend class SquareLabel;
public:
static ErrorOr<NonnullRefPtr<Field>> create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed);
static ErrorOr<NonnullRefPtr<Field>> create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button);
virtual ~Field() override = default;
enum class Difficulty {
@ -109,7 +109,7 @@ public:
void generate_field(size_t start_row, size_t start_column);
private:
Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed);
Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button);
void initialize();
@ -166,5 +166,4 @@ private:
bool m_chord_preview { false };
bool m_first_click { true };
bool m_single_chording { true };
Function<void(Gfx::IntSize)> m_on_size_changed;
};

View file

@ -47,20 +47,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto window = TRY(GUI::Window::try_create());
window->set_resizable(false);
window->set_title("Minesweeper");
window->resize(139, 177);
window->set_auto_shrink(true);
auto widget = TRY(window->set_main_widget<GUI::Widget>());
TRY(widget->load_from_gml(minesweeper_window_gml));
auto& separator = *widget->find_descendant_of_type_named<GUI::HorizontalSeparator>("separator");
auto& container = *widget->find_descendant_of_type_named<GUI::Widget>("container");
auto& flag_label = *widget->find_descendant_of_type_named<GUI::Label>("flag_label");
auto& time_label = *widget->find_descendant_of_type_named<GUI::Label>("time_label");
auto& face_button = *widget->find_descendant_of_type_named<GUI::Button>("face_button");
auto field = TRY(Field::create(flag_label, time_label, face_button, [&](auto size) {
size.set_height(size.height() + separator.height() + container.height());
window->resize(size);
}));
auto field = TRY(Field::create(flag_label, time_label, face_button));
TRY(widget->try_add_child(field));
auto game_menu = TRY(window->try_add_menu("&Game"));