فهرست منبع

LibWeb: Add stubs for the Screen Orientation API

As defined in: https://w3c.github.io/screen-orientation
Shannon Booth 1 سال پیش
والد
کامیت
ccebc7a905

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -85,6 +85,7 @@ set(SOURCES
     CSS/Resolution.cpp
     CSS/Resolution.cpp
     CSS/ResolvedCSSStyleDeclaration.cpp
     CSS/ResolvedCSSStyleDeclaration.cpp
     CSS/Screen.cpp
     CSS/Screen.cpp
+    CSS/ScreenOrientation.cpp
     CSS/Selector.cpp
     CSS/Selector.cpp
     CSS/SelectorEngine.cpp
     CSS/SelectorEngine.cpp
     CSS/Serialize.cpp
     CSS/Serialize.cpp

+ 9 - 0
Userland/Libraries/LibWeb/CSS/Screen.cpp

@@ -8,6 +8,7 @@
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/Bindings/ScreenPrototype.h>
 #include <LibWeb/Bindings/ScreenPrototype.h>
 #include <LibWeb/CSS/Screen.h>
 #include <LibWeb/CSS/Screen.h>
+#include <LibWeb/CSS/ScreenOrientation.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/Page/Page.h>
 #include <LibWeb/Page/Page.h>
 
 
@@ -36,6 +37,7 @@ void Screen::visit_edges(Cell::Visitor& visitor)
 {
 {
     Base::visit_edges(visitor);
     Base::visit_edges(visitor);
     visitor.visit(m_window);
     visitor.visit(m_window);
+    visitor.visit(m_orientation);
 }
 }
 
 
 Gfx::IntRect Screen::screen_rect() const
 Gfx::IntRect Screen::screen_rect() const
@@ -49,4 +51,11 @@ Gfx::IntRect Screen::screen_rect() const
     };
     };
 }
 }
 
 
+JS::NonnullGCPtr<ScreenOrientation> Screen::orientation()
+{
+    if (!m_orientation)
+        m_orientation = ScreenOrientation::create(realm());
+    return *m_orientation;
+}
+
 }
 }

+ 2 - 0
Userland/Libraries/LibWeb/CSS/Screen.h

@@ -26,6 +26,7 @@ public:
     i32 avail_height() const { return screen_rect().height(); }
     i32 avail_height() const { return screen_rect().height(); }
     u32 color_depth() const { return 24; }
     u32 color_depth() const { return 24; }
     u32 pixel_depth() const { return 24; }
     u32 pixel_depth() const { return 24; }
+    JS::NonnullGCPtr<ScreenOrientation> orientation();
 
 
 private:
 private:
     explicit Screen(HTML::Window&);
     explicit Screen(HTML::Window&);
@@ -38,6 +39,7 @@ private:
     Gfx::IntRect screen_rect() const;
     Gfx::IntRect screen_rect() const;
 
 
     JS::NonnullGCPtr<HTML::Window> m_window;
     JS::NonnullGCPtr<HTML::Window> m_window;
+    JS::GCPtr<ScreenOrientation> m_orientation;
 };
 };
 
 
 }
 }

+ 5 - 0
Userland/Libraries/LibWeb/CSS/Screen.idl

