ソースを参照

Browser: Add close on finished checkbox to download widget

Let the user choose if they want the download widget to close when
finished, also save the users choice for future downloads.
Marcus Nilsson 4 年 前
コミット
29cce65

+ 16 - 0
Userland/Applications/Browser/DownloadWidget.cpp

@@ -7,12 +7,14 @@
 #include "DownloadWidget.h"
 #include <AK/NumberFormat.h>
 #include <AK/StringBuilder.h>
+#include <LibCore/ConfigFile.h>
 #include <LibCore/File.h>
 #include <LibCore/FileStream.h>
 #include <LibCore/StandardPaths.h>
 #include <LibDesktop/Launcher.h>
 #include <LibGUI/BoxLayout.h>
 #include <LibGUI/Button.h>
+#include <LibGUI/CheckBox.h>
 #include <LibGUI/ImageWidget.h>
 #include <LibGUI/Label.h>
 #include <LibGUI/MessageBox.h>
@@ -35,6 +37,9 @@ DownloadWidget::DownloadWidget(const URL& url)
         m_destination_path = builder.to_string();
     }
 
+    auto browser_config = Core::ConfigFile::get_for_app("Browser");
+    auto close_on_finish = browser_config->read_bool_entry("Preferences", "CloseDownloadWidgetOnFinish", false);
+
     m_elapsed_timer.start();
     m_download = Web::ResourceLoader::the().protocol_client().start_request("GET", url);
     VERIFY(m_download);
@@ -82,6 +87,14 @@ DownloadWidget::DownloadWidget(const URL& url)
     destination_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
     destination_label.set_fixed_height(16);
 
+    m_close_on_finish_checkbox = add<GUI::CheckBox>("Close when finished");
+    m_close_on_finish_checkbox->set_checked(close_on_finish);
+
+    m_close_on_finish_checkbox->on_checked = [&]() {
+        auto browser_config = Core::ConfigFile::get_for_app("Browser");
+        browser_config->write_bool_entry("Preferences", "CloseDownloadWidgetOnFinish", m_close_on_finish_checkbox->is_checked());
+    };
+
     auto& button_container = add<GUI::Widget>();
     auto& button_container_layout = button_container.set_layout<GUI::HorizontalBoxLayout>();
     button_container_layout.add_spacer();
@@ -156,6 +169,9 @@ void DownloadWidget::did_finish(bool success)
         window()->close();
         return;
     }
+
+    if (m_close_on_finish_checkbox->is_checked())
+        window()->close();
 }
 
 }

+ 1 - 0
Userland/Applications/Browser/DownloadWidget.h

@@ -34,6 +34,7 @@ private:
     RefPtr<GUI::Label> m_progress_label;
     RefPtr<GUI::Button> m_cancel_button;
     RefPtr<GUI::Button> m_close_button;
+    RefPtr<GUI::CheckBox> m_close_on_finish_checkbox;
     OwnPtr<Core::OutputFileStream> m_output_file_stream;
     Core::ElapsedTimer m_elapsed_timer;
 };

+ 1 - 1
Userland/Applications/Browser/Tab.cpp

@@ -57,7 +57,7 @@ URL url_from_user_input(const String& input)
 void Tab::start_download(const URL& url)
 {
     auto window = GUI::Window::construct(&this->window());
-    window->resize(300, 150);
+    window->resize(300, 170);
     window->set_title(String::formatted("0% of {}", url.basename()));
     window->set_resizable(false);
     window->set_main_widget<DownloadWidget>(url);