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(
444 aContent
->OwnerDoc()->EventTargetFor(TaskCategory::Other
));
446 rv
= mAnimatedContentTimer
->InitWithCallback(this, timeout
,
447 nsITimer::TYPE_ONE_SHOT
);
448 NS_ENSURE_SUCCESS(rv
, false);
450 mAnimatedContentTimeout
= timeout
;
453 // XXX(Bug 1631371) Check if this should use a fallible operation as it
454 // pretended earlier.
455 mAnimatedContentList
.AppendElement(aContent
);
461 nsNativeTheme::Notify(nsITimer
* aTimer
) {
462 NS_ASSERTION(aTimer
== mAnimatedContentTimer
, "Wrong timer!");
464 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
465 // QueueAnimatedContentForRefresh.
467 uint32_t count
= mAnimatedContentList
.Length();
468 for (uint32_t index
= 0; index
< count
; index
++) {
469 nsIFrame
* frame
= mAnimatedContentList
[index
]->GetPrimaryFrame();
471 frame
->InvalidateFrame();
475 mAnimatedContentList
.Clear();
476 mAnimatedContentTimeout
= UINT32_MAX
;
481 nsNativeTheme::GetName(nsACString
& aName
) {
482 aName
.AssignLiteral("nsNativeTheme");
486 nsIFrame
* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
487 nsIFrame
* aFrame
, bool aNextSibling
) {
488 if (!aFrame
) return nullptr;
490 // Find the next visible sibling.
491 nsIFrame
* sibling
= aFrame
;
494 aNextSibling
? sibling
->GetNextSibling() : sibling
->GetPrevSibling();
495 } while (sibling
&& sibling
->GetRect().Width() == 0);
497 // Check same appearance and adjacency.
499 sibling
->StyleDisplay()->EffectiveAppearance() !=
500 aFrame
->StyleDisplay()->EffectiveAppearance() ||
501 (sibling
->GetRect().XMost() != aFrame
->GetRect().X() &&
502 aFrame
->GetRect().XMost() != sibling
->GetRect().X()))
507 bool nsNativeTheme::IsRangeHorizontal(nsIFrame
* aFrame
) {
508 nsIFrame
* rangeFrame
= aFrame
;
509 if (!rangeFrame
->IsRangeFrame()) {
510 // If the thumb's frame is passed in, get its range parent:
511 rangeFrame
= aFrame
->GetParent();
513 if (rangeFrame
->IsRangeFrame()) {
514 return static_cast<nsRangeFrame
*>(rangeFrame
)->IsHorizontal();
516 // Not actually a range frame - just use the ratio of the frame's size to
518 return aFrame
->GetSize().width
>= aFrame
->GetSize().height
;
522 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame
* aFrame
) {
523 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
526 nsIFrame
* frame
= aFrame
;
527 nsIScrollableFrame
* scrollFrame
= nullptr;
528 while (!scrollFrame
&& frame
) {
529 scrollFrame
= frame
->GetScrollTargetFrame();
530 frame
= frame
->GetParent();
533 aFrame
= scrollFrame
->GetScrolledFrame();
535 // Leave aFrame untouched.
539 return IsDarkBackground(aFrame
);
543 bool nsNativeTheme::IsDarkBackground(nsIFrame
* aFrame
) {
545 nsCSSRendering::FindEffectiveBackgroundColor(
546 aFrame
, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
548 return LookAndFeel::IsDarkColor(color
);
552 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance
) {
553 switch (aAppearance
) {
554 case StyleAppearance::ScrollbarVertical
:
555 case StyleAppearance::ScrollbarHorizontal
:
556 case StyleAppearance::ScrollbarbuttonUp
:
557 case StyleAppearance::ScrollbarbuttonDown
:
558 case StyleAppearance::ScrollbarbuttonLeft
:
559 case StyleAppearance::ScrollbarbuttonRight
:
560 case StyleAppearance::ScrollbarthumbVertical
:
561 case StyleAppearance::ScrollbarthumbHorizontal
:
562 case StyleAppearance::ScrollbartrackHorizontal
:
563 case StyleAppearance::ScrollbartrackVertical
:
564 case StyleAppearance::Scrollcorner
:
572 bool nsNativeTheme::IsWidgetAlwaysNonNative(nsIFrame
* aFrame
,
573 StyleAppearance aAppearance
) {
574 return IsWidgetScrollbarPart(aAppearance
) ||
575 aAppearance
== StyleAppearance::FocusOutline
||
576 (aFrame
&& aFrame
->StyleUI()->mMozTheme
== StyleMozTheme::NonNative
);