Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/browser/html/html.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ pub const Interfaces = .{
Location,
MediaQueryList,
Performance,
@import("screen.zig").Interfaces,
};
109 changes: 109 additions & 0 deletions src/browser/html/screen.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// 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 <https://www.gnu.org/licenses/>.

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" },
}, .{});
}
6 changes: 6 additions & 0 deletions src/browser/html/window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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("");
Expand Down Expand Up @@ -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 });
}
Expand Down
Loading