@@ -1337,6 +1337,13 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
1337
1337
// it gets the sidebar to restore its size.
1338
1338
let desiredSidebarSize = null ;
1339
1339
1340
+ // Sidebar resize debouncer.
1341
+ //
1342
+ // The sidebar itself is resized instantly, but the body HTML can be too
1343
+ // big for that, causing reflow jank. To reduce this, we queue up a separate
1344
+ // animation frame and throttle it.
1345
+ let pendingSidebarResizingFrame = false ;
1346
+
1340
1347
// If this page has no sidebar at all, bail out.
1341
1348
const resizer = document . querySelector ( ".sidebar-resizer" ) ;
1342
1349
const sidebar = document . querySelector ( ".sidebar" ) ;
@@ -1360,12 +1367,26 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
1360
1367
if ( isSrcPage ) {
1361
1368
window . rustdocCloseSourceSidebar ( ) ;
1362
1369
updateLocalStorage ( "src-sidebar-width" , null ) ;
1370
+ // [RUSTDOCIMPL] CSS variable fast path
1371
+ //
1372
+ // The sidebar width variable is attached to the <html> element by
1373
+ // storage.js, because the sidebar and resizer don't exist yet.
1374
+ // But the resize code, in `resize()`, sets the property on the
1375
+ // sidebar and resizer elements (which are the only elements that
1376
+ // use the variable) to avoid recalculating CSS on the entire
1377
+ // document on every frame.
1378
+ //
1379
+ // So, to clear it, we need to clear all three.
1363
1380
document . documentElement . style . removeProperty ( "--src-sidebar-width" ) ;
1381
+ sidebar . style . removeProperty ( "--src-sidebar-width" ) ;
1382
+ resizer . style . removeProperty ( "--src-sidebar-width" ) ;
1364
1383
} else {
1365
1384
addClass ( document . documentElement , "hide-sidebar" ) ;
1366
1385
updateLocalStorage ( "hide-sidebar" , "true" ) ;
1367
1386
updateLocalStorage ( "desktop-sidebar-width" , null ) ;
1368
1387
document . documentElement . style . removeProperty ( "--desktop-sidebar-width" ) ;
1388
+ sidebar . style . removeProperty ( "--desktop-sidebar-width" ) ;
1389
+ resizer . style . removeProperty ( "--desktop-sidebar-width" ) ;
1369
1390
}
1370
1391
}
1371
1392
@@ -1384,15 +1405,27 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
1384
1405
}
1385
1406
}
1386
1407
1387
- // Call this to set the correct CSS variable and setting.
1388
- // This function doesn't enforce size constraints. Do that before calling it!
1408
+ /**
1409
+ * Call this to set the correct CSS variable and setting.
1410
+ * This function doesn't enforce size constraints. Do that before calling it!
1411
+ *
1412
+ * @param {number } size - CSS px width of the sidebar.
1413
+ */
1389
1414
function changeSidebarSize ( size ) {
1390
1415
if ( isSrcPage ) {
1391
1416
updateLocalStorage ( "src-sidebar-width" , size ) ;
1392
- document . documentElement . style . setProperty ( "--src-sidebar-width" , size + "px" ) ;
1417
+ // [RUSTDOCIMPL] CSS variable fast path
1418
+ //
1419
+ // While this property is set on the HTML element at load time,
1420
+ // because the sidebar isn't actually loaded yet,
1421
+ // we scope this update to the sidebar to avoid hitting a slow
1422
+ // path in WebKit.
1423
+ sidebar . style . setProperty ( "--src-sidebar-width" , size + "px" ) ;
1424
+ resizer . style . setProperty ( "--src-sidebar-width" , size + "px" ) ;
1393
1425
} else {
1394
1426
updateLocalStorage ( "desktop-sidebar-width" , size ) ;
1395
- document . documentElement . style . setProperty ( "--desktop-sidebar-width" , size + "px" ) ;
1427
+ sidebar . style . setProperty ( "--desktop-sidebar-width" , size + "px" ) ;
1428
+ resizer . style . setProperty ( "--desktop-sidebar-width" , size + "px" ) ;
1396
1429
}
1397
1430
}
1398
1431
@@ -1424,6 +1457,19 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
1424
1457
const constrainedPos = Math . min ( pos , window . innerWidth - BODY_MIN , SIDEBAR_MAX ) ;
1425
1458
changeSidebarSize ( constrainedPos ) ;
1426
1459
desiredSidebarSize = constrainedPos ;
1460
+ if ( pendingSidebarResizingFrame !== false ) {
1461
+ clearTimeout ( pendingSidebarResizingFrame ) ;
1462
+ }
1463
+ pendingSidebarResizingFrame = setTimeout ( ( ) => {
1464
+ if ( currentPointerId === null || pendingSidebarResizingFrame === false ) {
1465
+ return ;
1466
+ }
1467
+ pendingSidebarResizingFrame = false ;
1468
+ document . documentElement . style . setProperty (
1469
+ "--resizing-sidebar-width" ,
1470
+ desiredSidebarSize + "px"
1471
+ ) ;
1472
+ } , 100 ) ;
1427
1473
}
1428
1474
}
1429
1475
// Respond to the window resize event.
@@ -1447,6 +1493,7 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
1447
1493
window . removeEventListener ( "pointermove" , resize , false ) ;
1448
1494
window . removeEventListener ( "pointerup" , stopResize , false ) ;
1449
1495
removeClass ( document . documentElement , "sidebar-resizing" ) ;
1496
+ document . documentElement . style . removeProperty ( "--resizing-sidebar-width" ) ;
1450
1497
if ( resizer . releasePointerCapture ) {
1451
1498
resizer . releasePointerCapture ( currentPointerId ) ;
1452
1499
currentPointerId = null ;
@@ -1472,6 +1519,8 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
1472
1519
window . addEventListener ( "pointerup" , stopResize , false ) ;
1473
1520
addClass ( resizer , "active" ) ;
1474
1521
addClass ( document . documentElement , "sidebar-resizing" ) ;
1522
+ const pos = e . clientX - sidebar . offsetLeft - 3 ;
1523
+ document . documentElement . style . setProperty ( "--resizing-sidebar-width" , pos + "px" ) ;
1475
1524
desiredSidebarSize = null ;
1476
1525
}
1477
1526
resizer . addEventListener ( "pointerdown" , initResize , false ) ;
0 commit comments