diff options
author | thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> | 2023-04-29 10:41:55 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-30 05:48:14 +0200 |
commit | b17d4a0ceda8c5448c19ae2e8a49f21bc9dcc70d (patch) | |
tree | 61ea642f595023950d4e9366c9e013974151c13f /Userland/Libraries | |
parent | 91bafc2653bcbcc0b8d05b5589324375b92f91b5 (diff) | |
download | serenity-b17d4a0ceda8c5448c19ae2e8a49f21bc9dcc70d.zip |
LibGUI: Propagate construction errors in LinkLabel
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibGUI/LinkLabel.cpp | 29 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/LinkLabel.h | 8 |
2 files changed, 26 insertions, 11 deletions
diff --git a/Userland/Libraries/LibGUI/LinkLabel.cpp b/Userland/Libraries/LibGUI/LinkLabel.cpp index 829353d909..3b47d7fa2a 100644 --- a/Userland/Libraries/LibGUI/LinkLabel.cpp +++ b/Userland/Libraries/LibGUI/LinkLabel.cpp @@ -19,24 +19,41 @@ REGISTER_WIDGET(GUI, LinkLabel) namespace GUI { +ErrorOr<NonnullRefPtr<LinkLabel>> LinkLabel::try_create(String text) +{ + auto label = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) LinkLabel(move(text)))); + TRY(label->create_actions()); + TRY(label->create_menus()); + return label; +} + LinkLabel::LinkLabel(String text) : Label(move(text)) { set_foreground_role(Gfx::ColorRole::Link); set_focus_policy(FocusPolicy::TabFocus); - setup_actions(); } -void LinkLabel::setup_actions() +ErrorOr<void> LinkLabel::create_actions() { m_open_action = GUI::Action::create( - "Show in File Manager", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-file-manager.png"sv).release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) { + "Show in File Manager", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-file-manager.png"sv)), [&](const GUI::Action&) { if (on_click) on_click(); }, this); m_copy_action = CommonActions::make_copy_action([this](auto&) { Clipboard::the().set_plain_text(text()); }, this); + return {}; +} + +ErrorOr<void> LinkLabel::create_menus() +{ + m_context_menu = TRY(Menu::try_create()); + TRY(m_context_menu->try_add_action(*m_open_action)); + TRY(m_context_menu->try_add_separator()); + TRY(m_context_menu->try_add_action(*m_copy_action)); + return {}; } void LinkLabel::set_hovered(bool hover) @@ -116,12 +133,6 @@ void LinkLabel::resize_event(ResizeEvent& event) void LinkLabel::context_menu_event(ContextMenuEvent& event) { - if (!m_context_menu) { - m_context_menu = Menu::construct(); - m_context_menu->add_action(*m_open_action); - m_context_menu->add_separator(); - m_context_menu->add_action(*m_copy_action); - } m_context_menu->popup(event.screen_position(), m_open_action); } diff --git a/Userland/Libraries/LibGUI/LinkLabel.h b/Userland/Libraries/LibGUI/LinkLabel.h index 36175936a7..80196c9f93 100644 --- a/Userland/Libraries/LibGUI/LinkLabel.h +++ b/Userland/Libraries/LibGUI/LinkLabel.h @@ -11,14 +11,19 @@ namespace GUI { class LinkLabel : public Label { - C_OBJECT(LinkLabel); + C_OBJECT_ABSTRACT(LinkLabel); public: + static ErrorOr<NonnullRefPtr<LinkLabel>> try_create(String text = {}); + Function<void()> on_click; private: explicit LinkLabel(String text = {}); + ErrorOr<void> create_actions(); + ErrorOr<void> create_menus(); + virtual void mousemove_event(MouseEvent&) override; virtual void mousedown_event(MouseEvent&) override; virtual void paint_event(PaintEvent&) override; @@ -30,7 +35,6 @@ private: virtual void did_change_text() override; void update_tooltip_if_needed(); - void setup_actions(); void set_hovered(bool); RefPtr<Menu> m_context_menu; |