Bug 1618601 - Make lazy-load margin configurable. r=hiro

But keep it being zero by default.

Depends on D64611

Differential Revision: https://phabricator.services.mozilla.com/D64612
This commit is contained in:
Emilio Cobos Álvarez
2020-02-27 21:14:43 +00:00
parent 4d662ea2c4
commit 33ce56e731
2 changed files with 75 additions and 12 deletions

View File

@@ -10,6 +10,7 @@
#include "nsContentUtils.h"
#include "nsLayoutUtils.h"
#include "mozilla/PresShell.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/dom/BrowserChild.h"
#include "mozilla/dom/BrowsingContext.h"
@@ -121,22 +122,39 @@ already_AddRefed<DOMIntersectionObserver> DOMIntersectionObserver::Constructor(
return observer.forget();
}
static void LazyLoadCallback(
const Sequence<OwningNonNull<DOMIntersectionObserverEntry>>& aEntries) {
for (const auto& entry : aEntries) {
MOZ_ASSERT(entry->Target()->IsHTMLElement(nsGkAtoms::img));
if (entry->IsIntersecting()) {
static_cast<HTMLImageElement*>(entry->Target())
->StopLazyLoadingAndStartLoadIfNeeded();
}
}
}
static LengthPercentage PrefMargin(float aValue, bool aIsPercentage) {
return aIsPercentage ? LengthPercentage::FromPercentage(aValue / 100.0f)
: LengthPercentage::FromPixels(aValue);
}
already_AddRefed<DOMIntersectionObserver>
DOMIntersectionObserver::CreateLazyLoadObserver(nsPIDOMWindowInner* aOwner) {
RefPtr<DOMIntersectionObserver> observer = new DOMIntersectionObserver(
aOwner,
[](const Sequence<OwningNonNull<DOMIntersectionObserverEntry>>& entries) {
for (const auto& entry : entries) {
MOZ_ASSERT(entry->Target()->IsHTMLElement(nsGkAtoms::img));
if (entry->IsIntersecting()) {
static_cast<HTMLImageElement*>(entry->Target())
->StopLazyLoadingAndStartLoadIfNeeded();
}
}
});
RefPtr<DOMIntersectionObserver> observer =
new DOMIntersectionObserver(aOwner, LazyLoadCallback);
observer->mThresholds.AppendElement(std::numeric_limits<double>::min());
#define SET_MARGIN(side_, side_lower_) \
observer->mRootMargin.Get(eSide##side_) = PrefMargin( \
StaticPrefs::dom_image_lazy_loading_root_margin_##side_lower_(), \
StaticPrefs:: \
dom_image_lazy_loading_root_margin_##side_lower_##_percentage());
SET_MARGIN(Top, top);
SET_MARGIN(Right, right);
SET_MARGIN(Bottom, bottom);
SET_MARGIN(Left, left);
#undef SET_MARGIN
return observer.forget();
}