summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-02-03 13:27:26 +0000
committerAndreas Kling <kling@serenityos.org>2023-02-04 12:27:10 +0100
commit16bbdc812d063956d12adba704a24745003c5475 (patch)
tree25a1358e7226c3e9f272080ea2c86bfcdf58a2e0
parentbcc4e5ee0b2c0cf48af7a6d4893e55dadd558ccd (diff)
downloadserenity-16bbdc812d063956d12adba704a24745003c5475.zip
SpaceAnalyzer: Extract ProgressWindow into its own class
-rw-r--r--Userland/Applications/SpaceAnalyzer/CMakeLists.txt3
-rw-r--r--Userland/Applications/SpaceAnalyzer/ProgressWindow.cpp48
-rw-r--r--Userland/Applications/SpaceAnalyzer/ProgressWindow.h23
-rw-r--r--Userland/Applications/SpaceAnalyzer/main.cpp41
4 files changed, 77 insertions, 38 deletions
diff --git a/Userland/Applications/SpaceAnalyzer/CMakeLists.txt b/Userland/Applications/SpaceAnalyzer/CMakeLists.txt
index 28beaa4ca6..6783b80b20 100644
--- a/Userland/Applications/SpaceAnalyzer/CMakeLists.txt
+++ b/Userland/Applications/SpaceAnalyzer/CMakeLists.txt
@@ -6,8 +6,9 @@ serenity_component(
compile_gml(SpaceAnalyzer.gml SpaceAnalyzerGML.h space_analyzer_gml)
set(SOURCES
- TreeMapWidget.cpp
+ ProgressWindow.cpp
Tree.cpp
+ TreeMapWidget.cpp
main.cpp
)
diff --git a/Userland/Applications/SpaceAnalyzer/ProgressWindow.cpp b/Userland/Applications/SpaceAnalyzer/ProgressWindow.cpp
new file mode 100644
index 0000000000..bf87d1ff5e
--- /dev/null
+++ b/Userland/Applications/SpaceAnalyzer/ProgressWindow.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2021-2022, the SerenityOS developers.
+ * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "ProgressWindow.h"
+#include <LibCore/EventLoop.h>
+#include <LibGUI/BoxLayout.h>
+#include <LibGUI/Label.h>
+
+ErrorOr<NonnullRefPtr<ProgressWindow>> ProgressWindow::try_create(StringView title, Window* parent)
+{
+ auto window = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProgressWindow(title, parent)));
+
+ auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
+ main_widget->set_fill_with_background_color(true);
+ (void)TRY(main_widget->try_set_layout<GUI::VerticalBoxLayout>());
+
+ auto label = TRY(main_widget->try_add<GUI::Label>("Analyzing storage space..."));
+ label->set_fixed_height(22);
+
+ window->m_progress_label = TRY(main_widget->try_add<GUI::Label>());
+ window->m_progress_label->set_fixed_height(22);
+
+ window->update_progress_label(0);
+ return window;
+}
+
+ProgressWindow::ProgressWindow(StringView title, GUI::Window* parent)
+ : GUI::Window(parent)
+{
+ set_title(title);
+ set_resizable(false);
+ set_closeable(false);
+ resize(240, 50);
+ center_on_screen();
+}
+
+ProgressWindow::~ProgressWindow() = default;
+
+void ProgressWindow::update_progress_label(size_t files_encountered_count)
+{
+ m_progress_label->set_text(DeprecatedString::formatted("{} files...", files_encountered_count));
+ // FIXME: Why is this necessary to make the window repaint?
+ Core::EventLoop::current().pump(Core::EventLoop::WaitMode::PollForEvents);
+}
diff --git a/Userland/Applications/SpaceAnalyzer/ProgressWindow.h b/Userland/Applications/SpaceAnalyzer/ProgressWindow.h
new file mode 100644
index 0000000000..c7e2e1c760
--- /dev/null
+++ b/Userland/Applications/SpaceAnalyzer/ProgressWindow.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibGUI/Window.h>
+
+class ProgressWindow final : public GUI::Window {
+ C_OBJECT_ABSTRACT(ProgressWindow)
+public:
+ static ErrorOr<NonnullRefPtr<ProgressWindow>> try_create(StringView title, GUI::Window* parent = nullptr);
+ virtual ~ProgressWindow() override;
+
+ void update_progress_label(size_t files_encountered_count);
+
+private:
+ ProgressWindow(StringView title, GUI::Window* parent = nullptr);
+
+ RefPtr<GUI::Label> m_progress_label;
+};
diff --git a/Userland/Applications/SpaceAnalyzer/main.cpp b/Userland/Applications/SpaceAnalyzer/main.cpp
index 6d4ad1591f..d4476a8784 100644
--- a/Userland/Applications/SpaceAnalyzer/main.cpp
+++ b/Userland/Applications/SpaceAnalyzer/main.cpp
@@ -1,9 +1,11 @@
/*
* Copyright (c) 2021-2022, the SerenityOS developers.
+ * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include "ProgressWindow.h"
#include "Tree.h"
#include "TreeMapWidget.h"
#include <AK/Error.h>
@@ -54,53 +56,18 @@ static ErrorOr<void> fill_mounts(Vector<MountInfo>& output)
return {};
}
-static NonnullRefPtr<GUI::Window> create_progress_window()
-{
- auto window = GUI::Window::construct();
-
- window->set_title(APP_NAME);
- window->set_resizable(false);
- window->set_closeable(false);
- window->resize(240, 50);
- window->center_on_screen();
-
- auto main_widget = window->set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors();
- main_widget->set_fill_with_background_color(true);
- main_widget->set_layout<GUI::VerticalBoxLayout>();
-
- auto& label = main_widget->add<GUI::Label>("Analyzing storage space...");
- label.set_fixed_height(22);
-
- auto& progresslabel = main_widget->add<GUI::Label>();
- progresslabel.set_name("progresslabel");
- progresslabel.set_fixed_height(22);
-
- return window;
-}
-
-static void update_progress_label(GUI::Label& progresslabel, size_t files_encountered_count)
-{
- auto text = DeprecatedString::formatted("{} files...", files_encountered_count);
- progresslabel.set_text(text);
-
- Core::EventLoop::current().pump(Core::EventLoop::WaitMode::PollForEvents);
-}
-
static ErrorOr<void> analyze(RefPtr<Tree> tree, SpaceAnalyzer::TreeMapWidget& treemapwidget, GUI::Statusbar& statusbar)
{
statusbar.set_text("");
- auto progress_window = create_progress_window();
+ auto progress_window = TRY(ProgressWindow::try_create(APP_NAME));
progress_window->show();
- auto& progresslabel = *progress_window->main_widget()->find_descendant_of_type_named<GUI::Label>("progresslabel");
- update_progress_label(progresslabel, 0);
-
// Build an in-memory tree mirroring the filesystem and for each node
// calculate the sum of the file size for all its descendants.
Vector<MountInfo> mounts;
TRY(fill_mounts(mounts));
auto errors = tree->root().populate_filesize_tree(mounts, [&](size_t processed_file_count) {
- update_progress_label(progresslabel, processed_file_count);
+ progress_window->update_progress_label(processed_file_count);
});
progress_window->close();