ColorInput.cpp 2.9 KB

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