Backed out 2 changesets (bug 1879680) for causing spidermonkey failures on dumpValue...
[gecko.git] / widget / nsNativeTheme.cpp
blob2484f802cd86d3c09975e612e5952229651490a6
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"
7 #include "nsIWidget.h"
8 #include "mozilla/dom/Document.h"
9 #include "nsIContent.h"
10 #include "nsIFrame.h"
11 #include "nsIScrollableFrame.h"
12 #include "nsLayoutUtils.h"
13 #include "nsNumberControlFrame.h"
14 #include "nsPresContext.h"
15 #include "nsString.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"
32 #include <algorithm>
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) {
43 if (!aFrame) {
44 return ElementState();
47 nsIContent* frameContent = aFrame->GetContent();
48 if (!frameContent || !frameContent->IsElement()) {
49 return ElementState();
52 const bool isXULElement = frameContent->IsXULElement();
53 if (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 ||
60 aAppearance == StyleAppearance::ButtonArrowDown) {
61 aFrame = aFrame->GetParent();
62 frameContent = aFrame->GetContent();
64 MOZ_ASSERT(frameContent && frameContent->IsElement());
67 ElementState flags = frameContent->AsElement()->StyleState();
68 nsNumberControlFrame* numberControlFrame =
69 nsNumberControlFrame::GetNumberControlFrameForSpinButton(aFrame);
70 if (numberControlFrame &&
71 numberControlFrame->GetContent()->AsElement()->StyleState().HasState(
72 ElementState::DISABLED)) {
73 flags |= ElementState::DISABLED;
76 if (!isXULElement) {
77 return flags;
80 if (CheckBooleanAttr(aFrame, nsGkAtoms::disabled)) {
81 flags |= ElementState::DISABLED;
84 switch (aAppearance) {
85 case StyleAppearance::Radio: {
86 if (CheckBooleanAttr(aFrame, nsGkAtoms::focused)) {
87 flags |= ElementState::FOCUS;
88 nsPIDOMWindowOuter* window =
89 aFrame->GetContent()->OwnerDoc()->GetWindow();
90 if (window && window->ShouldShowFocusRing()) {
91 flags |= ElementState::FOCUSRING;
94 if (CheckBooleanAttr(aFrame, nsGkAtoms::selected) ||
95 CheckBooleanAttr(aFrame, nsGkAtoms::checked)) {
96 flags |= ElementState::CHECKED;
98 break;
100 case StyleAppearance::Checkbox: {
101 if (CheckBooleanAttr(aFrame, nsGkAtoms::checked)) {
102 flags |= ElementState::CHECKED;
103 } else if (CheckBooleanAttr(aFrame, nsGkAtoms::indeterminate)) {
104 flags |= ElementState::INDETERMINATE;
106 break;
108 case StyleAppearance::Toolbarbutton:
109 if (CheckBooleanAttr(aFrame, nsGkAtoms::open)) {
110 flags |= ElementState::HOVER | ElementState::ACTIVE;
112 break;
113 case StyleAppearance::MenulistButton:
114 case StyleAppearance::Menulist:
115 case StyleAppearance::NumberInput:
116 case StyleAppearance::Textfield:
117 case StyleAppearance::Searchfield:
118 case StyleAppearance::Textarea: {
119 if (CheckBooleanAttr(aFrame, nsGkAtoms::focused)) {
120 flags |= ElementState::FOCUS | ElementState::FOCUSRING;
122 break;
124 default:
125 break;
128 return flags;
131 /* static */
132 bool nsNativeTheme::CheckBooleanAttr(nsIFrame* aFrame, nsAtom* aAtom) {
133 if (!aFrame) return false;
135 nsIContent* content = aFrame->GetContent();
136 if (!content || !content->IsElement()) return false;
138 if (content->IsHTMLElement()) return content->AsElement()->HasAttr(aAtom);
140 // For XML/XUL elements, an attribute must be equal to the literal
141 // string "true" to be counted as true. An empty string should _not_
142 // be counted as true.
143 return content->AsElement()->AttrValueIs(kNameSpaceID_None, aAtom, u"true"_ns,
144 eCaseMatters);
147 /* static */
148 int32_t nsNativeTheme::CheckIntAttr(nsIFrame* aFrame, nsAtom* aAtom,
149 int32_t defaultValue) {
150 if (!aFrame) return defaultValue;
152 nsIContent* content = aFrame->GetContent();
153 if (!content || !content->IsElement()) return defaultValue;
155 nsAutoString attr;
156 content->AsElement()->GetAttr(aAtom, attr);
157 nsresult err;
158 int32_t value = attr.ToInteger(&err);
159 if (attr.IsEmpty() || NS_FAILED(err)) return defaultValue;
161 return value;
164 /* static */
165 double nsNativeTheme::GetProgressValue(nsIFrame* aFrame) {
166 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
167 return 0;
170 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Value();
173 /* static */
174 double nsNativeTheme::GetProgressMaxValue(nsIFrame* aFrame) {
175 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
176 return 100;
179 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Max();
182 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame* aFrame) {
183 if (!aFrame) return false;
185 nsIContent* content = aFrame->GetContent();
186 return content->IsXULElement(nsGkAtoms::button) &&
187 content->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
188 u"menu"_ns, eCaseMatters);
191 bool nsNativeTheme::IsPressedButton(nsIFrame* aFrame) {
192 ElementState state = GetContentState(aFrame, StyleAppearance::Toolbarbutton);
193 if (state.HasState(ElementState::DISABLED)) {
194 return false;
197 return IsOpenButton(aFrame) ||
198 state.HasAllStates(ElementState::ACTIVE | ElementState::HOVER);
201 bool nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext,
202 nsIFrame* aFrame,
203 StyleAppearance aAppearance) {
204 // Check for specific widgets to see if HTML has overridden the style.
205 if (!aFrame) {
206 return false;
210 * Progress bar appearance should be the same for the bar and the container
211 * frame. nsProgressFrame owns the logic and will tell us what we should do.
213 if (aAppearance == StyleAppearance::Progresschunk ||
214 aAppearance == StyleAppearance::ProgressBar) {
215 nsProgressFrame* progressFrame = do_QueryFrame(
216 aAppearance == StyleAppearance::Progresschunk ? aFrame->GetParent()
217 : aFrame);
218 if (progressFrame) {
219 return !progressFrame->ShouldUseNativeStyle();
224 * Meter bar appearance should be the same for the bar and the container
225 * frame. nsMeterFrame owns the logic and will tell us what we should do.
227 if (aAppearance == StyleAppearance::Meterchunk ||
228 aAppearance == StyleAppearance::Meter) {
229 nsMeterFrame* meterFrame = do_QueryFrame(
230 aAppearance == StyleAppearance::Meterchunk ? aFrame->GetParent()
231 : aFrame);
232 if (meterFrame) {
233 return !meterFrame->ShouldUseNativeStyle();
238 * An nsRangeFrame and its children are treated atomically when it
239 * comes to native theming (either all parts, or no parts, are themed).
240 * nsRangeFrame owns the logic and will tell us what we should do.
242 if (aAppearance == StyleAppearance::Range ||
243 aAppearance == StyleAppearance::RangeThumb) {
244 nsRangeFrame* rangeFrame = do_QueryFrame(
245 aAppearance == StyleAppearance::RangeThumb ? aFrame->GetParent()
246 : aFrame);
247 if (rangeFrame) {
248 return !rangeFrame->ShouldUseNativeStyle();
252 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
253 aAppearance) &&
254 aFrame->GetContent()->IsHTMLElement() &&
255 aFrame->Style()->HasAuthorSpecifiedBorderOrBackground();
258 /* static */
259 bool nsNativeTheme::IsFrameRTL(nsIFrame* aFrame) {
260 if (!aFrame) {
261 return false;
263 return aFrame->GetWritingMode().IsPhysicalRTL();
266 /* static */
267 bool nsNativeTheme::IsHTMLContent(nsIFrame* aFrame) {
268 if (!aFrame) {
269 return false;
271 nsIContent* content = aFrame->GetContent();
272 return content && content->IsHTMLElement();
275 // treeheadercell:
276 nsNativeTheme::TreeSortDirection nsNativeTheme::GetTreeSortDirection(
277 nsIFrame* aFrame) {
278 if (!aFrame || !aFrame->GetContent()) return eTreeSortDirection_Natural;
280 static Element::AttrValuesArray strings[] = {nsGkAtoms::descending,
281 nsGkAtoms::ascending, nullptr};
283 nsIContent* content = aFrame->GetContent();
284 if (content->IsElement()) {
285 switch (content->AsElement()->FindAttrValueIn(
286 kNameSpaceID_None, nsGkAtoms::sortDirection, strings, eCaseMatters)) {
287 case 0:
288 return eTreeSortDirection_Descending;
289 case 1:
290 return eTreeSortDirection_Ascending;
294 return eTreeSortDirection_Natural;
297 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame* aFrame) {
298 if (!aFrame) {
299 return false;
302 // A tree column picker button is always the last header cell.
303 if (aFrame->GetContent()->IsXULElement(nsGkAtoms::button)) {
304 return true;
307 // Find the parent tree.
308 nsIContent* parent = aFrame->GetContent()->GetParent();
309 while (parent && !parent->IsXULElement(nsGkAtoms::tree)) {
310 parent = parent->GetParent();
313 // If the column picker is visible, this can't be the last column.
314 if (parent && !parent->AsElement()->AttrValueIs(kNameSpaceID_None,
315 nsGkAtoms::hidecolumnpicker,
316 u"true"_ns, eCaseMatters))
317 return false;
319 while ((aFrame = aFrame->GetNextSibling())) {
320 if (aFrame->GetRect().Width() > 0) return false;
322 return true;
325 // tab:
326 bool nsNativeTheme::IsBottomTab(nsIFrame* aFrame) {
327 if (!aFrame) return false;
329 nsAutoString classStr;
330 if (aFrame->GetContent()->IsElement()) {
331 aFrame->GetContent()->AsElement()->GetAttr(nsGkAtoms::_class, classStr);
333 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
334 return !classStr.IsEmpty() && classStr.Find(u"tab-bottom") != kNotFound;
337 bool nsNativeTheme::IsFirstTab(nsIFrame* aFrame) {
338 if (!aFrame) return false;
340 for (nsIFrame* first : aFrame->GetParent()->PrincipalChildList()) {
341 if (first->GetRect().Width() > 0 &&
342 first->GetContent()->IsXULElement(nsGkAtoms::tab))
343 return (first == aFrame);
345 return false;
348 bool nsNativeTheme::IsHorizontal(nsIFrame* aFrame) {
349 if (!aFrame) return false;
351 if (!aFrame->GetContent()->IsElement()) return true;
353 return !aFrame->GetContent()->AsElement()->AttrValueIs(
354 kNameSpaceID_None, nsGkAtoms::orient, nsGkAtoms::vertical, eCaseMatters);
357 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame* aFrame, int32_t aOffset) {
358 if (!aFrame) return false;
360 if (aOffset == 0) return IsSelectedTab(aFrame);
362 int32_t thisTabIndex = -1, selectedTabIndex = -1;
364 nsIFrame* currentTab = aFrame->GetParent()->PrincipalChildList().FirstChild();
365 for (int32_t i = 0; currentTab; currentTab = currentTab->GetNextSibling()) {
366 if (currentTab->GetRect().Width() == 0) continue;
367 if (aFrame == currentTab) thisTabIndex = i;
368 if (IsSelectedTab(currentTab)) selectedTabIndex = i;
369 ++i;
372 if (thisTabIndex == -1 || selectedTabIndex == -1) return false;
374 return (thisTabIndex - selectedTabIndex == aOffset);
377 bool nsNativeTheme::IsVerticalProgress(nsIFrame* aFrame) {
378 if (!aFrame) {
379 return false;
381 return IsVerticalMeter(aFrame);
384 bool nsNativeTheme::IsVerticalMeter(nsIFrame* aFrame) {
385 MOZ_ASSERT(aFrame, "You have to pass a non-null aFrame");
386 switch (aFrame->StyleDisplay()->mOrient) {
387 case StyleOrient::Horizontal:
388 return false;
389 case StyleOrient::Vertical:
390 return true;
391 case StyleOrient::Inline:
392 return aFrame->GetWritingMode().IsVertical();
393 case StyleOrient::Block:
394 return !aFrame->GetWritingMode().IsVertical();
396 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
397 return false;
400 // menupopup:
401 bool nsNativeTheme::IsSubmenu(nsIFrame* aFrame, bool* aLeftOfParent) {
402 if (!aFrame) return false;
404 nsIContent* parentContent = aFrame->GetContent()->GetParent();
405 if (!parentContent || !parentContent->IsXULElement(nsGkAtoms::menu))
406 return false;
408 nsIFrame* parent = aFrame;
409 while ((parent = parent->GetParent())) {
410 if (parent->GetContent() == parentContent) {
411 if (aLeftOfParent) {
412 LayoutDeviceIntRect selfBounds, parentBounds;
413 selfBounds = aFrame->GetNearestWidget()->GetScreenBounds();
414 parentBounds = parent->GetNearestWidget()->GetScreenBounds();
415 *aLeftOfParent = selfBounds.X() < parentBounds.X();
417 return true;
421 return false;
424 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent* aContent,
425 uint32_t aMinimumFrameRate) {
426 NS_ASSERTION(aContent, "Null pointer!");
427 NS_ASSERTION(aMinimumFrameRate, "aMinimumFrameRate must be non-zero!");
428 NS_ASSERTION(aMinimumFrameRate <= 1000,
429 "aMinimumFrameRate must be less than 1000!");
431 uint32_t timeout = 1000 / aMinimumFrameRate;
432 timeout = std::min(mAnimatedContentTimeout, timeout);
434 if (!mAnimatedContentTimer) {
435 mAnimatedContentTimer = NS_NewTimer();
436 NS_ENSURE_TRUE(mAnimatedContentTimer, false);
439 if (mAnimatedContentList.IsEmpty() || timeout != mAnimatedContentTimeout) {
440 nsresult rv;
441 if (!mAnimatedContentList.IsEmpty()) {
442 rv = mAnimatedContentTimer->Cancel();
443 NS_ENSURE_SUCCESS(rv, false);
446 if (XRE_IsContentProcess() && NS_IsMainThread()) {
447 mAnimatedContentTimer->SetTarget(GetMainThreadSerialEventTarget());
449 rv = mAnimatedContentTimer->InitWithCallback(this, timeout,
450 nsITimer::TYPE_ONE_SHOT);
451 NS_ENSURE_SUCCESS(rv, false);
453 mAnimatedContentTimeout = timeout;
456 // XXX(Bug 1631371) Check if this should use a fallible operation as it
457 // pretended earlier.
458 mAnimatedContentList.AppendElement(aContent);
460 return true;
463 NS_IMETHODIMP
464 nsNativeTheme::Notify(nsITimer* aTimer) {
465 NS_ASSERTION(aTimer == mAnimatedContentTimer, "Wrong timer!");
467 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
468 // QueueAnimatedContentForRefresh.
470 uint32_t count = mAnimatedContentList.Length();
471 for (uint32_t index = 0; index < count; index++) {
472 nsIFrame* frame = mAnimatedContentList[index]->GetPrimaryFrame();
473 if (frame) {
474 frame->InvalidateFrame();
478 mAnimatedContentList.Clear();
479 mAnimatedContentTimeout = UINT32_MAX;
480 return NS_OK;
483 NS_IMETHODIMP
484 nsNativeTheme::GetName(nsACString& aName) {
485 aName.AssignLiteral("nsNativeTheme");
486 return NS_OK;
489 nsIFrame* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
490 nsIFrame* aFrame, bool aNextSibling) {
491 if (!aFrame) return nullptr;
493 // Find the next visible sibling.
494 nsIFrame* sibling = aFrame;
495 do {
496 sibling =
497 aNextSibling ? sibling->GetNextSibling() : sibling->GetPrevSibling();
498 } while (sibling && sibling->GetRect().Width() == 0);
500 // Check same appearance and adjacency.
501 if (!sibling ||
502 sibling->StyleDisplay()->EffectiveAppearance() !=
503 aFrame->StyleDisplay()->EffectiveAppearance() ||
504 (sibling->GetRect().XMost() != aFrame->GetRect().X() &&
505 aFrame->GetRect().XMost() != sibling->GetRect().X()))
506 return nullptr;
507 return sibling;
510 bool nsNativeTheme::IsRangeHorizontal(nsIFrame* aFrame) {
511 nsIFrame* rangeFrame = aFrame;
512 if (!rangeFrame->IsRangeFrame()) {
513 // If the thumb's frame is passed in, get its range parent:
514 rangeFrame = aFrame->GetParent();
516 if (rangeFrame->IsRangeFrame()) {
517 return static_cast<nsRangeFrame*>(rangeFrame)->IsHorizontal();
519 // Not actually a range frame - just use the ratio of the frame's size to
520 // decide:
521 return aFrame->GetSize().width >= aFrame->GetSize().height;
524 /* static */
525 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame* aFrame) {
526 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
527 // might be none.
529 nsIFrame* frame = aFrame;
530 nsIScrollableFrame* scrollFrame = nullptr;
531 while (!scrollFrame && frame) {
532 scrollFrame = frame->GetScrollTargetFrame();
533 frame = frame->GetParent();
535 if (scrollFrame) {
536 aFrame = scrollFrame->GetScrolledFrame();
537 } else {
538 // Leave aFrame untouched.
542 return IsDarkBackground(aFrame);
545 /* static */
546 bool nsNativeTheme::IsDarkBackground(nsIFrame* aFrame) {
547 auto color =
548 nsCSSRendering::FindEffectiveBackgroundColor(
549 aFrame, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
550 .mColor;
551 return LookAndFeel::IsDarkColor(color);
554 /*static*/
555 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance) {
556 switch (aAppearance) {
557 case StyleAppearance::ScrollbarVertical:
558 case StyleAppearance::ScrollbarHorizontal:
559 case StyleAppearance::ScrollbarbuttonUp:
560 case StyleAppearance::ScrollbarbuttonDown:
561 case StyleAppearance::ScrollbarbuttonLeft:
562 case StyleAppearance::ScrollbarbuttonRight:
563 case StyleAppearance::ScrollbarthumbVertical:
564 case StyleAppearance::ScrollbarthumbHorizontal:
565 case StyleAppearance::ScrollbartrackHorizontal:
566 case StyleAppearance::ScrollbartrackVertical:
567 case StyleAppearance::Scrollcorner:
568 return true;
569 default:
570 return false;
574 /*static*/
575 bool nsNativeTheme::IsWidgetAlwaysNonNative(nsIFrame* aFrame,
576 StyleAppearance aAppearance) {
577 return IsWidgetScrollbarPart(aAppearance) ||
578 aAppearance == StyleAppearance::FocusOutline ||
579 (aFrame && aFrame->StyleUI()->mMozTheme == StyleMozTheme::NonNative);