Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / painting / check_box_paintable.lucb

#==============================================================================================
#
#   check_box_paintable - Painting::CheckBoxPaintable: the paintable of <input type=checkbox>
#
#   DESCRIPTION:
#       Port of LibWeb/Painting/CheckBoxPaintable.cpp (lines 1-112) and CheckBoxPaintable.h:
#       the check mark path, creation, and painting a checked, unchecked or indeterminate
#       checkbox 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/CheckBoxPaintable.cpp:20 Web::Painting::check_mark_path.
func paint_check_mark_path(checkbox_rect: gfx.DevicePixelRect) -> gfx.Path:
    var path: gfx.Path
    gfx.path_move_to(&path, gfx.FloatPoint(m_x = 72.0f32, m_y = 14.0f32))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = 37.0f32, m_y = 64.0f32))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = 19.0f32, m_y = 47.0f32))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = 8.0f32, m_y = 58.0f32))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = 40.0f32, m_y = 89.0f32))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = 85.0f32, m_y = 24.0f32))
    gfx.path_close(&path)

    let checkmark_width: f32 = 100.0
    let checkmark_height: f32 = 100.0
    var scale_checkmark_to_fit = layout_identity_affine_transform()
    gfx.affine_transform_scale_f32(&scale_checkmark_to_fit, (f32)gfx.device_pixel_rect_width(&checkbox_rect) / checkmark_width, (f32)gfx.device_pixel_rect_height(&checkbox_rect) / checkmark_height)
    return gfx.path_copy_transformed(&path, scale_checkmark_to_fit)

## Ported from Libraries/LibWeb/Painting/CheckBoxPaintable.cpp:39 Web::Painting::CheckBoxPaintable::create.
pub func paint_check_box_paintable_create(layout_box: LayoutCheckBox*) -> PaintCheckBoxPaintable*:
    let heap = gc.cell_heap(layout_box.cell())
    let this = (PaintCheckBoxPaintable*)web_heap_allocate_cell(heap, &paint_check_box_paintable_class)
    gc.heap_defer_gc(heap)
    paint_check_box_paintable_construct(this, layout_box)
    gc.heap_undefer_gc(heap)
    return this

## Ported from Libraries/LibWeb/Painting/CheckBoxPaintable.cpp:44 Web::Painting::CheckBoxPaintable::CheckBoxPaintable.
pub func paint_check_box_paintable_construct(this: PaintCheckBoxPaintable*, layout_box: LayoutCheckBox*):
    paint_paintable_box_construct_layout_box(this.paint_paintable_box(), layout_box.layout_box())

## The state the `shade` and `modify_color` lambdas of CheckBoxPaintable::paint capture by reference.
struct PaintCheckBoxPaintablePaintCaptures:
    var color_scheme: CssPreferredColorScheme
    var checkbox: HtmlInputElement*
    var enabled: bool

## Ported from Libraries/LibWeb/Painting/CheckBoxPaintable.cpp:64 the `shade` lambda of
## Web::Painting::CheckBoxPaintable::paint.
func paint_check_box_paintable_shade(captures: const PaintCheckBoxPaintablePaintCaptures*, color: gfx.Color, amount: f32) -> gfx.Color:
    return paint_input_colors_get_shade(color, amount, captures.color_scheme)

## Ported from Libraries/LibWeb/Painting/CheckBoxPaintable.cpp:68 the `modify_color` lambda of
## Web::Painting::CheckBoxPaintable::paint.
func paint_check_box_paintable_modify_color(captures: const PaintCheckBoxPaintablePaintCaptures*, color: gfx.Color) -> gfx.Color:
    # FIXME: Make this only take effect while this element or its labels are hovered.
    if dom_element_is_being_activated(captures.checkbox.dom_element()) and captures.enabled:
        return paint_check_box_paintable_shade(captures, color, 0.3f32)
    return color

