dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / painting / radio_button_paintable.lucb
#==============================================================================================
#
# radio_button_paintable - Painting::RadioButtonPaintable: the paintable of
# <input type=radio>
#
# DESCRIPTION:
# Port of LibWeb/Painting/RadioButtonPaintable.cpp (lines 1-89) and
# RadioButtonPaintable.h: creation, and painting the button's circles (a 1px outer and
# 2px inner border at 13x13) in the input colors of its color scheme and accent color.
#
# Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2023, MacDue <macdue@dueutil.tech>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/Painting/RadioButtonPaintable.cpp:20 Web::Painting::RadioButtonPaintable::create.
pub func paint_radio_button_paintable_create(layout_box: LayoutRadioButton*) -> PaintRadioButtonPaintable*:
let heap = gc.cell_heap(layout_box.cell())
let this = (PaintRadioButtonPaintable*)web_heap_allocate_cell(heap, &paint_radio_button_paintable_class)
gc.heap_defer_gc(heap)
paint_radio_button_paintable_construct(this, layout_box)
gc.heap_undefer_gc(heap)
return this
## Ported from Libraries/LibWeb/Painting/RadioButtonPaintable.cpp:25 Web::Painting::RadioButtonPaintable::RadioButtonPaintable.
pub func paint_radio_button_paintable_construct(this: PaintRadioButtonPaintable*, layout_box: LayoutRadioButton*):
paint_paintable_box_construct_layout_box(this.paint_paintable_box(), layout_box.layout_box())
## Ported from Libraries/LibWeb/Painting/RadioButtonPaintable.cpp:40 the `draw_circle` lambda of
## Web::Painting::RadioButtonPaintable::paint.
func paint_radio_button_paintable_draw_circle(context: DisplayListRecordingContext*, rect: gfx.DevicePixelRect, color: gfx.Color):
# Note: Doing this is a bit more forgiving than draw_circle() which will round to the nearest even radius.
# This will fudge it (which works better here).
display_list.display_list_recorder_fill_rect_with_rounded_corners_i32(display_list_recording_context_display_list_recorder(context), rect, color, gfx.device_pixel_rect_width(&rect) // 2)
## Ported from Libraries/LibWeb/Painting/RadioButtonPaintable.cpp:46 the `shrink_all` lambda of
## Web::Painting::RadioButtonPaintable::paint.
func paint_radio_button_paintable_shrink_all(rect: gfx.DevicePixelRect, amount: i32) -> gfx.DevicePixelRect:
return gfx.device_pixel_rect_shrunken_i32_i32_i32(&rect, amount, amount, amount, amount)
## Ported from Libraries/LibWeb/Painting/RadioButtonPaintable.cpp:30 Web::Painting::RadioButtonPaintable::paint (override).
pub func paint_radio_button_paintable_paint_impl(base: PaintPaintable*, context: DisplayListRecordingContext*, phase: PaintPhase):
if not paint_paintable_is_visible(base):
return
paint_paintable_box_paint_impl(base, context, phase)
if phase != PaintPhase.foreground:
return
let radio_button = (HtmlInputElement*)(paint_paintable_dom_node(base) else trap("VERIFY: a radio button paintable has a DOM node"))
let enabled = html_form_associated_element_enabled(&radio_button.html_element().form_associated_element)
let computed_values = &paint_paintable_computed_values(base).computed_values
var input_colors = paint_compute_input_colors(css_computed_values_color_scheme(computed_values), css_computed_values_accent_color(computed_values))
var background_color = paint_input_colors_background_color(&input_colors, enabled)
let accent = input_colors.accent
# The `radio_color` and `fill_color` lambdas (radio_color may darken the background).
var fill_color = input_colors.mid_gray
if enabled:
var radio_color = input_colors.gray
if html_input_element_checked(radio_button):
# Handle the awkward case where a light color has been used for the accent color.
var accent_color = accent
if gfx.color_contrast_ratio(&accent_color, background_color) < 2.0f32 and gfx.color_contrast_ratio(&accent_color, input_colors.dark_gray) > 2.0f32:
background_color = input_colors.dark_gray
radio_color = accent
fill_color = radio_color
# FIXME: Make this only take effect while this element or its labels are hovered.
if dom_element_is_being_activated(radio_button.dom_element()):
fill_color = paint_input_colors_get_shade(fill_color, 0.3f32, css_computed_values_color_scheme(computed_values))
# This is based on a 1px outer border and 2px inner border when drawn at 13x13.
let radio_button_rect = display_list_recording_context_enclosing_device_rect(context, paint_paintable_box_absolute_rect((PaintPaintableBox*)base))
let outer_border_width = ak.max(1i32, (i32)math.ceil(f64((f32)gfx.device_pixel_rect_width(&radio_button_rect) / 13.0f32)))
let inner_border_width = ak.max(2i32, (i32)math.ceil(f64((f32)gfx.device_pixel_rect_width(&radio_button_rect) / 4.0f32)))
paint_radio_button_paintable_draw_circle(context, radio_button_rect, fill_color)
paint_radio_button_paintable_draw_circle(context, paint_radio_button_paintable_shrink_all(radio_button_rect, outer_border_width), background_color)
if html_input_element_checked(radio_button):
paint_radio_button_paintable_draw_circle(context, paint_radio_button_paintable_shrink_all(radio_button_rect, inner_border_width), fill_color)