Skip to content

Commit 3d084dc

Browse files
committed
add Screen and ScreenOrientation
1 parent ac759a6 commit 3d084dc

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

src/browser/html/html.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ pub const Interfaces = .{
3838
Location,
3939
MediaQueryList,
4040
Performance,
41+
@import("screen.zig").Interfaces,
4142
};

src/browser/html/screen.zig

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <[email protected]>
4+
// Pierre Tachoire <[email protected]>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const std = @import("std");
20+
21+
const log = @import("../../log.zig");
22+
const parser = @import("../netsurf.zig");
23+
24+
const EventTarget = @import("../dom/event_target.zig").EventTarget;
25+
26+
pub const Interfaces = .{
27+
Screen,
28+
ScreenOrientation,
29+
};
30+
31+
// https://developer.mozilla.org/en-US/docs/Web/API/Screen
32+
pub const Screen = struct {
33+
pub const prototype = *EventTarget;
34+
35+
height: u32 = 1080,
36+
width: u32 = 1920,
37+
// https://developer.mozilla.org/en-US/docs/Web/API/Screen/colorDepth
38+
color_depth: u32 = 8,
39+
// https://developer.mozilla.org/en-US/docs/Web/API/Screen/pixelDepth
40+
pixel_depth: u32 = 8,
41+
orientation: ScreenOrientation = .{ .type = .landscape_primary },
42+
43+
pub fn get_availHeight(self: *const Screen) u32 {
44+
return self.height;
45+
}
46+
47+
pub fn get_availWidth(self: *const Screen) u32 {
48+
return self.width;
49+
}
50+
51+
pub fn get_height(self: *const Screen) u32 {
52+
return self.height;
53+
}
54+
55+
pub fn get_width(self: *const Screen) u32 {
56+
return self.width;
57+
}
58+
59+
pub fn get_pixelDepth(self: *const Screen) u32 {
60+
return self.pixel_depth;
61+
}
62+
63+
pub fn get_orientation(self: *const Screen) ScreenOrientation {
64+
return self.orientation;
65+
}
66+
};
67+
68+
const ScreenOrientationType = enum {
69+
portrait_primary,
70+
portrait_secondary,
71+
landscape_primary,
72+
landscape_secondary,
73+
74+
pub fn toString(self: ScreenOrientationType) []const u8 {
75+
return switch (self) {
76+
.portrait_primary => "portrait-primary",
77+
.portrait_secondary => "portrait-secondary",
78+
.landscape_primary => "landscape-primary",
79+
.landscape_secondary => "landscape-secondary",
80+
};
81+
}
82+
};
83+
84+
pub const ScreenOrientation = struct {
85+
pub const prototype = *EventTarget;
86+
87+
angle: u32 = 0,
88+
type: ScreenOrientationType,
89+
90+
pub fn get_angle(self: *const ScreenOrientation) u32 {
91+
return self.angle;
92+
}
93+
94+
pub fn get_type(self: *const ScreenOrientation) []const u8 {
95+
return self.type.toString();
96+
}
97+
};
98+
99+
const testing = @import("../../testing.zig");
100+
test "Browser.HTML.Screen" {
101+
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
102+
defer runner.deinit();
103+
104+
try runner.testCases(&.{
105+
.{ "let screen = window.screen", "undefined" },
106+
.{ "screen.width === 1920", "true" },
107+
.{ "screen.height === 1080", "true" },
108+
.{ "let orientation = screen.orientation", "undefined" },
109+
.{ "orientation.angle === 0", "true" },
110+
.{ "orientation.type === \"landscape-primary\"", "true" },
111+
}, .{});
112+
}

src/browser/html/window.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const MediaQueryList = @import("media_query_list.zig").MediaQueryList;
3434
const Performance = @import("performance.zig").Performance;
3535
const CSSStyleDeclaration = @import("../cssom/css_style_declaration.zig").CSSStyleDeclaration;
3636
const CustomElementRegistry = @import("../webcomponents/custom_element_registry.zig").CustomElementRegistry;
37+
const Screen = @import("screen.zig").Screen;
3738

3839
const storage = @import("../storage/storage.zig");
3940

@@ -60,6 +61,7 @@ pub const Window = struct {
6061
navigator: Navigator = .{},
6162
performance: Performance,
6263
custom_elements: CustomElementRegistry = .{},
64+
screen: Screen = .{},
6365

6466
pub fn create(target: ?[]const u8, navigator: ?Navigator) !Window {
6567
var fbs = std.io.fixedBufferStream("");
@@ -169,6 +171,10 @@ pub const Window = struct {
169171
return &self.custom_elements;
170172
}
171173

174+
pub fn get_screen(self: *Window) *Screen {
175+
return &self.screen;
176+
}
177+
172178
pub fn _requestAnimationFrame(self: *Window, cbk: Function, page: *Page) !u32 {
173179
return self.createTimeout(cbk, 5, page, .{ .animation_frame = true });
174180
}

0 commit comments

Comments
 (0)