## Ported from Libraries/LibWeb/Painting/CheckBoxPaintable.cpp:77 the `increase_contrast` lambda of
## Web::Painting::CheckBoxPaintable::paint.
func paint_check_box_paintable_increase_contrast(color: gfx.Color, background: gfx.Color) -> gfx.Color:
    let min_contrast: f32 = 2.0
    var result = color
    if gfx.color_contrast_ratio(&result, background) < min_contrast:
        result = gfx.color_inverted(&result)
        if gfx.color_contrast_ratio(&result, background) > min_contrast:
            return result
    return result

## Ported from Libraries/LibWeb/Painting/CheckBoxPaintable.cpp:49 Web::Painting::CheckBoxPaintable::paint (override).
pub func paint_check_box_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 checkbox = as_html_input_element(paint_paintable_dom_node(base) else trap("VERIFY: a checkbox paintable has a DOM node"))
    let enabled = html_form_associated_element_enabled(&checkbox.html_element().form_associated_element)
    let checkbox_rect = display_list_recording_context_enclosing_device_rect(context, paint_paintable_box_absolute_rect((PaintPaintableBox*)base))
    let checkbox_radius = gfx.device_pixel_rect_width(&checkbox_rect) // 5

    let computed_values = &paint_paintable_computed_values(base).computed_values
    let captures = PaintCheckBoxPaintablePaintCaptures(color_scheme = css_computed_values_color_scheme(computed_values), checkbox = checkbox, enabled = enabled)

    var input_colors = paint_compute_input_colors(css_computed_values_color_scheme(computed_values), css_computed_values_accent_color(computed_values))

    let recorder = display_list_recording_context_display_list_recorder(context)

    # Little heuristic that smaller things look better with more smoothness.
    if html_input_element_checked(checkbox) and not html_input_element_indeterminate(checkbox):
        let background_color = input_colors.accent if enabled else input_colors.mid_gray
        display_list.display_list_recorder_fill_rect_with_rounded_corners_i32(recorder, checkbox_rect, paint_check_box_paintable_modify_color(&captures, background_color), checkbox_radius)
        var tick_color = paint_check_box_paintable_increase_contrast(input_colors.base, background_color)
        if not enabled:
            tick_color = paint_check_box_paintable_shade(&captures, tick_color, 0.5f32)
        var path = paint_check_mark_path(checkbox_rect)
        let checkbox_location = gfx.device_pixel_rect_location(&checkbox_rect)
        gfx.path_offset(&path, gfx.int_point_to_type_f32(&checkbox_location))
        display_list.display_list_recorder_fill_path(recorder, display_list.DisplayListRecorderFillPathParams(path = path, paint_style_or_color = display_list.PaintStyleOrColor.color(tick_color)))
    else:
        let background_color = paint_input_colors_background_color(&input_colors, enabled)
        let border_thickness = ak.max(1i32, gfx.device_pixel_rect_width(&checkbox_rect) // 10)
        display_list.display_list_recorder_fill_rect_with_rounded_corners_i32(recorder, checkbox_rect, paint_check_box_paintable_modify_color(&captures, paint_input_colors_border_color(&input_colors, enabled)), checkbox_radius)
        display_list.display_list_recorder_fill_rect_with_rounded_corners_i32(recorder, gfx.device_pixel_rect_shrunken_i32_i32_i32(&checkbox_rect, border_thickness, border_thickness, border_thickness, border_thickness),
                                                                              background_color, ak.max(0i32, checkbox_radius - border_thickness))
        if html_input_element_indeterminate(checkbox):
            let radius = (i32)(0.05 * f64(gfx.device_pixel_rect_width(&checkbox_rect)))
            let dash_color = paint_check_box_paintable_increase_contrast(input_colors.dark_gray, background_color)
            let dash_rect = gfx.device_pixel_rect_inflated_i32_i32(&checkbox_rect, (i32)(-0.4 * f64(gfx.device_pixel_rect_width(&checkbox_rect))), (i32)(-0.8 * f64(gfx.device_pixel_rect_height(&checkbox_rect))))
            display_list.display_list_recorder_fill_rect_with_rounded_corners_i32(recorder, dash_rect, dash_color, radius)