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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "nsButtonFrameRenderer.h"
#include "nsCSSRendering.h"
#include "nsPresContext.h"
#include "nsPresContextInlines.h"
#include "nsGkAtoms.h"
#include "nsCSSPseudoElements.h"
#include "nsNameSpaceManager.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/Unused.h"
#include "nsDisplayList.h"
#include "nsITheme.h"
#include "nsIFrame.h"
#include "mozilla/dom/Element.h"
#include "gfxUtils.h"
#include "mozilla/layers/RenderRootStateManager.h"
using namespace mozilla;
using namespace mozilla::image;
using namespace mozilla::layers;
namespace mozilla {
class nsDisplayButtonBoxShadowOuter;
class nsDisplayButtonBorder;
class nsDisplayButtonForeground;
} // namespace mozilla
nsButtonFrameRenderer::nsButtonFrameRenderer() : mFrame(nullptr) {
MOZ_COUNT_CTOR(nsButtonFrameRenderer);
}
nsButtonFrameRenderer::~nsButtonFrameRenderer() {
MOZ_COUNT_DTOR(nsButtonFrameRenderer);
}
void nsButtonFrameRenderer::SetFrame(nsIFrame* aFrame,
nsPresContext* aPresContext) {
mFrame = aFrame;
ReResolveStyles(aPresContext);
}
nsIFrame* nsButtonFrameRenderer::GetFrame() { return mFrame; }
void nsButtonFrameRenderer::SetDisabled(bool aDisabled, bool aNotify) {
dom::Element* element = mFrame->GetContent()->AsElement();
if (aDisabled)
element->SetAttr(kNameSpaceID_None, nsGkAtoms::disabled, u""_ns, aNotify);
else
element->UnsetAttr(kNameSpaceID_None, nsGkAtoms::disabled, aNotify);
}
bool nsButtonFrameRenderer::isDisabled() {
return mFrame->GetContent()->AsElement()->IsDisabled();
}
nsresult nsButtonFrameRenderer::DisplayButton(nsDisplayListBuilder* aBuilder,
nsDisplayList* aBackground,
nsDisplayList* aForeground) {
if (!mFrame->StyleEffects()->mBoxShadow.IsEmpty()) {
aBackground->AppendNewToTop<nsDisplayButtonBoxShadowOuter>(aBuilder,
GetFrame());
}
nsRect buttonRect =
mFrame->GetRectRelativeToSelf() + aBuilder->ToReferenceFrame(mFrame);
const AppendedBackgroundType result =
nsDisplayBackgroundImage::AppendBackgroundItemsToTop(
aBuilder, mFrame, buttonRect, aBackground);
if (result == AppendedBackgroundType::None) {
aBuilder->BuildCompositorHitTestInfoIfNeeded(GetFrame(), aBackground);
}
aBackground->AppendNewToTop<nsDisplayButtonBorder>(aBuilder, GetFrame(),
this);
// Only display focus rings if we actually have them. Since at most one
// button would normally display a focus ring, most buttons won't have them.
const auto* disp = mFrame->StyleDisplay();
nsPresContext* pc = mFrame->PresContext();
if (mInnerFocusStyle && mInnerFocusStyle->StyleBorder()->HasBorder() &&
mFrame->IsThemed(disp) &&
pc->Theme()->ThemeWantsButtonInnerFocusRing(
mFrame, disp->EffectiveAppearance())) {
aForeground->AppendNewToTop<nsDisplayButtonForeground>(aBuilder, GetFrame(),
this);
}
return NS_OK;
}
void nsButtonFrameRenderer::GetButtonInnerFocusRect(const nsRect& aRect,
nsRect& aResult) {
aResult = aRect;
aResult.Deflate(mFrame->GetUsedBorderAndPadding());
if (mInnerFocusStyle) {
nsMargin innerFocusPadding(0, 0, 0, 0);
mInnerFocusStyle->StylePadding()->GetPadding(innerFocusPadding);
nsMargin framePadding = mFrame->GetUsedPadding();
innerFocusPadding.top = std::min(innerFocusPadding.top, framePadding.top);
innerFocusPadding.right =
std::min(innerFocusPadding.right, framePadding.right);
innerFocusPadding.bottom =
std::min(innerFocusPadding.bottom, framePadding.bottom);
innerFocusPadding.left =
std::min(innerFocusPadding.left, framePadding.left);
aResult.Inflate(innerFocusPadding);
}
}
ImgDrawResult nsButtonFrameRenderer::PaintInnerFocusBorder(
nsDisplayListBuilder* aBuilder, nsPresContext* aPresContext,
gfxContext& aRenderingContext, const nsRect& aDirtyRect,
const nsRect& aRect) {
// we draw the -moz-focus-inner border just inside the button's
// normal border and padding, to match Windows themes.
nsRect rect;
PaintBorderFlags flags = aBuilder->ShouldSyncDecodeImages()
? PaintBorderFlags::SyncDecodeImages
: PaintBorderFlags();
ImgDrawResult result = ImgDrawResult::SUCCESS;
if (mInnerFocusStyle) {
GetButtonInnerFocusRect(aRect, rect);
result &=
nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, mFrame,
aDirtyRect, rect, mInnerFocusStyle, flags);
}
return result;
}
Maybe<nsCSSBorderRenderer>
nsButtonFrameRenderer::CreateInnerFocusBorderRenderer(
nsDisplayListBuilder* aBuilder, nsPresContext* aPresContext,
gfxContext* aRenderingContext, const nsRect& aDirtyRect,
const nsRect& aRect, bool* aBorderIsEmpty) {
if (mInnerFocusStyle) {
nsRect rect;
GetButtonInnerFocusRect(aRect, rect);
gfx::DrawTarget* dt =
aRenderingContext ? aRenderingContext->GetDrawTarget() : nullptr;
return nsCSSRendering::CreateBorderRenderer(
aPresContext, dt, mFrame, aDirtyRect, rect, mInnerFocusStyle,
aBorderIsEmpty);
}
return Nothing();
}
ImgDrawResult nsButtonFrameRenderer::PaintBorder(nsDisplayListBuilder* aBuilder,
nsPresContext* aPresContext,
gfxContext& aRenderingContext,
const nsRect& aDirtyRect,
const nsRect& aRect) {
// get the button rect this is inside the focus and outline rects
nsRect buttonRect = aRect;
ComputedStyle* context = mFrame->Style();
PaintBorderFlags borderFlags = aBuilder->ShouldSyncDecodeImages()
? PaintBorderFlags::SyncDecodeImages
: PaintBorderFlags();
nsCSSRendering::PaintBoxShadowInner(aPresContext, aRenderingContext, mFrame,
buttonRect);
ImgDrawResult result =
nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, mFrame,
aDirtyRect, buttonRect, context, borderFlags);
return result;
}
/**
* Call this when styles change
*/
void nsButtonFrameRenderer::ReResolveStyles(nsPresContext* aPresContext) {
// get all the styles
ServoStyleSet* styleSet = aPresContext->StyleSet();
// get styles assigned to -moz-focus-inner (ie dotted border on Windows)
mInnerFocusStyle = styleSet->ProbePseudoElementStyle(
*mFrame->GetContent()->AsElement(), PseudoStyleType::mozFocusInner,
mFrame->Style());
}
ComputedStyle* nsButtonFrameRenderer::GetComputedStyle(int32_t aIndex) const {
switch (aIndex) {
case NS_BUTTON_RENDERER_FOCUS_INNER_CONTEXT_INDEX:
return mInnerFocusStyle;
default:
return nullptr;
}
}
void nsButtonFrameRenderer::SetComputedStyle(int32_t aIndex,
ComputedStyle* aComputedStyle) {
switch (aIndex) {
case NS_BUTTON_RENDERER_FOCUS_INNER_CONTEXT_INDEX:
mInnerFocusStyle = aComputedStyle;
break;
}
}
namespace mozilla {
class nsDisplayButtonBoxShadowOuter : public nsPaintedDisplayItem {
public:
nsDisplayButtonBoxShadowOuter(nsDisplayListBuilder* aBuilder,
nsIFrame* aFrame)
: nsPaintedDisplayItem(aBuilder, aFrame) {
MOZ_COUNT_CTOR(nsDisplayButtonBoxShadowOuter);
}
MOZ_COUNTED_DTOR_OVERRIDE(nsDisplayButtonBoxShadowOuter)
virtual bool CreateWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder,
mozilla::wr::IpcResourceUpdateQueue& aResources,
const StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) override;
bool CanBuildWebRenderDisplayItems();
virtual void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override;
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder,
bool* aSnap) const override;
NS_DISPLAY_DECL_NAME("ButtonBoxShadowOuter", TYPE_BUTTON_BOX_SHADOW_OUTER)
};
nsRect nsDisplayButtonBoxShadowOuter::GetBounds(nsDisplayListBuilder* aBuilder,
bool* aSnap) const {
*aSnap = false;
return mFrame->InkOverflowRectRelativeToSelf() + ToReferenceFrame();
}
void nsDisplayButtonBoxShadowOuter::Paint(nsDisplayListBuilder* aBuilder,
gfxContext* aCtx) {
nsRect frameRect = nsRect(ToReferenceFrame(), mFrame->GetSize());
nsCSSRendering::PaintBoxShadowOuter(mFrame->PresContext(), *aCtx, mFrame,
frameRect, GetPaintRect(aBuilder, aCtx));
}
bool nsDisplayButtonBoxShadowOuter::CanBuildWebRenderDisplayItems() {
// FIXME(emilio): Is this right? That doesn't make much sense.
if (mFrame->StyleEffects()->mBoxShadow.IsEmpty()) {
return false;
}
bool hasBorderRadius;
bool nativeTheme =
nsCSSRendering::HasBoxShadowNativeTheme(mFrame, hasBorderRadius);
// We don't support native themed things yet like box shadows around
// input buttons.
return !nativeTheme;
}
bool nsDisplayButtonBoxShadowOuter::CreateWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder,
mozilla::wr::IpcResourceUpdateQueue& aResources,
const StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) {
if (!CanBuildWebRenderDisplayItems()) {
return false;
}
int32_t appUnitsPerDevPixel = mFrame->PresContext()->AppUnitsPerDevPixel();
nsRect shadowRect = nsRect(ToReferenceFrame(), mFrame->GetSize());
LayoutDeviceRect deviceBox =
LayoutDeviceRect::FromAppUnits(shadowRect, appUnitsPerDevPixel);
wr::LayoutRect deviceBoxRect = wr::ToLayoutRect(deviceBox);
bool dummy;
LayoutDeviceRect clipRect = LayoutDeviceRect::FromAppUnits(
GetBounds(aDisplayListBuilder, &dummy), appUnitsPerDevPixel);
wr::LayoutRect deviceClipRect = wr::ToLayoutRect(clipRect);
bool hasBorderRadius;
Unused << nsCSSRendering::HasBoxShadowNativeTheme(mFrame, hasBorderRadius);
LayoutDeviceSize zeroSize;
wr::BorderRadius borderRadius =
wr::ToBorderRadius(zeroSize, zeroSize, zeroSize, zeroSize);
if (hasBorderRadius) {
gfx::RectCornerRadii borderRadii;
hasBorderRadius = nsCSSRendering::GetBorderRadii(shadowRect, shadowRect,
mFrame, borderRadii);
if (hasBorderRadius) {
borderRadius = wr::ToBorderRadius(borderRadii);
}
}
const Span<const StyleBoxShadow> shadows =
mFrame->StyleEffects()->mBoxShadow.AsSpan();
MOZ_ASSERT(!shadows.IsEmpty());
for (const StyleBoxShadow& shadow : Reversed(shadows)) {
if (shadow.inset) {
continue;
}
float blurRadius =
float(shadow.base.blur.ToAppUnits()) / float(appUnitsPerDevPixel);
gfx::DeviceColor shadowColor =
ToDeviceColor(nsCSSRendering::GetShadowColor(shadow.base, mFrame, 1.0));
LayoutDevicePoint shadowOffset = LayoutDevicePoint::FromAppUnits(
nsPoint(shadow.base.horizontal.ToAppUnits(),
shadow.base.vertical.ToAppUnits()),
appUnitsPerDevPixel);
float spreadRadius =
float(shadow.spread.ToAppUnits()) / float(appUnitsPerDevPixel);
aBuilder.PushBoxShadow(deviceBoxRect, deviceClipRect, !BackfaceIsHidden(),
deviceBoxRect, wr::ToLayoutVector2D(shadowOffset),
wr::ToColorF(shadowColor), blurRadius, spreadRadius,
borderRadius, wr::BoxShadowClipMode::Outset);
}
return true;
}
class nsDisplayButtonBorder final : public nsPaintedDisplayItem {
public:
nsDisplayButtonBorder(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
nsButtonFrameRenderer* aRenderer)
: nsPaintedDisplayItem(aBuilder, aFrame), mBFR(aRenderer) {
MOZ_COUNT_CTOR(nsDisplayButtonBorder);
}
MOZ_COUNTED_DTOR_OVERRIDE(nsDisplayButtonBorder)
virtual bool MustPaintOnContentSide() const override { return true; }
virtual void HitTest(nsDisplayListBuilder* aBuilder, const nsRect& aRect,
HitTestState* aState,
nsTArray<nsIFrame*>* aOutFrames) override {
aOutFrames->AppendElement(mFrame);
}
virtual void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override;
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder,
bool* aSnap) const override;
virtual bool CreateWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder,
mozilla::wr::IpcResourceUpdateQueue& aResources,
const StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) override;
NS_DISPLAY_DECL_NAME("ButtonBorderBackground", TYPE_BUTTON_BORDER_BACKGROUND)
private:
nsButtonFrameRenderer* mBFR;
};
bool nsDisplayButtonBorder::CreateWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder,
mozilla::wr::IpcResourceUpdateQueue& aResources,
const StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) {
// This is really a combination of paint box shadow inner +
// paint border.
aBuilder.StartGroup(this);
const nsRect buttonRect = nsRect(ToReferenceFrame(), mFrame->GetSize());
bool snap;
nsRect visible = GetBounds(aDisplayListBuilder, &snap);
nsDisplayBoxShadowInner::CreateInsetBoxShadowWebRenderCommands(
aBuilder, aSc, visible, mFrame, buttonRect);
bool borderIsEmpty = false;
Maybe<nsCSSBorderRenderer> br = nsCSSRendering::CreateBorderRenderer(
mFrame->PresContext(), nullptr, mFrame, nsRect(),
nsRect(ToReferenceFrame(), mFrame->GetSize()), mFrame->Style(),
&borderIsEmpty, mFrame->GetSkipSides());
if (!br) {
if (borderIsEmpty) {
aBuilder.FinishGroup();
} else {
aBuilder.CancelGroup(true);
}
return borderIsEmpty;
}
br->CreateWebRenderCommands(this, aBuilder, aResources, aSc);
aBuilder.FinishGroup();
return true;
}
void nsDisplayButtonBorder::Paint(nsDisplayListBuilder* aBuilder,
gfxContext* aCtx) {
NS_ASSERTION(mFrame, "No frame?");
nsPresContext* pc = mFrame->PresContext();
nsRect r = nsRect(ToReferenceFrame(), mFrame->GetSize());
// draw the border and background inside the focus and outline borders
Unused << mBFR->PaintBorder(aBuilder, pc, *aCtx, GetPaintRect(aBuilder, aCtx),
r);
}
nsRect nsDisplayButtonBorder::GetBounds(nsDisplayListBuilder* aBuilder,
bool* aSnap) const {
*aSnap = false;
return aBuilder->IsForEventDelivery()
? nsRect(ToReferenceFrame(), mFrame->GetSize())
: mFrame->InkOverflowRectRelativeToSelf() + ToReferenceFrame();
}
class nsDisplayButtonForeground final : public nsPaintedDisplayItem {
public:
nsDisplayButtonForeground(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
nsButtonFrameRenderer* aRenderer)
: nsPaintedDisplayItem(aBuilder, aFrame), mBFR(aRenderer) {
MOZ_COUNT_CTOR(nsDisplayButtonForeground);
}
MOZ_COUNTED_DTOR_OVERRIDE(nsDisplayButtonForeground)
void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override;
bool CreateWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder,
mozilla::wr::IpcResourceUpdateQueue& aResources,
const StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) override;
NS_DISPLAY_DECL_NAME("ButtonForeground", TYPE_BUTTON_FOREGROUND)
private:
nsButtonFrameRenderer* mBFR;
};
void nsDisplayButtonForeground::Paint(nsDisplayListBuilder* aBuilder,
gfxContext* aCtx) {
nsRect r = nsRect(ToReferenceFrame(), mFrame->GetSize());
// Draw the -moz-focus-inner border
Unused << mBFR->PaintInnerFocusBorder(aBuilder, mFrame->PresContext(), *aCtx,
GetPaintRect(aBuilder, aCtx), r);
}
bool nsDisplayButtonForeground::CreateWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder,
mozilla::wr::IpcResourceUpdateQueue& aResources,
const StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) {
Maybe<nsCSSBorderRenderer> br;
bool borderIsEmpty = false;
bool dummy;
nsRect r = nsRect(ToReferenceFrame(), mFrame->GetSize());
br = mBFR->CreateInnerFocusBorderRenderer(
aDisplayListBuilder, mFrame->PresContext(), nullptr,
GetBounds(aDisplayListBuilder, &dummy), r, &borderIsEmpty);
if (!br) {
return borderIsEmpty;
}
aBuilder.StartGroup(this);
br->CreateWebRenderCommands(this, aBuilder, aResources, aSc);
aBuilder.FinishGroup();
return true;
}
} // namespace mozilla
|