LinkLabel.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. ErrorOr<NonnullRefPtr<LinkLabel>> LinkLabel::try_create(String text)
  19. {
  20. auto label = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) LinkLabel(move(text))));
  21. TRY(label->create_actions());
  22. TRY(label->create_menus());
  23. return label;
  24. }
  25. LinkLabel::LinkLabel(String text)
  26. : Label(move(text))
  27. {
  28. set_foreground_role(Gfx::ColorRole::Link);
  29. set_focus_policy(FocusPolicy::TabFocus);
  30. }
  31. ErrorOr<void> LinkLabel::create_actions()
  32. {
  33. m_open_action = GUI::Action::create(
  34. "Show in File Manager", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-file-manager.png"sv)), [&](const GUI::Action&) {
  35. if (on_click)
  36. on_click();
  37. },
  38. this);
  39. m_copy_action = CommonActions::make_copy_action([this](auto&) { Clipboard::the().set_plain_text(text()); }, this);
  40. return {};
  41. }
  42. ErrorOr<void> LinkLabel::create_menus()
  43. {
  44. m_context_menu = TRY(Menu::try_create());
  45. TRY(m_context_menu->try_add_action(*m_open_action));
  46. TRY(m_context_menu->try_add_separator());
  47. TRY(m_context_menu->try_add_action(*m_copy_action));
  48. return {};
  49. }
  50. void LinkLabel::set_hovered(bool hover)
  51. {
  52. if (hover == m_hovered)
  53. return;
  54. m_hovered = hover;
  55. set_override_cursor(hover ? Gfx::StandardCursor::Hand : Gfx::StandardCursor::None);
  56. update();
  57. }
  58. void LinkLabel::mousemove_event(MouseEvent& event)
  59. {
  60. constexpr int extra_target_width = 3;
  61. set_hovered(event.position().x() <= font().width(text()) + extra_target_width);
  62. }
  63. void LinkLabel::mousedown_event(MouseEvent& event)
  64. {
  65. if (event.button() != MouseButton::Primary)
  66. return;
  67. Label::mousedown_event(event);
  68. if (m_hovered && on_click) {
  69. on_click();
  70. }
  71. }
  72. void LinkLabel::keydown_event(KeyEvent& event)
  73. {
  74. Label::keydown_event(event);
  75. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  76. if (on_click)
  77. on_click();
  78. }
  79. }
  80. void LinkLabel::paint_event(PaintEvent& event)
  81. {
  82. Label::paint_event(event);
  83. GUI::Painter painter(*this);
  84. if (m_hovered)
  85. painter.draw_line({ 0, rect().bottom() - 1 }, { font().width_rounded_up(text()), rect().bottom() - 1 }, palette().link());
  86. if (is_focused())
  87. painter.draw_focus_rect(text_rect(), palette().focus_outline());
  88. }
  89. void LinkLabel::leave_event(Core::Event& event)
  90. {
  91. Label::leave_event(event);
  92. set_hovered(false);
  93. }
  94. void LinkLabel::did_change_text()
  95. {
  96. Label::did_change_text();
  97. update_tooltip_if_needed();
  98. }
  99. void LinkLabel::update_tooltip_if_needed()
  100. {
  101. if (width() < font().width(text())) {
  102. set_tooltip(text().to_deprecated_string());
  103. } else {
  104. set_tooltip({});
  105. }
  106. }
  107. void LinkLabel::resize_event(ResizeEvent& event)
  108. {
  109. Label::resize_event(event);
  110. update_tooltip_if_needed();
  111. }
  112. void LinkLabel::context_menu_event(ContextMenuEvent& event)
  113. {
  114. m_context_menu->popup(event.screen_position(), m_open_action);
  115. }
  116. }