1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsNativeTheme.h"
8 #include "mozilla/dom/Document.h"
9 #include "nsIContent.h"
11 #include "nsIScrollableFrame.h"
12 #include "nsLayoutUtils.h"
13 #include "nsNumberControlFrame.h"
14 #include "nsPresContext.h"
16 #include "nsNameSpaceManager.h"
17 #include "nsStyleConsts.h"
18 #include "nsPIDOMWindow.h"
19 #include "nsProgressFrame.h"
20 #include "nsMeterFrame.h"
21 #include "nsRangeFrame.h"
22 #include "nsCSSRendering.h"
23 #include "ImageContainer.h"
24 #include "mozilla/ComputedStyle.h"
25 #include "mozilla/dom/Element.h"
26 #include "mozilla/dom/HTMLBodyElement.h"
27 #include "mozilla/dom/HTMLInputElement.h"
28 #include "mozilla/dom/HTMLProgressElement.h"
29 #include "mozilla/PresShell.h"
30 #include "mozilla/StaticPrefs_layout.h"
31 #include "mozilla/dom/DocumentInlines.h"
34 using namespace mozilla
;
35 using namespace mozilla::dom
;
37 nsNativeTheme::nsNativeTheme() : mAnimatedContentTimeout(UINT32_MAX
) {}
39 NS_IMPL_ISUPPORTS(nsNativeTheme
, nsITimerCallback
, nsINamed
)
41 /* static */ ElementState
nsNativeTheme::GetContentState(
42 nsIFrame
* aFrame
, StyleAppearance aAppearance
) {
44 return ElementState();
47 nsIContent
* frameContent
= aFrame
->GetContent();
48 if (!frameContent
|| !frameContent
->IsElement()) {
49 return ElementState();
52 const bool isXULElement
= frameContent
->IsXULElement();
54 if (aAppearance
== StyleAppearance::CheckboxLabel
||
55 aAppearance
== StyleAppearance::RadioLabel
) {
56 aFrame
= aFrame
->GetParent()->GetParent();
57 frameContent
= aFrame
->GetContent();
58 } else if (aAppearance
== StyleAppearance::Checkbox
||
59 aAppearance
== StyleAppearance::Radio
||
60 aAppearance
== StyleAppearance::ToolbarbuttonDropdown
||
61 aAppearance
== StyleAppearance::Treeheadersortarrow
||
62 aAppearance
== StyleAppearance::ButtonArrowPrevious
||
63 aAppearance
== StyleAppearance::ButtonArrowNext
||
64 aAppearance
== StyleAppearance::ButtonArrowUp
||
65 aAppearance
== StyleAppearance::ButtonArrowDown
) {
66 aFrame
= aFrame
->GetParent();
67 frameContent
= aFrame
->GetContent();
69 MOZ_ASSERT(frameContent
&& frameContent
->IsElement());
72 ElementState flags
= frameContent
->AsElement()->StyleState();
73 nsNumberControlFrame
* numberControlFrame
=
74 nsNumberControlFrame::GetNumberControlFrameForSpinButton(aFrame
);
75 if (numberControlFrame
&&
76 numberControlFrame
->GetContent()->AsElement()->StyleState().HasState(
77 ElementState::DISABLED
)) {
78 flags
|= ElementState::DISABLED
;
85 if (CheckBooleanAttr(aFrame
, nsGkAtoms::disabled
)) {
86 flags
|= ElementState::DISABLED
;
89 switch (aAppearance
) {
90 case StyleAppearance::RadioLabel
:
91 case StyleAppearance::Radio
: {
92 if (CheckBooleanAttr(aFrame
, nsGkAtoms::focused
)) {
93 flags
|= ElementState::FOCUS
;
94 nsPIDOMWindowOuter
* window
=
95 aFrame
->GetContent()->OwnerDoc()->GetWindow();
96 if (window
&& window
->ShouldShowFocusRing()) {
97 flags
|= ElementState::FOCUSRING
;
100 if (CheckBooleanAttr(aFrame
, nsGkAtoms::selected
)) {
101 flags
|= ElementState::CHECKED
;
105 case StyleAppearance::CheckboxLabel
:
106 case StyleAppearance::Checkbox
: {
107 if (CheckBooleanAttr(aFrame
, nsGkAtoms::checked
)) {
108 flags
|= ElementState::CHECKED
;
109 } else if (CheckBooleanAttr(aFrame
, nsGkAtoms::indeterminate
)) {
110 flags
|= ElementState::INDETERMINATE
;
114 case StyleAppearance::MenulistButton
:
115 case StyleAppearance::Menulist
:
116 case StyleAppearance::NumberInput
:
117 case StyleAppearance::Textfield
:
118 case StyleAppearance::Searchfield
:
119 case StyleAppearance::Textarea
: {
120 if (CheckBooleanAttr(aFrame
, nsGkAtoms::focused
)) {
121 flags
|= ElementState::FOCUS
| ElementState::FOCUSRING
;
133 bool nsNativeTheme::CheckBooleanAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
) {
134 if (!aFrame
) return false;
136 nsIContent
* content
= aFrame
->GetContent();
137 if (!content
|| !content
->IsElement()) return false;
139 if (content
->IsHTMLElement())
140 return content
->AsElement()->HasAttr(kNameSpaceID_None
, aAtom
);
142 // For XML/XUL elements, an attribute must be equal to the literal
143 // string "true" to be counted as true. An empty string should _not_
144 // be counted as true.
145 return content
->AsElement()->AttrValueIs(kNameSpaceID_None
, aAtom
, u
"true"_ns
,
150 int32_t nsNativeTheme::CheckIntAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
,
151 int32_t defaultValue
) {
152 if (!aFrame
) return defaultValue
;
154 nsIContent
* content
= aFrame
->GetContent();
155 if (!content
|| !content
->IsElement()) return defaultValue
;
158 content
->AsElement()->GetAttr(kNameSpaceID_None
, aAtom
, attr
);
160 int32_t value
= attr
.ToInteger(&err
);
161 if (attr
.IsEmpty() || NS_FAILED(err
)) return defaultValue
;
167 double nsNativeTheme::GetProgressValue(nsIFrame
* aFrame
) {
168 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
172 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Value();
176 double nsNativeTheme::GetProgressMaxValue(nsIFrame
* aFrame
) {
177 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
181 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Max();
184 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame
* aFrame
) {
185 if (!aFrame
) return false;
187 nsIContent
* content
= aFrame
->GetContent();
188 return content
->IsXULElement(nsGkAtoms::button
) &&
189 content
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
190 u
"menu"_ns
, eCaseMatters
);
193 bool nsNativeTheme::IsPressedButton(nsIFrame
* aFrame
) {
194 ElementState state
= GetContentState(aFrame
, StyleAppearance::Toolbarbutton
);
195 if (state
.HasState(ElementState::DISABLED
)) {
199 return IsOpenButton(aFrame
) ||
200 state
.HasAllStates(ElementState::ACTIVE
| ElementState::HOVER
);
203 bool nsNativeTheme::IsWidgetStyled(nsPresContext
* aPresContext
,
205 StyleAppearance aAppearance
) {
206 // Check for specific widgets to see if HTML has overridden the style.
211 // Resizers have some special handling, dependent on whether in a scrollable
212 // container or not. If so, use the scrollable container's to determine
213 // whether the style is overriden instead of the resizer. This allows a
214 // non-native transparent resizer to be used instead. Otherwise, we just
215 // fall through and return false.
216 if (aAppearance
== StyleAppearance::Resizer
) {
217 nsIFrame
* parentFrame
= aFrame
->GetParent();
218 if (parentFrame
&& parentFrame
->IsScrollFrame()) {
219 // if the parent is a scrollframe, the resizer should be native themed
220 // only if the scrollable area doesn't override the widget style.
222 // note that the condition below looks a bit suspect but it's the right
223 // one. If there's no valid appearance, then we should return true, it's
224 // effectively the same as if it had overridden the appearance.
225 parentFrame
= parentFrame
->GetParent();
229 auto parentAppearance
=
230 parentFrame
->StyleDisplay()->EffectiveAppearance();
231 return parentAppearance
== StyleAppearance::None
||
232 IsWidgetStyled(aPresContext
, parentFrame
, parentAppearance
);
237 * Progress bar appearance should be the same for the bar and the container
238 * frame. nsProgressFrame owns the logic and will tell us what we should do.
240 if (aAppearance
== StyleAppearance::Progresschunk
||
241 aAppearance
== StyleAppearance::ProgressBar
) {
242 nsProgressFrame
* progressFrame
= do_QueryFrame(
243 aAppearance
== StyleAppearance::Progresschunk
? aFrame
->GetParent()
246 return !progressFrame
->ShouldUseNativeStyle();
251 * Meter bar appearance should be the same for the bar and the container
252 * frame. nsMeterFrame owns the logic and will tell us what we should do.
254 if (aAppearance
== StyleAppearance::Meterchunk
||
255 aAppearance
== StyleAppearance::Meter
) {
256 nsMeterFrame
* meterFrame
= do_QueryFrame(
257 aAppearance
== StyleAppearance::Meterchunk
? aFrame
->GetParent()
260 return !meterFrame
->ShouldUseNativeStyle();
265 * An nsRangeFrame and its children are treated atomically when it
266 * comes to native theming (either all parts, or no parts, are themed).
267 * nsRangeFrame owns the logic and will tell us what we should do.
269 if (aAppearance
== StyleAppearance::Range
||
270 aAppearance
== StyleAppearance::RangeThumb
) {
271 nsRangeFrame
* rangeFrame
= do_QueryFrame(
272 aAppearance
== StyleAppearance::RangeThumb
? aFrame
->GetParent()
275 return !rangeFrame
->ShouldUseNativeStyle();
279 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
281 aFrame
->GetContent()->IsHTMLElement() &&
282 aFrame
->Style()->HasAuthorSpecifiedBorderOrBackground();
286 bool nsNativeTheme::IsFrameRTL(nsIFrame
* aFrame
) {
290 return aFrame
->GetWritingMode().IsPhysicalRTL();
294 bool nsNativeTheme::IsHTMLContent(nsIFrame
* aFrame
) {
298 nsIContent
* content
= aFrame
->GetContent();
299 return content
&& content
->IsHTMLElement();
303 nsNativeTheme::TreeSortDirection
nsNativeTheme::GetTreeSortDirection(
305 if (!aFrame
|| !aFrame
->GetContent()) return eTreeSortDirection_Natural
;
307 static Element::AttrValuesArray strings
[] = {nsGkAtoms::descending
,
308 nsGkAtoms::ascending
, nullptr};
310 nsIContent
* content
= aFrame
->GetContent();
311 if (content
->IsElement()) {
312 switch (content
->AsElement()->FindAttrValueIn(
313 kNameSpaceID_None
, nsGkAtoms::sortDirection
, strings
, eCaseMatters
)) {
315 return eTreeSortDirection_Descending
;
317 return eTreeSortDirection_Ascending
;
321 return eTreeSortDirection_Natural
;
324 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame
* aFrame
) {
329 // A tree column picker button is always the last header cell.
330 if (aFrame
->GetContent()->IsXULElement(nsGkAtoms::button
)) {
334 // Find the parent tree.
335 nsIContent
* parent
= aFrame
->GetContent()->GetParent();
336 while (parent
&& !parent
->IsXULElement(nsGkAtoms::tree
)) {
337 parent
= parent
->GetParent();
340 // If the column picker is visible, this can't be the last column.
341 if (parent
&& !parent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
342 nsGkAtoms::hidecolumnpicker
,
343 u
"true"_ns
, eCaseMatters
))
346 while ((aFrame
= aFrame
->GetNextSibling())) {
347 if (aFrame
->GetRect().Width() > 0) return false;
353 bool nsNativeTheme::IsBottomTab(nsIFrame
* aFrame
) {
354 if (!aFrame
) return false;
356 nsAutoString classStr
;
357 if (aFrame
->GetContent()->IsElement()) {
358 aFrame
->GetContent()->AsElement()->GetAttr(kNameSpaceID_None
,
359 nsGkAtoms::_class
, classStr
);
361 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
362 return !classStr
.IsEmpty() && classStr
.Find(u
"tab-bottom") != kNotFound
;
365 bool nsNativeTheme::IsFirstTab(nsIFrame
* aFrame
) {
366 if (!aFrame
) return false;
368 for (nsIFrame
* first
: aFrame
->GetParent()->PrincipalChildList()) {
369 if (first
->GetRect().Width() > 0 &&
370 first
->GetContent()->IsXULElement(nsGkAtoms::tab
))
371 return (first
== aFrame
);
376 bool nsNativeTheme::IsHorizontal(nsIFrame
* aFrame
) {
377 if (!aFrame
) return false;
379 if (!aFrame
->GetContent()->IsElement()) return true;
381 return !aFrame
->GetContent()->AsElement()->AttrValueIs(
382 kNameSpaceID_None
, nsGkAtoms::orient
, nsGkAtoms::vertical
, eCaseMatters
);
385 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame
* aFrame
, int32_t aOffset
) {
386 if (!aFrame
) return false;
388 if (aOffset
== 0) return IsSelectedTab(aFrame
);
390 int32_t thisTabIndex
= -1, selectedTabIndex
= -1;
392 nsIFrame
* currentTab
= aFrame
->GetParent()->PrincipalChildList().FirstChild();
393 for (int32_t i
= 0; currentTab
; currentTab
= currentTab
->GetNextSibling()) {
394 if (currentTab
->GetRect().Width() == 0) continue;
395 if (aFrame
== currentTab
) thisTabIndex
= i
;
396 if (IsSelectedTab(currentTab
)) selectedTabIndex
= i
;
400 if (thisTabIndex
== -1 || selectedTabIndex
== -1) return false;
402 return (thisTabIndex
- selectedTabIndex
== aOffset
);
405 bool nsNativeTheme::IsVerticalProgress(nsIFrame
* aFrame
) {
409 return IsVerticalMeter(aFrame
);
412 bool nsNativeTheme::IsVerticalMeter(nsIFrame
* aFrame
) {
413 MOZ_ASSERT(aFrame
, "You have to pass a non-null aFrame");
414 switch (aFrame
->StyleDisplay()->mOrient
) {
415 case StyleOrient::Horizontal
:
417 case StyleOrient::Vertical
:
419 case StyleOrient::Inline
:
420 return aFrame
->GetWritingMode().IsVertical();
421 case StyleOrient::Block
:
422 return !aFrame
->GetWritingMode().IsVertical();
424 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
429 bool nsNativeTheme::IsSubmenu(nsIFrame
* aFrame
, bool* aLeftOfParent
) {
430 if (!aFrame
) return false;
432 nsIContent
* parentContent
= aFrame
->GetContent()->GetParent();
433 if (!parentContent
|| !parentContent
->IsXULElement(nsGkAtoms::menu
))
436 nsIFrame
* parent
= aFrame
;
437 while ((parent
= parent
->GetParent())) {
438 if (parent
->GetContent() == parentContent
) {
440 LayoutDeviceIntRect selfBounds
, parentBounds
;
441 selfBounds
= aFrame
->GetNearestWidget()->GetScreenBounds();
442 parentBounds
= parent
->GetNearestWidget()->GetScreenBounds();
443 *aLeftOfParent
= selfBounds
.X() < parentBounds
.X();
452 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent
* aContent
,
453 uint32_t aMinimumFrameRate
) {
454 NS_ASSERTION(aContent
, "Null pointer!");
455 NS_ASSERTION(aMinimumFrameRate
, "aMinimumFrameRate must be non-zero!");
456 NS_ASSERTION(aMinimumFrameRate
<= 1000,
457 "aMinimumFrameRate must be less than 1000!");
459 uint32_t timeout
= 1000 / aMinimumFrameRate
;
460 timeout
= std::min(mAnimatedContentTimeout
, timeout
);
462 if (!mAnimatedContentTimer
) {
463 mAnimatedContentTimer
= NS_NewTimer();
464 NS_ENSURE_TRUE(mAnimatedContentTimer
, false);
467 if (mAnimatedContentList
.IsEmpty() || timeout
!= mAnimatedContentTimeout
) {
469 if (!mAnimatedContentList
.IsEmpty()) {
470 rv
= mAnimatedContentTimer
->Cancel();
471 NS_ENSURE_SUCCESS(rv
, false);
474 if (XRE_IsContentProcess() && NS_IsMainThread()) {
475 mAnimatedContentTimer
->SetTarget(
476 aContent
->OwnerDoc()->EventTargetFor(TaskCategory::Other
));
478 rv
= mAnimatedContentTimer
->InitWithCallback(this, timeout
,
479 nsITimer::TYPE_ONE_SHOT
);
480 NS_ENSURE_SUCCESS(rv
, false);
482 mAnimatedContentTimeout
= timeout
;
485 // XXX(Bug 1631371) Check if this should use a fallible operation as it
486 // pretended earlier.
487 mAnimatedContentList
.AppendElement(aContent
);
493 nsNativeTheme::Notify(nsITimer
* aTimer
) {
494 NS_ASSERTION(aTimer
== mAnimatedContentTimer
, "Wrong timer!");
496 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
497 // QueueAnimatedContentForRefresh.
499 uint32_t count
= mAnimatedContentList
.Length();
500 for (uint32_t index
= 0; index
< count
; index
++) {
501 nsIFrame
* frame
= mAnimatedContentList
[index
]->GetPrimaryFrame();
503 frame
->InvalidateFrame();
507 mAnimatedContentList
.Clear();
508 mAnimatedContentTimeout
= UINT32_MAX
;
513 nsNativeTheme::GetName(nsACString
& aName
) {
514 aName
.AssignLiteral("nsNativeTheme");
518 nsIFrame
* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
519 nsIFrame
* aFrame
, bool aNextSibling
) {
520 if (!aFrame
) return nullptr;
522 // Find the next visible sibling.
523 nsIFrame
* sibling
= aFrame
;
526 aNextSibling
? sibling
->GetNextSibling() : sibling
->GetPrevSibling();
527 } while (sibling
&& sibling
->GetRect().Width() == 0);
529 // Check same appearance and adjacency.
531 sibling
->StyleDisplay()->EffectiveAppearance() !=
532 aFrame
->StyleDisplay()->EffectiveAppearance() ||
533 (sibling
->GetRect().XMost() != aFrame
->GetRect().X() &&
534 aFrame
->GetRect().XMost() != sibling
->GetRect().X()))
539 bool nsNativeTheme::IsRangeHorizontal(nsIFrame
* aFrame
) {
540 nsIFrame
* rangeFrame
= aFrame
;
541 if (!rangeFrame
->IsRangeFrame()) {
542 // If the thumb's frame is passed in, get its range parent:
543 rangeFrame
= aFrame
->GetParent();
545 if (rangeFrame
->IsRangeFrame()) {
546 return static_cast<nsRangeFrame
*>(rangeFrame
)->IsHorizontal();
548 // Not actually a range frame - just use the ratio of the frame's size to
550 return aFrame
->GetSize().width
>= aFrame
->GetSize().height
;
554 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame
* aFrame
) {
555 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
558 nsIFrame
* frame
= aFrame
;
559 nsIScrollableFrame
* scrollFrame
= nullptr;
560 while (!scrollFrame
&& frame
) {
561 scrollFrame
= frame
->GetScrollTargetFrame();
562 frame
= frame
->GetParent();
565 aFrame
= scrollFrame
->GetScrolledFrame();
567 // Leave aFrame untouched.
571 return IsDarkBackground(aFrame
);
575 bool nsNativeTheme::IsDarkBackground(nsIFrame
* aFrame
) {
577 nsCSSRendering::FindEffectiveBackgroundColor(
578 aFrame
, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
580 return LookAndFeel::IsDarkColor(color
);
584 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance
) {
585 switch (aAppearance
) {
586 case StyleAppearance::ScrollbarVertical
:
587 case StyleAppearance::ScrollbarHorizontal
:
588 case StyleAppearance::ScrollbarbuttonUp
:
589 case StyleAppearance::ScrollbarbuttonDown
:
590 case StyleAppearance::ScrollbarbuttonLeft
:
591 case StyleAppearance::ScrollbarbuttonRight
:
592 case StyleAppearance::ScrollbarthumbVertical
:
593 case StyleAppearance::ScrollbarthumbHorizontal
:
594 case StyleAppearance::ScrollbartrackHorizontal
:
595 case StyleAppearance::ScrollbartrackVertical
:
596 case StyleAppearance::Scrollcorner
: