Skip to content

Commit 2c93ee5

Browse files
ForLoveOfCatsawesomekling
authored andcommitted
SpaceAnalyzer: Display scan progress with popup window during analysis
1 parent 8f9948a commit 2c93ee5

File tree

1 file changed

+51
-2
lines changed
  • Userland/Applications/SpaceAnalyzer

1 file changed

+51
-2
lines changed

Userland/Applications/SpaceAnalyzer/main.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
#include <LibCore/File.h>
1515
#include <LibDesktop/Launcher.h>
1616
#include <LibGUI/Application.h>
17+
#include <LibGUI/BoxLayout.h>
1718
#include <LibGUI/Breadcrumbbar.h>
1819
#include <LibGUI/Clipboard.h>
1920
#include <LibGUI/FileIconProvider.h>
2021
#include <LibGUI/Icon.h>
22+
#include <LibGUI/Label.h>
2123
#include <LibGUI/Menu.h>
2224
#include <LibGUI/Menubar.h>
2325
#include <LibGUI/MessageBox.h>
@@ -27,6 +29,7 @@
2729
#include <unistd.h>
2830

2931
static const char* APP_NAME = "Space Analyzer";
32+
static constexpr size_t FILES_ENCOUNTERED_UPDATE_STEP_SIZE = 25;
3033

3134
struct TreeNode : public SpaceAnalyzer::TreeMapNode {
3235
TreeNode(String name)
@@ -123,6 +126,38 @@ static long long int update_totals(TreeNode& node)
123126
return result;
124127
}
125128

129+
static NonnullRefPtr<GUI::Window> create_progress_window()
130+
{
131+
auto window = GUI::Window::construct();
132+
133+
window->set_title(APP_NAME);
134+
window->set_resizable(false);
135+
window->set_closeable(false);
136+
window->resize(240, 50);
137+
window->center_on_screen();
138+
139+
auto& main_widget = window->set_main_widget<GUI::Widget>();
140+
main_widget.set_fill_with_background_color(true);
141+
main_widget.set_layout<GUI::VerticalBoxLayout>();
142+
143+
auto& label = main_widget.add<GUI::Label>("Analyzing storage space...");
144+
label.set_fixed_height(22);
145+
146+
auto& progresslabel = main_widget.add<GUI::Label>();
147+
progresslabel.set_name("progresslabel");
148+
progresslabel.set_fixed_height(22);
149+
150+
return window;
151+
}
152+
153+
static void update_progress_label(GUI::Label& progresslabel, size_t files_encountered_count)
154+
{
155+
auto text = String::formatted("{} files...", files_encountered_count);
156+
progresslabel.set_text(text);
157+
158+
Core::EventLoop::current().pump(Core::EventLoop::WaitMode::PollForEvents);
159+
}
160+
126161
struct QueueEntry {
127162
QueueEntry(String path, TreeNode* node)
128163
: path(move(path))
@@ -131,12 +166,13 @@ struct QueueEntry {
131166
TreeNode* node { nullptr };
132167
};
133168

134-
static void populate_filesize_tree(TreeNode& root, Vector<MountInfo>& mounts, HashMap<int, int>& error_accumulator)
169+
static void populate_filesize_tree(TreeNode& root, Vector<MountInfo>& mounts, HashMap<int, int>& error_accumulator, GUI::Label& progresslabel)
135170
{
136171
VERIFY(!root.m_name.ends_with("/"));
137172

138173
Queue<QueueEntry> queue;
139174
queue.enqueue(QueueEntry(root.m_name, &root));
175+
size_t files_encountered_count = 0;
140176

141177
StringBuilder builder = StringBuilder();
142178
builder.append(root.m_name);
@@ -167,6 +203,10 @@ static void populate_filesize_tree(TreeNode& root, Vector<MountInfo>& mounts, Ha
167203
queue_entry.node->m_children->append(TreeNode(dir_iterator.next_path()));
168204
}
169205
for (auto& child : *queue_entry.node->m_children) {
206+
files_encountered_count += 1;
207+
if (!(files_encountered_count % FILES_ENCOUNTERED_UPDATE_STEP_SIZE))
208+
update_progress_label(progresslabel, files_encountered_count);
209+
170210
String& name = child.m_name;
171211
int name_len = name.length();
172212
builder.append(name);
@@ -192,13 +232,22 @@ static void populate_filesize_tree(TreeNode& root, Vector<MountInfo>& mounts, Ha
192232

193233
static void analyze(RefPtr<Tree> tree, SpaceAnalyzer::TreeMapWidget& treemapwidget, GUI::Statusbar& statusbar)
194234
{
235+
statusbar.set_text("");
236+
auto progress_window = create_progress_window();
237+
progress_window->show();
238+
239+
auto& progresslabel = *progress_window->main_widget()->find_descendant_of_type_named<GUI::Label>("progresslabel");
240+
update_progress_label(progresslabel, 0);
241+
195242
// Build an in-memory tree mirroring the filesystem and for each node
196243
// calculate the sum of the file size for all its descendants.
197244
TreeNode* root = &tree->m_root;
198245
Vector<MountInfo> mounts;
199246
fill_mounts(mounts);
200247
HashMap<int, int> error_accumulator;
201-
populate_filesize_tree(*root, mounts, error_accumulator);
248+
populate_filesize_tree(*root, mounts, error_accumulator, progresslabel);
249+
250+
progress_window->close();
202251

203252
// Display an error summary in the statusbar.
204253
if (!error_accumulator.is_empty()) {

0 commit comments

Comments
 (0)