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> 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 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 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> try_create(String text = {}); + Function on_click; private: explicit LinkLabel(String text = {}); + ErrorOr create_actions(); + ErrorOr 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 m_context_menu;