RadioButtonPaintable.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/BrowsingContext.h>
  9. #include <LibWeb/HTML/HTMLInputElement.h>
  10. #include <LibWeb/Layout/Label.h>
  11. #include <LibWeb/Layout/RadioButton.h>
  12. #include <LibWeb/Painting/InputColors.h>
  13. #include <LibWeb/Painting/RadioButtonPaintable.h>
  14. namespace Web::Painting {
  15. JS_DEFINE_ALLOCATOR(RadioButtonPaintable);
  16. JS::NonnullGCPtr<RadioButtonPaintable> RadioButtonPaintable::create(Layout::RadioButton const& layout_box)
  17. {
  18. return layout_box.heap().allocate_without_realm<RadioButtonPaintable>(layout_box);
  19. }
  20. RadioButtonPaintable::RadioButtonPaintable(Layout::RadioButton const& layout_box)
  21. : LabelablePaintable(layout_box)
  22. {
  23. }
  24. void RadioButtonPaintable::paint(PaintContext& context, PaintPhase phase) const
  25. {
  26. if (!is_visible())
  27. return;
  28. PaintableBox::paint(context, phase);
  29. if (phase != PaintPhase::Foreground)
  30. return;
  31. auto draw_circle = [&](auto const& rect, Color color) {
  32. // Note: Doing this is a bit more forgiving than draw_circle() which will round to the nearset even radius.
  33. // This will fudge it (which works better here).
  34. context.display_list_recorder().fill_rect_with_rounded_corners(rect, color, rect.width() / 2);
  35. };
  36. auto shrink_all = [&](auto const& rect, int amount) {
  37. return rect.shrunken(amount, amount, amount, amount);
  38. };
  39. auto const& radio_button = static_cast<HTML::HTMLInputElement const&>(layout_box().dom_node());
  40. auto& palette = context.palette();
  41. bool enabled = layout_box().dom_node().enabled();
  42. auto input_colors = compute_input_colors(palette, computed_values().accent_color());
  43. auto background_color = input_colors.background_color(enabled);
  44. auto accent = input_colors.accent;
  45. auto radio_color = [&] {
  46. if (radio_button.checked()) {
  47. // Handle the awkward case where a light color has been used for the accent color.
  48. if (accent.contrast_ratio(background_color) < 2 && accent.contrast_ratio(input_colors.dark_gray) > 2)
  49. background_color = input_colors.dark_gray;
  50. return accent;
  51. }
  52. return input_colors.gray;
  53. };
  54. auto fill_color = [&] {
  55. if (!enabled)
  56. return input_colors.mid_gray;
  57. auto color = radio_color();
  58. if (being_pressed())
  59. color = InputColors::get_shade(color, 0.3f, palette.is_dark());
  60. return color;
  61. }();
  62. // This is based on a 1px outer border and 2px inner border when drawn at 13x13.
  63. auto radio_button_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
  64. auto outer_border_width = max(1, static_cast<int>(ceilf(radio_button_rect.width() / 13.0f)));
  65. auto inner_border_width = max(2, static_cast<int>(ceilf(radio_button_rect.width() / 4.0f)));
  66. draw_circle(radio_button_rect, fill_color);
  67. draw_circle(shrink_all(radio_button_rect, outer_border_width), background_color);
  68. if (radio_button.checked())
  69. draw_circle(shrink_all(radio_button_rect, inner_border_width), fill_color);
  70. }
  71. }