Skip to content

Truly independent web browser

7.2 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

use crate::css::css_pixels::{CssPixelPoint, CssPixelRect};
use crate::layout::node_data::NodeSlotId;
use crate::painting::ffi::FfiChromeMetrics;
use crate::painting::force_dark::ForceDarkSettings;
use crate::painting::host::{FfiFlexOverlayInput, FfiGridOverlayInput, RootBackgroundSource};
use libgfx_rust::font::FontHandle;
use libgfx_rust::{Color, IntRect, IntSize};

/// The inputs read by content that is recorded outside per-box captures: scroll metadata,
/// viewport scrollbars, the wheel-target facts of hit-test items. Reusing a subtree capture from
/// the previous tape requires this whole bundle to be unchanged, so every reader takes these
/// values from here and a new one is part of that check automatically.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct UncapturedContentInputs {
    pub root_background_source: RootBackgroundSource,
    // Scroll commands use a scrollport at the origin. Its position is compositor state.
    pub device_viewport_size: IntSize,
    pub has_blocking_wheel_event_region_covering_viewport: bool,
    // The compositor hands a wheel over a nested context to it only where no box painted later covers it, which it can
    // tell only from the wheel hit test targets of every box.
    pub has_navigable_container_with_composited_context: bool,
    pub chrome_metrics: FfiChromeMetrics,
    pub paint_viewport_scrollbars: bool,
    pub middle_button_scroll_origin: Option<CssPixelPoint>,
    pub canvas_color: Color,
    pub background_color: Color,
}

/// The inputs of one recording but for what the frame it records decides, which only the frame frozen for the recording
/// fills in: the host makes them before the render owner freezes the frame.
pub(crate) struct UnframedRecordingInputs(pub(in crate::painting) RecordingInputs);

impl UnframedRecordingInputs {
    /// The inputs of the recording of a frame frozen with `tree_inputs`, whose canvas background `root_background_source`
    /// paints.
    pub(crate) fn for_frame(
        self,
        tree_inputs: crate::painting::host::FfiVisualContextTreeInputs,
        root_background_source: RootBackgroundSource,
    ) -> RecordingInputs {
        let Self(mut inputs) = self;
        inputs.device_pixels_per_css_pixel = tree_inputs.device_pixels_per_css_pixel;
        inputs.uncaptured.root_background_source = root_background_source;
        inputs
    }
}

/// The inputs of one recording, which own what they read, so that the recording may outlive the
/// call that started it. Recording results retain their own resources and never borrow these inputs.
#[derive(Clone)]
pub(crate) struct RecordingInputs {
    pub device_pixels_per_css_pixel: f64,
    pub uncaptured: UncapturedContentInputs,
    // Carried on the recording so the compositor can tell which listener state it saw; no
    // recorded content reads it.
    pub wheel_event_listener_state_generation: u64,
    pub css_viewport_rect: CssPixelRect,
    pub should_show_line_box_borders: bool,
    pub force_dark_enabled: bool,
    pub force_dark_settings: ForceDarkSettings,
    pub should_paint_overlay: bool,
    /// Cuts the plans a recording walks as finely as they go: every child scope with a bound is walked on its own,
    /// and a run holds only what starts in one pixel row. Tests ask for it to see which boxes a change records
    /// again on pages too small to span the rows a recording is otherwise cut along.
    pub records_in_finest_runs: bool,
    /// Records everything the document paints, wherever it is, and not just what shows in a window around the
    /// viewport: what a screenshot of more than the viewport and a dump of the display list read.
    pub records_whole_document: bool,
    /// Notes beside the records which box and phase recorded each and the text each glyph run draws, for the display
    /// list dump.
    pub notes_record_origins: bool,
    pub canvas_fill_rect: Option<IntRect>,
    pub opaque_canvas: bool,
    pub bitmap_rect: IntRect,
    pub publishes_recording: bool,
    /// What the compositor clears the surface with below the recording, for a top-level traversable.
    pub surface_clear_color: Option<Color>,
    /// What the recording's display list is stamped with for async scrolling, but for the keyboard scroll state.
    pub async_scrolling_metadata: Option<libcompositing_rust::ipc_types::Compositing_AsyncScrollingMetadata>,
    pub window_is_focused: bool,
    pub outline_auto_color: Color,
    pub selection_background_from_palette: Color,
    pub selection_background_light: Color,
    pub selection_background_dark: Color,
    pub inactive_selection_background_from_palette: Color,
    pub inactive_selection_background_light: Color,
    pub inactive_selection_background_dark: Color,
    pub palette_is_dark: bool,
    // Whether the document's supported color schemes name `light` or `dark`.
    pub document_supports_light_or_dark: bool,
    // The color scheme an SVG image renders in where neither the box that draws it nor its document opts into one.
    pub image_fallback_color_scheme: u8,
    // Whether the document is an SVG image's, whose records go into the tapes of the documents that draw the image: the
    // transforms, clips and effects of its boxes are recorded in the records, which name no visual context.
    pub records_without_visual_contexts: bool,
    // The paintable the assistive technology's browse-mode focus is on, which paints the AT focus ring.
    pub accessibility_focus_target: Option<NodeSlotId>,
    pub inspector_highlight: Option<InspectorHighlight>,
    pub tooltip_color: Color,
    pub tooltip_text_color: Color,
    pub tooltip_border_color: Color,
    pub grid_overlays: Option<GridOverlays>,
    // These array elements are already plain values without pointers or optional-value tags.
    pub flex_overlays: Box<[FfiFlexOverlayInput]>,
    pub caret_debug_rect: Option<CssPixelRect>,
    pub caret: Option<CaretPaint>,
    pub focused_text_control: Option<FocusedTextControlSelection>,
    pub focused_area_outline: Option<FocusedAreaOutline>,
    pub text_fragment_indications: Box<[crate::painting::host::FfiTextFragmentIndicationRange]>,
    pub mark_color: Color,
    pub mark_text_color: Color,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum CaretTarget {
    InBlock {
        block: NodeSlotId,
        owner: Option<NodeSlotId>,
    },
    EmptyInline(NodeSlotId),
}

#[derive(Clone, Copy)]
pub(crate) struct CaretPaint {
    pub target: CaretTarget,
    pub rect: CssPixelRect,
    pub color: Color,
    pub blink_cycle_start_time_ns: i64,
    pub should_blink: bool,
}

#[derive(Clone, Copy)]
pub(crate) struct FocusedTextControlSelection {
    pub text_node: NodeSlotId,
    pub start: usize,
    pub end: usize,
}

#[derive(Clone)]
pub(crate) struct FocusedAreaOutline {
    pub image: NodeSlotId,
    /// The style-tree identity of the area.
    pub area: u32,
}

#[derive(Clone)]
pub(crate) struct OverlayLabelFonts {
    pub css_font: FontHandle,
    pub device_font: FontHandle,
}

#[derive(Clone)]
pub(crate) struct InspectorHighlight {
    pub paintable: NodeSlotId,
    pub label: String,
    pub fonts: OverlayLabelFonts,
}

#[derive(Clone)]
pub(crate) struct GridOverlays {
    pub inputs: Box<[FfiGridOverlayInput]>,
    pub fonts: OverlayLabelFonts,
}