LinkLabel.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 = GUI::Action::create("Show in File Manager", {}, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-file-manager.png"), [&](const GUI::Action&) {
  48. if (on_click)
  49. on_click();
  50. });
  51. m_copy_action = CommonActions::make_copy_action([this](auto&) { Clipboard::the().set_plain_text(text()); }, this);
  52. }
  53. void LinkLabel::mousedown_event(MouseEvent& event)
  54. {
  55. if (event.button() != MouseButton::Left)
  56. return;
  57. Label::mousedown_event(event);
  58. if (on_click) {
  59. on_click();
  60. }
  61. }
  62. void LinkLabel::keydown_event(KeyEvent& event)
  63. {
  64. Label::keydown_event(event);
  65. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  66. if (on_click)
  67. on_click();
  68. }
  69. }
  70. void LinkLabel::paint_event(PaintEvent& event)
  71. {
  72. Label::paint_event(event);
  73. GUI::Painter painter(*this);
  74. if (m_hovered)
  75. painter.draw_line({ 0, rect().bottom() }, { font().width(text()), rect().bottom() }, palette().link());
  76. if (is_focused())
  77. painter.draw_focus_rect(text_rect(), palette().focus_outline());
  78. }
  79. void LinkLabel::enter_event(Core::Event& event)
  80. {
  81. Label::enter_event(event);
  82. m_hovered = true;
  83. update();
  84. }
  85. void LinkLabel::leave_event(Core::Event& event)
  86. {
  87. Label::leave_event(event);
  88. m_hovered = false;
  89. update();
  90. }
  91. void LinkLabel::did_change_text()
  92. {
  93. Label::did_change_text();
  94. update_tooltip_if_needed();
  95. }
  96. void LinkLabel::update_tooltip_if_needed()
  97. {
  98. if (width() < font().width(text())) {
  99. set_tooltip(text());
  100. } else {
  101. set_tooltip({});
  102. }
  103. }
  104. void LinkLabel::resize_event(ResizeEvent& event)
  105. {
  106. Label::resize_event(event);
  107. update_tooltip_if_needed();
  108. }
  109. void LinkLabel::context_menu_event(ContextMenuEvent& event)
  110. {
  111. if (!m_context_menu) {
  112. m_context_menu = Menu::construct();
  113. m_context_menu->add_action(*m_open_action);
  114. m_context_menu->add_separator();
  115. m_context_menu->add_action(*m_copy_action);
  116. }
  117. m_context_menu->popup(event.screen_position(), m_open_action);
  118. }
  119. }