From 23146f64abc952b306a8b7267763fa74b9590872 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Fri, 12 Dec 2025 18:21:30 +0800 Subject: [PATCH] Screen and ScreenOrientation (legacy) --- src/browser/EventManager.zig | 2 +- src/browser/Page.zig | 3 + src/browser/tests/legacy/html/screen.html | 2 +- src/browser/tests/legacy/html/template.html | 168 -------------------- src/browser/tests/window/screen.html | 21 +++ src/browser/webapi/EventTarget.zig | 2 + src/browser/webapi/Screen.zig | 77 +++++++-- src/browser/webapi/Window.zig | 4 +- 8 files changed, 97 insertions(+), 182 deletions(-) delete mode 100644 src/browser/tests/legacy/html/template.html create mode 100644 src/browser/tests/window/screen.html diff --git a/src/browser/EventManager.zig b/src/browser/EventManager.zig index 1bda2ec8..d458a3b2 100644 --- a/src/browser/EventManager.zig +++ b/src/browser/EventManager.zig @@ -119,7 +119,7 @@ pub fn dispatch(self: *EventManager, target: *EventTarget, event: *Event) !void switch (target._type) { .node => |node| try self.dispatchNode(node, event, &was_handled), - .xhr, .window, .abort_signal, .media_query_list, .message_port, .text_track_cue, .navigation => { + .xhr, .window, .abort_signal, .media_query_list, .message_port, .text_track_cue, .navigation, .screen, .screen_orientation => { const list = self.lookup.getPtr(@intFromPtr(target)) orelse return; try self.dispatchAll(list, target, event, &was_handled); }, diff --git a/src/browser/Page.zig b/src/browser/Page.zig index c05fcb14..fab0690b 100644 --- a/src/browser/Page.zig +++ b/src/browser/Page.zig @@ -52,6 +52,7 @@ const Document = @import("webapi/Document.zig"); const DocumentFragment = @import("webapi/DocumentFragment.zig"); const ShadowRoot = @import("webapi/ShadowRoot.zig"); const Performance = @import("webapi/Performance.zig"); +const Screen = @import("webapi/Screen.zig"); const HtmlScript = @import("webapi/Element.zig").Html.Script; const MutationObserver = @import("webapi/MutationObserver.zig"); const IntersectionObserver = @import("webapi/IntersectionObserver.zig"); @@ -209,12 +210,14 @@ fn reset(self: *Page, comptime initializing: bool) !void { if (comptime initializing == true) { const storage_bucket = try self._factory.create(storage.Bucket{}); + const screen = try Screen.init(self); self.window = try self._factory.eventTarget(Window{ ._document = self.document, ._storage_bucket = storage_bucket, ._performance = Performance.init(), ._proto = undefined, ._location = &default_location, + ._screen = screen, }); } else { self.window._document = self.document; diff --git a/src/browser/tests/legacy/html/screen.html b/src/browser/tests/legacy/html/screen.html index 82f4b71c..5239ba43 100644 --- a/src/browser/tests/legacy/html/screen.html +++ b/src/browser/tests/legacy/html/screen.html @@ -17,5 +17,5 @@ diff --git a/src/browser/tests/legacy/html/template.html b/src/browser/tests/legacy/html/template.html deleted file mode 100644 index bc605584..00000000 --- a/src/browser/tests/legacy/html/template.html +++ /dev/null @@ -1,168 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/browser/tests/window/screen.html b/src/browser/tests/window/screen.html new file mode 100644 index 00000000..5239ba43 --- /dev/null +++ b/src/browser/tests/window/screen.html @@ -0,0 +1,21 @@ + + + + + + diff --git a/src/browser/webapi/EventTarget.zig b/src/browser/webapi/EventTarget.zig index 4f0eabfe..b399f7cb 100644 --- a/src/browser/webapi/EventTarget.zig +++ b/src/browser/webapi/EventTarget.zig @@ -38,6 +38,8 @@ pub const Type = union(enum) { message_port: *@import("MessagePort.zig"), text_track_cue: *@import("media/TextTrackCue.zig"), navigation: *@import("navigation/NavigationEventTarget.zig"), + screen: *@import("Screen.zig"), + screen_orientation: *@import("Screen.zig").Orientation, }; pub fn dispatchEvent(self: *EventTarget, event: *Event, page: *Page) !bool { diff --git a/src/browser/webapi/Screen.zig b/src/browser/webapi/Screen.zig index 1ed5b139..a9e28e8c 100644 --- a/src/browser/webapi/Screen.zig +++ b/src/browser/webapi/Screen.zig @@ -17,42 +17,65 @@ // along with this program. If not, see . const js = @import("../js/js.zig"); +const Page = @import("../Page.zig"); +const EventTarget = @import("EventTarget.zig"); + +pub fn registerTypes() []const type { + return &.{ + Screen, + Orientation, + }; +} const Screen = @This(); -_pad: bool = false, -pub const init: Screen = .{}; +_proto: *EventTarget, +_orientation: ?*Orientation = null, + +pub fn init(page: *Page) !*Screen { + return page._factory.eventTarget(Screen{ + ._proto = undefined, + ._orientation = null, + }); +} + +pub fn asEventTarget(self: *Screen) *EventTarget { + return self._proto; +} -/// Total width of the screen in pixels pub fn getWidth(_: *const Screen) u32 { return 1920; } -/// Total height of the screen in pixels pub fn getHeight(_: *const Screen) u32 { return 1080; } -/// Available width (excluding OS UI elements like taskbar) pub fn getAvailWidth(_: *const Screen) u32 { return 1920; } -/// Available height (excluding OS UI elements like taskbar) pub fn getAvailHeight(_: *const Screen) u32 { return 1040; // 40px reserved for taskbar/dock } -/// Color depth in bits per pixel pub fn getColorDepth(_: *const Screen) u32 { return 24; } -/// Pixel depth in bits per pixel (typically same as colorDepth) pub fn getPixelDepth(_: *const Screen) u32 { return 24; } +pub fn getOrientation(self: *Screen, page: *Page) !*Orientation { + if (self._orientation) |orientation| { + return orientation; + } + const orientation = try Orientation.init(page); + self._orientation = orientation; + return orientation; +} + pub const JsApi = struct { pub const bridge = js.Bridge(Screen); @@ -60,14 +83,48 @@ pub const JsApi = struct { pub const name = "Screen"; pub const prototype_chain = bridge.prototypeChain(); pub var class_id: bridge.ClassId = undefined; - pub const empty_with_no_proto = true; }; - // Read-only properties pub const width = bridge.accessor(Screen.getWidth, null, .{}); pub const height = bridge.accessor(Screen.getHeight, null, .{}); pub const availWidth = bridge.accessor(Screen.getAvailWidth, null, .{}); pub const availHeight = bridge.accessor(Screen.getAvailHeight, null, .{}); pub const colorDepth = bridge.accessor(Screen.getColorDepth, null, .{}); pub const pixelDepth = bridge.accessor(Screen.getPixelDepth, null, .{}); + pub const orientation = bridge.accessor(Screen.getOrientation, null, .{}); +}; + +pub const Orientation = struct { + _proto: *EventTarget, + + pub fn init(page: *Page) !*Orientation { + return page._factory.eventTarget(Orientation{ + ._proto = undefined, + }); + } + + pub fn asEventTarget(self: *Orientation) *EventTarget { + return self._proto; + } + + pub fn getAngle(_: *const Orientation) u32 { + return 0; + } + + pub fn getType(_: *const Orientation) []const u8 { + return "landscape-primary"; + } + + pub const JsApi = struct { + pub const bridge = js.Bridge(Orientation); + + pub const Meta = struct { + pub const name = "ScreenOrientation"; + pub const prototype_chain = bridge.prototypeChain(); + pub var class_id: bridge.ClassId = undefined; + }; + + pub const angle = bridge.accessor(Orientation.getAngle, null, .{}); + pub const @"type" = bridge.accessor(Orientation.getType, null, .{}); + }; }; diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zig index 792ca8dd..51213ecd 100644 --- a/src/browser/webapi/Window.zig +++ b/src/browser/webapi/Window.zig @@ -50,7 +50,7 @@ _css: CSS = .init, _crypto: Crypto = .init, _console: Console = .init, _navigator: Navigator = .init, -_screen: Screen = .init, +_screen: *Screen, _performance: Performance, _storage_bucket: *storage.Bucket, _on_load: ?js.Function = null, @@ -88,7 +88,7 @@ pub fn getNavigator(self: *Window) *Navigator { } pub fn getScreen(self: *Window) *Screen { - return &self._screen; + return self._screen; } pub fn getCrypto(self: *Window) *Crypto {