summaryrefslogtreecommitdiffstats
path: root/widget/DimensionRequest.cpp
blob: 61d4ae5cf79a3ed5e653d39e53e1205aec7e3b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "DimensionRequest.h"

#include "nsIBaseWindow.h"
#include "nsIDocShellTreeOwner.h"

namespace mozilla {

nsresult DimensionRequest::SupplementFrom(nsIBaseWindow* aSource) {
  NS_ENSURE_ARG_POINTER(aSource);
  int32_t x = 0, y = 0, width = 0, height = 0;

  bool needsPosition = mX.isSome() != mY.isSome();
  bool needsSize = mWidth.isSome() != mHeight.isSome();

  if (!needsPosition && !needsSize) {
    return NS_OK;
  }

  MOZ_TRY(aSource->GetDimensions(mDimensionKind, needsPosition ? &x : nullptr,
                                 needsPosition ? &y : nullptr,
                                 needsSize ? &width : nullptr,
                                 needsSize ? &height : nullptr));

  if (needsPosition) {
    if (mX.isNothing()) {
      mX.emplace(x);
    }
    if (mY.isNothing()) {
      mY.emplace(y);
    }
  }
  if (needsSize) {
    if (mWidth.isNothing()) {
      mWidth.emplace(width);
    }
    if (mHeight.isNothing()) {
      mHeight.emplace(height);
    }
  }

  MOZ_ASSERT(mX.isSome() == mY.isSome());
  MOZ_ASSERT(mWidth.isSome() == mHeight.isSome());
  return NS_OK;
}

nsresult DimensionRequest::ApplyOuterTo(nsIBaseWindow* aTarget) {
  NS_ENSURE_ARG_POINTER(aTarget);
  MOZ_ASSERT(mX.isSome() == mY.isSome(),
             "Missing dimensions should have been completed.");
  MOZ_ASSERT(mWidth.isSome() == mHeight.isSome(),
             "Missing dimensions should have been completed.");
  if (mDimensionKind != DimensionKind::Outer) {
    MOZ_ASSERT_UNREACHABLE("Expected outer dimensions.");
    return NS_ERROR_UNEXPECTED;
  }

  bool havePosition = mX.isSome() && mY.isSome();
  bool haveSize = mWidth.isSome() && mHeight.isSome();

  if (!havePosition && !haveSize) {
    return NS_OK;
  }

  if (havePosition && haveSize) {
    return aTarget->SetPositionAndSize(*mX, *mY, *mWidth, *mHeight, true);
  }

  if (havePosition) {
    return aTarget->SetPosition(*mX, *mY);
  }

  if (haveSize) {
    return aTarget->SetSize(*mWidth, *mHeight, true);
  }

  MOZ_ASSERT_UNREACHABLE();
  return NS_ERROR_UNEXPECTED;
}

nsresult DimensionRequest::ApplyInnerTo(nsIDocShellTreeOwner* aTarget,
                                        bool aAsRootShell) {
  NS_ENSURE_ARG_POINTER(aTarget);
  MOZ_ASSERT(mX.isSome() == mY.isSome(),
             "Missing dimensions should have been completed.");
  MOZ_ASSERT(mWidth.isSome() == mHeight.isSome(),
             "Missing dimensions should have been completed.");
  if (mDimensionKind != DimensionKind::Inner) {
    MOZ_ASSERT_UNREACHABLE("Expected inner dimensions.");
    return NS_ERROR_UNEXPECTED;
  }

  bool havePosition = mX.isSome() && mY.isSome();
  bool haveSize = mWidth.isSome() && mHeight.isSome();

  if (!havePosition && !haveSize) {
    return NS_OK;
  }

  if (havePosition) {
    MOZ_ASSERT_UNREACHABLE("Inner position is not implemented.");
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  if (haveSize) {
    if (aAsRootShell) {
      return aTarget->SetRootShellSize(*mWidth, *mHeight);
    }
    return aTarget->SetPrimaryContentSize(*mWidth, *mHeight);
  }

  MOZ_ASSERT_UNREACHABLE();
  return NS_ERROR_UNEXPECTED;
}

}  // namespace mozilla