1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:27:45 +00:00

LibGUI: Propagate construction errors in LinkLabel

This commit is contained in:
thankyouverycool 2023-04-29 10:41:55 -04:00 committed by Andreas Kling
parent 91bafc2653
commit b17d4a0ced
2 changed files with 26 additions and 11 deletions

View file

@ -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);
}

View file

@ -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;