From 5bb9af8297ca18d36e70ab235f8cf104c66e08bf Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Sun, 16 Apr 2023 18:35:29 -0400 Subject: [PATCH] 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. --- Userland/Games/Minesweeper/Field.cpp | 10 +++------- Userland/Games/Minesweeper/Field.h | 5 ++--- Userland/Games/Minesweeper/main.cpp | 9 ++------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/Userland/Games/Minesweeper/Field.cpp b/Userland/Games/Minesweeper/Field.cpp index 88f8216e3c..36b0215515 100644 --- a/Userland/Games/Minesweeper/Field.cpp +++ b/Userland/Games/Minesweeper/Field.cpp @@ -108,9 +108,9 @@ private: bool m_chord { false }; }; -ErrorOr> Field::create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function on_size_changed) +ErrorOr> 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> 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 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) diff --git a/Userland/Games/Minesweeper/Field.h b/Userland/Games/Minesweeper/Field.h index 4a3e574a7f..609d337956 100644 --- a/Userland/Games/Minesweeper/Field.h +++ b/Userland/Games/Minesweeper/Field.h @@ -44,7 +44,7 @@ class Field final : public GUI::Frame { friend class SquareLabel; public: - static ErrorOr> create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function on_size_changed); + static ErrorOr> 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 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 m_on_size_changed; }; diff --git a/Userland/Games/Minesweeper/main.cpp b/Userland/Games/Minesweeper/main.cpp index 9ac5e94242..f45d46beb0 100644 --- a/Userland/Games/Minesweeper/main.cpp +++ b/Userland/Games/Minesweeper/main.cpp @@ -47,20 +47,15 @@ ErrorOr 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()); TRY(widget->load_from_gml(minesweeper_window_gml)); - auto& separator = *widget->find_descendant_of_type_named("separator"); - auto& container = *widget->find_descendant_of_type_named("container"); auto& flag_label = *widget->find_descendant_of_type_named("flag_label"); auto& time_label = *widget->find_descendant_of_type_named("time_label"); auto& face_button = *widget->find_descendant_of_type_named("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"));