KeyButton.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * Copyright (c) 2021, Rasmus Nylander <RasmusNylander.SerenityOS@gmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "KeyButton.h"
  9. #include <LibGUI/Painter.h>
  10. #include <LibGUI/Window.h>
  11. #include <LibGfx/Font/Font.h>
  12. #include <LibGfx/Palette.h>
  13. void KeyButton::paint_event(GUI::PaintEvent& event)
  14. {
  15. GUI::Painter painter(*this);
  16. painter.add_clip_rect(event.rect());
  17. auto cont_rect = rect();
  18. auto& font = this->font();
  19. Color face_color;
  20. if (m_pressed) {
  21. face_color = Color::Cyan;
  22. } else if (!is_enabled()) {
  23. face_color = Color::LightGray;
  24. } else {
  25. face_color = Color::White;
  26. }
  27. Gfx::IntRect key_cap_side_rect = { cont_rect.x() + 1, cont_rect.y() + 1, cont_rect.width() - 2, cont_rect.height() - 2 };
  28. Gfx::IntRect key_cap_face_border_rect = { cont_rect.x() + 6, cont_rect.y() + 3, cont_rect.width() - 12, cont_rect.height() - 12 };
  29. Gfx::IntRect key_cap_face_rect = { cont_rect.x() + 7, cont_rect.y() + 4, cont_rect.width() - 14, cont_rect.height() - 14 };
  30. painter.draw_rect(cont_rect, Color::Black); // Key cap border
  31. painter.fill_rect(key_cap_side_rect, Color::from_rgb(0x999999));
  32. painter.draw_rect(key_cap_face_border_rect, Color::from_rgb(0x8C7272), false);
  33. painter.fill_rect(key_cap_face_rect, face_color);
  34. if (text().is_empty() || text().starts_with('\0'))
  35. return;
  36. Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  37. text_rect.align_within(key_cap_face_rect, Gfx::TextAlignment::Center);
  38. painter.draw_text(text_rect, text(), font, Gfx::TextAlignment::Center, Color::Black, Gfx::TextElision::Right);
  39. if (is_focused())
  40. painter.draw_rect(text_rect.inflated(6, 4), palette().focus_outline());
  41. }
  42. void KeyButton::click(unsigned)
  43. {
  44. if (on_click && m_face_hovered)
  45. on_click();
  46. }
  47. void KeyButton::mousemove_event(GUI::MouseEvent& event)
  48. {
  49. if (!is_enabled())
  50. return;
  51. Gfx::IntRect key_cap_face_rect = { rect().x() + 7, rect().y() + 4, rect().width() - 14, rect().height() - 14 };
  52. set_face_hovered(key_cap_face_rect.contains(event.position()));
  53. AbstractButton::mousemove_event(event);
  54. }
  55. void KeyButton::leave_event(Core::Event& event)
  56. {
  57. set_face_hovered(false);
  58. AbstractButton::leave_event(event);
  59. }
  60. void KeyButton::set_face_hovered(bool value)
  61. {
  62. m_face_hovered = value;
  63. if (m_face_hovered)
  64. set_override_cursor(Gfx::StandardCursor::Hand);
  65. else
  66. set_override_cursor(Gfx::StandardCursor::None);
  67. }