LibGUI: Make links only clickable where the text is

This commit is contained in:
Ben Wiederhake 2021-10-21 19:36:04 +02:00 committed by Linus Groh
parent ef1d247d79
commit 9bf6d51aec
Notes: sideshowbarker 2024-07-18 02:05:27 +09:00
2 changed files with 20 additions and 12 deletions

View file

@ -21,7 +21,6 @@ namespace GUI {
LinkLabel::LinkLabel(String text) LinkLabel::LinkLabel(String text)
: Label(move(text)) : Label(move(text))
{ {
set_override_cursor(Gfx::StandardCursor::Hand);
set_foreground_role(Gfx::ColorRole::Link); set_foreground_role(Gfx::ColorRole::Link);
set_focus_policy(FocusPolicy::TabFocus); set_focus_policy(FocusPolicy::TabFocus);
setup_actions(); setup_actions();
@ -37,13 +36,29 @@ void LinkLabel::setup_actions()
m_copy_action = CommonActions::make_copy_action([this](auto&) { Clipboard::the().set_plain_text(text()); }, this); m_copy_action = CommonActions::make_copy_action([this](auto&) { Clipboard::the().set_plain_text(text()); }, this);
} }
void LinkLabel::set_hovered(bool hover)
{
if (hover == m_hovered)
return;
m_hovered = hover;
set_override_cursor(hover ? Gfx::StandardCursor::Hand : Gfx::StandardCursor::None);
update();
}
void LinkLabel::mousemove_event(MouseEvent& event)
{
static const int extra_target_width = 3;
set_hovered(event.position().x() <= font().width(text()) + extra_target_width);
}
void LinkLabel::mousedown_event(MouseEvent& event) void LinkLabel::mousedown_event(MouseEvent& event)
{ {
if (event.button() != MouseButton::Left) if (event.button() != MouseButton::Left)
return; return;
Label::mousedown_event(event); Label::mousedown_event(event);
if (on_click) { if (m_hovered && on_click) {
on_click(); on_click();
} }
} }
@ -69,18 +84,10 @@ void LinkLabel::paint_event(PaintEvent& event)
painter.draw_focus_rect(text_rect(), palette().focus_outline()); painter.draw_focus_rect(text_rect(), palette().focus_outline());
} }
void LinkLabel::enter_event(Core::Event& event)
{
Label::enter_event(event);
m_hovered = true;
update();
}
void LinkLabel::leave_event(Core::Event& event) void LinkLabel::leave_event(Core::Event& event)
{ {
Label::leave_event(event); Label::leave_event(event);
m_hovered = false; set_hovered(false);
update();
} }
void LinkLabel::did_change_text() void LinkLabel::did_change_text()

View file

@ -19,10 +19,10 @@ public:
private: private:
explicit LinkLabel(String text = {}); explicit LinkLabel(String text = {});
virtual void mousemove_event(MouseEvent&) override;
virtual void mousedown_event(MouseEvent&) override; virtual void mousedown_event(MouseEvent&) override;
virtual void paint_event(PaintEvent&) override; virtual void paint_event(PaintEvent&) override;
virtual void resize_event(ResizeEvent&) override; virtual void resize_event(ResizeEvent&) override;
virtual void enter_event(Core::Event&) override;
virtual void leave_event(Core::Event&) override; virtual void leave_event(Core::Event&) override;
virtual void keydown_event(KeyEvent&) override; virtual void keydown_event(KeyEvent&) override;
virtual void context_menu_event(ContextMenuEvent&) override; virtual void context_menu_event(ContextMenuEvent&) override;
@ -31,6 +31,7 @@ private:
void update_tooltip_if_needed(); void update_tooltip_if_needed();
void setup_actions(); void setup_actions();
void set_hovered(bool);
RefPtr<Menu> m_context_menu; RefPtr<Menu> m_context_menu;
RefPtr<Action> m_open_action; RefPtr<Action> m_open_action;