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 "nsLayoutUtils.h"
12 #include "nsNumberControlFrame.h"
13 #include "nsPresContext.h"
15 #include "nsNameSpaceManager.h"
16 #include "nsStyleConsts.h"
17 #include "nsPIDOMWindow.h"
18 #include "nsProgressFrame.h"
19 #include "nsMeterFrame.h"
20 #include "nsRangeFrame.h"
21 #include "nsCSSRendering.h"
22 #include "ImageContainer.h"
23 #include "mozilla/ComputedStyle.h"
24 #include "mozilla/dom/Element.h"
25 #include "mozilla/dom/HTMLBodyElement.h"
26 #include "mozilla/dom/HTMLInputElement.h"
27 #include "mozilla/dom/HTMLProgressElement.h"
28 #include "mozilla/PresShell.h"
29 #include "mozilla/ScrollContainerFrame.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::Checkbox
||
55 aAppearance
== StyleAppearance::Radio
||
56 aAppearance
== StyleAppearance::ToolbarbuttonDropdown
||
57 aAppearance
== StyleAppearance::ButtonArrowPrevious
||
58 aAppearance
== StyleAppearance::ButtonArrowNext
||
59 aAppearance
== StyleAppearance::ButtonArrowUp
||
61 aAppearance
== StyleAppearance::MozWindowButtonClose
||
62 aAppearance
== StyleAppearance::MozWindowButtonMinimize
||
63 aAppearance
== StyleAppearance::MozWindowButtonRestore
||
64 aAppearance
== StyleAppearance::MozWindowButtonMaximize
||
66 aAppearance
== StyleAppearance::ButtonArrowDown
) {
67 aFrame
= aFrame
->GetParent();
68 frameContent
= aFrame
->GetContent();
70 MOZ_ASSERT(frameContent
&& frameContent
->IsElement());
73 ElementState flags
= frameContent
->AsElement()->StyleState();
74 nsNumberControlFrame
* numberControlFrame
=
75 nsNumberControlFrame::GetNumberControlFrameForSpinButton(aFrame
);
76 if (numberControlFrame
&&
77 numberControlFrame
->GetContent()->AsElement()->StyleState().HasState(
78 ElementState::DISABLED
)) {
79 flags
|= ElementState::DISABLED
;
86 if (CheckBooleanAttr(aFrame
, nsGkAtoms::disabled
)) {
87 flags
|= ElementState::DISABLED
;
90 switch (aAppearance
) {
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::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::Toolbarbutton
:
115 if (CheckBooleanAttr(aFrame
, nsGkAtoms::open
)) {
116 flags
|= ElementState::HOVER
| ElementState::ACTIVE
;
119 case StyleAppearance::MenulistButton
:
120 case StyleAppearance::Menulist
:
121 case StyleAppearance::NumberInput
:
122 case StyleAppearance::Textfield
:
123 case StyleAppearance::PasswordInput
:
124 case StyleAppearance::Searchfield
:
125 case StyleAppearance::Textarea
: {
126 if (CheckBooleanAttr(aFrame
, nsGkAtoms::focused
)) {
127 flags
|= ElementState::FOCUS
| ElementState::FOCUSRING
;
139 bool nsNativeTheme::CheckBooleanAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
) {
140 if (!aFrame
) return false;
142 nsIContent
* content
= aFrame
->GetContent();
143 if (!content
|| !content
->IsElement()) return false;
145 if (content
->IsHTMLElement()) return content
->AsElement()->HasAttr(aAtom
);
147 // For XML/XUL elements, an attribute must be equal to the literal
148 // string "true" to be counted as true. An empty string should _not_
149 // be counted as true.
150 return content
->AsElement()->AttrValueIs(kNameSpaceID_None
, aAtom
, u
"true"_ns
,
155 int32_t nsNativeTheme::CheckIntAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
,
156 int32_t defaultValue
) {
157 if (!aFrame
) return defaultValue
;
159 nsIContent
* content
= aFrame
->GetContent();
160 if (!content
|| !content
->IsElement()) return defaultValue
;
163 content
->AsElement()->GetAttr(aAtom
, attr
);
165 int32_t value
= attr
.ToInteger(&err
);
166 if (attr
.IsEmpty() || NS_FAILED(err
)) return defaultValue
;
172 double nsNativeTheme::GetProgressValue(nsIFrame
* aFrame
) {
173 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
177 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Value();
181 double nsNativeTheme::GetProgressMaxValue(nsIFrame
* aFrame
) {
182 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
186 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Max();
189 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame
* aFrame
) {
190 if (!aFrame
) return false;
192 nsIContent
* content
= aFrame
->GetContent();
193 return content
->IsXULElement(nsGkAtoms::button
) &&
194 content
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
195 u
"menu"_ns
, eCaseMatters
);
198 bool nsNativeTheme::IsPressedButton(nsIFrame
* aFrame
) {
199 ElementState state
= GetContentState(aFrame
, StyleAppearance::Toolbarbutton
);
200 if (state
.HasState(ElementState::DISABLED
)) {
204 return IsOpenButton(aFrame
) ||
205 state
.HasAllStates(ElementState::ACTIVE
| ElementState::HOVER
);
208 bool nsNativeTheme::IsWidgetStyled(nsPresContext
* aPresContext
,
210 StyleAppearance aAppearance
) {
211 // Check for specific widgets to see if HTML has overridden the style.
217 * Progress bar appearance should be the same for the bar and the container
218 * frame. nsProgressFrame owns the logic and will tell us what we should do.
220 if (aAppearance
== StyleAppearance::Progresschunk
||
221 aAppearance
== StyleAppearance::ProgressBar
) {
222 nsProgressFrame
* progressFrame
= do_QueryFrame(
223 aAppearance
== StyleAppearance::Progresschunk
? aFrame
->GetParent()
226 return !progressFrame
->ShouldUseNativeStyle();
231 * Meter bar appearance should be the same for the bar and the container
232 * frame. nsMeterFrame owns the logic and will tell us what we should do.
234 if (aAppearance
== StyleAppearance::Meterchunk
||
235 aAppearance
== StyleAppearance::Meter
) {
236 nsMeterFrame
* meterFrame
= do_QueryFrame(
237 aAppearance
== StyleAppearance::Meterchunk
? aFrame
->GetParent()
240 return !meterFrame
->ShouldUseNativeStyle();
245 * An nsRangeFrame and its children are treated atomically when it
246 * comes to native theming (either all parts, or no parts, are themed).
247 * nsRangeFrame owns the logic and will tell us what we should do.
249 if (aAppearance
== StyleAppearance::Range
||
250 aAppearance
== StyleAppearance::RangeThumb
) {
251 nsRangeFrame
* rangeFrame
= do_QueryFrame(
252 aAppearance
== StyleAppearance::RangeThumb
? aFrame
->GetParent()
255 return !rangeFrame
->ShouldUseNativeStyle();
259 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
261 aFrame
->GetContent()->IsHTMLElement() &&
262 aFrame
->Style()->HasAuthorSpecifiedBorderOrBackground();
266 bool nsNativeTheme::IsFrameRTL(nsIFrame
* aFrame
) {
270 return aFrame
->GetWritingMode().IsPhysicalRTL();
274 bool nsNativeTheme::IsHTMLContent(nsIFrame
* aFrame
) {
278 nsIContent
* content
= aFrame
->GetContent();
279 return content
&& content
->IsHTMLElement();
283 nsNativeTheme::TreeSortDirection
nsNativeTheme::GetTreeSortDirection(
285 if (!aFrame
|| !aFrame
->GetContent()) return eTreeSortDirection_Natural
;
287 static Element::AttrValuesArray strings
[] = {nsGkAtoms::descending
,
288 nsGkAtoms::ascending
, nullptr};
290 nsIContent
* content
= aFrame
->GetContent();
291 if (content
->IsElement()) {
292 switch (content
->AsElement()->FindAttrValueIn(
293 kNameSpaceID_None
, nsGkAtoms::sortDirection
, strings
, eCaseMatters
)) {
295 return eTreeSortDirection_Descending
;
297 return eTreeSortDirection_Ascending
;
301 return eTreeSortDirection_Natural
;
304 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame
* aFrame
) {
309 // A tree column picker button is always the last header cell.
310 if (aFrame
->GetContent()->IsXULElement(nsGkAtoms::button
)) {
314 // Find the parent tree.
315 nsIContent
* parent
= aFrame
->GetContent()->GetParent();
316 while (parent
&& !parent
->IsXULElement(nsGkAtoms::tree
)) {
317 parent
= parent
->GetParent();
320 // If the column picker is visible, this can't be the last column.
321 if (parent
&& !parent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
322 nsGkAtoms::hidecolumnpicker
,
323 u
"true"_ns
, eCaseMatters
))
326 while ((aFrame
= aFrame
->GetNextSibling())) {
327 if (aFrame
->GetRect().Width() > 0) return false;
333 bool nsNativeTheme::IsBottomTab(nsIFrame
* aFrame
) {
334 if (!aFrame
) return false;
336 nsAutoString classStr
;
337 if (aFrame
->GetContent()->IsElement()) {
338 aFrame
->GetContent()->AsElement()->GetAttr(nsGkAtoms::_class
, classStr
);
340 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
341 return !classStr
.IsEmpty() && classStr
.Find(u
"tab-bottom") != kNotFound
;
344 bool nsNativeTheme::IsFirstTab(nsIFrame
* aFrame
) {
345 if (!aFrame
) return false;
347 for (nsIFrame
* first
: aFrame
->GetParent()->PrincipalChildList()) {
348 if (first
->GetRect().Width() > 0 &&
349 first
->GetContent()->IsXULElement(nsGkAtoms::tab
))
350 return (first
== aFrame
);
355 bool nsNativeTheme::IsHorizontal(nsIFrame
* aFrame
) {
356 if (!aFrame
) return false;
358 if (!aFrame
->GetContent()->IsElement()) return true;
360 return !aFrame
->GetContent()->AsElement()->AttrValueIs(
361 kNameSpaceID_None
, nsGkAtoms::orient
, nsGkAtoms::vertical
, eCaseMatters
);
364 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame
* aFrame
, int32_t aOffset
) {
365 if (!aFrame
) return false;
367 if (aOffset
== 0) return IsSelectedTab(aFrame
);
369 int32_t thisTabIndex
= -1, selectedTabIndex
= -1;
371 nsIFrame
* currentTab
= aFrame
->GetParent()->PrincipalChildList().FirstChild();
372 for (int32_t i
= 0; currentTab
; currentTab
= currentTab
->GetNextSibling()) {
373 if (currentTab
->GetRect().Width() == 0) continue;
374 if (aFrame
== currentTab
) thisTabIndex
= i
;
375 if (IsSelectedTab(currentTab
)) selectedTabIndex
= i
;
379 if (thisTabIndex
== -1 || selectedTabIndex
== -1) return false;
381 return (thisTabIndex
- selectedTabIndex
== aOffset
);
384 bool nsNativeTheme::IsVerticalProgress(nsIFrame
* aFrame
) {
388 return IsVerticalMeter(aFrame
);
391 bool nsNativeTheme::IsVerticalMeter(nsIFrame
* aFrame
) {
392 MOZ_ASSERT(aFrame
, "You have to pass a non-null aFrame");
393 switch (aFrame
->StyleDisplay()->mOrient
) {
394 case StyleOrient::Horizontal
:
396 case StyleOrient::Vertical
:
398 case StyleOrient::Inline
:
399 return aFrame
->GetWritingMode().IsVertical();
400 case StyleOrient::Block
:
401 return !aFrame
->GetWritingMode().IsVertical();
403 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
408 bool nsNativeTheme::IsSubmenu(nsIFrame
* aFrame
, bool* aLeftOfParent
) {
409 if (!aFrame
) return false;
411 nsIContent
* parentContent
= aFrame
->GetContent()->GetParent();
412 if (!parentContent
|| !parentContent
->IsXULElement(nsGkAtoms::menu
))
415 nsIFrame
* parent
= aFrame
;
416 while ((parent
= parent
->GetParent())) {
417 if (parent
->GetContent() == parentContent
) {
419 LayoutDeviceIntRect selfBounds
, parentBounds
;
420 selfBounds
= aFrame
->GetNearestWidget()->GetScreenBounds();
421 parentBounds
= parent
->GetNearestWidget()->GetScreenBounds();
422 *aLeftOfParent
= selfBounds
.X() < parentBounds
.X();
431 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent
* aContent
,
432 uint32_t aMinimumFrameRate
) {
433 NS_ASSERTION(aContent
, "Null pointer!");
434 NS_ASSERTION(aMinimumFrameRate
, "aMinimumFrameRate must be non-zero!");
435 NS_ASSERTION(aMinimumFrameRate
<= 1000,
436 "aMinimumFrameRate must be less than 1000!");
438 uint32_t timeout
= 1000 / aMinimumFrameRate
;
439 timeout
= std::min(mAnimatedContentTimeout
, timeout
);
441 if (!mAnimatedContentTimer
) {
442 mAnimatedContentTimer
= NS_NewTimer();
443 NS_ENSURE_TRUE(mAnimatedContentTimer
, false);
446 if (mAnimatedContentList
.IsEmpty() || timeout
!= mAnimatedContentTimeout
) {
448 if (!mAnimatedContentList
.IsEmpty()) {
449 rv
= mAnimatedContentTimer
->Cancel();
450 NS_ENSURE_SUCCESS(rv
, false);
453 if (XRE_IsContentProcess() && NS_IsMainThread()) {
454 mAnimatedContentTimer
->SetTarget(GetMainThreadSerialEventTarget());
456 rv
= mAnimatedContentTimer
->InitWithCallback(this, timeout
,
457 nsITimer::TYPE_ONE_SHOT
);
458 NS_ENSURE_SUCCESS(rv
, false);
460 mAnimatedContentTimeout
= timeout
;
463 // XXX(Bug 1631371) Check if this should use a fallible operation as it
464 // pretended earlier.
465 mAnimatedContentList
.AppendElement(aContent
);
471 nsNativeTheme::Notify(nsITimer
* aTimer
) {
472 NS_ASSERTION(aTimer
== mAnimatedContentTimer
, "Wrong timer!");
474 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
475 // QueueAnimatedContentForRefresh.
477 uint32_t count
= mAnimatedContentList
.Length();
478 for (uint32_t index
= 0; index
< count
; index
++) {
479 nsIFrame
* frame
= mAnimatedContentList
[index
]->GetPrimaryFrame();
481 frame
->InvalidateFrame();
485 mAnimatedContentList
.Clear();
486 mAnimatedContentTimeout
= UINT32_MAX
;
491 nsNativeTheme::GetName(nsACString
& aName
) {
492 aName
.AssignLiteral("nsNativeTheme");
496 nsIFrame
* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
497 nsIFrame
* aFrame
, bool aNextSibling
) {
498 if (!aFrame
) return nullptr;
500 // Find the next visible sibling.
501 nsIFrame
* sibling
= aFrame
;
504 aNextSibling
? sibling
->GetNextSibling() : sibling
->GetPrevSibling();
505 } while (sibling
&& sibling
->GetRect().Width() == 0);
507 // Check same appearance and adjacency.
509 sibling
->StyleDisplay()->EffectiveAppearance() !=
510 aFrame
->StyleDisplay()->EffectiveAppearance() ||
511 (sibling
->GetRect().XMost() != aFrame
->GetRect().X() &&
512 aFrame
->GetRect().XMost() != sibling
->GetRect().X()))
517 bool nsNativeTheme::IsRangeHorizontal(nsIFrame
* aFrame
) {
518 nsIFrame
* rangeFrame
= aFrame
;
519 if (!rangeFrame
->IsRangeFrame()) {
520 // If the thumb's frame is passed in, get its range parent:
521 rangeFrame
= aFrame
->GetParent();
523 if (rangeFrame
->IsRangeFrame()) {
524 return static_cast<nsRangeFrame
*>(rangeFrame
)->IsHorizontal();
526 // Not actually a range frame - just use the ratio of the frame's size to
528 return aFrame
->GetSize().width
>= aFrame
->GetSize().height
;
532 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame
* aFrame
) {
533 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
536 nsIFrame
* frame
= aFrame
;
537 ScrollContainerFrame
* scrollContainerFrame
= nullptr;
538 while (!scrollContainerFrame
&& frame
) {
539 scrollContainerFrame
= frame
->GetScrollTargetFrame();
540 frame
= frame
->GetParent();
542 if (scrollContainerFrame
) {
543 aFrame
= scrollContainerFrame
->GetScrolledFrame();
545 // Leave aFrame untouched.
549 return IsDarkBackground(aFrame
);
553 bool nsNativeTheme::IsDarkBackground(nsIFrame
* aFrame
) {
555 nsCSSRendering::FindEffectiveBackgroundColor(
556 aFrame
, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
558 return LookAndFeel::IsDarkColor(color
);
562 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance
) {
563 switch (aAppearance
) {
564 case StyleAppearance::ScrollbarVertical
:
565 case StyleAppearance::ScrollbarHorizontal
:
566 case StyleAppearance::ScrollbarbuttonUp
:
567 case StyleAppearance::ScrollbarbuttonDown
:
568 case StyleAppearance::ScrollbarbuttonLeft
:
569 case StyleAppearance::ScrollbarbuttonRight
:
570 case StyleAppearance::ScrollbarthumbVertical
:
571 case StyleAppearance::ScrollbarthumbHorizontal
:
572 case StyleAppearance::Scrollcorner
:
580 bool nsNativeTheme::IsWidgetAlwaysNonNative(nsIFrame
* aFrame
,
581 StyleAppearance aAppearance
) {
582 return IsWidgetScrollbarPart(aAppearance
) ||
583 aAppearance
== StyleAppearance::FocusOutline
||
584 (aFrame
&& aFrame
->StyleUI()->mMozTheme
== StyleMozTheme::NonNative
);