LinkLabel.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2020, Alex McGrath <amk@amk.ie>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Action.h>
  27. #include <LibGUI/Clipboard.h>
  28. #include <LibGUI/Event.h>
  29. #include <LibGUI/LinkLabel.h>
  30. #include <LibGUI/Menu.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/Window.h>
  33. #include <LibGfx/Font.h>
  34. #include <LibGfx/Palette.h>
  35. REGISTER_WIDGET(GUI, LinkLabel)
  36. namespace GUI {
  37. LinkLabel::LinkLabel(String text)
  38. : Label(move(text))
  39. {
  40. set_override_cursor(Gfx::StandardCursor::Hand);
  41. set_foreground_role(Gfx::ColorRole::Link);
  42. set_focus_policy(FocusPolicy::TabFocus);
  43. setup_actions();
  44. }
  45. void LinkLabel::setup_actions()
  46. {
  47. m_open_action = CommonActions::make_open_action([this](auto&) {
  48. if (on_click)
  49. on_click();
  50. },
  51. this);
  52. m_copy_action = CommonActions::make_copy_action([this](auto&) { Clipboard::the().set_plain_text(text()); }, this);
  53. }
  54. void LinkLabel::mousedown_event(MouseEvent& event)
  55. {
  56. if (event.button() != MouseButton::Left)
  57. return;
  58. Label::mousedown_event(event);
  59. if (on_click) {
  60. on_click();
  61. }
  62. }
  63. void LinkLabel::keydown_event(KeyEvent& event)
  64. {
  65. Label::keydown_event(event);
  66. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  67. if (on_click)
  68. on_click();
  69. }
  70. }
  71. void LinkLabel::paint_event(PaintEvent& event)
  72. {
  73. Label::paint_event(event);
  74. GUI::Painter painter(*this);
  75. if (m_hovered)
  76. painter.draw_line({ 0, rect().bottom() }, { font().width(text()), rect().bottom() }, palette().link());
  77. if (is_focused())
  78. painter.draw_focus_rect(text_rect(), palette().focus_outline());
  79. }
  80. void LinkLabel::enter_event(Core::Event& event)
  81. {
  82. Label::enter_event(event);
  83. m_hovered = true;
  84. update();
  85. }
  86. void LinkLabel::leave_event(Core::Event& event)
  87. {
  88. Label::leave_event(event);
  89. m_hovered = false;
  90. update();
  91. }
  92. void LinkLabel::did_change_text()
  93. {
  94. Label::did_change_text();
  95. update_tooltip_if_needed();
  96. }
  97. void LinkLabel::update_tooltip_if_needed()
  98. {
  99. if (width() < font().width(text())) {
  100. set_tooltip(text());
  101. } else {
  102. set_tooltip({});
  103. }
  104. }
  105. void LinkLabel::resize_event(ResizeEvent& event)
  106. {
  107. Label::resize_event(event);
  108. update_tooltip_if_needed();
  109. }
  110. void LinkLabel::context_menu_event(ContextMenuEvent& event)
  111. {
  112. if (!m_context_menu) {
  113. m_context_menu = Menu::construct();
  114. m_context_menu->add_action(*m_open_action);
  115. m_context_menu->add_separator();
  116. m_context_menu->add_action(*m_copy_action);
  117. }
  118. m_context_menu->popup(event.screen_position(), m_open_action);
  119. }
  120. }