LibGUI: Show context menu when right-clicking a LinkLabel

Fixes #4794
This commit is contained in:
Mițca Dumitru 2021-02-22 00:06:38 +02:00 committed by Andreas Kling
parent 8d2d080923
commit 8d6525fc50
2 changed files with 35 additions and 0 deletions

View file

@ -24,8 +24,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGUI/Action.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/Event.h>
#include <LibGUI/LinkLabel.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
#include <LibGfx/Font.h>
@ -41,10 +44,25 @@ LinkLabel::LinkLabel(String text)
set_override_cursor(Gfx::StandardCursor::Hand);
set_foreground_role(Gfx::ColorRole::Link);
set_focus_policy(FocusPolicy::TabFocus);
setup_actions();
}
void LinkLabel::setup_actions()
{
m_open_action = CommonActions::make_open_action([this](auto&) {
if (on_click)
on_click();
},
this);
m_copy_action = CommonActions::make_copy_action([this](auto&) { Clipboard::the().set_plain_text(text()); }, this);
}
void LinkLabel::mousedown_event(MouseEvent& event)
{
if (event.button() != MouseButton::Left)
return;
Label::mousedown_event(event);
if (on_click) {
on_click();
@ -107,4 +125,15 @@ void LinkLabel::resize_event(ResizeEvent& event)
update_tooltip_if_needed();
}
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

@ -45,10 +45,16 @@ private:
virtual void enter_event(Core::Event&) override;
virtual void leave_event(Core::Event&) override;
virtual void keydown_event(KeyEvent&) override;
virtual void context_menu_event(ContextMenuEvent&) override;
virtual void did_change_text() override;
void update_tooltip_if_needed();
void setup_actions();
RefPtr<Menu> m_context_menu;
RefPtr<Action> m_open_action;
RefPtr<Action> m_copy_action;
bool m_hovered { false };
};