From 16bbdc812d063956d12adba704a24745003c5475 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 3 Feb 2023 13:27:26 +0000 Subject: [PATCH] SpaceAnalyzer: Extract ProgressWindow into its own class --- .../Applications/SpaceAnalyzer/CMakeLists.txt | 3 +- .../SpaceAnalyzer/ProgressWindow.cpp | 48 +++++++++++++++++++ .../SpaceAnalyzer/ProgressWindow.h | 23 +++++++++ Userland/Applications/SpaceAnalyzer/main.cpp | 41 ++-------------- 4 files changed, 77 insertions(+), 38 deletions(-) create mode 100644 Userland/Applications/SpaceAnalyzer/ProgressWindow.cpp create mode 100644 Userland/Applications/SpaceAnalyzer/ProgressWindow.h 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 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "ProgressWindow.h" +#include +#include +#include + +ErrorOr> 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()); + main_widget->set_fill_with_background_color(true); + (void)TRY(main_widget->try_set_layout()); + + auto label = TRY(main_widget->try_add("Analyzing storage space...")); + label->set_fixed_height(22); + + window->m_progress_label = TRY(main_widget->try_add()); + 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 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +class ProgressWindow final : public GUI::Window { + C_OBJECT_ABSTRACT(ProgressWindow) +public: + static ErrorOr> 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 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 * * SPDX-License-Identifier: BSD-2-Clause */ +#include "ProgressWindow.h" #include "Tree.h" #include "TreeMapWidget.h" #include @@ -54,53 +56,18 @@ static ErrorOr fill_mounts(Vector& output) return {}; } -static NonnullRefPtr 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().release_value_but_fixme_should_propagate_errors(); - main_widget->set_fill_with_background_color(true); - main_widget->set_layout(); - - auto& label = main_widget->add("Analyzing storage space..."); - label.set_fixed_height(22); - - auto& progresslabel = main_widget->add(); - 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 analyze(RefPtr 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("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 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();