|
| 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 | +} |
0 commit comments