LinkLabel.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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. void LinkLabel::create_menus()
  43. {
  44. m_context_menu = Menu::construct();
  45. m_context_menu->add_action(*m_open_action);
  46. m_context_menu->add_separator();
  47. m_context_menu->add_action(*m_copy_action);
  48. }
  49. void LinkLabel::set_hovered(bool hover)
  50. {
  51. if (hover == m_hovered)
  52. return;
  53. m_hovered = hover;
  54. set_override_cursor(hover ? Gfx::StandardCursor::Hand : Gfx::StandardCursor::None);
  55. update();
  56. }
  57. void LinkLabel::mousemove_event(MouseEvent& event)
  58. {
  59. constexpr int extra_target_width = 3;
  60. set_hovered(event.position().x() <= font().width(text()) + extra_target_width);
  61. }
  62. void LinkLabel::mousedown_event(MouseEvent& event)
  63. {
  64. if (event.button() != MouseButton::Primary)
  65. return;
  66. Label::mousedown_event(event);
  67. if (m_hovered && on_click) {
  68. on_click();
  69. }
  70. }
  71. void LinkLabel::keydown_event(KeyEvent& event)
  72. {
  73. Label::keydown_event(event);
  74. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  75. if (on_click)
  76. on_click();
  77. }
  78. }
  79. void LinkLabel::paint_event(PaintEvent& event)
  80. {
  81. Label::paint_event(event);
  82. GUI::Painter painter(*this);
  83. if (m_hovered)
  84. painter.draw_line({ 0, rect().bottom() - 1 }, { font().width_rounded_up(text()), rect().bottom() - 1 }, palette().link());
  85. if (is_focused())
  86. painter.draw_focus_rect(text_rect(), palette().focus_outline());
  87. }
  88. void LinkLabel::leave_event(Core::Event& event)
  89. {
  90. Label::leave_event(event);
  91. set_hovered(false);
  92. }
  93. void LinkLabel::did_change_text()
  94. {
  95. Label::did_change_text();
  96. update_tooltip_if_needed();
  97. }
  98. void LinkLabel::update_tooltip_if_needed()
  99. {
  100. if (width() < font().width(text())) {
  101. set_tooltip_deprecated(text().to_deprecated_string());
  102. } else {
  103. set_tooltip_deprecated({});
  104. }
  105. }
  106. void LinkLabel::resize_event(ResizeEvent& event)
  107. {
  108. Label::resize_event(event);
  109. update_tooltip_if_needed();
  110. }
  111. void LinkLabel::context_menu_event(ContextMenuEvent& event)
  112. {
  113. m_context_menu->popup(event.screen_position(), m_open_action);
  114. }
  115. }