ColorInput.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/Timer.h>
  8. #include <LibGUI/ColorInput.h>
  9. #include <LibGUI/ColorPicker.h>
  10. #include <LibGUI/Painter.h>
  11. #include <LibGfx/Palette.h>
  12. REGISTER_WIDGET(GUI, ColorInput)
  13. namespace GUI {
  14. ColorInput::ColorInput()
  15. : TextEditor(TextEditor::SingleLine)
  16. {
  17. set_min_width(32);
  18. set_fixed_height(22);
  19. TextEditor::on_change = [this] {
  20. auto parsed_color = Color::from_string(text());
  21. if (parsed_color.has_value())
  22. set_color_internal(parsed_color.value(), AllowCallback::Yes, false);
  23. };
  24. REGISTER_STRING_PROPERTY("color_picker_title", color_picker_title, set_color_picker_title);
  25. REGISTER_BOOL_PROPERTY("has_alpha_channel", has_alpha_channel, set_color_has_alpha_channel);
  26. }
  27. Gfx::IntRect ColorInput::color_rect() const
  28. {
  29. auto color_box_padding = 3;
  30. auto color_box_size = height() - color_box_padding - color_box_padding;
  31. return { width() - color_box_size - color_box_padding, color_box_padding, color_box_size, color_box_size };
  32. }
  33. void ColorInput::set_color_internal(Color color, AllowCallback allow_callback, bool change_text)
  34. {
  35. if (m_color == color)
  36. return;
  37. m_color = color;
  38. if (change_text)
  39. set_text(m_color_has_alpha_channel ? color.to_string() : color.to_string_without_alpha(), AllowCallback::No);
  40. update();
  41. if (allow_callback == AllowCallback::Yes && on_change)
  42. on_change();
  43. }
  44. void ColorInput::set_color(Color color, AllowCallback allow_callback)
  45. {
  46. set_color_internal(color, allow_callback, true);
  47. };
  48. void ColorInput::mousedown_event(MouseEvent& event)
  49. {
  50. if (event.button() == MouseButton::Primary && color_rect().contains(event.position())) {
  51. m_may_be_color_rect_click = true;
  52. return;
  53. }
  54. TextEditor::mousedown_event(event);
  55. }
  56. void ColorInput::mouseup_event(MouseEvent& event)
  57. {
  58. if (event.button() == MouseButton::Primary) {
  59. bool is_color_rect_click = m_may_be_color_rect_click && color_rect().contains(event.position());
  60. m_may_be_color_rect_click = false;
  61. if (is_color_rect_click) {
  62. auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title);
  63. dialog->set_color_has_alpha_channel(m_color_has_alpha_channel);
  64. if (dialog->exec() == GUI::Dialog::ExecResult::OK)
  65. set_color(dialog->color());
  66. event.accept();
  67. return;
  68. }
  69. }
  70. TextEditor::mouseup_event(event);
  71. }
  72. void ColorInput::mousemove_event(MouseEvent& event)
  73. {
  74. if (color_rect().contains(event.position())) {
  75. set_override_cursor(Gfx::StandardCursor::Hand);
  76. event.accept();
  77. return;
  78. } else {
  79. set_override_cursor(Gfx::StandardCursor::IBeam);
  80. }
  81. TextEditor::mousemove_event(event);
  82. }
  83. void ColorInput::paint_event(PaintEvent& event)
  84. {
  85. TextEditor::paint_event(event);
  86. Painter painter(*this);
  87. painter.add_clip_rect(event.rect());
  88. painter.fill_rect(color_rect(), m_color);
  89. painter.draw_rect(color_rect(), Color::Black);
  90. }
  91. }