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::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::Searchfield
:
124 case StyleAppearance::Textarea
: {
125 if (CheckBooleanAttr(aFrame
, nsGkAtoms::focused
)) {
126 flags
|= ElementState::FOCUS
| ElementState::FOCUSRING
;
138 bool nsNativeTheme::CheckBooleanAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
) {
139 if (!aFrame
) return false;
141 nsIContent
* content
= aFrame
->GetContent();
142 if (!content
|| !content
->IsElement()) return false;
144 if (content
->IsHTMLElement()) return content
->AsElement()->HasAttr(aAtom
);
146 // For XML/XUL elements, an attribute must be equal to the literal
147 // string "true" to be counted as true. An empty string should _not_
148 // be counted as true.
149 return content
->AsElement()->AttrValueIs(kNameSpaceID_None
, aAtom
, u
"true"_ns
,
154 int32_t nsNativeTheme::CheckIntAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
,
155 int32_t defaultValue
) {
156 if (!aFrame
) return defaultValue
;
158 nsIContent
* content
= aFrame
->GetContent();
159 if (!content
|| !content
->IsElement()) return defaultValue
;
162 content
->AsElement()->GetAttr(aAtom
, attr
);
164 int32_t value
= attr
.ToInteger(&err
);
165 if (attr
.IsEmpty() || NS_FAILED(err
)) return defaultValue
;
171 double nsNativeTheme::GetProgressValue(nsIFrame
* aFrame
) {
172 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
176 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Value();
180 double nsNativeTheme::GetProgressMaxValue(nsIFrame
* aFrame
) {
181 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
185 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Max();
188 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame
* aFrame
) {
189 if (!aFrame
) return false;
191 nsIContent
* content
= aFrame
->GetContent();
192 return content
->IsXULElement(nsGkAtoms::button
) &&
193 content
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
194 u
"menu"_ns
, eCaseMatters
);
197 bool nsNativeTheme::IsPressedButton(nsIFrame
* aFrame
) {
198 ElementState state
= GetContentState(aFrame
, StyleAppearance::Toolbarbutton
);
199 if (state
.HasState(ElementState::DISABLED
)) {
203 return IsOpenButton(aFrame
) ||
204 state
.HasAllStates(ElementState::ACTIVE
| ElementState::HOVER
);
207 bool nsNativeTheme::IsWidgetStyled(nsPresContext
* aPresContext
,
209 StyleAppearance aAppearance
) {
210 // Check for specific widgets to see if HTML has overridden the style.
216 * Progress bar appearance should be the same for the bar and the container
217 * frame. nsProgressFrame owns the logic and will tell us what we should do.
219 if (aAppearance
== StyleAppearance::Progresschunk
||
220 aAppearance
== StyleAppearance::ProgressBar
) {
221 nsProgressFrame
* progressFrame
= do_QueryFrame(
222 aAppearance
== StyleAppearance::Progresschunk
? aFrame
->GetParent()
225 return !progressFrame
->ShouldUseNativeStyle();
230 * Meter bar appearance should be the same for the bar and the container
231 * frame. nsMeterFrame owns the logic and will tell us what we should do.
233 if (aAppearance
== StyleAppearance::Meterchunk
||
234 aAppearance
== StyleAppearance::Meter
) {
235 nsMeterFrame
* meterFrame
= do_QueryFrame(
236 aAppearance
== StyleAppearance::Meterchunk
? aFrame
->GetParent()
239 return !meterFrame
->ShouldUseNativeStyle();
244 * An nsRangeFrame and its children are treated atomically when it
245 * comes to native theming (either all parts, or no parts, are themed).
246 * nsRangeFrame owns the logic and will tell us what we should do.
248 if (aAppearance
== StyleAppearance::Range
||
249 aAppearance
== StyleAppearance::RangeThumb
) {
250 nsRangeFrame
* rangeFrame
= do_QueryFrame(
251 aAppearance
== StyleAppearance::RangeThumb
? aFrame
->GetParent()
254 return !rangeFrame
->ShouldUseNativeStyle();
258 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
260 aFrame
->GetContent()->IsHTMLElement() &&
261 aFrame
->Style()->HasAuthorSpecifiedBorderOrBackground();
265 bool nsNativeTheme::IsFrameRTL(nsIFrame
* aFrame
) {
269 return aFrame
->GetWritingMode().IsPhysicalRTL();
273 bool nsNativeTheme::IsHTMLContent(nsIFrame
* aFrame
) {
277 nsIContent
* content
= aFrame
->GetContent();
278 return content
&& content
->IsHTMLElement();
282 nsNativeTheme::TreeSortDirection
nsNativeTheme::GetTreeSortDirection(
284 if (!aFrame
|| !aFrame
->GetContent()) return eTreeSortDirection_Natural
;
286 static Element::AttrValuesArray strings
[] = {nsGkAtoms::descending
,
287 nsGkAtoms::ascending
, nullptr};
289 nsIContent
* content
= aFrame
->GetContent();
290 if (content
->IsElement()) {
291 switch (content
->AsElement()->FindAttrValueIn(
292 kNameSpaceID_None
, nsGkAtoms::sortDirection
, strings
, eCaseMatters
)) {
294 return eTreeSortDirection_Descending
;
296 return eTreeSortDirection_Ascending
;
300 return eTreeSortDirection_Natural
;
303 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame
* aFrame
) {
308 // A tree column picker button is always the last header cell.
309 if (aFrame
->GetContent()->IsXULElement(nsGkAtoms::button
)) {
313 // Find the parent tree.
314 nsIContent
* parent
= aFrame
->GetContent()->GetParent();
315 while (parent
&& !parent
->IsXULElement(nsGkAtoms::tree
)) {
316 parent
= parent
->GetParent();
319 // If the column picker is visible, this can't be the last column.
320 if (parent
&& !parent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
321 nsGkAtoms::hidecolumnpicker
,
322 u
"true"_ns
, eCaseMatters
))
325 while ((aFrame
= aFrame
->GetNextSibling())) {
326 if (aFrame
->GetRect().Width() > 0) return false;
332 bool nsNativeTheme::IsBottomTab(nsIFrame
* aFrame
) {
333 if (!aFrame
) return false;
335 nsAutoString classStr
;
336 if (aFrame
->GetContent()->IsElement()) {
337 aFrame
->GetContent()->AsElement()->GetAttr(nsGkAtoms::_class
, classStr
);
339 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
340 return !classStr
.IsEmpty() && classStr
.Find(u
"tab-bottom") != kNotFound
;
343 bool nsNativeTheme::IsFirstTab(nsIFrame
* aFrame
) {
344 if (!aFrame
) return false;
346 for (nsIFrame
* first
: aFrame
->GetParent()->PrincipalChildList()) {
347 if (first
->GetRect().Width() > 0 &&
348 first
->GetContent()->IsXULElement(nsGkAtoms::tab
))
349 return (first
== aFrame
);
354 bool nsNativeTheme::IsHorizontal(nsIFrame
* aFrame
) {
355 if (!aFrame
) return false;
357 if (!aFrame
->GetContent()->IsElement()) return true;
359 return !aFrame
->GetContent()->AsElement()->AttrValueIs(
360 kNameSpaceID_None
, nsGkAtoms::orient
, nsGkAtoms::vertical
, eCaseMatters
);
363 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame
* aFrame
, int32_t aOffset
) {
364 if (!aFrame
) return false;
366 if (aOffset
== 0) return IsSelectedTab(aFrame
);
368 int32_t thisTabIndex
= -1, selectedTabIndex
= -1;
370 nsIFrame
* currentTab
= aFrame
->GetParent()->PrincipalChildList().FirstChild();
371 for (int32_t i
= 0; currentTab
; currentTab
= currentTab
->GetNextSibling()) {
372 if (currentTab
->GetRect().Width() == 0) continue;
373 if (aFrame
== currentTab
) thisTabIndex
= i
;
374 if (IsSelectedTab(currentTab
)) selectedTabIndex
= i
;
378 if (thisTabIndex
== -1 || selectedTabIndex
== -1) return false;
380 return (thisTabIndex
- selectedTabIndex
== aOffset
);
383 bool nsNativeTheme::IsVerticalProgress(nsIFrame
* aFrame
) {
387 return IsVerticalMeter(aFrame
);
390 bool nsNativeTheme::IsVerticalMeter(nsIFrame
* aFrame
) {
391 MOZ_ASSERT(aFrame
, "You have to pass a non-null aFrame");
392 switch (aFrame
->StyleDisplay()->mOrient
) {
393 case StyleOrient::Horizontal
:
395 case StyleOrient::Vertical
:
397 case StyleOrient::Inline
:
398 return aFrame
->GetWritingMode().IsVertical();
399 case StyleOrient::Block
:
400 return !aFrame
->GetWritingMode().IsVertical();
402 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
407 bool nsNativeTheme::IsSubmenu(nsIFrame
* aFrame
, bool* aLeftOfParent
) {
408 if (!aFrame
) return false;
410 nsIContent
* parentContent
= aFrame
->GetContent()->GetParent();
411 if (!parentContent
|| !parentContent
->IsXULElement(nsGkAtoms::menu
))
414 nsIFrame
* parent
= aFrame
;
415 while ((parent
= parent
->GetParent())) {
416 if (parent
->GetContent() == parentContent
) {
418 LayoutDeviceIntRect selfBounds
, parentBounds
;
419 selfBounds
= aFrame
->GetNearestWidget()->GetScreenBounds();
420 parentBounds
= parent
->GetNearestWidget()->GetScreenBounds();
421 *aLeftOfParent
= selfBounds
.X() < parentBounds
.X();
430 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent
* aContent
,
431 uint32_t aMinimumFrameRate
) {
432 NS_ASSERTION(aContent
, "Null pointer!");
433 NS_ASSERTION(aMinimumFrameRate
, "aMinimumFrameRate must be non-zero!");
434 NS_ASSERTION(aMinimumFrameRate
<= 1000,
435 "aMinimumFrameRate must be less than 1000!");
437 uint32_t timeout
= 1000 / aMinimumFrameRate
;
438 timeout
= std::min(mAnimatedContentTimeout
, timeout
);
440 if (!mAnimatedContentTimer
) {
441 mAnimatedContentTimer
= NS_NewTimer();
442 NS_ENSURE_TRUE(mAnimatedContentTimer
, false);
445 if (mAnimatedContentList
.IsEmpty() || timeout
!= mAnimatedContentTimeout
) {
447 if (!mAnimatedContentList
.IsEmpty()) {
448 rv
= mAnimatedContentTimer
->Cancel();
449 NS_ENSURE_SUCCESS(rv
, false);
452 if (XRE_IsContentProcess() && NS_IsMainThread()) {
453 mAnimatedContentTimer
->SetTarget(GetMainThreadSerialEventTarget());
455 rv
= mAnimatedContentTimer
->InitWithCallback(this, timeout
,
456 nsITimer::TYPE_ONE_SHOT
);
457 NS_ENSURE_SUCCESS(rv
, false);
459 mAnimatedContentTimeout
= timeout
;
462 // XXX(Bug 1631371) Check if this should use a fallible operation as it
463 // pretended earlier.
464 mAnimatedContentList
.AppendElement(aContent
);
470 nsNativeTheme::Notify(nsITimer
* aTimer
) {
471 NS_ASSERTION(aTimer
== mAnimatedContentTimer
, "Wrong timer!");
473 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
474 // QueueAnimatedContentForRefresh.
476 uint32_t count
= mAnimatedContentList
.Length();
477 for (uint32_t index
= 0; index
< count
; index
++) {
478 nsIFrame
* frame
= mAnimatedContentList
[index
]->GetPrimaryFrame();
480 frame
->InvalidateFrame();
484 mAnimatedContentList
.Clear();
485 mAnimatedContentTimeout
= UINT32_MAX
;
490 nsNativeTheme::GetName(nsACString
& aName
) {
491 aName
.AssignLiteral("nsNativeTheme");
495 nsIFrame
* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
496 nsIFrame
* aFrame
, bool aNextSibling
) {
497 if (!aFrame
) return nullptr;
499 // Find the next visible sibling.
500 nsIFrame
* sibling
= aFrame
;
503 aNextSibling
? sibling
->GetNextSibling() : sibling
->GetPrevSibling();
504 } while (sibling
&& sibling
->GetRect().Width() == 0);
506 // Check same appearance and adjacency.
508 sibling
->StyleDisplay()->EffectiveAppearance() !=
509 aFrame
->StyleDisplay()->EffectiveAppearance() ||
510 (sibling
->GetRect().XMost() != aFrame
->GetRect().X() &&
511 aFrame
->GetRect().XMost() != sibling
->GetRect().X()))
516 bool nsNativeTheme::IsRangeHorizontal(nsIFrame
* aFrame
) {
517 nsIFrame
* rangeFrame
= aFrame
;
518 if (!rangeFrame
->IsRangeFrame()) {
519 // If the thumb's frame is passed in, get its range parent:
520 rangeFrame
= aFrame
->GetParent();
522 if (rangeFrame
->IsRangeFrame()) {
523 return static_cast<nsRangeFrame
*>(rangeFrame
)->IsHorizontal();
525 // Not actually a range frame - just use the ratio of the frame's size to
527 return aFrame
->GetSize().width
>= aFrame
->GetSize().height
;
531 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame
* aFrame
) {
532 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
535 nsIFrame
* frame
= aFrame
;
536 nsIScrollableFrame
* scrollFrame
= nullptr;
537 while (!scrollFrame
&& frame
) {
538 scrollFrame
= frame
->GetScrollTargetFrame();
539 frame
= frame
->GetParent();
542 aFrame
= scrollFrame
->GetScrolledFrame();
544 // Leave aFrame untouched.
548 return IsDarkBackground(aFrame
);
552 bool nsNativeTheme::IsDarkBackground(nsIFrame
* aFrame
) {
554 nsCSSRendering::FindEffectiveBackgroundColor(
555 aFrame
, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
557 return LookAndFeel::IsDarkColor(color
);
561 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance
) {
562 switch (aAppearance
) {
563 case StyleAppearance::ScrollbarVertical
:
564 case StyleAppearance::ScrollbarHorizontal
:
565 case StyleAppearance::ScrollbarbuttonUp
:
566 case StyleAppearance::ScrollbarbuttonDown
:
567 case StyleAppearance::ScrollbarbuttonLeft
:
568 case StyleAppearance::ScrollbarbuttonRight
:
569 case StyleAppearance::ScrollbarthumbVertical
:
570 case StyleAppearance::ScrollbarthumbHorizontal
:
571 case StyleAppearance::ScrollbartrackHorizontal
:
572 case StyleAppearance::ScrollbartrackVertical
:
573 case StyleAppearance::Scrollcorner
:
581 bool nsNativeTheme::IsWidgetAlwaysNonNative(nsIFrame
* aFrame
,
582 StyleAppearance aAppearance
) {
583 return IsWidgetScrollbarPart(aAppearance
) ||
584 aAppearance
== StyleAppearance::FocusOutline
||
585 (aFrame
&& aFrame
->StyleUI()->mMozTheme
== StyleMozTheme::NonNative
);