From ccebc7a905e5e358a508dbc580afe9572fffd85d Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Tue, 2 Apr 2024 22:44:05 +0200 Subject: [PATCH] LibWeb: Add stubs for the Screen Orientation API As defined in: https://w3c.github.io/screen-orientation --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + Userland/Libraries/LibWeb/CSS/Screen.cpp | 9 +++ Userland/Libraries/LibWeb/CSS/Screen.h | 2 + Userland/Libraries/LibWeb/CSS/Screen.idl | 5 ++ .../LibWeb/CSS/ScreenOrientation.cpp | 69 +++++++++++++++++++ .../Libraries/LibWeb/CSS/ScreenOrientation.h | 37 ++++++++++ .../LibWeb/CSS/ScreenOrientation.idl | 31 +++++++++ Userland/Libraries/LibWeb/Forward.h | 1 + Userland/Libraries/LibWeb/idl_files.cmake | 1 + 9 files changed, 156 insertions(+) create mode 100644 Userland/Libraries/LibWeb/CSS/ScreenOrientation.cpp create mode 100644 Userland/Libraries/LibWeb/CSS/ScreenOrientation.h create mode 100644 Userland/Libraries/LibWeb/CSS/ScreenOrientation.idl diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index f0ac1ef82a6..973ab5efd4b 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -85,6 +85,7 @@ set(SOURCES CSS/Resolution.cpp CSS/ResolvedCSSStyleDeclaration.cpp CSS/Screen.cpp + CSS/ScreenOrientation.cpp CSS/Selector.cpp CSS/SelectorEngine.cpp CSS/Serialize.cpp diff --git a/Userland/Libraries/LibWeb/CSS/Screen.cpp b/Userland/Libraries/LibWeb/CSS/Screen.cpp index d78a35e87e1..9fcf70e8000 100644 --- a/Userland/Libraries/LibWeb/CSS/Screen.cpp +++ b/Userland/Libraries/LibWeb/CSS/Screen.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,7 @@ void Screen::visit_edges(Cell::Visitor& visitor) { Base::visit_edges(visitor); visitor.visit(m_window); + visitor.visit(m_orientation); } Gfx::IntRect Screen::screen_rect() const @@ -49,4 +51,11 @@ Gfx::IntRect Screen::screen_rect() const }; } +JS::NonnullGCPtr Screen::orientation() +{ + if (!m_orientation) + m_orientation = ScreenOrientation::create(realm()); + return *m_orientation; +} + } diff --git a/Userland/Libraries/LibWeb/CSS/Screen.h b/Userland/Libraries/LibWeb/CSS/Screen.h index 8fa0a8e0c15..ae84012a5c0 100644 --- a/Userland/Libraries/LibWeb/CSS/Screen.h +++ b/Userland/Libraries/LibWeb/CSS/Screen.h @@ -26,6 +26,7 @@ public: i32 avail_height() const { return screen_rect().height(); } u32 color_depth() const { return 24; } u32 pixel_depth() const { return 24; } + JS::NonnullGCPtr orientation(); private: explicit Screen(HTML::Window&); @@ -38,6 +39,7 @@ private: Gfx::IntRect screen_rect() const; JS::NonnullGCPtr m_window; + JS::GCPtr m_orientation; }; } diff --git a/Userland/Libraries/LibWeb/CSS/Screen.idl b/Userland/Libraries/LibWeb/CSS/Screen.idl index edd7f92273f..1266a071b6c 100644 --- a/Userland/Libraries/LibWeb/CSS/Screen.idl +++ b/Userland/Libraries/LibWeb/CSS/Screen.idl @@ -1,3 +1,5 @@ +#import + // https://w3c.github.io/csswg-drafts/cssom-view-1/#screen [Exposed=Window] interface Screen { @@ -7,4 +9,7 @@ interface Screen { readonly attribute long height; readonly attribute unsigned long colorDepth; readonly attribute unsigned long pixelDepth; + + // https://w3c.github.io/screen-orientation/#extensions-to-the-screen-interface + [SameObject] readonly attribute ScreenOrientation orientation; }; diff --git a/Userland/Libraries/LibWeb/CSS/ScreenOrientation.cpp b/Userland/Libraries/LibWeb/CSS/ScreenOrientation.cpp new file mode 100644 index 00000000000..13e9ccaa455 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/ScreenOrientation.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::CSS { + +JS_DEFINE_ALLOCATOR(ScreenOrientation); + +ScreenOrientation::ScreenOrientation(JS::Realm& realm) + : DOM::EventTarget(realm) +{ +} + +void ScreenOrientation::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(ScreenOrientation); +} + +JS::NonnullGCPtr ScreenOrientation::create(JS::Realm& realm) +{ + return realm.heap().allocate(realm, realm); +} + +// https://w3c.github.io/screen-orientation/#lock-method +WebIDL::ExceptionOr> ScreenOrientation::lock(Bindings::OrientationLockType) +{ + return WebIDL::NotSupportedError::create(realm(), "FIXME: ScreenOrientation::lock() is not implemented"_fly_string); +} + +// https://w3c.github.io/screen-orientation/#unlock-method +void ScreenOrientation::unlock() +{ + dbgln("FIXME: Stubbed ScreenOrientation::unlock()"); +} + +// https://w3c.github.io/screen-orientation/#type-attribute +Bindings::OrientationType ScreenOrientation::type() const +{ + dbgln("FIXME: Stubbed ScreenOrientation::type()"); + return Bindings::OrientationType::LandscapePrimary; +} + +// https://w3c.github.io/screen-orientation/#angle-attribute +WebIDL::UnsignedShort ScreenOrientation::angle() const +{ + dbgln("FIXME: Stubbed ScreenOrientation::angle()"); + return 0; +} + +// https://w3c.github.io/screen-orientation/#onchange-event-handler-attribute +void ScreenOrientation::set_onchange(JS::GCPtr event_handler) +{ + set_event_handler_attribute(HTML::EventNames::change, event_handler); +} + +// https://w3c.github.io/screen-orientation/#onchange-event-handler-attribute +JS::GCPtr ScreenOrientation::onchange() +{ + return event_handler_attribute(HTML::EventNames::change); +} + +} diff --git a/Userland/Libraries/LibWeb/CSS/ScreenOrientation.h b/Userland/Libraries/LibWeb/CSS/ScreenOrientation.h new file mode 100644 index 00000000000..9c839ddc7cc --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/ScreenOrientation.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Web::CSS { + +class ScreenOrientation final : public DOM::EventTarget { + WEB_PLATFORM_OBJECT(ScreenOrientation, DOM::EventTarget); + JS_DECLARE_ALLOCATOR(ScreenOrientation); + +public: + [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&); + + WebIDL::ExceptionOr> lock(Bindings::OrientationLockType); + void unlock(); + Bindings::OrientationType type() const; + WebIDL::UnsignedShort angle() const; + + void set_onchange(JS::GCPtr); + JS::GCPtr onchange(); + +private: + explicit ScreenOrientation(JS::Realm&); + + virtual void initialize(JS::Realm&) override; +}; + +} diff --git a/Userland/Libraries/LibWeb/CSS/ScreenOrientation.idl b/Userland/Libraries/LibWeb/CSS/ScreenOrientation.idl new file mode 100644 index 00000000000..15a506e1fcf --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/ScreenOrientation.idl @@ -0,0 +1,31 @@ +#import + +// https://w3c.github.io/screen-orientation/#orientationtype-enum +enum OrientationType { + "portrait-primary", + "portrait-secondary", + "landscape-primary", + "landscape-secondary" +}; + +// https://w3c.github.io/screen-orientation/#orientationlocktype-enum +enum OrientationLockType { + "any", + "natural", + "landscape", + "portrait", + "portrait-primary", + "portrait-secondary", + "landscape-primary", + "landscape-secondary" +}; + +// https://w3c.github.io/screen-orientation/#screenorientation-interface +[Exposed=Window] +interface ScreenOrientation : EventTarget { + Promise lock(OrientationLockType orientation); + undefined unlock(); + readonly attribute OrientationType type; + readonly attribute unsigned short angle; + attribute EventHandler onchange; +}; diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 810f26ec1b7..7fb2c236851 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -172,6 +172,7 @@ class ResolutionOrCalculated; class ResolutionStyleValue; class RevertStyleValue; class Screen; +class ScreenOrientation; class Selector; class ShadowStyleValue; class ShorthandStyleValue; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 97e1bd45a78..4d276ca96f1 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -33,6 +33,7 @@ libweb_js_bindings(CSS/MediaList) libweb_js_bindings(CSS/MediaQueryList) libweb_js_bindings(CSS/MediaQueryListEvent) libweb_js_bindings(CSS/Screen) +libweb_js_bindings(CSS/ScreenOrientation) libweb_js_bindings(CSS/StyleSheet) libweb_js_bindings(CSS/StyleSheetList) libweb_js_bindings(CSS/VisualViewport)