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 CheckBooleanAttr(aFrame
, nsGkAtoms::checked
)) {
102 flags
|= ElementState::CHECKED
;
106 case StyleAppearance::CheckboxLabel
:
107 case StyleAppearance::Checkbox
: {
108 if (CheckBooleanAttr(aFrame
, nsGkAtoms::checked
)) {
109 flags
|= ElementState::CHECKED
;
110 } else if (CheckBooleanAttr(aFrame
, nsGkAtoms::indeterminate
)) {
111 flags
|= ElementState::INDETERMINATE
;
115 case StyleAppearance::MenulistButton
:
116 case StyleAppearance::Menulist
:
117 case StyleAppearance::NumberInput
:
118 case StyleAppearance::Textfield
:
119 case StyleAppearance::Searchfield
:
120 case StyleAppearance::Textarea
: {
121 if (CheckBooleanAttr(aFrame
, nsGkAtoms::focused
)) {
122 flags
|= ElementState::FOCUS
| ElementState::FOCUSRING
;
134 bool nsNativeTheme::CheckBooleanAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
) {
135 if (!aFrame
) return false;
137 nsIContent
* content
= aFrame
->GetContent();
138 if (!content
|| !content
->IsElement()) return false;
140 if (content
->IsHTMLElement())
141 return content
->AsElement()->HasAttr(kNameSpaceID_None
, aAtom
);
143 // For XML/XUL elements, an attribute must be equal to the literal
144 // string "true" to be counted as true. An empty string should _not_
145 // be counted as true.
146 return content
->AsElement()->AttrValueIs(kNameSpaceID_None
, aAtom
, u
"true"_ns
,
151 int32_t nsNativeTheme::CheckIntAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
,
152 int32_t defaultValue
) {
153 if (!aFrame
) return defaultValue
;
155 nsIContent
* content
= aFrame
->GetContent();
156 if (!content
|| !content
->IsElement()) return defaultValue
;
159 content
->AsElement()->GetAttr(kNameSpaceID_None
, aAtom
, attr
);
161 int32_t value
= attr
.ToInteger(&err
);
162 if (attr
.IsEmpty() || NS_FAILED(err
)) return defaultValue
;
168 double nsNativeTheme::GetProgressValue(nsIFrame
* aFrame
) {
169 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
173 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Value();
177 double nsNativeTheme::GetProgressMaxValue(nsIFrame
* aFrame
) {
178 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
182 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Max();
185 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame
* aFrame
) {
186 if (!aFrame
) return false;
188 nsIContent
* content
= aFrame
->GetContent();
189 return content
->IsXULElement(nsGkAtoms::button
) &&
190 content
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
191 u
"menu"_ns
, eCaseMatters
);
194 bool nsNativeTheme::IsPressedButton(nsIFrame
* aFrame
) {
195 ElementState state
= GetContentState(aFrame
, StyleAppearance::Toolbarbutton
);
196 if (state
.HasState(ElementState::DISABLED
)) {
200 return IsOpenButton(aFrame
) ||
201 state
.HasAllStates(ElementState::ACTIVE
| ElementState::HOVER
);
204 bool nsNativeTheme::IsWidgetStyled(nsPresContext
* aPresContext
,
206 StyleAppearance aAppearance
) {
207 // Check for specific widgets to see if HTML has overridden the style.
213 * Progress bar appearance should be the same for the bar and the container
214 * frame. nsProgressFrame owns the logic and will tell us what we should do.
216 if (aAppearance
== StyleAppearance::Progresschunk
||
217 aAppearance
== StyleAppearance::ProgressBar
) {
218 nsProgressFrame
* progressFrame
= do_QueryFrame(
219 aAppearance
== StyleAppearance::Progresschunk
? aFrame
->GetParent()
222 return !progressFrame
->ShouldUseNativeStyle();
227 * Meter bar appearance should be the same for the bar and the container
228 * frame. nsMeterFrame owns the logic and will tell us what we should do.
230 if (aAppearance
== StyleAppearance::Meterchunk
||
231 aAppearance
== StyleAppearance::Meter
) {
232 nsMeterFrame
* meterFrame
= do_QueryFrame(
233 aAppearance
== StyleAppearance::Meterchunk
? aFrame
->GetParent()
236 return !meterFrame
->ShouldUseNativeStyle();
241 * An nsRangeFrame and its children are treated atomically when it
242 * comes to native theming (either all parts, or no parts, are themed).
243 * nsRangeFrame owns the logic and will tell us what we should do.
245 if (aAppearance
== StyleAppearance::Range
||
246 aAppearance
== StyleAppearance::RangeThumb
) {
247 nsRangeFrame
* rangeFrame
= do_QueryFrame(
248 aAppearance
== StyleAppearance::RangeThumb
? aFrame
->GetParent()
251 return !rangeFrame
->ShouldUseNativeStyle();
255 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
257 aFrame
->GetContent()->IsHTMLElement() &&
258 aFrame
->Style()->HasAuthorSpecifiedBorderOrBackground();
262 bool nsNativeTheme::IsFrameRTL(nsIFrame
* aFrame
) {
266 return aFrame
->GetWritingMode().IsPhysicalRTL();
270 bool nsNativeTheme::IsHTMLContent(nsIFrame
* aFrame
) {
274 nsIContent
* content
= aFrame
->GetContent();
275 return content
&& content
->IsHTMLElement();
279 nsNativeTheme::TreeSortDirection
nsNativeTheme::GetTreeSortDirection(
281 if (!aFrame
|| !aFrame
->GetContent()) return eTreeSortDirection_Natural
;
283 static Element::AttrValuesArray strings
[] = {nsGkAtoms::descending
,
284 nsGkAtoms::ascending
, nullptr};
286 nsIContent
* content
= aFrame
->GetContent();
287 if (content
->IsElement()) {
288 switch (content
->AsElement()->FindAttrValueIn(
289 kNameSpaceID_None
, nsGkAtoms::sortDirection
, strings
, eCaseMatters
)) {
291 return eTreeSortDirection_Descending
;
293 return eTreeSortDirection_Ascending
;
297 return eTreeSortDirection_Natural
;
300 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame
* aFrame
) {
305 // A tree column picker button is always the last header cell.
306 if (aFrame
->GetContent()->IsXULElement(nsGkAtoms::button
)) {
310 // Find the parent tree.
311 nsIContent
* parent
= aFrame
->GetContent()->GetParent();
312 while (parent
&& !parent
->IsXULElement(nsGkAtoms::tree
)) {
313 parent
= parent
->GetParent();
316 // If the column picker is visible, this can't be the last column.
317 if (parent
&& !parent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
318 nsGkAtoms::hidecolumnpicker
,
319 u
"true"_ns
, eCaseMatters
))
322 while ((aFrame
= aFrame
->GetNextSibling())) {
323 if (aFrame
->GetRect().Width() > 0) return false;
329 bool nsNativeTheme::IsBottomTab(nsIFrame
* aFrame
) {
330 if (!aFrame
) return false;
332 nsAutoString classStr
;
333 if (aFrame
->GetContent()->IsElement()) {
334 aFrame
->GetContent()->AsElement()->GetAttr(kNameSpaceID_None
,
335 nsGkAtoms::_class
, classStr
);
337 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
338 return !classStr
.IsEmpty() && classStr
.Find(u
"tab-bottom") != kNotFound
;
341 bool nsNativeTheme::IsFirstTab(nsIFrame
* aFrame
) {
342 if (!aFrame
) return false;
344 for (nsIFrame
* first
: aFrame
->GetParent()->PrincipalChildList()) {
345 if (first
->GetRect().Width() > 0 &&
346 first
->GetContent()->IsXULElement(nsGkAtoms::tab
))
347 return (first
== aFrame
);
352 bool nsNativeTheme::IsHorizontal(nsIFrame
* aFrame
) {
353 if (!aFrame
) return false;
355 if (!aFrame
->GetContent()->IsElement()) return true;
357 return !aFrame
->GetContent()->AsElement()->AttrValueIs(
358 kNameSpaceID_None
, nsGkAtoms::orient
, nsGkAtoms::vertical
, eCaseMatters
);
361 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame
* aFrame
, int32_t aOffset
) {
362 if (!aFrame
) return false;
364 if (aOffset
== 0) return IsSelectedTab(aFrame
);
366 int32_t thisTabIndex
= -1, selectedTabIndex
= -1;
368 nsIFrame
* currentTab
= aFrame
->GetParent()->PrincipalChildList().FirstChild();
369 for (int32_t i
= 0; currentTab
; currentTab
= currentTab
->GetNextSibling()) {
370 if (currentTab
->GetRect().Width() == 0) continue;
371 if (aFrame
== currentTab
) thisTabIndex
= i
;
372 if (IsSelectedTab(currentTab
)) selectedTabIndex
= i
;
376 if (thisTabIndex
== -1 || selectedTabIndex
== -1) return false;
378 return (thisTabIndex
- selectedTabIndex
== aOffset
);
381 bool nsNativeTheme::IsVerticalProgress(nsIFrame
* aFrame
) {
385 return IsVerticalMeter(aFrame
);
388 bool nsNativeTheme::IsVerticalMeter(nsIFrame
* aFrame
) {
389 MOZ_ASSERT(aFrame
, "You have to pass a non-null aFrame");
390 switch (aFrame
->StyleDisplay()->mOrient
) {
391 case StyleOrient::Horizontal
:
393 case StyleOrient::Vertical
:
395 case StyleOrient::Inline
:
396 return aFrame
->GetWritingMode().IsVertical();
397 case StyleOrient::Block
:
398 return !aFrame
->GetWritingMode().IsVertical();
400 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
405 bool nsNativeTheme::IsSubmenu(nsIFrame
* aFrame
, bool* aLeftOfParent
) {
406 if (!aFrame
) return false;
408 nsIContent
* parentContent
= aFrame
->GetContent()->GetParent();
409 if (!parentContent
|| !parentContent
->IsXULElement(nsGkAtoms::menu
))
412 nsIFrame
* parent
= aFrame
;
413 while ((parent
= parent
->GetParent())) {
414 if (parent
->GetContent() == parentContent
) {
416 LayoutDeviceIntRect selfBounds
, parentBounds
;
417 selfBounds
= aFrame
->GetNearestWidget()->GetScreenBounds();
418 parentBounds
= parent
->GetNearestWidget()->GetScreenBounds();
419 *aLeftOfParent
= selfBounds
.X() < parentBounds
.X();
428 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent
* aContent
,
429 uint32_t aMinimumFrameRate
) {
430 NS_ASSERTION(aContent
, "Null pointer!");
431 NS_ASSERTION(aMinimumFrameRate
, "aMinimumFrameRate must be non-zero!");
432 NS_ASSERTION(aMinimumFrameRate
<= 1000,
433 "aMinimumFrameRate must be less than 1000!");
435 uint32_t timeout
= 1000 / aMinimumFrameRate
;
436 timeout
= std::min(mAnimatedContentTimeout
, timeout
);
438 if (!mAnimatedContentTimer
) {
439 mAnimatedContentTimer
= NS_NewTimer();
440 NS_ENSURE_TRUE(mAnimatedContentTimer
, false);
443 if (mAnimatedContentList
.IsEmpty() || timeout
!= mAnimatedContentTimeout
) {
445 if (!mAnimatedContentList
.IsEmpty()) {
446 rv
= mAnimatedContentTimer
->Cancel();
447 NS_ENSURE_SUCCESS(rv
, false);
450 if (XRE_IsContentProcess() && NS_IsMainThread()) {
451 mAnimatedContentTimer
->SetTarget(
452 aContent
->OwnerDoc()->EventTargetFor(TaskCategory::Other
));
454 rv
= mAnimatedContentTimer
->InitWithCallback(this, timeout
,
455 nsITimer::TYPE_ONE_SHOT
);
456 NS_ENSURE_SUCCESS(rv
, false);
458 mAnimatedContentTimeout
= timeout
;
461 // XXX(Bug 1631371) Check if this should use a fallible operation as it
462 // pretended earlier.
463 mAnimatedContentList
.AppendElement(aContent
);
469 nsNativeTheme::Notify(nsITimer
* aTimer
) {
470 NS_ASSERTION(aTimer
== mAnimatedContentTimer
, "Wrong timer!");
472 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
473 // QueueAnimatedContentForRefresh.
475 uint32_t count
= mAnimatedContentList
.Length();
476 for (uint32_t index
= 0; index
< count
; index
++) {
477 nsIFrame
* frame
= mAnimatedContentList
[index
]->GetPrimaryFrame();
479 frame
->InvalidateFrame();
483 mAnimatedContentList
.Clear();
484 mAnimatedContentTimeout
= UINT32_MAX
;
489 nsNativeTheme::GetName(nsACString
& aName
) {
490 aName
.AssignLiteral("nsNativeTheme");
494 nsIFrame
* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
495 nsIFrame
* aFrame
, bool aNextSibling
) {
496 if (!aFrame
) return nullptr;
498 // Find the next visible sibling.
499 nsIFrame
* sibling
= aFrame
;
502 aNextSibling
? sibling
->GetNextSibling() : sibling
->GetPrevSibling();
503 } while (sibling
&& sibling
->GetRect().Width() == 0);
505 // Check same appearance and adjacency.
507 sibling
->StyleDisplay()->EffectiveAppearance() !=
508 aFrame
->StyleDisplay()->EffectiveAppearance() ||
509 (sibling
->GetRect().XMost() != aFrame
->GetRect().X() &&
510 aFrame
->GetRect().XMost() != sibling
->GetRect().X()))
515 bool nsNativeTheme::IsRangeHorizontal(nsIFrame
* aFrame
) {
516 nsIFrame
* rangeFrame
= aFrame
;
517 if (!rangeFrame
->IsRangeFrame()) {
518 // If the thumb's frame is passed in, get its range parent:
519 rangeFrame
= aFrame
->GetParent();
521 if (rangeFrame
->IsRangeFrame()) {
522 return static_cast<nsRangeFrame
*>(rangeFrame
)->IsHorizontal();
524 // Not actually a range frame - just use the ratio of the frame's size to
526 return aFrame
->GetSize().width
>= aFrame
->GetSize().height
;
530 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame
* aFrame
) {
531 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
534 nsIFrame
* frame
= aFrame
;
535 nsIScrollableFrame
* scrollFrame
= nullptr;
536 while (!scrollFrame
&& frame
) {
537 scrollFrame
= frame
->GetScrollTargetFrame();
538 frame
= frame
->GetParent();
541 aFrame
= scrollFrame
->GetScrolledFrame();
543 // Leave aFrame untouched.
547 return IsDarkBackground(aFrame
);
551 bool nsNativeTheme::IsDarkBackground(nsIFrame
* aFrame
) {
553 nsCSSRendering::FindEffectiveBackgroundColor(
554 aFrame
, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
556 return LookAndFeel::IsDarkColor(color
);
560 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance
) {
561 switch (aAppearance
) {
562 case StyleAppearance::ScrollbarVertical
:
563 case StyleAppearance::ScrollbarHorizontal
:
564 case StyleAppearance::ScrollbarbuttonUp
:
565 case StyleAppearance::ScrollbarbuttonDown
:
566 case StyleAppearance::ScrollbarbuttonLeft
:
567 case StyleAppearance::ScrollbarbuttonRight
:
568 case StyleAppearance::ScrollbarthumbVertical
:
569 case StyleAppearance::ScrollbarthumbHorizontal
:
570 case StyleAppearance::ScrollbartrackHorizontal
:
571 case StyleAppearance::ScrollbartrackVertical
:
572 case StyleAppearance::Scrollcorner
: