LinkLabel.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Alex McGrath <amk@amk.ie>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/Action.h>
  8. #include <LibGUI/Clipboard.h>
  9. #include <LibGUI/Event.h>
  10. #include <LibGUI/LinkLabel.h>
  11. #include <LibGUI/Menu.h>
  12. #include <LibGUI/Painter.h>
  13. #include <LibGUI/Window.h>
  14. #include <LibGfx/Font/Font.h>
  15. #include <LibGfx/Palette.h>
  16. REGISTER_WIDGET(GUI, LinkLabel)
  17. namespace GUI {
  18. LinkLabel::LinkLabel(DeprecatedString text)
  19. : Label(move(text))
  20. {
  21. set_foreground_role(Gfx::ColorRole::Link);
  22. set_focus_policy(FocusPolicy::TabFocus);
  23. setup_actions();
  24. }
  25. void LinkLabel::setup_actions()
  26. {
  27. m_open_action = GUI::Action::create(
  28. "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&) {
  29. if (on_click)
  30. on_click();
  31. },
  32. this);
  33. m_copy_action = CommonActions::make_copy_action([this](auto&) { Clipboard::the().set_plain_text(text()); }, this);
  34. }
  35. void LinkLabel::set_hovered(bool hover)
  36. {
  37. if (hover == m_hovered)
  38. return;
  39. m_hovered = hover;
  40. set_override_cursor(hover ? Gfx::StandardCursor::Hand : Gfx::StandardCursor::None);
  41. update();
  42. }
  43. void LinkLabel::mousemove_event(MouseEvent& event)
  44. {
  45. constexpr int extra_target_width = 3;
  46. set_hovered(event.position().x() <= font().width(text()) + extra_target_width);
  47. }
  48. void LinkLabel::mousedown_event(MouseEvent& event)
  49. {
  50. if (event.button() != MouseButton::Primary)
  51. return;
  52. Label::mousedown_event(event);
  53. if (m_hovered && on_click) {
  54. on_click();
  55. }
  56. }
  57. void LinkLabel::keydown_event(KeyEvent& event)
  58. {
  59. Label::keydown_event(event);
  60. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  61. if (on_click)
  62. on_click();
  63. }
  64. }
  65. void LinkLabel::paint_event(PaintEvent& event)
  66. {
  67. Label::paint_event(event);
  68. GUI::Painter painter(*this);
  69. if (m_hovered)
  70. painter.draw_line({ 0, rect().bottom() }, { font().width_rounded_up(text()), rect().bottom() }, palette().link());
  71. if (is_focused())
  72. painter.draw_focus_rect(text_rect(), palette().focus_outline());
  73. }
  74. void LinkLabel::leave_event(Core::Event& event)
  75. {
  76. Label::leave_event(event);
  77. set_hovered(false);
  78. }
  79. void LinkLabel::did_change_text()
  80. {
  81. Label::did_change_text();
  82. update_tooltip_if_needed();
  83. }
  84. void LinkLabel::update_tooltip_if_needed()
  85. {
  86. if (width() < font().width(text())) {
  87. set_tooltip(text());
  88. } else {
  89. set_tooltip({});
  90. }
  91. }
  92. void LinkLabel::resize_event(ResizeEvent& event)
  93. {
  94. Label::resize_event(event);
  95. update_tooltip_if_needed();
  96. }
  97. void LinkLabel::context_menu_event(ContextMenuEvent& event)
  98. {
  99. if (!m_context_menu) {
  100. m_context_menu = Menu::construct();
  101. m_context_menu->add_action(*m_open_action);
  102. m_context_menu->add_separator();
  103. m_context_menu->add_action(*m_copy_action);
  104. }
  105. m_context_menu->popup(event.screen_position(), m_open_action);
  106. }
  107. }