浏览代码

LibWeb/HTML: Port Window.devicePixelRatio to IDL

Linus Groh 2 年之前
父节点
当前提交
8454eb874f

+ 12 - 13
Userland/Libraries/LibWeb/HTML/Window.cpp

@@ -744,12 +744,6 @@ void Window::queue_microtask_impl(WebIDL::CallbackType& callback)
     });
 }
 
-float Window::device_pixel_ratio() const
-{
-    // FIXME: Return 2.0f if we're in HiDPI mode!
-    return 1.0f;
-}
-
 // https://html.spec.whatwg.org/multipage/webstorage.html#dom-localstorage
 JS::NonnullGCPtr<HTML::Storage> Window::local_storage()
 {
@@ -956,7 +950,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
     MUST_OR_THROW_OOM(Bindings::WindowGlobalMixin::initialize(realm, *this));
 
     // FIXME: These should be native accessors, not properties
-    define_native_accessor(realm, "devicePixelRatio", device_pixel_ratio_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
     u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable;
     define_native_function(realm, "setInterval", set_interval, 1, attr);
     define_native_function(realm, "setTimeout", set_timeout, 1, attr);
@@ -1453,6 +1446,18 @@ i32 Window::outer_height() const
     return 0;
 }
 
+// https://w3c.github.io/csswg-drafts/cssom-view/#dom-window-devicepixelratio
+double Window::device_pixel_ratio() const
+{
+    // 1. If there is no output device, return 1 and abort these steps.
+    // 2. Let CSS pixel size be the size of a CSS pixel at the current page zoom and using a scale factor of 1.0.
+    // 3. Let device pixel size be the vertical size of a device pixel of the output device.
+    // 4. Return the result of dividing CSS pixel size by device pixel size.
+    if (auto* page = this->page())
+        return page->client().device_pixels_per_css_pixel();
+    return 1;
+}
+
 // https://w3c.github.io/hr-time/#dom-windoworworkerglobalscope-performance
 WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> Window::performance()
 {
@@ -1649,12 +1654,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::location_setter)
     return JS::js_undefined();
 }
 
-JS_DEFINE_NATIVE_FUNCTION(Window::device_pixel_ratio_getter)
-{
-    auto* impl = TRY(impl_from(vm));
-    return JS::Value(impl->device_pixel_ratio());
-}
-
 JS_DEFINE_NATIVE_FUNCTION(Window::get_computed_style)
 {
     auto* impl = TRY(impl_from(vm));

+ 1 - 4
Userland/Libraries/LibWeb/HTML/Window.h

@@ -113,8 +113,6 @@ public:
 
     void fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted);
 
-    float device_pixel_ratio() const;
-
     JS::NonnullGCPtr<HTML::Storage> local_storage();
     JS::NonnullGCPtr<HTML::Storage> session_storage();
 
@@ -178,6 +176,7 @@ public:
     i32 screen_y() const;
     i32 outer_width() const;
     i32 outer_height() const;
+    double device_pixel_ratio() const;
 
     WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> performance();
 
@@ -245,8 +244,6 @@ public:
 private:
     JS_DECLARE_NATIVE_FUNCTION(location_setter);
 
-    JS_DECLARE_NATIVE_FUNCTION(device_pixel_ratio_getter);
-
     JS_DECLARE_NATIVE_FUNCTION(structured_clone);
 
     JS_DECLARE_NATIVE_FUNCTION(local_storage_getter);

+ 1 - 0
Userland/Libraries/LibWeb/HTML/Window.idl

@@ -71,6 +71,7 @@ interface Window : EventTarget {
     [Replaceable, ImplementedAs=screen_y] readonly attribute long screenTop;
     [Replaceable] readonly attribute long outerWidth;
     [Replaceable] readonly attribute long outerHeight;
+    [Replaceable] readonly attribute double devicePixelRatio;
 
     // FIXME: Everything from here on should be shared through WindowOrWorkerGlobalScope
     // https://w3c.github.io/hr-time/#the-performance-attribute