diff options
author | Arda Cinar <kuzux92@gmail.com> | 2022-12-23 21:44:10 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-12-26 09:31:44 +0100 |
commit | f40cb8d7719d9b9d16c2c692a5f1a993883fe8ee (patch) | |
tree | 2c9582d15617b6776eacdb8f126b2ecae8477a67 /Userland/Services | |
parent | fa1416987acb3a3aac8853eb649c0fb091f01a8f (diff) | |
download | serenity-f40cb8d7719d9b9d16c2c692a5f1a993883fe8ee.zip |
Taskbar: Add a factory function for QuickLaunchWidget
This helps propagate errors that might happen when constructing the
quick launch menu. However, the errors are not propagated all the way
yet.
Diffstat (limited to 'Userland/Services')
-rw-r--r-- | Userland/Services/Taskbar/QuickLaunchWidget.cpp | 41 | ||||
-rw-r--r-- | Userland/Services/Taskbar/QuickLaunchWidget.h | 7 | ||||
-rw-r--r-- | Userland/Services/Taskbar/TaskbarWindow.cpp | 3 |
3 files changed, 39 insertions, 12 deletions
diff --git a/Userland/Services/Taskbar/QuickLaunchWidget.cpp b/Userland/Services/Taskbar/QuickLaunchWidget.cpp index 85985ac924..78db4e10e1 100644 --- a/Userland/Services/Taskbar/QuickLaunchWidget.cpp +++ b/Userland/Services/Taskbar/QuickLaunchWidget.cpp @@ -6,6 +6,7 @@ #include "QuickLaunchWidget.h" #include <AK/LexicalPath.h> +#include <AK/OwnPtr.h> #include <Kernel/API/InodeWatcherFlags.h> #include <LibConfig/Client.h> #include <LibCore/FileWatcher.h> @@ -82,7 +83,25 @@ DeprecatedString QuickLaunchEntryFile::name() const return m_path; } -QuickLaunchWidget::QuickLaunchWidget() +ErrorOr<NonnullRefPtr<QuickLaunchWidget>> QuickLaunchWidget::create() +{ + Vector<NonnullOwnPtr<QuickLaunchEntry>> entries; + auto keys = Config::list_keys("Taskbar"sv, quick_launch); + for (auto& name : keys) { + auto value = Config::read_string("Taskbar"sv, quick_launch, name); + auto entry = QuickLaunchEntry::create_from_config_value(value); + if (!entry) + continue; + + entries.append(entry.release_nonnull()); + } + + auto widget = TRY(AK::adopt_nonnull_ref_or_enomem(new (nothrow) QuickLaunchWidget(move(entries)))); + TRY(widget->create_context_menu()); + return widget; +} + +QuickLaunchWidget::QuickLaunchWidget(Vector<NonnullOwnPtr<QuickLaunchEntry>> entries) { set_shrink_to_fit(true); set_layout<GUI::HorizontalBoxLayout>(); @@ -90,8 +109,17 @@ QuickLaunchWidget::QuickLaunchWidget() set_frame_thickness(0); set_fixed_height(24); + for (auto& entry : entries) { + auto name = entry->name(); + add_or_adjust_button(name, move(entry)); + } +} + +ErrorOr<void> QuickLaunchWidget::create_context_menu() +{ + auto icon = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png"sv)); m_context_menu = GUI::Menu::construct(); - m_context_menu_default_action = GUI::Action::create("&Remove", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png"sv).release_value_but_fixme_should_propagate_errors(), [this](auto&) { + m_context_menu_default_action = GUI::Action::create("&Remove", icon, [this](auto&) { Config::remove_key("Taskbar"sv, quick_launch, m_context_menu_app_name); auto button = find_child_of_type_named<GUI::Button>(m_context_menu_app_name); if (button) { @@ -100,14 +128,7 @@ QuickLaunchWidget::QuickLaunchWidget() }); m_context_menu->add_action(*m_context_menu_default_action); - auto keys = Config::list_keys("Taskbar"sv, quick_launch); - for (auto& name : keys) { - auto value = Config::read_string("Taskbar"sv, quick_launch, name); - auto entry = QuickLaunchEntry::create_from_config_value(value); - if (!entry) - continue; - add_or_adjust_button(name, entry.release_nonnull()); - } + return {}; } OwnPtr<QuickLaunchEntry> QuickLaunchEntry::create_from_config_value(StringView value) diff --git a/Userland/Services/Taskbar/QuickLaunchWidget.h b/Userland/Services/Taskbar/QuickLaunchWidget.h index 78a9bc19e0..e3203d7704 100644 --- a/Userland/Services/Taskbar/QuickLaunchWidget.h +++ b/Userland/Services/Taskbar/QuickLaunchWidget.h @@ -6,6 +6,8 @@ #pragma once +#include <AK/Error.h> +#include <AK/RefPtr.h> #include <LibConfig/Listener.h> #include <LibCore/FileWatcher.h> #include <LibDesktop/AppFile.h> @@ -78,6 +80,7 @@ class QuickLaunchWidget : public GUI::Frame C_OBJECT(QuickLaunchWidget); public: + static ErrorOr<NonnullRefPtr<QuickLaunchWidget>> create(); virtual ~QuickLaunchWidget() override = default; virtual void config_key_was_removed(DeprecatedString const&, DeprecatedString const&, DeprecatedString const&) override; @@ -87,8 +90,10 @@ public: virtual void drop_event(GUI::DropEvent&) override; private: - QuickLaunchWidget(); + explicit QuickLaunchWidget(Vector<NonnullOwnPtr<QuickLaunchEntry>> entries); void add_or_adjust_button(DeprecatedString const&, NonnullOwnPtr<QuickLaunchEntry>&&); + ErrorOr<void> create_context_menu(); + RefPtr<GUI::Menu> m_context_menu; RefPtr<GUI::Action> m_context_menu_default_action; DeprecatedString m_context_menu_app_name; diff --git a/Userland/Services/Taskbar/TaskbarWindow.cpp b/Userland/Services/Taskbar/TaskbarWindow.cpp index bcbaead65a..3730a7d49a 100644 --- a/Userland/Services/Taskbar/TaskbarWindow.cpp +++ b/Userland/Services/Taskbar/TaskbarWindow.cpp @@ -63,7 +63,8 @@ TaskbarWindow::TaskbarWindow() main_widget.set_layout<GUI::HorizontalBoxLayout>(); main_widget.layout()->set_margins({ 2, 3, 0, 3 }); - m_quick_launch = Taskbar::QuickLaunchWidget::construct(); + // FIXME: Propagate this error up as well + m_quick_launch = MUST(Taskbar::QuickLaunchWidget::create()); main_widget.add_child(*m_quick_launch); m_task_button_container = main_widget.add<GUI::Widget>(); |