Skip to content

Commit 1c37b1c

Browse files
Merge pull request #1173 from lightpanda-io/renderer-size
renderer: set a default box size of 5 pixels
2 parents fb6fbff + 2422c87 commit 1c37b1c

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

src/browser/renderer.zig

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const FlatRenderer = struct {
4141

4242
const Element = @import("dom/element.zig").Element;
4343

44+
// Define the size of each element in the grid.
45+
const default_w = 5;
46+
const default_h = 5;
47+
4448
// we expect allocator to be an arena
4549
pub fn init(allocator: Allocator) FlatRenderer {
4650
return .{
@@ -62,10 +66,10 @@ const FlatRenderer = struct {
6266
gop.value_ptr.* = x;
6367
}
6468

65-
const _x: f64 = @floatFromInt(x);
69+
const _x: f64 = @floatFromInt(x * default_w);
6670
const y: f64 = 0.0;
67-
const w: f64 = 1.0;
68-
const h: f64 = 1.0;
71+
const w: f64 = default_w;
72+
const h: f64 = default_h;
6973

7074
return .{
7175
.x = _x,
@@ -98,18 +102,20 @@ const FlatRenderer = struct {
98102
}
99103

100104
pub fn width(self: *const FlatRenderer) u32 {
101-
return @max(@as(u32, @intCast(self.elements.items.len)), 1); // At least 1 pixel even if empty
105+
return @max(@as(u32, @intCast(self.elements.items.len * default_w)), default_w); // At least default width pixels even if empty
102106
}
103107

104108
pub fn height(_: *const FlatRenderer) u32 {
105-
return 1;
109+
return 5;
106110
}
107111

108-
pub fn getElementAtPosition(self: *const FlatRenderer, x: i32, y: i32) ?*parser.Element {
109-
if (y != 0 or x < 0) {
112+
pub fn getElementAtPosition(self: *const FlatRenderer, _x: i32, y: i32) ?*parser.Element {
113+
if (y < 0 or y > default_h or _x < 0) {
110114
return null;
111115
}
112116

117+
const x = @divFloor(_x, default_w);
118+
113119
const elements = self.elements.items;
114120
return if (x < elements.len) @ptrFromInt(elements[@intCast(x)]) else null;
115121
}

src/cdp/domains/dom.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,11 @@ test "cdp.dom: getBoxModel" {
663663
.params = .{ .nodeId = 6 },
664664
});
665665
try ctx.expectSentResult(.{ .model = BoxModel{
666-
.content = Quad{ 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0 },
667-
.padding = Quad{ 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0 },
668-
.border = Quad{ 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0 },
669-
.margin = Quad{ 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0 },
670-
.width = 1,
671-
.height = 1,
666+
.content = Quad{ 0.0, 0.0, 5.0, 0.0, 5.0, 5.0, 0.0, 5.0 },
667+
.padding = Quad{ 0.0, 0.0, 5.0, 0.0, 5.0, 5.0, 0.0, 5.0 },
668+
.border = Quad{ 0.0, 0.0, 5.0, 0.0, 5.0, 5.0, 0.0, 5.0 },
669+
.margin = Quad{ 0.0, 0.0, 5.0, 0.0, 5.0, 5.0, 0.0, 5.0 },
670+
.width = 5,
671+
.height = 5,
672672
} }, .{ .id = 5 });
673673
}

src/tests/dom/element.html

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -168,35 +168,35 @@
168168

169169
<script id=dimensions>
170170
const para = document.getElementById('para');
171-
testing.expectEqual(1, para.clientWidth);
172-
testing.expectEqual(1, para.clientHeight);
173-
174-
// let r1 = document.getElementById('para').getBoundingClientRect();
175-
// testing.expectEqual(0, r1.x);
176-
// testing.expectEqual(0, r1.y);
177-
// testing.expectEqual(1, r1.width);
178-
// testing.expectEqual(2, r1.height);
179-
180-
// let r2 = document.getElementById('content').getBoundingClientRect();
181-
// testing.expectEqual(1, r2.x);
182-
// testing.expectEqual(0, r2.y);
183-
// testing.expectEqual(1, r2.width);
184-
// testing.expectEqual(1, r2.height);
185-
186-
// let r3 = document.getElementById('para').getBoundingClientRect();
187-
// testing.expectEqual(0, r3.x);
188-
// testing.expectEqual(0, r3.y);
189-
// testing.expectEqual(1, r3.width);
190-
// testing.expectEqual(1, r3.height);
191-
192-
// testing.expectEqual(1, para.clientWidth);
193-
// testing.expectEqual(1, para.clientHeight);
194-
195-
// let r4 = document.createElement('div').getBoundingClientRect();
196-
// testing.expectEqual(0, r4.x);
197-
// testing.expectEqual(0, r4.y);
198-
// testing.expectEqual(0, r4.width);
199-
// testing.expectEqual(0, r4.height);
171+
testing.expectEqual(5, para.clientWidth);
172+
testing.expectEqual(5, para.clientHeight);
173+
174+
let r1 = document.getElementById('para').getBoundingClientRect();
175+
testing.expectEqual(0, r1.x);
176+
testing.expectEqual(0, r1.y);
177+
testing.expectEqual(5, r1.width);
178+
testing.expectEqual(5, r1.height);
179+
180+
let r2 = document.getElementById('content').getBoundingClientRect();
181+
testing.expectEqual(5, r2.x);
182+
testing.expectEqual(0, r2.y);
183+
testing.expectEqual(5, r2.width);
184+
testing.expectEqual(5, r2.height);
185+
186+
let r3 = document.getElementById('para').getBoundingClientRect();
187+
testing.expectEqual(0, r3.x);
188+
testing.expectEqual(0, r3.y);
189+
testing.expectEqual(5, r3.width);
190+
testing.expectEqual(5, r3.height);
191+
192+
testing.expectEqual(10, para.clientWidth);
193+
testing.expectEqual(5, para.clientHeight);
194+
195+
let r4 = document.createElement('div').getBoundingClientRect();
196+
testing.expectEqual(0, r4.x);
197+
testing.expectEqual(0, r4.y);
198+
testing.expectEqual(0, r4.width);
199+
testing.expectEqual(0, r4.height);
200200
</script>
201201

202202
<script id=matches>

src/tests/dom/intersection_observer.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@
122122
testing.expectEqual(1, entry.intersectionRatio);
123123
testing.expectEqual(0, entry.intersectionRect.x);
124124
testing.expectEqual(0, entry.intersectionRect.y);
125-
testing.expectEqual(1, entry.intersectionRect.width);
126-
testing.expectEqual(1, entry.intersectionRect.height);
125+
testing.expectEqual(5, entry.intersectionRect.width);
126+
testing.expectEqual(5, entry.intersectionRect.height);
127127
testing.expectEqual(true, entry.isIntersecting);
128128
testing.expectEqual(0, entry.rootBounds.x);
129129
testing.expectEqual(0, entry.rootBounds.y);
130-
testing.expectEqual(1, entry.rootBounds.width);
131-
testing.expectEqual(1, entry.rootBounds.height);
130+
testing.expectEqual(5, entry.rootBounds.width);
131+
testing.expectEqual(5, entry.rootBounds.height);
132132
testing.expectEqual('[object HTMLDivElement]', entry.target.toString());
133133
});
134134
}

src/tests/html/document.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@
4646
testing.expectEqual('name=Oeschger; favorite_food=tripe', document.cookie);
4747

4848
// Return null since we only return elements when they have previously been localized
49-
testing.expectEqual(null, document.elementFromPoint(0.5, 0.5));
50-
testing.expectEqual([], document.elementsFromPoint(0.5, 0.5));
49+
testing.expectEqual(null, document.elementFromPoint(2.5, 2.5));
50+
testing.expectEqual([], document.elementsFromPoint(2.5, 2.5));
5151

5252
let div1 = document.createElement('div');
5353
document.body.appendChild(div1);
5454
div1.getClientRects(); // clal this to position it
55-
testing.expectEqual('[object HTMLDivElement]', document.elementFromPoint(0.5, 0.5).toString());
55+
testing.expectEqual('[object HTMLDivElement]', document.elementFromPoint(2.5, 2.5).toString());
5656

57-
let elems = document.elementsFromPoint(0.5, 0.5);
57+
let elems = document.elementsFromPoint(2.5, 2.5);
5858
testing.expectEqual(3, elems.length);
5959
testing.expectEqual('[object HTMLDivElement]', elems[0].toString());
6060
testing.expectEqual('[object HTMLBodyElement]', elems[1].toString());
@@ -66,11 +66,11 @@
6666
// Note this will be placed after the div of previous test
6767
a.getClientRects();
6868

69-
let a_again = document.elementFromPoint(1.5, 0.5);
69+
let a_again = document.elementFromPoint(7.5, 0.5);
7070
testing.expectEqual('[object HTMLAnchorElement]', a_again.toString());
7171
testing.expectEqual('https://lightpanda.io', a_again.href);
7272

73-
let a_agains = document.elementsFromPoint(1.5, 0.5);
73+
let a_agains = document.elementsFromPoint(7.5, 0.5);
7474
testing.expectEqual('https://lightpanda.io', a_agains[0].href);
7575

7676

src/tests/window/window.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
</script>
2626

2727
<script id=dimensions>
28-
testing.expectEqual(1, innerHeight);
29-
// Width is 1 even if there are no elements
30-
testing.expectEqual(1, innerWidth);
28+
testing.expectEqual(5, innerHeight);
29+
// Width is 5 even if there are no elements
30+
testing.expectEqual(5, innerWidth);
3131

3232
let div1 = document.createElement('div');
3333
document.body.appendChild(div1);
@@ -37,8 +37,8 @@
3737
document.body.appendChild(div2);
3838
div2.getClientRects();
3939

40-
testing.expectEqual(1, innerHeight);
41-
testing.expectEqual(2, innerWidth);
40+
testing.expectEqual(5, innerHeight);
41+
testing.expectEqual(10, innerWidth);
4242
</script>
4343

4444
<script id=setTimeout>

0 commit comments

Comments
 (0)