SpaceAnalyzer: Extract ProgressWindow into its own class

This commit is contained in:
Sam Atkins 2023-02-03 13:27:26 +00:00 committed by Andreas Kling
parent bcc4e5ee0b
commit 16bbdc812d
4 changed files with 77 additions and 38 deletions

View file

@ -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
)

View file

@ -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);
}

View file

@ -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;
};

View file

@ -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();