Commit ab8ce397 authored by Emilio Cobos Álvarez's avatar Emilio Cobos Álvarez
Browse files

Bug 1792337 - Add a more convenient API to size a window constrained to a...

Bug 1792337 - Add a more convenient API to size a window constrained to a default size. r=bytesized,extension-reviewers,robwu,TYLin

Differential Revision: https://phabricator.services.mozilla.com/D158235
parent c54e38db
Loading
Loading
Loading
Loading
+2 −19
Original line number Diff line number Diff line
@@ -109,25 +109,8 @@ async function init(aEvent) {
    document.getElementById("release").hidden = false;
  }

  // TODO: Add a window.sizeToContentConstrained(width, height) that does this instead.
  const prefSize = 620;
  let w = {};
  let h = {};
  window.docShell.contentViewer.getContentSizeConstrained(
    prefSize * window.devicePixelRatio,
    0,
    w,
    h
  );
  w = Math.ceil(w.value / window.devicePixelRatio);
  h = Math.ceil(h.value / window.devicePixelRatio);
  const appWin = window.docShell.treeOwner
    .QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIAppWindow);

  h += appWin.outerToInnerHeightDifferenceInCSSPixels;
  w += appWin.outerToInnerWidthDifferenceInCSSPixels;
  window.resizeTo(w, h);
  const prefWidth = 620;
  window.sizeToContentConstrained(prefWidth, 0);

  if (AppConstants.platform == "macosx") {
    window.moveTo(
+16 −8
Original line number Diff line number Diff line
@@ -11,8 +11,10 @@ webidl Document;
webidl Node;

%{ C++
#include "mozilla/Maybe.h"
#include "nsTArray.h"
#include "nsRect.h"
#include "Units.h"

class nsIWidget;
class nsPresContext;
@@ -273,21 +275,27 @@ interface nsIContentViewer : nsISupports
   */
  attribute boolean authorStyleDisabled;

  /**
   * Requests the size of the content to the container.
   */
  void getContentSize(out long width, out long height);

  /**
   * Returns the preferred width and height of the content, constrained to the
   * given maximum values. If either maxWidth or maxHeight is less than zero,
   * that dimension is not constrained.
   *
   * All input and output values are in device pixels, rather than CSS pixels.
   * All input and output values are in CSS pixels.
   */
  void getContentSizeConstrained(in long maxWidth, in long maxHeight,
  void getContentSize(in long maxWidth, in long maxHeight,
                      out long width, out long height);

%{C++
  mozilla::Maybe<mozilla::CSSIntSize> GetContentSize(int32_t aMaxWidth = 0, int32_t aMaxHeight = 0) {
    int32_t w = 0;
    int32_t h = 0;
    if (NS_SUCCEEDED(GetContentSize(aMaxWidth, aMaxHeight, &w, &h))) {
      return mozilla::Some(mozilla::CSSIntSize(w, h));
    }
    return mozilla::Nothing();
  }
%}

  [noscript, notxpcom] Encoding getReloadEncodingAndSource(out int32_t aSource);
  [noscript, notxpcom] void setReloadEncodingAndSource(in Encoding aEncoding, in int32_t aSource);
  [noscript, notxpcom] void forgetReloadEncoding();
+9 −1
Original line number Diff line number Diff line
@@ -3931,7 +3931,15 @@ void nsGlobalWindowInner::ResizeBy(int32_t aWidthDif, int32_t aHeightDif,

void nsGlobalWindowInner::SizeToContent(CallerType aCallerType,
                                        ErrorResult& aError) {
  FORWARD_TO_OUTER_OR_THROW(SizeToContentOuter, (aCallerType, aError),
  FORWARD_TO_OUTER_OR_THROW(SizeToContentOuter, (aCallerType, 0, 0, aError),
                            aError, );
}

void nsGlobalWindowInner::SizeToContentConstrained(int32_t aMaxWidth,
                                                   int32_t aMaxHeight,
                                                   ErrorResult& aError) {
  FORWARD_TO_OUTER_OR_THROW(SizeToContentOuter,
                            (CallerType::System, aMaxWidth, aMaxHeight, aError),
                            aError, );
}

+2 −0
Original line number Diff line number Diff line
@@ -862,6 +862,8 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget,
      mozilla::ErrorResult& aError);
  void SizeToContent(mozilla::dom::CallerType aCallerType,
                     mozilla::ErrorResult& aError);
  void SizeToContentConstrained(int32_t aMaxWidth, int32_t aMaxHeight,
                                mozilla::ErrorResult& aError);
  mozilla::dom::Crypto* GetCrypto(mozilla::ErrorResult& aError);
  mozilla::dom::U2F* GetU2f(mozilla::ErrorResult& aError);
  nsIControllers* GetControllers(mozilla::ErrorResult& aError);
+11 −13
Original line number Diff line number Diff line
@@ -5466,6 +5466,8 @@ void nsGlobalWindowOuter::ResizeByOuter(int32_t aWidthDif, int32_t aHeightDif,
}

void nsGlobalWindowOuter::SizeToContentOuter(CallerType aCallerType,
                                             int32_t aMaxWidth,
                                             int32_t aMaxHeight,
                                             ErrorResult& aError) {
  if (!mDocShell) {
    return;
@@ -5485,22 +5487,19 @@ void nsGlobalWindowOuter::SizeToContentOuter(CallerType aCallerType,
  nsCOMPtr<nsIContentViewer> cv;
  mDocShell->GetContentViewer(getter_AddRefs(cv));
  if (!cv) {
    aError.Throw(NS_ERROR_FAILURE);
    return;
    return aError.Throw(NS_ERROR_FAILURE);
  }

  nsIntSize contentSize;
  aError = cv->GetContentSize(&contentSize.width, &contentSize.height);
  if (aError.Failed()) {
    return;
  auto contentSize = cv->GetContentSize(aMaxWidth, aMaxHeight);
  if (!contentSize) {
    return aError.Throw(NS_ERROR_FAILURE);
  }

  // Make sure the new size is following the CheckSecurityWidthAndHeight
  // rules.
  nsCOMPtr<nsIDocShellTreeOwner> treeOwner = GetTreeOwner();
  if (!treeOwner) {
    aError.Throw(NS_ERROR_FAILURE);
    return;
    return aError.Throw(NS_ERROR_FAILURE);
  }

  // Don't use DevToCSSIntPixelsForBaseWindow() nor
@@ -5511,12 +5510,11 @@ void nsGlobalWindowOuter::SizeToContentOuter(CallerType aCallerType,
  MOZ_ASSERT(
      presContext,
      "Should be non-nullptr if nsIContentViewer::GetContentSize() succeeded");
  nsIntSize cssSize(presContext->DevPixelsToIntCSSPixels(contentSize.width),
                    presContext->DevPixelsToIntCSSPixels(contentSize.height));

  CSSIntSize cssSize = *contentSize;
  CheckSecurityWidthAndHeight(&cssSize.width, &cssSize.height, aCallerType);

  nsIntSize newDevSize(presContext->CSSPixelsToDevPixels(cssSize.width),
  LayoutDeviceIntSize newDevSize(
      presContext->CSSPixelsToDevPixels(cssSize.width),
      presContext->CSSPixelsToDevPixels(cssSize.height));

  nsCOMPtr<nsIDocShell> docShell = mDocShell;
Loading