Skip to content

Commit 54ec9b4

Browse files
hjanuschkaDevtools-frontend LUCI CQ
authored andcommitted
Grid overlay: allow explicit writing-mode transform root
Use an optional writingModeRoot to transform vertical grid overlays from the correct origin. Bug: 40215569 Change-Id: I96ced9ebd8028920c6ed5ce53d35cab3e8c4c7aa Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7632884 Reviewed-by: Paul Irish <paulirish@chromium.org> Reviewed-by: Alex Rudenko <alexrudenko@chromium.org> Commit-Queue: Helmut Januschka <helmut@januschka.com>
1 parent 33803e8 commit 54ec9b4

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

inspector_overlay/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ ts_library("unittests") {
113113
"css_grid_label_helpers.test.ts",
114114
"highlight_common.test.ts",
115115
"highlight_flex_common.test.ts",
116+
"highlight_grid_common.test.ts",
116117
"tool_highlight.test.ts",
117118
"tool_source_order.test.ts",
118119
"tool_window_controls.test.ts",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2026 The Chromium Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import type {Position} from './common.js';
6+
import {drawLayoutGridHighlight, type GridHighlight} from './highlight_grid_common.js';
7+
import {getGridAreaNameLabelContainer, initFrameForGridLabels} from './testing/InspectorOverlayHelpers.js';
8+
9+
function createGridHighlight(writingModeRoot?: Position): GridHighlight {
10+
const areaPath: Array<string|number> = ['M', 100, 100, 'L', 200, 100, 'L', 200, 200, 'L', 100, 200, 'Z'];
11+
12+
return {
13+
gridBorder: areaPath,
14+
writingMode: 'vertical-rl',
15+
writingModeRoot,
16+
rowGaps: [],
17+
rotationAngle: 0,
18+
columnGaps: [],
19+
rows: [],
20+
columns: [],
21+
areaNames: {foo: areaPath},
22+
gridHighlightConfig: {
23+
gridBorderDash: false,
24+
rowLineDash: false,
25+
columnLineDash: false,
26+
showGridExtensionLines: false,
27+
showPositiveLineNumbers: false,
28+
showNegativeLineNumbers: false,
29+
rowLineColor: '',
30+
columnLineColor: '',
31+
rowHatchColor: '',
32+
columnHatchColor: '',
33+
showLineNames: false,
34+
},
35+
};
36+
}
37+
38+
function drawGridAndGetAreaLabel(highlight: GridHighlight): HTMLElement {
39+
const canvas = document.createElement('canvas');
40+
canvas.width = 500;
41+
canvas.height = 500;
42+
43+
const context = canvas.getContext('2d');
44+
assert.instanceOf(context, CanvasRenderingContext2D);
45+
46+
drawLayoutGridHighlight(highlight, context, 1, canvas.width, canvas.height, 1, {gridLayerCounter: 1});
47+
48+
const areaLabelsContainer = getGridAreaNameLabelContainer(1);
49+
const label = areaLabelsContainer.querySelector('.grid-label-content');
50+
assert.exists(label, `Expected area label, container html: ${areaLabelsContainer.innerHTML}`);
51+
assert.instanceOf(label, HTMLElement);
52+
53+
return label;
54+
}
55+
56+
describe('drawLayoutGridHighlight', () => {
57+
beforeEach(initFrameForGridLabels);
58+
59+
it('positions area labels in vertical-rl mode from grid bounds by default', () => {
60+
const label = drawGridAndGetAreaLabel(createGridHighlight());
61+
62+
assert.strictEqual(label.style.left, '100px');
63+
assert.strictEqual(label.style.top, '100px');
64+
});
65+
66+
it('positions area labels in vertical-rl mode from writingModeRoot when provided', () => {
67+
const label = drawGridAndGetAreaLabel(createGridHighlight({x: 150, y: 100}));
68+
69+
assert.strictEqual(label.style.left, '150px');
70+
assert.strictEqual(label.style.top, '50px');
71+
});
72+
});

inspector_overlay/highlight_grid_common.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import type {AreaBounds, Bounds} from './common.js';
5+
import type {AreaBounds, Bounds, Position} from './common.js';
66
import {drawGridLabels, type GridLabelState, isHorizontalWritingMode} from './css_grid_label_helpers.js';
77
import {applyMatrixToPoint, buildPath, emptyBounds, hatchFillPath} from './highlight_common.js';
88

@@ -188,6 +188,7 @@ export const gridStyle = `
188188
export interface GridHighlight {
189189
gridBorder: Array<string|number>;
190190
writingMode: string;
191+
writingModeRoot?: Position;
191192
rowGaps: Array<string|number>;
192193
rotationAngle: number;
193194
columnGaps: Array<string|number>;
@@ -222,7 +223,7 @@ export function drawLayoutGridHighlight(
222223

223224
// Transform the context to match the current writing-mode.
224225
context.save();
225-
applyWritingModeTransformation(highlight.writingMode, gridBounds, context);
226+
applyWritingModeTransformation(highlight.writingMode, gridBounds, context, highlight.writingModeRoot);
226227

227228
// Draw grid background
228229
if (highlight.gridHighlightConfig.gridBackgroundColor) {
@@ -285,21 +286,23 @@ export function drawLayoutGridHighlight(
285286
writingModeMatrix);
286287
}
287288

288-
function applyWritingModeTransformation(writingMode: string, gridBounds: Bounds, context: CanvasRenderingContext2D) {
289+
function applyWritingModeTransformation(
290+
writingMode: string, gridBounds: Bounds, context: CanvasRenderingContext2D, writingModeRoot?: Position) {
289291
if (isHorizontalWritingMode(writingMode)) {
290292
return;
291293
}
292294

293295
const topLeft = gridBounds.allPoints[0];
294296
const topRight = gridBounds.allPoints[1];
295297
const bottomLeft = gridBounds.allPoints[3];
298+
const origin = writingModeRoot ?? topLeft;
296299

297-
// Move to the top-left corner to do all transformations there.
298-
context.translate(topLeft.x, topLeft.y);
300+
// Move to the origin corner to do all transformations there.
301+
context.translate(origin.x, origin.y);
299302

300303
if (writingMode === 'vertical-rl' || writingMode === 'sideways-rl') {
301304
context.rotate(90 * Math.PI / 180);
302-
context.translate(0, -1 * (bottomLeft.y - topLeft.y));
305+
context.translate(0, -(bottomLeft.y - topLeft.y));
303306
}
304307

305308
if (writingMode === 'vertical-lr') {
@@ -309,11 +312,11 @@ function applyWritingModeTransformation(writingMode: string, gridBounds: Bounds,
309312

310313
if (writingMode === 'sideways-lr') {
311314
context.rotate(-90 * Math.PI / 180);
312-
context.translate(-1 * (topRight.x - topLeft.x), 0);
315+
context.translate(-(topRight.x - topLeft.x), 0);
313316
}
314317

315318
// Move back to the original point.
316-
context.translate(topLeft.x * -1, topLeft.y * -1);
319+
context.translate(-origin.x, -origin.y);
317320
}
318321

319322
function drawGridLines(

0 commit comments

Comments
 (0)