From 269eb7e1546d6cc83c723c237ff81ab781204eb6 Mon Sep 17 00:00:00 2001 From: Muki Kiboigo Date: Wed, 18 Jun 2025 12:49:25 -0700 Subject: [PATCH] add Screen and ScreenOrientation --- src/browser/html/html.zig | 1 + src/browser/html/screen.zig | 109 ++++++++++++++++++++++++++++++++++++ src/browser/html/window.zig | 6 ++ 3 files changed, 116 insertions(+) create mode 100644 src/browser/html/screen.zig diff --git a/src/browser/html/html.zig b/src/browser/html/html.zig index ef75dde0..33b6b2ad 100644 --- a/src/browser/html/html.zig +++ b/src/browser/html/html.zig @@ -38,4 +38,5 @@ pub const Interfaces = .{ Location, MediaQueryList, Performance, + @import("screen.zig").Interfaces, }; diff --git a/src/browser/html/screen.zig b/src/browser/html/screen.zig new file mode 100644 index 00000000..6d705b6f --- /dev/null +++ b/src/browser/html/screen.zig @@ -0,0 +1,109 @@ +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) +// +// Francis Bouvier +// Pierre Tachoire +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +const std = @import("std"); + +const EventTarget = @import("../dom/event_target.zig").EventTarget; + +pub const Interfaces = .{ + Screen, + ScreenOrientation, +}; + +// https://developer.mozilla.org/en-US/docs/Web/API/Screen +pub const Screen = struct { + pub const prototype = *EventTarget; + + height: u32 = 1080, + width: u32 = 1920, + // https://developer.mozilla.org/en-US/docs/Web/API/Screen/colorDepth + color_depth: u32 = 8, + // https://developer.mozilla.org/en-US/docs/Web/API/Screen/pixelDepth + pixel_depth: u32 = 8, + orientation: ScreenOrientation = .{ .type = .landscape_primary }, + + pub fn get_availHeight(self: *const Screen) u32 { + return self.height; + } + + pub fn get_availWidth(self: *const Screen) u32 { + return self.width; + } + + pub fn get_height(self: *const Screen) u32 { + return self.height; + } + + pub fn get_width(self: *const Screen) u32 { + return self.width; + } + + pub fn get_pixelDepth(self: *const Screen) u32 { + return self.pixel_depth; + } + + pub fn get_orientation(self: *const Screen) ScreenOrientation { + return self.orientation; + } +}; + +const ScreenOrientationType = enum { + portrait_primary, + portrait_secondary, + landscape_primary, + landscape_secondary, + + pub fn toString(self: ScreenOrientationType) []const u8 { + return switch (self) { + .portrait_primary => "portrait-primary", + .portrait_secondary => "portrait-secondary", + .landscape_primary => "landscape-primary", + .landscape_secondary => "landscape-secondary", + }; + } +}; + +pub const ScreenOrientation = struct { + pub const prototype = *EventTarget; + + angle: u32 = 0, + type: ScreenOrientationType, + + pub fn get_angle(self: *const ScreenOrientation) u32 { + return self.angle; + } + + pub fn get_type(self: *const ScreenOrientation) []const u8 { + return self.type.toString(); + } +}; + +const testing = @import("../../testing.zig"); +test "Browser.HTML.Screen" { + var runner = try testing.jsRunner(testing.tracking_allocator, .{}); + defer runner.deinit(); + + try runner.testCases(&.{ + .{ "let screen = window.screen", "undefined" }, + .{ "screen.width === 1920", "true" }, + .{ "screen.height === 1080", "true" }, + .{ "let orientation = screen.orientation", "undefined" }, + .{ "orientation.angle === 0", "true" }, + .{ "orientation.type === \"landscape-primary\"", "true" }, + }, .{}); +} diff --git a/src/browser/html/window.zig b/src/browser/html/window.zig index f18b0cd2..c1fbdef2 100644 --- a/src/browser/html/window.zig +++ b/src/browser/html/window.zig @@ -34,6 +34,7 @@ const MediaQueryList = @import("media_query_list.zig").MediaQueryList; const Performance = @import("performance.zig").Performance; const CSSStyleDeclaration = @import("../cssom/css_style_declaration.zig").CSSStyleDeclaration; const CustomElementRegistry = @import("../webcomponents/custom_element_registry.zig").CustomElementRegistry; +const Screen = @import("screen.zig").Screen; const storage = @import("../storage/storage.zig"); @@ -60,6 +61,7 @@ pub const Window = struct { navigator: Navigator = .{}, performance: Performance, custom_elements: CustomElementRegistry = .{}, + screen: Screen = .{}, pub fn create(target: ?[]const u8, navigator: ?Navigator) !Window { var fbs = std.io.fixedBufferStream(""); @@ -169,6 +171,10 @@ pub const Window = struct { return &self.custom_elements; } + pub fn get_screen(self: *Window) *Screen { + return &self.screen; + } + pub fn _requestAnimationFrame(self: *Window, cbk: Function, page: *Page) !u32 { return self.createTimeout(cbk, 5, page, .{ .animation_frame = true }); }