mirror of
https://github.com/lexmount/moli.git
synced 2026-11-24 08:02:58 +00:00
fix(layout): stabilize automatic scrollbars during container query feedback
This commit is contained in:
@@ -73,10 +73,11 @@ pub use normalize_source::{
|
||||
NormalizedLayoutSourceNode, NormalizedLayoutSourceTree, normalize_layout_source,
|
||||
};
|
||||
pub use pass::{
|
||||
EmbeddedFrameRenderer, EmbeddedFrameSnapshot, LayoutFrameCoverage, LayoutPassRequest,
|
||||
MeasuredLayoutPass, PreparedLayoutPass, ScreenshotLayoutRequest, build_layout_pass,
|
||||
build_layout_pass_with_embedded_frames, build_screenshot_snapshot, finish_measured_layout_pass,
|
||||
finish_prepared_layout_pass, measure_prepared_layout_pass, prepare_layout_pass,
|
||||
AutoScrollbars, EmbeddedFrameRenderer, EmbeddedFrameSnapshot, LayoutFrameCoverage,
|
||||
LayoutPassRequest, MeasuredLayoutPass, PreparedLayoutPass, ScreenshotLayoutRequest,
|
||||
build_layout_pass, build_layout_pass_with_embedded_frames, build_screenshot_snapshot,
|
||||
finish_measured_layout_pass, finish_prepared_layout_pass, measure_prepared_layout_pass,
|
||||
prepare_layout_pass,
|
||||
};
|
||||
pub use scrollbar::{
|
||||
LayoutControlSurfaceHit, LayoutScrollbarAxis, LayoutScrollbarColors, LayoutScrollbarCornerHit,
|
||||
|
||||
+105
-3
@@ -9,7 +9,8 @@ use crate::{
|
||||
projection::finish_layout_pass,
|
||||
taffy_tree::{compute_prepared_world_layout, prepare_world_layout},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::hash::Hash;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// Owned products for one embedded browsing context in the same synchronous
|
||||
@@ -280,6 +281,48 @@ where
|
||||
numeric_metrics: NumericLayoutMetrics,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
enum ScrollbarOwner<N> {
|
||||
Viewport,
|
||||
Element(N),
|
||||
Pseudo(N, crate::LayoutPseudo),
|
||||
}
|
||||
|
||||
impl<N: Copy> ScrollbarOwner<N> {
|
||||
fn for_box(layout_box: &crate::LayoutBox<N>) -> Option<Self> {
|
||||
if let Some(source) = layout_box.source() {
|
||||
return Some(Self::Element(source));
|
||||
}
|
||||
let (owner, pseudo) = layout_box.owner().zip(layout_box.pseudo())?;
|
||||
Some(Self::Pseudo(owner, pseudo))
|
||||
}
|
||||
}
|
||||
|
||||
const SCROLLBAR_AXES: [LayoutScrollbarAxis; 2] = [
|
||||
LayoutScrollbarAxis::Horizontal,
|
||||
LayoutScrollbarAxis::Vertical,
|
||||
];
|
||||
|
||||
/// Revealed automatic scrollbars, keyed by stable sources across box-tree rebuilds.
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub struct AutoScrollbars<N: Eq + Hash>(HashSet<(ScrollbarOwner<N>, LayoutScrollbarAxis)>);
|
||||
|
||||
impl<N: Eq + Hash> Default for AutoScrollbars<N> {
|
||||
fn default() -> Self {
|
||||
Self(HashSet::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Copy + Eq + Hash> AutoScrollbars<N> {
|
||||
/// Keep each axis that changed visibility within a repeated query-size cycle.
|
||||
pub fn freeze_changed(&mut self, first: &Self, second: &Self) -> bool {
|
||||
let previous_count = self.0.len();
|
||||
self.0
|
||||
.extend(first.0.symmetric_difference(&second.0).copied());
|
||||
self.0.len() != previous_count
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> MeasuredLayoutPass<N>
|
||||
where
|
||||
N: Copy + std::fmt::Debug + Eq + std::hash::Hash,
|
||||
@@ -288,6 +331,7 @@ where
|
||||
pub fn remeasure(
|
||||
self,
|
||||
services: &mut DocumentLayoutServices,
|
||||
frozen_scrollbars: &AutoScrollbars<N>,
|
||||
prepare: impl FnOnce() -> Result<PreparedLayoutPass<N>, LayoutError>,
|
||||
) -> Result<Self, LayoutError> {
|
||||
let Self {
|
||||
@@ -305,7 +349,8 @@ where
|
||||
..
|
||||
} = prepared;
|
||||
drop(world);
|
||||
let mut next = measure_prepared_layout_pass(prepare()?, services)?;
|
||||
let mut next =
|
||||
measure_prepared_layout_pass_with_scrollbars(prepare()?, services, frozen_scrollbars)?;
|
||||
next.prepared.started = started;
|
||||
next.prepared.box_tree_elapsed += box_tree_elapsed;
|
||||
next.prepared.list_marker_elapsed += list_marker_elapsed;
|
||||
@@ -316,6 +361,29 @@ where
|
||||
Ok(next)
|
||||
}
|
||||
|
||||
pub fn auto_scrollbars(&self) -> AutoScrollbars<N> {
|
||||
let world = &self.prepared.world;
|
||||
let mut scrollbars = AutoScrollbars::default();
|
||||
if world.environment.scrollbars_hidden {
|
||||
return scrollbars;
|
||||
}
|
||||
for axis in SCROLLBAR_AXES {
|
||||
if world.viewport_scroll_policy.auto_scrollbar_revealed(axis) {
|
||||
scrollbars.0.insert((ScrollbarOwner::Viewport, axis));
|
||||
}
|
||||
}
|
||||
for layout_box in &world.boxes {
|
||||
for axis in SCROLLBAR_AXES {
|
||||
if layout_box.style.auto_scrollbar_revealed(axis)
|
||||
&& let Some(owner) = ScrollbarOwner::for_box(layout_box)
|
||||
{
|
||||
scrollbars.0.insert((owner, axis));
|
||||
}
|
||||
}
|
||||
}
|
||||
scrollbars
|
||||
}
|
||||
|
||||
/// Untransformed content-box dimensions on the queryable physical axes.
|
||||
pub fn query_container_sizes(&self) -> impl Iterator<Item = (N, [Option<f32>; 2])> + '_ {
|
||||
use style::values::specified::box_::ContainerType;
|
||||
@@ -413,8 +481,19 @@ where
|
||||
}
|
||||
|
||||
pub fn measure_prepared_layout_pass<N>(
|
||||
prepared: PreparedLayoutPass<N>,
|
||||
services: &mut DocumentLayoutServices,
|
||||
) -> Result<MeasuredLayoutPass<N>, LayoutError>
|
||||
where
|
||||
N: Copy + std::fmt::Debug + Eq + std::hash::Hash,
|
||||
{
|
||||
measure_prepared_layout_pass_with_scrollbars(prepared, services, &AutoScrollbars::default())
|
||||
}
|
||||
|
||||
fn measure_prepared_layout_pass_with_scrollbars<N>(
|
||||
mut prepared: PreparedLayoutPass<N>,
|
||||
services: &mut DocumentLayoutServices,
|
||||
frozen_scrollbars: &AutoScrollbars<N>,
|
||||
) -> Result<MeasuredLayoutPass<N>, LayoutError>
|
||||
where
|
||||
N: Copy + std::fmt::Debug + Eq + std::hash::Hash,
|
||||
@@ -425,7 +504,8 @@ where
|
||||
let inline_preparation_elapsed = phase_started.elapsed();
|
||||
world.validate_invariants()?;
|
||||
let phase_started = Instant::now();
|
||||
let numeric_metrics = compute_world_layout_with_scrollbars(world, prepared.request.viewport);
|
||||
let numeric_metrics =
|
||||
compute_world_layout_with_scrollbars(world, prepared.request.viewport, frozen_scrollbars);
|
||||
let numeric_layout_elapsed = phase_started.elapsed();
|
||||
if moli_trace::cpu_profile_enabled() {
|
||||
tracing::info!(
|
||||
@@ -637,6 +717,7 @@ impl NumericLayoutMetrics {
|
||||
fn compute_world_layout_with_scrollbars<N>(
|
||||
world: &mut crate::LayoutWorld<N>,
|
||||
viewport: LayoutViewport,
|
||||
frozen_scrollbars: &AutoScrollbars<N>,
|
||||
) -> NumericLayoutMetrics
|
||||
where
|
||||
N: Copy + std::fmt::Debug + Eq + std::hash::Hash,
|
||||
@@ -647,6 +728,17 @@ where
|
||||
world
|
||||
.viewport_scroll_policy
|
||||
.prepare_scrollbar_layout(scrollbars_hidden);
|
||||
for axis in SCROLLBAR_AXES {
|
||||
if !scrollbars_hidden
|
||||
&& frozen_scrollbars
|
||||
.0
|
||||
.contains(&(ScrollbarOwner::Viewport, axis))
|
||||
{
|
||||
world
|
||||
.viewport_scroll_policy
|
||||
.reveal_auto_scrollbar(axis, true);
|
||||
}
|
||||
}
|
||||
for (index, layout_box) in world.boxes.iter_mut().enumerate() {
|
||||
let id = crate::LayoutBoxId::from_index(index);
|
||||
if id == root_id {
|
||||
@@ -657,6 +749,16 @@ where
|
||||
layout_box
|
||||
.style
|
||||
.prepare_scrollbar_layout(false, scrollbars_hidden);
|
||||
if !scrollbars_hidden
|
||||
&& !frozen_scrollbars.0.is_empty()
|
||||
&& let Some(owner) = ScrollbarOwner::for_box(layout_box)
|
||||
{
|
||||
for axis in SCROLLBAR_AXES {
|
||||
if frozen_scrollbars.0.contains(&(owner, axis)) {
|
||||
layout_box.style.reveal_auto_scrollbar(axis, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ impl LayoutScrollbarColors {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum LayoutScrollbarAxis {
|
||||
Horizontal,
|
||||
Vertical,
|
||||
|
||||
@@ -1852,7 +1852,7 @@ impl ResolvedLayoutStyle {
|
||||
}
|
||||
match self.overflow_mode(axis) {
|
||||
LayoutOverflowMode::Scroll => true,
|
||||
LayoutOverflowMode::Auto => overflowing,
|
||||
LayoutOverflowMode::Auto => overflowing || self.auto_scrollbar_revealed(axis),
|
||||
LayoutOverflowMode::Visible if is_root => overflowing,
|
||||
LayoutOverflowMode::Visible | LayoutOverflowMode::Clip | LayoutOverflowMode::Hidden => {
|
||||
false
|
||||
@@ -1860,6 +1860,14 @@ impl ResolvedLayoutStyle {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn auto_scrollbar_revealed(&self, axis: LayoutScrollbarAxis) -> bool {
|
||||
self.overflow_mode(axis) == LayoutOverflowMode::Auto
|
||||
&& match axis {
|
||||
LayoutScrollbarAxis::Horizontal => self.revealed_scrollbar_x,
|
||||
LayoutScrollbarAxis::Vertical => self.revealed_scrollbar_y,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn overflow_mode(&self, axis: LayoutScrollbarAxis) -> LayoutOverflowMode {
|
||||
match axis {
|
||||
LayoutScrollbarAxis::Horizontal => self.overflow_x,
|
||||
|
||||
@@ -431,13 +431,21 @@ impl ViewportScrollPolicy {
|
||||
}
|
||||
match self.overflow_mode(axis) {
|
||||
LayoutOverflowMode::Scroll => true,
|
||||
LayoutOverflowMode::Auto => overflowing,
|
||||
LayoutOverflowMode::Auto => overflowing || self.auto_scrollbar_revealed(axis),
|
||||
LayoutOverflowMode::Visible | LayoutOverflowMode::Clip | LayoutOverflowMode::Hidden => {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn auto_scrollbar_revealed(self, axis: LayoutScrollbarAxis) -> bool {
|
||||
self.overflow_mode(axis) == LayoutOverflowMode::Auto
|
||||
&& match axis {
|
||||
LayoutScrollbarAxis::Horizontal => self.revealed_scrollbar_x,
|
||||
LayoutScrollbarAxis::Vertical => self.revealed_scrollbar_y,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const fn scrollbar_control_thickness(self) -> f32 {
|
||||
if self.allows_scrollbar_controls() {
|
||||
self.scrollbar_style.width.thickness()
|
||||
|
||||
@@ -8,8 +8,9 @@ mod style_resolver;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use moli_layout::{
|
||||
DocumentLayoutServices, EmbeddedFrameRenderer, LayoutPassRequest, LayoutPassResult,
|
||||
LayoutViewport, finish_measured_layout_pass, measure_prepared_layout_pass, prepare_layout_pass,
|
||||
AutoScrollbars, DocumentLayoutServices, EmbeddedFrameRenderer, LayoutPassRequest,
|
||||
LayoutPassResult, LayoutViewport, finish_measured_layout_pass, measure_prepared_layout_pass,
|
||||
prepare_layout_pass,
|
||||
};
|
||||
use moli_selector::QueryContainerSizes;
|
||||
|
||||
@@ -125,6 +126,11 @@ fn build_native_layout_pass_recursive(
|
||||
let mut measured = measure_prepared_layout_pass(prepared, services)?;
|
||||
let mut iterations = 0;
|
||||
let mut largest_container_count = 0;
|
||||
let mut previous_scrollbars = None;
|
||||
let mut scrollbar_transitions =
|
||||
Vec::<(QueryContainerSizes, AutoScrollbars<DomHandle>)>::new();
|
||||
let mut frozen_scrollbars = AutoScrollbars::default();
|
||||
let mut froze_a_scrollbar = false;
|
||||
loop {
|
||||
let sizes: QueryContainerSizes = measured
|
||||
.query_container_sizes()
|
||||
@@ -139,26 +145,55 @@ fn build_native_layout_pass_recursive(
|
||||
})
|
||||
.collect();
|
||||
largest_container_count = largest_container_count.max(sizes.len());
|
||||
let repeated = scrollbar_transitions
|
||||
.iter()
|
||||
.rposition(|(earlier, _)| earlier == &sizes);
|
||||
let Some(previous) = runtime.replace_layout_container_sizes(document, sizes) else {
|
||||
break;
|
||||
};
|
||||
largest_container_count = largest_container_count.max(previous.len());
|
||||
frames
|
||||
.original_container_sizes
|
||||
.entry(document)
|
||||
.or_insert(previous);
|
||||
let scrollbars = measured.auto_scrollbars();
|
||||
// Keep only the size tables measured at a change in scrollbar
|
||||
// visibility. An exact repeat identifies a feedback cycle, and
|
||||
// only the gutters that toggled within it need to be frozen.
|
||||
let changed = previous_scrollbars
|
||||
.as_ref()
|
||||
.is_none_or(|(earlier, _)| earlier != &scrollbars);
|
||||
match previous_scrollbars.replace((scrollbars, changed)) {
|
||||
Some((earlier, true)) => scrollbar_transitions.push((previous, earlier)),
|
||||
Some((_, false)) => {}
|
||||
None => {
|
||||
frames
|
||||
.original_container_sizes
|
||||
.entry(document)
|
||||
.or_insert(previous);
|
||||
}
|
||||
}
|
||||
if let Some(start) = repeated
|
||||
&& let Some((current, record_current)) = &mut previous_scrollbars
|
||||
{
|
||||
for (_, earlier) in scrollbar_transitions.drain(start..) {
|
||||
froze_a_scrollbar |= frozen_scrollbars.freeze_changed(current, &earlier);
|
||||
}
|
||||
scrollbar_transitions.clear();
|
||||
// The next pass starts a new history with the frozen gutters.
|
||||
*record_current = false;
|
||||
}
|
||||
// A chain can advance by one nested container per feedback
|
||||
// pass. Allow work proportional to the number of containers,
|
||||
// including newly revealed boxes, and fail without publishing
|
||||
// if the measured sizes still change at the budget limit.
|
||||
let budget = largest_container_count.saturating_mul(2).saturating_add(1);
|
||||
// including newly revealed boxes. A corrective scrollbar pass
|
||||
// gets a second bounded chance to propagate stable sizes.
|
||||
let budget = largest_container_count
|
||||
.saturating_mul(2)
|
||||
.saturating_add(1)
|
||||
.saturating_mul(1 + usize::from(froze_a_scrollbar));
|
||||
if iterations >= budget {
|
||||
return Err(
|
||||
moli_layout::LayoutError::ContainerQuerySizesDidNotConverge { iterations },
|
||||
);
|
||||
}
|
||||
iterations += 1;
|
||||
measured = measured.remeasure(services, &mut prepare)?;
|
||||
measured = measured.remeasure(services, &frozen_scrollbars, &mut prepare)?;
|
||||
}
|
||||
finish_measured_layout_pass(measured, &source, &mut frames)
|
||||
};
|
||||
|
||||
@@ -80,6 +80,32 @@ fn realtime_publication_temporary_viewport_preserves_screen_container_query_size
|
||||
assert_eq!(vm.layout_pass_observability_for_test().1, builds + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realtime_publication_container_query_auto_scrollbars_stabilize_on_reflow() {
|
||||
let (mut vm, viewport) = fixture(
|
||||
r#"<!doctype html><style>
|
||||
.scroller { width: 200px; height: 100px; overflow-y: auto }
|
||||
.query { container-type: inline-size }
|
||||
.content { height: 200px }
|
||||
@container (width < 190px) { #content { height: 50px } }
|
||||
@container (width >= 180px) { #short { height: 50px } }
|
||||
</style>
|
||||
<div id=scroller class=scroller><div id=query class=query><div id=content class=content></div></div></div>
|
||||
<div id=other class=scroller><div class=query><div id=short class=content></div></div></div>"#,
|
||||
);
|
||||
let read = "JSON.stringify([scroller.clientWidth, query.offsetWidth, getComputedStyle(content).height, other.clientWidth])";
|
||||
let initial = vm.eval(read).unwrap();
|
||||
assert_eq!(initial, r#"[185,185,"50px",200]"#);
|
||||
let builds = vm.layout_pass_observability_for_test().1;
|
||||
vm.eval("content.style.borderBottom = '2px solid';")
|
||||
.unwrap();
|
||||
assert_eq!(vm.eval(read).unwrap(), initial);
|
||||
assert_eq!(vm.layout_pass_observability_for_test().1, builds + 1);
|
||||
vm.screenshot_layout_snapshot(viewport).unwrap().unwrap();
|
||||
vm.eval("content.style.height = '40px'").unwrap();
|
||||
assert_eq!(vm.eval(read).unwrap(), r#"[200,200,"40px",200]"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realtime_publication_transient_intent_does_not_publish_even_with_screenshot_reason() {
|
||||
let (mut vm, viewport) = fixture("<!doctype html><div id=target style='width:100px'></div>");
|
||||
|
||||
Reference in New Issue
Block a user