summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthankyouverycool <66646555+thankyouverycool@users.noreply.github.com>2023-04-16 18:35:29 -0400
committerAndreas Kling <kling@serenityos.org>2023-04-17 20:31:12 +0200
commit5bb9af8297ca18d36e70ab235f8cf104c66e08bf (patch)
tree5fb3fca9f7198285607714204edc2d42efdc3f82
parent76d17e6a8ee54f64b12b0f4f11be48ec172b00a7 (diff)
downloadserenity-5bb9af8297ca18d36e70ab235f8cf104c66e08bf.zip
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.
-rw-r--r--Userland/Games/Minesweeper/Field.cpp10
-rw-r--r--Userland/Games/Minesweeper/Field.h5
-rw-r--r--Userland/Games/Minesweeper/main.cpp9
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<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)
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<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;
};
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<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"));