@@ -1,3 +1,5 @@
+#import <CSS/ScreenOrientation.idl>
+
 // https://w3c.github.io/csswg-drafts/cssom-view-1/#screen
 // https://w3c.github.io/csswg-drafts/cssom-view-1/#screen
 [Exposed=Window]
 [Exposed=Window]
 interface Screen {
 interface Screen {
@@ -7,4 +9,7 @@ interface Screen {
     readonly attribute long height;
     readonly attribute long height;
     readonly attribute unsigned long colorDepth;
     readonly attribute unsigned long colorDepth;
     readonly attribute unsigned long pixelDepth;
     readonly attribute unsigned long pixelDepth;
+
+    // https://w3c.github.io/screen-orientation/#extensions-to-the-screen-interface
+    [SameObject] readonly attribute ScreenOrientation orientation;
 };
 };

+ 69 - 0
Userland/Libraries/LibWeb/CSS/ScreenOrientation.cpp

@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Bindings/Intrinsics.h>
+#include <LibWeb/CSS/ScreenOrientation.h>
+#include <LibWeb/HTML/EventNames.h>
+
+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> ScreenOrientation::create(JS::Realm& realm)
+{
+    return realm.heap().allocate<ScreenOrientation>(realm, realm);
+}
+
+// https://w3c.github.io/screen-orientation/#lock-method
+WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> 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<WebIDL::CallbackType> event_handler)
+{
+    set_event_handler_attribute(HTML::EventNames::change, event_handler);
+}
+
+// https://w3c.github.io/screen-orientation/#onchange-event-handler-attribute
+JS::GCPtr<WebIDL::CallbackType> ScreenOrientation::onchange()
+{
+    return event_handler_attribute(HTML::EventNames::change);
+}
+
+}

+ 37 - 0
Userland/Libraries/LibWeb/CSS/ScreenOrientation.h

@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWeb/Bindings/PlatformObject.h>
+#include <LibWeb/Bindings/ScreenOrientationPrototype.h>
+#include <LibWeb/DOM/EventTarget.h>
+#include <LibWeb/WebIDL/Types.h>
+
+namespace Web::CSS {
+
+class ScreenOrientation final : public DOM::EventTarget {
+    WEB_PLATFORM_OBJECT(ScreenOrientation, DOM::EventTarget);
+    JS_DECLARE_ALLOCATOR(ScreenOrientation);
+
+public:
+    [[nodiscard]] static JS::NonnullGCPtr<ScreenOrientation> create(JS::Realm&);
+
+    WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> lock(Bindings::OrientationLockType);
+    void unlock();
+    Bindings::OrientationType type() const;
+    WebIDL::UnsignedShort angle() const;
+
+    void set_onchange(JS::GCPtr<WebIDL::CallbackType>);
+    JS::GCPtr<WebIDL::CallbackType> onchange();
+
+private:
+    explicit ScreenOrientation(JS::Realm&);
+
+    virtual void initialize(JS::Realm&) override;
+};
+
+}

+ 31 - 0
Userland/Libraries/LibWeb/CSS/ScreenOrientation.idl

@@ -0,0 +1,31 @@
+#import <DOM/EventHandler.idl>
+
+// 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<undefined> lock(OrientationLockType orientation);
+    undefined unlock();
+    readonly attribute OrientationType type;
+    readonly attribute unsigned short angle;
+    attribute EventHandler onchange;
+};

+ 1 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -172,6 +172,7 @@ class ResolutionOrCalculated;
 class ResolutionStyleValue;
 class ResolutionStyleValue;
 class RevertStyleValue;
 class RevertStyleValue;
 class Screen;
 class Screen;
+class ScreenOrientation;
 class Selector;
 class Selector;
 class ShadowStyleValue;
 class ShadowStyleValue;
 class ShorthandStyleValue;
 class ShorthandStyleValue;

+ 1 - 0
Userland/Libraries/LibWeb/idl_files.cmake

@@ -33,6 +33,7 @@ libweb_js_bindings(CSS/MediaList)
 libweb_js_bindings(CSS/MediaQueryList)
 libweb_js_bindings(CSS/MediaQueryList)
 libweb_js_bindings(CSS/MediaQueryListEvent)
 libweb_js_bindings(CSS/MediaQueryListEvent)
 libweb_js_bindings(CSS/Screen)
 libweb_js_bindings(CSS/Screen)
+libweb_js_bindings(CSS/ScreenOrientation)
 libweb_js_bindings(CSS/StyleSheet)
 libweb_js_bindings(CSS/StyleSheet)
 libweb_js_bindings(CSS/StyleSheetList)
 libweb_js_bindings(CSS/StyleSheetList)
 libweb_js_bindings(CSS/VisualViewport)
 libweb_js_bindings(CSS/VisualViewport)