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::Checkbox
||
55 aAppearance
== StyleAppearance::Radio
||
56 aAppearance
== StyleAppearance::ToolbarbuttonDropdown
||
57 aAppearance
== StyleAppearance::Treeheadersortarrow
||
58 aAppearance
== StyleAppearance::ButtonArrowPrevious
||
59 aAppearance
== StyleAppearance::ButtonArrowNext
||
60 aAppearance
== StyleAppearance::ButtonArrowUp
||
61 aAppearance
== StyleAppearance::ButtonArrowDown
) {
62 aFrame
= aFrame
->GetParent();
63 frameContent
= aFrame
->GetContent();
65 MOZ_ASSERT(frameContent
&& frameContent
->IsElement());
68 ElementState flags
= frameContent
->AsElement()->StyleState();
69 nsNumberControlFrame
* numberControlFrame
=
70 nsNumberControlFrame::GetNumberControlFrameForSpinButton(aFrame
);
71 if (numberControlFrame
&&
72 numberControlFrame
->GetContent()->AsElement()->StyleState().HasState(
73 ElementState::DISABLED
)) {
74 flags
|= ElementState::DISABLED
;
81 if (CheckBooleanAttr(aFrame
, nsGkAtoms::disabled
)) {
82 flags
|= ElementState::DISABLED
;
85 switch (aAppearance
) {
86 case StyleAppearance::Radio
: {
87 if (CheckBooleanAttr(aFrame
, nsGkAtoms::focused
)) {
88 flags
|= ElementState::FOCUS
;
89 nsPIDOMWindowOuter
* window
=
90 aFrame
->GetContent()->OwnerDoc()->GetWindow();
91 if (window
&& window
->ShouldShowFocusRing()) {
92 flags
|= ElementState::FOCUSRING
;
95 if (CheckBooleanAttr(aFrame
, nsGkAtoms::selected
) ||
96 CheckBooleanAttr(aFrame
, nsGkAtoms::checked
)) {
97 flags
|= ElementState::CHECKED
;
101 case StyleAppearance::Checkbox
: {
102 if (CheckBooleanAttr(aFrame
, nsGkAtoms::checked
)) {
103 flags
|= ElementState::CHECKED
;
104 } else if (CheckBooleanAttr(aFrame
, nsGkAtoms::indeterminate
)) {
105 flags
|= ElementState::INDETERMINATE
;
109 case StyleAppearance::MenulistButton
:
110 case StyleAppearance::Menulist
:
111 case StyleAppearance::NumberInput
:
112 case StyleAppearance::Textfield
:
113 case StyleAppearance::Searchfield
:
114 case StyleAppearance::Textarea
: {
115 if (CheckBooleanAttr(aFrame
, nsGkAtoms::focused
)) {
116 flags
|= ElementState::FOCUS
| ElementState::FOCUSRING
;
128 bool nsNativeTheme::CheckBooleanAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
) {
129 if (!aFrame
) return false;
131 nsIContent
* content
= aFrame
->GetContent();
132 if (!content
|| !content
->IsElement()) return false;
134 if (content
->IsHTMLElement()) return content
->AsElement()->HasAttr(aAtom
);
136 // For XML/XUL elements, an attribute must be equal to the literal
137 // string "true" to be counted as true. An empty string should _not_
138 // be counted as true.
139 return content
->AsElement()->AttrValueIs(kNameSpaceID_None
, aAtom
, u
"true"_ns
,
144 int32_t nsNativeTheme::CheckIntAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
,
145 int32_t defaultValue
) {
146 if (!aFrame
) return defaultValue
;
148 nsIContent
* content
= aFrame
->GetContent();
149 if (!content
|| !content
->IsElement()) return defaultValue
;
152 content
->AsElement()->GetAttr(aAtom
, attr
);
154 int32_t value
= attr
.ToInteger(&err
);
155 if (attr
.IsEmpty() || NS_FAILED(err
)) return defaultValue
;
161 double nsNativeTheme::GetProgressValue(nsIFrame
* aFrame
) {
162 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
166 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Value();
170 double nsNativeTheme::GetProgressMaxValue(nsIFrame
* aFrame
) {
171 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
175 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Max();
178 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame
* aFrame
) {
179 if (!aFrame
) return false;
181 nsIContent
* content
= aFrame
->GetContent();
182 return content
->IsXULElement(nsGkAtoms::button
) &&
183 content
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
184 u
"menu"_ns
, eCaseMatters
);
187 bool nsNativeTheme::IsPressedButton(nsIFrame
* aFrame
) {
188 ElementState state
= GetContentState(aFrame
, StyleAppearance::Toolbarbutton
);
189 if (state
.HasState(ElementState::DISABLED
)) {
193 return IsOpenButton(aFrame
) ||
194 state
.HasAllStates(ElementState::ACTIVE
| ElementState::HOVER
);
197 bool nsNativeTheme::IsWidgetStyled(nsPresContext
* aPresContext
,
199 StyleAppearance aAppearance
) {
200 // Check for specific widgets to see if HTML has overridden the style.
206 * Progress bar appearance should be the same for the bar and the container
207 * frame. nsProgressFrame owns the logic and will tell us what we should do.
209 if (aAppearance
== StyleAppearance::Progresschunk
||
210 aAppearance
== StyleAppearance::ProgressBar
) {
211 nsProgressFrame
* progressFrame
= do_QueryFrame(
212 aAppearance
== StyleAppearance::Progresschunk
? aFrame
->GetParent()
215 return !progressFrame
->ShouldUseNativeStyle();
220 * Meter bar appearance should be the same for the bar and the container
221 * frame. nsMeterFrame owns the logic and will tell us what we should do.
223 if (aAppearance
== StyleAppearance::Meterchunk
||
224 aAppearance
== StyleAppearance::Meter
) {
225 nsMeterFrame
* meterFrame
= do_QueryFrame(
226 aAppearance
== StyleAppearance::Meterchunk
? aFrame
->GetParent()
229 return !meterFrame
->ShouldUseNativeStyle();
234 * An nsRangeFrame and its children are treated atomically when it
235 * comes to native theming (either all parts, or no parts, are themed).
236 * nsRangeFrame owns the logic and will tell us what we should do.
238 if (aAppearance
== StyleAppearance::Range
||
239 aAppearance
== StyleAppearance::RangeThumb
) {
240 nsRangeFrame
* rangeFrame
= do_QueryFrame(
241 aAppearance
== StyleAppearance::RangeThumb
? aFrame
->GetParent()
244 return !rangeFrame
->ShouldUseNativeStyle();
248 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
250 aFrame
->GetContent()->IsHTMLElement() &&
251 aFrame
->Style()->HasAuthorSpecifiedBorderOrBackground();
255 bool nsNativeTheme::IsFrameRTL(nsIFrame
* aFrame
) {
259 return aFrame
->GetWritingMode().IsPhysicalRTL();
263 bool nsNativeTheme::IsHTMLContent(nsIFrame
* aFrame
) {
267 nsIContent
* content
= aFrame
->GetContent();
268 return content
&& content
->IsHTMLElement();
272 nsNativeTheme::TreeSortDirection
nsNativeTheme::GetTreeSortDirection(
274 if (!aFrame
|| !aFrame
->GetContent()) return eTreeSortDirection_Natural
;
276 static Element::AttrValuesArray strings
[] = {nsGkAtoms::descending
,
277 nsGkAtoms::ascending
, nullptr};
279 nsIContent
* content
= aFrame
->GetContent();
280 if (content
->IsElement()) {
281 switch (content
->AsElement()->FindAttrValueIn(
282 kNameSpaceID_None
, nsGkAtoms::sortDirection
, strings
, eCaseMatters
)) {
284 return eTreeSortDirection_Descending
;
286 return eTreeSortDirection_Ascending
;
290 return eTreeSortDirection_Natural
;
293 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame
* aFrame
) {
298 // A tree column picker button is always the last header cell.
299 if (aFrame
->GetContent()->IsXULElement(nsGkAtoms::button
)) {
303 // Find the parent tree.
304 nsIContent
* parent
= aFrame
->GetContent()->GetParent();
305 while (parent
&& !parent
->IsXULElement(nsGkAtoms::tree
)) {
306 parent
= parent
->GetParent();
309 // If the column picker is visible, this can't be the last column.
310 if (parent
&& !parent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
311 nsGkAtoms::hidecolumnpicker
,
312 u
"true"_ns
, eCaseMatters
))
315 while ((aFrame
= aFrame
->GetNextSibling())) {
316 if (aFrame
->GetRect().Width() > 0) return false;
322 bool nsNativeTheme::IsBottomTab(nsIFrame
* aFrame
) {
323 if (!aFrame
) return false;
325 nsAutoString classStr
;
326 if (aFrame
->GetContent()->IsElement()) {
327 aFrame
->GetContent()->AsElement()->GetAttr(nsGkAtoms::_class
, classStr
);
329 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
330 return !classStr
.IsEmpty() && classStr
.Find(u
"tab-bottom") != kNotFound
;
333 bool nsNativeTheme::IsFirstTab(nsIFrame
* aFrame
) {
334 if (!aFrame
) return false;
336 for (nsIFrame
* first
: aFrame
->GetParent()->PrincipalChildList()) {
337 if (first
->GetRect().Width() > 0 &&
338 first
->GetContent()->IsXULElement(nsGkAtoms::tab
))
339 return (first
== aFrame
);
344 bool nsNativeTheme::IsHorizontal(nsIFrame
* aFrame
) {
345 if (!aFrame
) return false;
347 if (!aFrame
->GetContent()->IsElement()) return true;
349 return !aFrame
->GetContent()->AsElement()->AttrValueIs(
350 kNameSpaceID_None
, nsGkAtoms::orient
, nsGkAtoms::vertical
, eCaseMatters
);
353 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame
* aFrame
, int32_t aOffset
) {
354 if (!aFrame
) return false;
356 if (aOffset
== 0) return IsSelectedTab(aFrame
);
358 int32_t thisTabIndex
= -1, selectedTabIndex
= -1;
360 nsIFrame
* currentTab
= aFrame
->GetParent()->PrincipalChildList().FirstChild();
361 for (int32_t i
= 0; currentTab
; currentTab
= currentTab
->GetNextSibling()) {
362 if (currentTab
->GetRect().Width() == 0) continue;
363 if (aFrame
== currentTab
) thisTabIndex
= i
;
364 if (IsSelectedTab(currentTab
)) selectedTabIndex
= i
;
368 if (thisTabIndex
== -1 || selectedTabIndex
== -1) return false;
370 return (thisTabIndex
- selectedTabIndex
== aOffset
);
373 bool nsNativeTheme::IsVerticalProgress(nsIFrame
* aFrame
) {
377 return IsVerticalMeter(aFrame
);
380 bool nsNativeTheme::IsVerticalMeter(nsIFrame
* aFrame
) {
381 MOZ_ASSERT(aFrame
, "You have to pass a non-null aFrame");
382 switch (aFrame
->StyleDisplay()->mOrient
) {
383 case StyleOrient::Horizontal
:
385 case StyleOrient::Vertical
:
387 case StyleOrient::Inline
:
388 return aFrame
->GetWritingMode().IsVertical();
389 case StyleOrient::Block
:
390 return !aFrame
->GetWritingMode().IsVertical();
392 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
397 bool nsNativeTheme::IsSubmenu(nsIFrame
* aFrame
, bool* aLeftOfParent
) {
398 if (!aFrame
) return false;
400 nsIContent
* parentContent
= aFrame
->GetContent()->GetParent();
401 if (!parentContent
|| !parentContent
->IsXULElement(nsGkAtoms::menu
))
404 nsIFrame
* parent
= aFrame
;
405 while ((parent
= parent
->GetParent())) {
406 if (parent
->GetContent() == parentContent
) {
408 LayoutDeviceIntRect selfBounds
, parentBounds
;
409 selfBounds
= aFrame
->GetNearestWidget()->GetScreenBounds();
410 parentBounds
= parent
->GetNearestWidget()->GetScreenBounds();
411 *aLeftOfParent
= selfBounds
.X() < parentBounds
.X();
420 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent
* aContent
,
421 uint32_t aMinimumFrameRate
) {
422 NS_ASSERTION(aContent
, "Null pointer!");
423 NS_ASSERTION(aMinimumFrameRate
, "aMinimumFrameRate must be non-zero!");
424 NS_ASSERTION(aMinimumFrameRate
<= 1000,
425 "aMinimumFrameRate must be less than 1000!");
427 uint32_t timeout
= 1000 / aMinimumFrameRate
;
428 timeout
= std::min(mAnimatedContentTimeout
, timeout
);
430 if (!mAnimatedContentTimer
) {
431 mAnimatedContentTimer
= NS_NewTimer();
432 NS_ENSURE_TRUE(mAnimatedContentTimer
, false);
435 if (mAnimatedContentList
.IsEmpty() || timeout
!= mAnimatedContentTimeout
) {
437 if (!mAnimatedContentList
.IsEmpty()) {
438 rv
= mAnimatedContentTimer
->Cancel();
439 NS_ENSURE_SUCCESS(rv
, false);
442 if (XRE_IsContentProcess() && NS_IsMainThread()) {
443 mAnimatedContentTimer
->SetTarget(GetMainThreadSerialEventTarget());
445 rv
= mAnimatedContentTimer
->InitWithCallback(this, timeout
,
446 nsITimer::TYPE_ONE_SHOT
);
447 NS_ENSURE_SUCCESS(rv
, false);
449 mAnimatedContentTimeout
= timeout
;
452 // XXX(Bug 1631371) Check if this should use a fallible operation as it
453 // pretended earlier.
454 mAnimatedContentList
.AppendElement(aContent
);
460 nsNativeTheme::Notify(nsITimer
* aTimer
) {
461 NS_ASSERTION(aTimer
== mAnimatedContentTimer
, "Wrong timer!");
463 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
464 // QueueAnimatedContentForRefresh.
466 uint32_t count
= mAnimatedContentList
.Length();
467 for (uint32_t index
= 0; index
< count
; index
++) {
468 nsIFrame
* frame
= mAnimatedContentList
[index
]->GetPrimaryFrame();
470 frame
->InvalidateFrame();
474 mAnimatedContentList
.Clear();
475 mAnimatedContentTimeout
= UINT32_MAX
;
480 nsNativeTheme::GetName(nsACString
& aName
) {
481 aName
.AssignLiteral("nsNativeTheme");
485 nsIFrame
* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
486 nsIFrame
* aFrame
, bool aNextSibling
) {
487 if (!aFrame
) return nullptr;
489 // Find the next visible sibling.
490 nsIFrame
* sibling
= aFrame
;
493 aNextSibling
? sibling
->GetNextSibling() : sibling
->GetPrevSibling();
494 } while (sibling
&& sibling
->GetRect().Width() == 0);
496 // Check same appearance and adjacency.
498 sibling
->StyleDisplay()->EffectiveAppearance() !=
499 aFrame
->StyleDisplay()->EffectiveAppearance() ||
500 (sibling
->GetRect().XMost() != aFrame
->GetRect().X() &&
501 aFrame
->GetRect().XMost() != sibling
->GetRect().X()))
506 bool nsNativeTheme::IsRangeHorizontal(nsIFrame
* aFrame
) {
507 nsIFrame
* rangeFrame
= aFrame
;
508 if (!rangeFrame
->IsRangeFrame()) {
509 // If the thumb's frame is passed in, get its range parent:
510 rangeFrame
= aFrame
->GetParent();
512 if (rangeFrame
->IsRangeFrame()) {
513 return static_cast<nsRangeFrame
*>(rangeFrame
)->IsHorizontal();
515 // Not actually a range frame - just use the ratio of the frame's size to
517 return aFrame
->GetSize().width
>= aFrame
->GetSize().height
;
521 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame
* aFrame
) {
522 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
525 nsIFrame
* frame
= aFrame
;
526 nsIScrollableFrame
* scrollFrame
= nullptr;
527 while (!scrollFrame
&& frame
) {
528 scrollFrame
= frame
->GetScrollTargetFrame();
529 frame
= frame
->GetParent();
532 aFrame
= scrollFrame
->GetScrolledFrame();
534 // Leave aFrame untouched.
538 return IsDarkBackground(aFrame
);
542 bool nsNativeTheme::IsDarkBackground(nsIFrame
* aFrame
) {
544 nsCSSRendering::FindEffectiveBackgroundColor(
545 aFrame
, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
547 return LookAndFeel::IsDarkColor(color
);
551 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance
) {
552 switch (aAppearance
) {
553 case StyleAppearance::ScrollbarVertical
:
554 case StyleAppearance::ScrollbarHorizontal
:
555 case StyleAppearance::ScrollbarbuttonUp
:
556 case StyleAppearance::ScrollbarbuttonDown
:
557 case StyleAppearance::ScrollbarbuttonLeft
:
558 case StyleAppearance::ScrollbarbuttonRight
:
559 case StyleAppearance::ScrollbarthumbVertical
:
560 case StyleAppearance::ScrollbarthumbHorizontal
:
561 case StyleAppearance::ScrollbartrackHorizontal
:
562 case StyleAppearance::ScrollbartrackVertical
:
563 case StyleAppearance::Scrollcorner
:
571 bool nsNativeTheme::IsWidgetAlwaysNonNative(nsIFrame
* aFrame
,
572 StyleAppearance aAppearance
) {
573 return IsWidgetScrollbarPart(aAppearance
) ||
574 aAppearance
== StyleAppearance::FocusOutline
||
575 (aFrame
&& aFrame
->StyleUI()->mMozTheme
== StyleMozTheme::NonNative
);