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 "nsMenuFrame.h"
22 #include "nsRangeFrame.h"
23 #include "nsCSSRendering.h"
24 #include "ImageContainer.h"
25 #include "mozilla/ComputedStyle.h"
26 #include "mozilla/dom/Element.h"
27 #include "mozilla/dom/HTMLBodyElement.h"
28 #include "mozilla/dom/HTMLInputElement.h"
29 #include "mozilla/dom/HTMLProgressElement.h"
30 #include "mozilla/PresShell.h"
31 #include "mozilla/StaticPrefs_layout.h"
32 #include "mozilla/dom/DocumentInlines.h"
35 using namespace mozilla
;
36 using namespace mozilla::dom
;
38 nsNativeTheme::nsNativeTheme() : mAnimatedContentTimeout(UINT32_MAX
) {}
40 NS_IMPL_ISUPPORTS(nsNativeTheme
, nsITimerCallback
, nsINamed
)
42 /* static */ ElementState
nsNativeTheme::GetContentState(
43 nsIFrame
* aFrame
, StyleAppearance aAppearance
) {
45 return ElementState();
48 nsIContent
* frameContent
= aFrame
->GetContent();
49 if (!frameContent
|| !frameContent
->IsElement()) {
50 return ElementState();
53 const bool isXULElement
= frameContent
->IsXULElement();
55 if (aAppearance
== StyleAppearance::CheckboxLabel
||
56 aAppearance
== StyleAppearance::RadioLabel
) {
57 aFrame
= aFrame
->GetParent()->GetParent();
58 frameContent
= aFrame
->GetContent();
59 } else if (aAppearance
== StyleAppearance::Checkbox
||
60 aAppearance
== StyleAppearance::Radio
||
61 aAppearance
== StyleAppearance::ToolbarbuttonDropdown
||
62 aAppearance
== StyleAppearance::Treeheadersortarrow
||
63 aAppearance
== StyleAppearance::ButtonArrowPrevious
||
64 aAppearance
== StyleAppearance::ButtonArrowNext
||
65 aAppearance
== StyleAppearance::ButtonArrowUp
||
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::RadioLabel
:
92 case StyleAppearance::Radio
: {
93 if (CheckBooleanAttr(aFrame
, nsGkAtoms::focused
)) {
94 flags
|= ElementState::FOCUS
;
95 nsPIDOMWindowOuter
* window
=
96 aFrame
->GetContent()->OwnerDoc()->GetWindow();
97 if (window
&& window
->ShouldShowFocusRing()) {
98 flags
|= ElementState::FOCUSRING
;
101 if (CheckBooleanAttr(aFrame
, nsGkAtoms::selected
)) {
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())
141 return content
->AsElement()->HasAttr(kNameSpaceID_None
, aAtom
);
143 // For XML/XUL elements, an attribute must be equal to the literal
144 // string "true" to be counted as true. An empty string should _not_
145 // be counted as true.
146 return content
->AsElement()->AttrValueIs(kNameSpaceID_None
, aAtom
, u
"true"_ns
,
151 int32_t nsNativeTheme::CheckIntAttr(nsIFrame
* aFrame
, nsAtom
* aAtom
,
152 int32_t defaultValue
) {
153 if (!aFrame
) return defaultValue
;
155 nsIContent
* content
= aFrame
->GetContent();
156 if (!content
|| !content
->IsElement()) return defaultValue
;
159 content
->AsElement()->GetAttr(kNameSpaceID_None
, aAtom
, attr
);
161 int32_t value
= attr
.ToInteger(&err
);
162 if (attr
.IsEmpty() || NS_FAILED(err
)) return defaultValue
;
168 double nsNativeTheme::GetProgressValue(nsIFrame
* aFrame
) {
169 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
173 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Value();
177 double nsNativeTheme::GetProgressMaxValue(nsIFrame
* aFrame
) {
178 if (!aFrame
|| !aFrame
->GetContent()->IsHTMLElement(nsGkAtoms::progress
)) {
182 return static_cast<HTMLProgressElement
*>(aFrame
->GetContent())->Max();
185 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame
* aFrame
) {
186 if (!aFrame
) return false;
188 nsIContent
* content
= aFrame
->GetContent();
189 return content
->IsXULElement(nsGkAtoms::button
) &&
190 content
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
191 u
"menu"_ns
, eCaseMatters
);
194 bool nsNativeTheme::IsPressedButton(nsIFrame
* aFrame
) {
195 ElementState state
= GetContentState(aFrame
, StyleAppearance::Toolbarbutton
);
196 if (state
.HasState(ElementState::DISABLED
)) {
200 return IsOpenButton(aFrame
) ||
201 state
.HasAllStates(ElementState::ACTIVE
| ElementState::HOVER
);
204 bool nsNativeTheme::IsWidgetStyled(nsPresContext
* aPresContext
,
206 StyleAppearance aAppearance
) {
207 // Check for specific widgets to see if HTML has overridden the style.
212 // Resizers have some special handling, dependent on whether in a scrollable
213 // container or not. If so, use the scrollable container's to determine
214 // whether the style is overriden instead of the resizer. This allows a
215 // non-native transparent resizer to be used instead. Otherwise, we just
216 // fall through and return false.
217 if (aAppearance
== StyleAppearance::Resizer
) {
218 nsIFrame
* parentFrame
= aFrame
->GetParent();
219 if (parentFrame
&& parentFrame
->IsScrollFrame()) {
220 // if the parent is a scrollframe, the resizer should be native themed
221 // only if the scrollable area doesn't override the widget style.
223 // note that the condition below looks a bit suspect but it's the right
224 // one. If there's no valid appearance, then we should return true, it's
225 // effectively the same as if it had overridden the appearance.
226 parentFrame
= parentFrame
->GetParent();
230 auto parentAppearance
=
231 parentFrame
->StyleDisplay()->EffectiveAppearance();
232 return parentAppearance
== StyleAppearance::None
||
233 IsWidgetStyled(aPresContext
, parentFrame
, parentAppearance
);
238 * Progress bar appearance should be the same for the bar and the container
239 * frame. nsProgressFrame owns the logic and will tell us what we should do.
241 if (aAppearance
== StyleAppearance::Progresschunk
||
242 aAppearance
== StyleAppearance::ProgressBar
) {
243 nsProgressFrame
* progressFrame
= do_QueryFrame(
244 aAppearance
== StyleAppearance::Progresschunk
? aFrame
->GetParent()
247 return !progressFrame
->ShouldUseNativeStyle();
252 * Meter bar appearance should be the same for the bar and the container
253 * frame. nsMeterFrame owns the logic and will tell us what we should do.
255 if (aAppearance
== StyleAppearance::Meterchunk
||
256 aAppearance
== StyleAppearance::Meter
) {
257 nsMeterFrame
* meterFrame
= do_QueryFrame(
258 aAppearance
== StyleAppearance::Meterchunk
? aFrame
->GetParent()
261 return !meterFrame
->ShouldUseNativeStyle();
266 * An nsRangeFrame and its children are treated atomically when it
267 * comes to native theming (either all parts, or no parts, are themed).
268 * nsRangeFrame owns the logic and will tell us what we should do.
270 if (aAppearance
== StyleAppearance::Range
||
271 aAppearance
== StyleAppearance::RangeThumb
) {
272 nsRangeFrame
* rangeFrame
= do_QueryFrame(
273 aAppearance
== StyleAppearance::RangeThumb
? aFrame
->GetParent()
276 return !rangeFrame
->ShouldUseNativeStyle();
280 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
282 aFrame
->GetContent()->IsHTMLElement() &&
283 aFrame
->Style()->HasAuthorSpecifiedBorderOrBackground();
287 bool nsNativeTheme::IsFrameRTL(nsIFrame
* aFrame
) {
291 return aFrame
->GetWritingMode().IsPhysicalRTL();
295 bool nsNativeTheme::IsHTMLContent(nsIFrame
* aFrame
) {
299 nsIContent
* content
= aFrame
->GetContent();
300 return content
&& content
->IsHTMLElement();
304 nsNativeTheme::TreeSortDirection
nsNativeTheme::GetTreeSortDirection(
306 if (!aFrame
|| !aFrame
->GetContent()) return eTreeSortDirection_Natural
;
308 static Element::AttrValuesArray strings
[] = {nsGkAtoms::descending
,
309 nsGkAtoms::ascending
, nullptr};
311 nsIContent
* content
= aFrame
->GetContent();
312 if (content
->IsElement()) {
313 switch (content
->AsElement()->FindAttrValueIn(
314 kNameSpaceID_None
, nsGkAtoms::sortDirection
, strings
, eCaseMatters
)) {
316 return eTreeSortDirection_Descending
;
318 return eTreeSortDirection_Ascending
;
322 return eTreeSortDirection_Natural
;
325 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame
* aFrame
) {
326 if (!aFrame
) return false;
328 // A tree column picker is always the last header cell.
329 if (aFrame
->GetContent()->IsXULElement(nsGkAtoms::treecolpicker
)) return true;
331 // Find the parent tree.
332 nsIContent
* parent
= aFrame
->GetContent()->GetParent();
333 while (parent
&& !parent
->IsXULElement(nsGkAtoms::tree
)) {
334 parent
= parent
->GetParent();
337 // If the column picker is visible, this can't be the last column.
338 if (parent
&& !parent
->AsElement()->AttrValueIs(kNameSpaceID_None
,
339 nsGkAtoms::hidecolumnpicker
,
340 u
"true"_ns
, eCaseMatters
))
343 while ((aFrame
= aFrame
->GetNextSibling())) {
344 if (aFrame
->GetRect().Width() > 0) return false;
350 bool nsNativeTheme::IsBottomTab(nsIFrame
* aFrame
) {
351 if (!aFrame
) return false;
353 nsAutoString classStr
;
354 if (aFrame
->GetContent()->IsElement()) {
355 aFrame
->GetContent()->AsElement()->GetAttr(kNameSpaceID_None
,
356 nsGkAtoms::_class
, classStr
);
358 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
359 return !classStr
.IsEmpty() && classStr
.Find(u
"tab-bottom") != kNotFound
;
362 bool nsNativeTheme::IsFirstTab(nsIFrame
* aFrame
) {
363 if (!aFrame
) return false;
365 for (nsIFrame
* first
: aFrame
->GetParent()->PrincipalChildList()) {
366 if (first
->GetRect().Width() > 0 &&
367 first
->GetContent()->IsXULElement(nsGkAtoms::tab
))
368 return (first
== aFrame
);
373 bool nsNativeTheme::IsHorizontal(nsIFrame
* aFrame
) {
374 if (!aFrame
) return false;
376 if (!aFrame
->GetContent()->IsElement()) return true;
378 return !aFrame
->GetContent()->AsElement()->AttrValueIs(
379 kNameSpaceID_None
, nsGkAtoms::orient
, nsGkAtoms::vertical
, eCaseMatters
);
382 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame
* aFrame
, int32_t aOffset
) {
383 if (!aFrame
) return false;
385 if (aOffset
== 0) return IsSelectedTab(aFrame
);
387 int32_t thisTabIndex
= -1, selectedTabIndex
= -1;
389 nsIFrame
* currentTab
= aFrame
->GetParent()->PrincipalChildList().FirstChild();
390 for (int32_t i
= 0; currentTab
; currentTab
= currentTab
->GetNextSibling()) {
391 if (currentTab
->GetRect().Width() == 0) continue;
392 if (aFrame
== currentTab
) thisTabIndex
= i
;
393 if (IsSelectedTab(currentTab
)) selectedTabIndex
= i
;
397 if (thisTabIndex
== -1 || selectedTabIndex
== -1) return false;
399 return (thisTabIndex
- selectedTabIndex
== aOffset
);
402 bool nsNativeTheme::IsVerticalProgress(nsIFrame
* aFrame
) {
406 return IsVerticalMeter(aFrame
);
409 bool nsNativeTheme::IsVerticalMeter(nsIFrame
* aFrame
) {
410 MOZ_ASSERT(aFrame
, "You have to pass a non-null aFrame");
411 switch (aFrame
->StyleDisplay()->mOrient
) {
412 case StyleOrient::Horizontal
:
414 case StyleOrient::Vertical
:
416 case StyleOrient::Inline
:
417 return aFrame
->GetWritingMode().IsVertical();
418 case StyleOrient::Block
:
419 return !aFrame
->GetWritingMode().IsVertical();
421 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
426 bool nsNativeTheme::IsSubmenu(nsIFrame
* aFrame
, bool* aLeftOfParent
) {
427 if (!aFrame
) return false;
429 nsIContent
* parentContent
= aFrame
->GetContent()->GetParent();
430 if (!parentContent
|| !parentContent
->IsXULElement(nsGkAtoms::menu
))
433 nsIFrame
* parent
= aFrame
;
434 while ((parent
= parent
->GetParent())) {
435 if (parent
->GetContent() == parentContent
) {
437 LayoutDeviceIntRect selfBounds
, parentBounds
;
438 selfBounds
= aFrame
->GetNearestWidget()->GetScreenBounds();
439 parentBounds
= parent
->GetNearestWidget()->GetScreenBounds();
440 *aLeftOfParent
= selfBounds
.X() < parentBounds
.X();
449 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent
* aContent
,
450 uint32_t aMinimumFrameRate
) {
451 NS_ASSERTION(aContent
, "Null pointer!");
452 NS_ASSERTION(aMinimumFrameRate
, "aMinimumFrameRate must be non-zero!");
453 NS_ASSERTION(aMinimumFrameRate
<= 1000,
454 "aMinimumFrameRate must be less than 1000!");
456 uint32_t timeout
= 1000 / aMinimumFrameRate
;
457 timeout
= std::min(mAnimatedContentTimeout
, timeout
);
459 if (!mAnimatedContentTimer
) {
460 mAnimatedContentTimer
= NS_NewTimer();
461 NS_ENSURE_TRUE(mAnimatedContentTimer
, false);
464 if (mAnimatedContentList
.IsEmpty() || timeout
!= mAnimatedContentTimeout
) {
466 if (!mAnimatedContentList
.IsEmpty()) {
467 rv
= mAnimatedContentTimer
->Cancel();
468 NS_ENSURE_SUCCESS(rv
, false);
471 if (XRE_IsContentProcess() && NS_IsMainThread()) {
472 mAnimatedContentTimer
->SetTarget(
473 aContent
->OwnerDoc()->EventTargetFor(TaskCategory::Other
));
475 rv
= mAnimatedContentTimer
->InitWithCallback(this, timeout
,
476 nsITimer::TYPE_ONE_SHOT
);
477 NS_ENSURE_SUCCESS(rv
, false);
479 mAnimatedContentTimeout
= timeout
;
482 // XXX(Bug 1631371) Check if this should use a fallible operation as it
483 // pretended earlier.
484 mAnimatedContentList
.AppendElement(aContent
);
490 nsNativeTheme::Notify(nsITimer
* aTimer
) {
491 NS_ASSERTION(aTimer
== mAnimatedContentTimer
, "Wrong timer!");
493 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
494 // QueueAnimatedContentForRefresh.
496 uint32_t count
= mAnimatedContentList
.Length();
497 for (uint32_t index
= 0; index
< count
; index
++) {
498 nsIFrame
* frame
= mAnimatedContentList
[index
]->GetPrimaryFrame();
500 frame
->InvalidateFrame();
504 mAnimatedContentList
.Clear();
505 mAnimatedContentTimeout
= UINT32_MAX
;
510 nsNativeTheme::GetName(nsACString
& aName
) {
511 aName
.AssignLiteral("nsNativeTheme");
515 nsIFrame
* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
516 nsIFrame
* aFrame
, bool aNextSibling
) {
517 if (!aFrame
) return nullptr;
519 // Find the next visible sibling.
520 nsIFrame
* sibling
= aFrame
;
523 aNextSibling
? sibling
->GetNextSibling() : sibling
->GetPrevSibling();
524 } while (sibling
&& sibling
->GetRect().Width() == 0);
526 // Check same appearance and adjacency.
528 sibling
->StyleDisplay()->EffectiveAppearance() !=
529 aFrame
->StyleDisplay()->EffectiveAppearance() ||
530 (sibling
->GetRect().XMost() != aFrame
->GetRect().X() &&
531 aFrame
->GetRect().XMost() != sibling
->GetRect().X()))
536 bool nsNativeTheme::IsRangeHorizontal(nsIFrame
* aFrame
) {
537 nsIFrame
* rangeFrame
= aFrame
;
538 if (!rangeFrame
->IsRangeFrame()) {
539 // If the thumb's frame is passed in, get its range parent:
540 rangeFrame
= aFrame
->GetParent();
542 if (rangeFrame
->IsRangeFrame()) {
543 return static_cast<nsRangeFrame
*>(rangeFrame
)->IsHorizontal();
545 // Not actually a range frame - just use the ratio of the frame's size to
547 return aFrame
->GetSize().width
>= aFrame
->GetSize().height
;
551 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame
* aFrame
) {
552 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
555 nsIFrame
* frame
= aFrame
;
556 nsIScrollableFrame
* scrollFrame
= nullptr;
557 while (!scrollFrame
&& frame
) {
558 scrollFrame
= frame
->GetScrollTargetFrame();
559 frame
= frame
->GetParent();
562 aFrame
= scrollFrame
->GetScrolledFrame();
564 // Leave aFrame untouched.
568 return IsDarkBackground(aFrame
);
572 bool nsNativeTheme::IsDarkBackground(nsIFrame
* aFrame
) {
574 nsCSSRendering::FindEffectiveBackgroundColor(
575 aFrame
, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
577 return LookAndFeel::IsDarkColor(color
);
581 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance
) {
582 switch (aAppearance
) {
583 case StyleAppearance::ScrollbarVertical
:
584 case StyleAppearance::ScrollbarHorizontal
:
585 case StyleAppearance::ScrollbarbuttonUp
:
586 case StyleAppearance::ScrollbarbuttonDown
:
587 case StyleAppearance::ScrollbarbuttonLeft
:
588 case StyleAppearance::ScrollbarbuttonRight
:
589 case StyleAppearance::ScrollbarthumbVertical
:
590 case StyleAppearance::ScrollbarthumbHorizontal
:
591 case StyleAppearance::ScrollbartrackHorizontal
:
592 case StyleAppearance::ScrollbartrackVertical
:
593 case StyleAppearance::Scrollcorner
: