Skip to content

Commit c691764

Browse files
authored
Merge pull request #794 from lightpanda-io/window-screen
add Screen and ScreenOrientation
2 parents 0b846b1 + 269eb7e commit c691764

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <francis@lightpanda.io>
4+
// Pierre Tachoire <pierre@lightpanda.io>
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 EventTarget = @import("../dom/event_target.zig").EventTarget;
22+
23+
pub const Interfaces = .{
24+
Screen,
25+
ScreenOrientation,
26+
};
27+
28+
// https://developer.mozilla.org/en-US/docs/Web/API/Screen
29+
pub const Screen = struct {
30+
pub const prototype = *EventTarget;
31+
32+
height: u32 = 1080,
33+
width: u32 = 1920,
34+
// https://developer.mozilla.org/en-US/docs/Web/API/Screen/colorDepth
35+
color_depth: u32 = 8,
36+
// https://developer.mozilla.org/en-US/docs/Web/API/Screen/pixelDepth
37+
pixel_depth: u32 = 8,
38+
orientation: ScreenOrientation = .{ .type = .landscape_primary },
39+
40+
pub fn get_availHeight(self: *const Screen) u32 {
41+
return self.height;
42+
}
43+
44+
pub fn get_availWidth(self: *const Screen) u32 {
45+
return self.width;
46+
}
47+
48+
pub fn get_height(self: *const Screen) u32 {
49+
return self.height;
50+
}
51+
52+
pub fn get_width(self: *const Screen) u32 {
53+
return self.width;
54+
}
55+
56+
pub fn get_pixelDepth(self: *const Screen) u32 {
57+
return self.pixel_depth;
58+
}
59+
60+
pub fn get_orientation(self: *const Screen) ScreenOrientation {
61+
return self.orientation;
62+
}
63+
};
64+
65+
const ScreenOrientationType = enum {
66+
portrait_primary,
67+
portrait_secondary,
68+
landscape_primary,
69+
landscape_secondary,
70+
71+
pub fn toString(self: ScreenOrientationType) []const u8 {
72+
return switch (self) {
73+
.portrait_primary => "portrait-primary",
74+
.portrait_secondary => "portrait-secondary",
75+
.landscape_primary => "landscape-primary",
76+
.landscape_secondary => "landscape-secondary",
77+
};
78+
}
79+
};
80+
81+
pub const ScreenOrientation = struct {
82+
pub const prototype = *EventTarget;
83+
84+
angle: u32 = 0,
85+
type: ScreenOrientationType,
86+
87+
pub fn get_angle(self: *const ScreenOrientation) u32 {
88+
return self.angle;
89+
}
90+
91+
pub fn get_type(self: *const ScreenOrientation) []const u8 {
92+
return self.type.toString();
93+
}
94+
};
95+
96+
const testing = @import("../../testing.zig");
97+
test "Browser.HTML.Screen" {
98+
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
99+
defer runner.deinit();
100+
101+
try runner.testCases(&.{
102+
.{ "let screen = window.screen", "undefined" },
103+
.{ "screen.width === 1920", "true" },
104+
.{ "screen.height === 1080", "true" },
105+
.{ "let orientation = screen.orientation", "undefined" },
106+
.{ "orientation.angle === 0", "true" },
107+
.{ "orientation.type === \"landscape-primary\"", "true" },
108+
}, .{});
109+
}

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)