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()) return content
->AsElement()->HasAttr(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(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.
212 * Progress bar appearance should be the same for the bar and the container
213 * frame. nsProgressFrame owns the logic and will tell us what we should do.
215 if (aAppearance
== StyleAppearance::Progresschunk
||
216 aAppearance
== StyleAppearance::ProgressBar
) {
217 nsProgressFrame
* progressFrame
= do_QueryFrame(
218 aAppearance
== StyleAppearance::Progresschunk
? aFrame
->GetParent()
221 return !progressFrame
->ShouldUseNativeStyle();
226 * Meter bar appearance should be the same for the bar and the container
227 * frame. nsMeterFrame owns the logic and will tell us what we should do.
229 if (aAppearance
== StyleAppearance::Meterchunk
||
230 aAppearance
== StyleAppearance::Meter
) {
231 nsMeterFrame
* meterFrame
= do_QueryFrame(
232 aAppearance
== StyleAppearance::Meterchunk
? aFrame
->GetParent()
235 return !meterFrame
->ShouldUseNativeStyle();
240 * An nsRangeFrame and its children are treated atomically when it
241 * comes to native theming (either all parts, or no parts, are themed).
242 * nsRangeFrame owns the logic and will tell us what we should do.
244 if (aAppearance
== StyleAppearance::Range
||
245 aAppearance
== StyleAppearance::RangeThumb
) {
246 nsRangeFrame
* rangeFrame
= do_QueryFrame(
247 aAppearance
== StyleAppearance::RangeThumb
? aFrame
->GetParent()
250 return !rangeFrame
->ShouldUseNativeStyle();
254 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
256 aFrame
->GetContent()->IsHTMLElement() &&
257 aFrame
->Style()->HasAuthorSpecifiedBorderOrBackground();
261 bool nsNativeTheme::IsFrameRTL(nsIFrame
* aFrame
) {
265 return aFrame
->GetWritingMode().IsPhysicalRTL();
269 bool nsNativeTheme::IsHTMLContent(nsIFrame
* aFrame
) {
273 nsIContent
* content
= aFrame
->GetContent();
274 return content
&& content
->IsHTMLElement();
278 nsNativeTheme::TreeSortDirection
nsNativeTheme::GetTreeSortDirection(
280 if (!aFrame
|| !aFrame
->GetContent()) return eTreeSortDirection_Natural
;
282 static Element::AttrValuesArray strings
[] = {nsGkAtoms::descending
,
283 nsGkAtoms::ascending
, nullptr};
285 nsIContent
* content
= aFrame
->GetContent();
286 if (content
->IsElement()) {
287 switch (content
->AsElement()->FindAttrValueIn(
288 kNameSpaceID_None
, nsGkAtoms::sortDirection
, strings
, eCaseMatters
)) {
290 return eTreeSortDirection_Descending
;
292 return eTreeSortDirection_Ascending
;
296 return eTreeSortDirection_Natural
;
299 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame
* aFrame
) {
304 // A tree column picker button is always the last header cell.
305 if (aFrame
->GetContent()->IsXULElement(nsGkAtoms::button
)) {
309 // Find the parent tree.
310 nsIContent
* parent
= aFrame
->GetContent()->GetParent();
311 while (parent
&& !parent
->IsXULElement(nsGkAtoms::tree
)) {
312 parent
= parent
->GetParent();
315 // If the column picker is visible, this can't be the last column.
316 if (parent
&& !parent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
317 nsGkAtoms::hidecolumnpicker
,
318 u
"true"_ns
, eCaseMatters
))
321 while ((aFrame
= aFrame
->GetNextSibling())) {
322 if (aFrame
->GetRect().Width() > 0) return false;
328 bool nsNativeTheme::IsBottomTab(nsIFrame
* aFrame
) {
329 if (!aFrame
) return false;
331 nsAutoString classStr
;
332 if (aFrame
->GetContent()->IsElement()) {
333 aFrame
->GetContent()->AsElement()->GetAttr(nsGkAtoms::_class
, classStr
);
335 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
336 return !classStr
.IsEmpty() && classStr
.Find(u
"tab-bottom") != kNotFound
;
339 bool nsNativeTheme::IsFirstTab(nsIFrame
* aFrame
) {
340 if (!aFrame
) return false;
342 for (nsIFrame
* first
: aFrame
->GetParent()->PrincipalChildList()) {
343 if (first
->GetRect().Width() > 0 &&
344 first
->GetContent()->IsXULElement(nsGkAtoms::tab
))
345 return (first
== aFrame
);
350 bool nsNativeTheme::IsHorizontal(nsIFrame
* aFrame
) {
351 if (!aFrame
) return false;
353 if (!aFrame
->GetContent()->IsElement()) return true;
355 return !aFrame
->GetContent()->AsElement()->AttrValueIs(
356 kNameSpaceID_None
, nsGkAtoms::orient
, nsGkAtoms::vertical
, eCaseMatters
);
359 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame
* aFrame
, int32_t aOffset
) {
360 if (!aFrame
) return false;
362 if (aOffset
== 0) return IsSelectedTab(aFrame
);
364 int32_t thisTabIndex
= -1, selectedTabIndex
= -1;
366 nsIFrame
* currentTab
= aFrame
->GetParent()->PrincipalChildList().FirstChild();
367 for (int32_t i
= 0; currentTab
; currentTab
= currentTab
->GetNextSibling()) {
368 if (currentTab
->GetRect().Width() == 0) continue;
369 if (aFrame
== currentTab
) thisTabIndex
= i
;
370 if (IsSelectedTab(currentTab
)) selectedTabIndex
= i
;
374 if (thisTabIndex
== -1 || selectedTabIndex
== -1) return false;
376 return (thisTabIndex
- selectedTabIndex
== aOffset
);
379 bool nsNativeTheme::IsVerticalProgress(nsIFrame
* aFrame
) {
383 return IsVerticalMeter(aFrame
);
386 bool nsNativeTheme::IsVerticalMeter(nsIFrame
* aFrame
) {
387 MOZ_ASSERT(aFrame
, "You have to pass a non-null aFrame");
388 switch (aFrame
->StyleDisplay()->mOrient
) {
389 case StyleOrient::Horizontal
:
391 case StyleOrient::Vertical
:
393 case StyleOrient::Inline
:
394 return aFrame
->GetWritingMode().IsVertical();
395 case StyleOrient::Block
:
396 return !aFrame
->GetWritingMode().IsVertical();
398 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
403 bool nsNativeTheme::IsSubmenu(nsIFrame
* aFrame
, bool* aLeftOfParent
) {
404 if (!aFrame
) return false;
406 nsIContent
* parentContent
= aFrame
->GetContent()->GetParent();
407 if (!parentContent
|| !parentContent
->IsXULElement(nsGkAtoms::menu
))
410 nsIFrame
* parent
= aFrame
;
411 while ((parent
= parent
->GetParent())) {
412 if (parent
->GetContent() == parentContent
) {
414 LayoutDeviceIntRect selfBounds
, parentBounds
;
415 selfBounds
= aFrame
->GetNearestWidget()->GetScreenBounds();
416 parentBounds
= parent
->GetNearestWidget()->GetScreenBounds();
417 *aLeftOfParent
= selfBounds
.X() < parentBounds
.X();
426 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent
* aContent
,
427 uint32_t aMinimumFrameRate
) {
428 NS_ASSERTION(aContent
, "Null pointer!");
429 NS_ASSERTION(aMinimumFrameRate
, "aMinimumFrameRate must be non-zero!");
430 NS_ASSERTION(aMinimumFrameRate
<= 1000,
431 "aMinimumFrameRate must be less than 1000!");
433 uint32_t timeout
= 1000 / aMinimumFrameRate
;
434 timeout
= std::min(mAnimatedContentTimeout
, timeout
);
436 if (!mAnimatedContentTimer
) {
437 mAnimatedContentTimer
= NS_NewTimer();
438 NS_ENSURE_TRUE(mAnimatedContentTimer
, false);
441 if (mAnimatedContentList
.IsEmpty() || timeout
!= mAnimatedContentTimeout
) {
443 if (!mAnimatedContentList
.IsEmpty()) {
444 rv
= mAnimatedContentTimer
->Cancel();
445 NS_ENSURE_SUCCESS(rv
, false);
448 if (XRE_IsContentProcess() && NS_IsMainThread()) {
449 mAnimatedContentTimer
->SetTarget(
450 aContent
->OwnerDoc()->EventTargetFor(TaskCategory::Other
));
452 rv
= mAnimatedContentTimer
->InitWithCallback(this, timeout
,
453 nsITimer::TYPE_ONE_SHOT
);
454 NS_ENSURE_SUCCESS(rv
, false);
456 mAnimatedContentTimeout
= timeout
;
459 // XXX(Bug 1631371) Check if this should use a fallible operation as it
460 // pretended earlier.
461 mAnimatedContentList
.AppendElement(aContent
);
467 nsNativeTheme::Notify(nsITimer
* aTimer
) {
468 NS_ASSERTION(aTimer
== mAnimatedContentTimer
, "Wrong timer!");
470 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
471 // QueueAnimatedContentForRefresh.
473 uint32_t count
= mAnimatedContentList
.Length();
474 for (uint32_t index
= 0; index
< count
; index
++) {
475 nsIFrame
* frame
= mAnimatedContentList
[index
]->GetPrimaryFrame();
477 frame
->InvalidateFrame();
481 mAnimatedContentList
.Clear();
482 mAnimatedContentTimeout
= UINT32_MAX
;
487 nsNativeTheme::GetName(nsACString
& aName
) {
488 aName
.AssignLiteral("nsNativeTheme");
492 nsIFrame
* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
493 nsIFrame
* aFrame
, bool aNextSibling
) {
494 if (!aFrame
) return nullptr;
496 // Find the next visible sibling.
497 nsIFrame
* sibling
= aFrame
;
500 aNextSibling
? sibling
->GetNextSibling() : sibling
->GetPrevSibling();
501 } while (sibling
&& sibling
->GetRect().Width() == 0);
503 // Check same appearance and adjacency.
505 sibling
->StyleDisplay()->EffectiveAppearance() !=
506 aFrame
->StyleDisplay()->EffectiveAppearance() ||
507 (sibling
->GetRect().XMost() != aFrame
->GetRect().X() &&
508 aFrame
->GetRect().XMost() != sibling
->GetRect().X()))
513 bool nsNativeTheme::IsRangeHorizontal(nsIFrame
* aFrame
) {
514 nsIFrame
* rangeFrame
= aFrame
;
515 if (!rangeFrame
->IsRangeFrame()) {
516 // If the thumb's frame is passed in, get its range parent:
517 rangeFrame
= aFrame
->GetParent();
519 if (rangeFrame
->IsRangeFrame()) {
520 return static_cast<nsRangeFrame
*>(rangeFrame
)->IsHorizontal();
522 // Not actually a range frame - just use the ratio of the frame's size to
524 return aFrame
->GetSize().width
>= aFrame
->GetSize().height
;
528 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame
* aFrame
) {
529 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
532 nsIFrame
* frame
= aFrame
;
533 nsIScrollableFrame
* scrollFrame
= nullptr;
534 while (!scrollFrame
&& frame
) {
535 scrollFrame
= frame
->GetScrollTargetFrame();
536 frame
= frame
->GetParent();
539 aFrame
= scrollFrame
->GetScrolledFrame();
541 // Leave aFrame untouched.
545 return IsDarkBackground(aFrame
);
549 bool nsNativeTheme::IsDarkBackground(nsIFrame
* aFrame
) {
551 nsCSSRendering::FindEffectiveBackgroundColor(
552 aFrame
, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
554 return LookAndFeel::IsDarkColor(color
);
558 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance
) {
559 switch (aAppearance
) {
560 case StyleAppearance::ScrollbarVertical
:
561 case StyleAppearance::ScrollbarHorizontal
:
562 case StyleAppearance::ScrollbarbuttonUp
:
563 case StyleAppearance::ScrollbarbuttonDown
:
564 case StyleAppearance::ScrollbarbuttonLeft
:
565 case StyleAppearance::ScrollbarbuttonRight
:
566 case StyleAppearance::ScrollbarthumbVertical
:
567 case StyleAppearance::ScrollbarthumbHorizontal
:
568 case StyleAppearance::ScrollbartrackHorizontal
:
569 case StyleAppearance::ScrollbartrackVertical
:
570 case StyleAppearance::Scrollcorner
: