Backed out changeset 0c01a856e4c3 (bug 1870427) as requested by Emilio CLOSED TREE
[gecko.git] / widget / nsNativeTheme.cpp
blob60e94b35d3433095049f457dc35a9f40a02fc8c5
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 "nsLayoutUtils.h"
12 #include "nsNumberControlFrame.h"
13 #include "nsPresContext.h"
14 #include "nsString.h"
15 #include "nsNameSpaceManager.h"
16 #include "nsStyleConsts.h"
17 #include "nsPIDOMWindow.h"
18 #include "nsProgressFrame.h"
19 #include "nsMeterFrame.h"
20 #include "nsRangeFrame.h"
21 #include "nsCSSRendering.h"
22 #include "ImageContainer.h"
23 #include "mozilla/ComputedStyle.h"
24 #include "mozilla/dom/Element.h"
25 #include "mozilla/dom/HTMLBodyElement.h"
26 #include "mozilla/dom/HTMLInputElement.h"
27 #include "mozilla/dom/HTMLProgressElement.h"
28 #include "mozilla/PresShell.h"
29 #include "mozilla/ScrollContainerFrame.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 #ifdef MOZ_WIDGET_GTK
61 aAppearance == StyleAppearance::MozWindowButtonClose ||
62 aAppearance == StyleAppearance::MozWindowButtonMinimize ||
63 aAppearance == StyleAppearance::MozWindowButtonRestore ||
64 aAppearance == StyleAppearance::MozWindowButtonMaximize ||
65 #endif
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;
82 if (!isXULElement) {
83 return flags;
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;
104 break;
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;
112 break;
114 case StyleAppearance::Toolbarbutton:
115 if (CheckBooleanAttr(aFrame, nsGkAtoms::open)) {
116 flags |= ElementState::HOVER | ElementState::ACTIVE;
118 break;
119 case StyleAppearance::MenulistButton:
120 case StyleAppearance::Menulist:
121 case StyleAppearance::NumberInput:
122 case StyleAppearance::Textfield:
123 case StyleAppearance::PasswordInput:
124 case StyleAppearance::Searchfield:
125 case StyleAppearance::Textarea: {
126 if (CheckBooleanAttr(aFrame, nsGkAtoms::focused)) {
127 flags |= ElementState::FOCUS | ElementState::FOCUSRING;
129 break;
131 default:
132 break;
135 return flags;
138 /* static */
139 bool nsNativeTheme::CheckBooleanAttr(nsIFrame* aFrame, nsAtom* aAtom) {
140 if (!aFrame) return false;
142 nsIContent* content = aFrame->GetContent();
143 if (!content || !content->IsElement()) return false;
145 if (content->IsHTMLElement()) return content->AsElement()->HasAttr(aAtom);
147 // For XML/XUL elements, an attribute must be equal to the literal
148 // string "true" to be counted as true. An empty string should _not_
149 // be counted as true.
150 return content->AsElement()->AttrValueIs(kNameSpaceID_None, aAtom, u"true"_ns,
151 eCaseMatters);
154 /* static */
155 int32_t nsNativeTheme::CheckIntAttr(nsIFrame* aFrame, nsAtom* aAtom,
156 int32_t defaultValue) {
157 if (!aFrame) return defaultValue;
159 nsIContent* content = aFrame->GetContent();
160 if (!content || !content->IsElement()) return defaultValue;
162 nsAutoString attr;
163 content->AsElement()->GetAttr(aAtom, attr);
164 nsresult err;
165 int32_t value = attr.ToInteger(&err);
166 if (attr.IsEmpty() || NS_FAILED(err)) return defaultValue;
168 return value;
171 /* static */
172 double nsNativeTheme::GetProgressValue(nsIFrame* aFrame) {
173 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
174 return 0;
177 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Value();
180 /* static */
181 double nsNativeTheme::GetProgressMaxValue(nsIFrame* aFrame) {
182 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
183 return 100;
186 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Max();
189 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame* aFrame) {
190 if (!aFrame) return false;
192 nsIContent* content = aFrame->GetContent();
193 return content->IsXULElement(nsGkAtoms::button) &&
194 content->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
195 u"menu"_ns, eCaseMatters);
198 bool nsNativeTheme::IsPressedButton(nsIFrame* aFrame) {
199 ElementState state = GetContentState(aFrame, StyleAppearance::Toolbarbutton);
200 if (state.HasState(ElementState::DISABLED)) {
201 return false;
204 return IsOpenButton(aFrame) ||
205 state.HasAllStates(ElementState::ACTIVE | ElementState::HOVER);
208 bool nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext,
209 nsIFrame* aFrame,
210 StyleAppearance aAppearance) {
211 // Check for specific widgets to see if HTML has overridden the style.
212 if (!aFrame) {
213 return false;
217 * Progress bar appearance should be the same for the bar and the container
218 * frame. nsProgressFrame owns the logic and will tell us what we should do.
220 if (aAppearance == StyleAppearance::Progresschunk ||
221 aAppearance == StyleAppearance::ProgressBar) {
222 nsProgressFrame* progressFrame = do_QueryFrame(
223 aAppearance == StyleAppearance::Progresschunk ? aFrame->GetParent()
224 : aFrame);
225 if (progressFrame) {
226 return !progressFrame->ShouldUseNativeStyle();
231 * Meter bar appearance should be the same for the bar and the container
232 * frame. nsMeterFrame owns the logic and will tell us what we should do.
234 if (aAppearance == StyleAppearance::Meterchunk ||
235 aAppearance == StyleAppearance::Meter) {
236 nsMeterFrame* meterFrame = do_QueryFrame(
237 aAppearance == StyleAppearance::Meterchunk ? aFrame->GetParent()
238 : aFrame);
239 if (meterFrame) {
240 return !meterFrame->ShouldUseNativeStyle();
245 * An nsRangeFrame and its children are treated atomically when it
246 * comes to native theming (either all parts, or no parts, are themed).
247 * nsRangeFrame owns the logic and will tell us what we should do.
249 if (aAppearance == StyleAppearance::Range ||
250 aAppearance == StyleAppearance::RangeThumb) {
251 nsRangeFrame* rangeFrame = do_QueryFrame(
252 aAppearance == StyleAppearance::RangeThumb ? aFrame->GetParent()
253 : aFrame);
254 if (rangeFrame) {
255 return !rangeFrame->ShouldUseNativeStyle();
259 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
260 aAppearance) &&
261 aFrame->GetContent()->IsHTMLElement() &&
262 aFrame->Style()->HasAuthorSpecifiedBorderOrBackground();
265 /* static */
266 bool nsNativeTheme::IsFrameRTL(nsIFrame* aFrame) {
267 if (!aFrame) {
268 return false;
270 return aFrame->GetWritingMode().IsPhysicalRTL();
273 /* static */
274 bool nsNativeTheme::IsHTMLContent(nsIFrame* aFrame) {
275 if (!aFrame) {
276 return false;
278 nsIContent* content = aFrame->GetContent();
279 return content && content->IsHTMLElement();
282 // treeheadercell:
283 nsNativeTheme::TreeSortDirection nsNativeTheme::GetTreeSortDirection(
284 nsIFrame* aFrame) {
285 if (!aFrame || !aFrame->GetContent()) return eTreeSortDirection_Natural;
287 static Element::AttrValuesArray strings[] = {nsGkAtoms::descending,
288 nsGkAtoms::ascending, nullptr};
290 nsIContent* content = aFrame->GetContent();
291 if (content->IsElement()) {
292 switch (content->AsElement()->FindAttrValueIn(
293 kNameSpaceID_None, nsGkAtoms::sortDirection, strings, eCaseMatters)) {
294 case 0:
295 return eTreeSortDirection_Descending;
296 case 1:
297 return eTreeSortDirection_Ascending;
301 return eTreeSortDirection_Natural;
304 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame* aFrame) {
305 if (!aFrame) {
306 return false;
309 // A tree column picker button is always the last header cell.
310 if (aFrame->GetContent()->IsXULElement(nsGkAtoms::button)) {
311 return true;
314 // Find the parent tree.
315 nsIContent* parent = aFrame->GetContent()->GetParent();
316 while (parent && !parent->IsXULElement(nsGkAtoms::tree)) {
317 parent = parent->GetParent();
320 // If the column picker is visible, this can't be the last column.
321 if (parent && !parent->AsElement()->AttrValueIs(kNameSpaceID_None,
322 nsGkAtoms::hidecolumnpicker,
323 u"true"_ns, eCaseMatters))
324 return false;
326 while ((aFrame = aFrame->GetNextSibling())) {
327 if (aFrame->GetRect().Width() > 0) return false;
329 return true;
332 // tab:
333 bool nsNativeTheme::IsBottomTab(nsIFrame* aFrame) {
334 if (!aFrame) return false;
336 nsAutoString classStr;
337 if (aFrame->GetContent()->IsElement()) {
338 aFrame->GetContent()->AsElement()->GetAttr(nsGkAtoms::_class, classStr);
340 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
341 return !classStr.IsEmpty() && classStr.Find(u"tab-bottom") != kNotFound;
344 bool nsNativeTheme::IsFirstTab(nsIFrame* aFrame) {
345 if (!aFrame) return false;
347 for (nsIFrame* first : aFrame->GetParent()->PrincipalChildList()) {
348 if (first->GetRect().Width() > 0 &&
349 first->GetContent()->IsXULElement(nsGkAtoms::tab))
350 return (first == aFrame);
352 return false;
355 bool nsNativeTheme::IsHorizontal(nsIFrame* aFrame) {
356 if (!aFrame) return false;
358 if (!aFrame->GetContent()->IsElement()) return true;
360 return !aFrame->GetContent()->AsElement()->AttrValueIs(
361 kNameSpaceID_None, nsGkAtoms::orient, nsGkAtoms::vertical, eCaseMatters);
364 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame* aFrame, int32_t aOffset) {
365 if (!aFrame) return false;
367 if (aOffset == 0) return IsSelectedTab(aFrame);
369 int32_t thisTabIndex = -1, selectedTabIndex = -1;
371 nsIFrame* currentTab = aFrame->GetParent()->PrincipalChildList().FirstChild();
372 for (int32_t i = 0; currentTab; currentTab = currentTab->GetNextSibling()) {
373 if (currentTab->GetRect().Width() == 0) continue;
374 if (aFrame == currentTab) thisTabIndex = i;
375 if (IsSelectedTab(currentTab)) selectedTabIndex = i;
376 ++i;
379 if (thisTabIndex == -1 || selectedTabIndex == -1) return false;
381 return (thisTabIndex - selectedTabIndex == aOffset);
384 bool nsNativeTheme::IsVerticalProgress(nsIFrame* aFrame) {
385 if (!aFrame) {
386 return false;
388 return IsVerticalMeter(aFrame);
391 bool nsNativeTheme::IsVerticalMeter(nsIFrame* aFrame) {
392 MOZ_ASSERT(aFrame, "You have to pass a non-null aFrame");
393 switch (aFrame->StyleDisplay()->mOrient) {
394 case StyleOrient::Horizontal:
395 return false;
396 case StyleOrient::Vertical:
397 return true;
398 case StyleOrient::Inline:
399 return aFrame->GetWritingMode().IsVertical();
400 case StyleOrient::Block:
401 return !aFrame->GetWritingMode().IsVertical();
403 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
404 return false;
407 // menupopup:
408 bool nsNativeTheme::IsSubmenu(nsIFrame* aFrame, bool* aLeftOfParent) {
409 if (!aFrame) return false;
411 nsIContent* parentContent = aFrame->GetContent()->GetParent();
412 if (!parentContent || !parentContent->IsXULElement(nsGkAtoms::menu))
413 return false;
415 nsIFrame* parent = aFrame;
416 while ((parent = parent->GetParent())) {
417 if (parent->GetContent() == parentContent) {
418 if (aLeftOfParent) {
419 LayoutDeviceIntRect selfBounds, parentBounds;
420 selfBounds = aFrame->GetNearestWidget()->GetScreenBounds();
421 parentBounds = parent->GetNearestWidget()->GetScreenBounds();
422 *aLeftOfParent = selfBounds.X() < parentBounds.X();
424 return true;
428 return false;
431 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent* aContent,
432 uint32_t aMinimumFrameRate) {
433 NS_ASSERTION(aContent, "Null pointer!");
434 NS_ASSERTION(aMinimumFrameRate, "aMinimumFrameRate must be non-zero!");
435 NS_ASSERTION(aMinimumFrameRate <= 1000,
436 "aMinimumFrameRate must be less than 1000!");
438 uint32_t timeout = 1000 / aMinimumFrameRate;
439 timeout = std::min(mAnimatedContentTimeout, timeout);
441 if (!mAnimatedContentTimer) {
442 mAnimatedContentTimer = NS_NewTimer();
443 NS_ENSURE_TRUE(mAnimatedContentTimer, false);
446 if (mAnimatedContentList.IsEmpty() || timeout != mAnimatedContentTimeout) {
447 nsresult rv;
448 if (!mAnimatedContentList.IsEmpty()) {
449 rv = mAnimatedContentTimer->Cancel();
450 NS_ENSURE_SUCCESS(rv, false);
453 if (XRE_IsContentProcess() && NS_IsMainThread()) {
454 mAnimatedContentTimer->SetTarget(GetMainThreadSerialEventTarget());
456 rv = mAnimatedContentTimer->InitWithCallback(this, timeout,
457 nsITimer::TYPE_ONE_SHOT);
458 NS_ENSURE_SUCCESS(rv, false);
460 mAnimatedContentTimeout = timeout;
463 // XXX(Bug 1631371) Check if this should use a fallible operation as it
464 // pretended earlier.
465 mAnimatedContentList.AppendElement(aContent);
467 return true;
470 NS_IMETHODIMP
471 nsNativeTheme::Notify(nsITimer* aTimer) {
472 NS_ASSERTION(aTimer == mAnimatedContentTimer, "Wrong timer!");
474 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
475 // QueueAnimatedContentForRefresh.
477 uint32_t count = mAnimatedContentList.Length();
478 for (uint32_t index = 0; index < count; index++) {
479 nsIFrame* frame = mAnimatedContentList[index]->GetPrimaryFrame();
480 if (frame) {
481 frame->InvalidateFrame();
485 mAnimatedContentList.Clear();
486 mAnimatedContentTimeout = UINT32_MAX;
487 return NS_OK;
490 NS_IMETHODIMP
491 nsNativeTheme::GetName(nsACString& aName) {
492 aName.AssignLiteral("nsNativeTheme");
493 return NS_OK;
496 nsIFrame* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
497 nsIFrame* aFrame, bool aNextSibling) {
498 if (!aFrame) return nullptr;
500 // Find the next visible sibling.
501 nsIFrame* sibling = aFrame;
502 do {
503 sibling =
504 aNextSibling ? sibling->GetNextSibling() : sibling->GetPrevSibling();
505 } while (sibling && sibling->GetRect().Width() == 0);
507 // Check same appearance and adjacency.
508 if (!sibling ||
509 sibling->StyleDisplay()->EffectiveAppearance() !=
510 aFrame->StyleDisplay()->EffectiveAppearance() ||
511 (sibling->GetRect().XMost() != aFrame->GetRect().X() &&
512 aFrame->GetRect().XMost() != sibling->GetRect().X()))
513 return nullptr;
514 return sibling;
517 bool nsNativeTheme::IsRangeHorizontal(nsIFrame* aFrame) {
518 nsIFrame* rangeFrame = aFrame;
519 if (!rangeFrame->IsRangeFrame()) {
520 // If the thumb's frame is passed in, get its range parent:
521 rangeFrame = aFrame->GetParent();
523 if (rangeFrame->IsRangeFrame()) {
524 return static_cast<nsRangeFrame*>(rangeFrame)->IsHorizontal();
526 // Not actually a range frame - just use the ratio of the frame's size to
527 // decide:
528 return aFrame->GetSize().width >= aFrame->GetSize().height;
531 /* static */
532 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame* aFrame) {
533 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
534 // might be none.
536 nsIFrame* frame = aFrame;
537 ScrollContainerFrame* scrollContainerFrame = nullptr;
538 while (!scrollContainerFrame && frame) {
539 scrollContainerFrame = frame->GetScrollTargetFrame();
540 frame = frame->GetParent();
542 if (scrollContainerFrame) {
543 aFrame = scrollContainerFrame->GetScrolledFrame();
544 } else {
545 // Leave aFrame untouched.
549 return IsDarkBackground(aFrame);
552 /* static */
553 bool nsNativeTheme::IsDarkBackground(nsIFrame* aFrame) {
554 auto color =
555 nsCSSRendering::FindEffectiveBackgroundColor(
556 aFrame, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
557 .mColor;
558 return LookAndFeel::IsDarkColor(color);
561 /*static*/
562 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance) {
563 switch (aAppearance) {
564 case StyleAppearance::ScrollbarVertical:
565 case StyleAppearance::ScrollbarHorizontal:
566 case StyleAppearance::ScrollbarbuttonUp:
567 case StyleAppearance::ScrollbarbuttonDown:
568 case StyleAppearance::ScrollbarbuttonLeft:
569 case StyleAppearance::ScrollbarbuttonRight:
570 case StyleAppearance::ScrollbarthumbVertical:
571 case StyleAppearance::ScrollbarthumbHorizontal:
572 case StyleAppearance::Scrollcorner:
573 return true;
574 default:
575 return false;
579 /*static*/
580 bool nsNativeTheme::IsWidgetAlwaysNonNative(nsIFrame* aFrame,
581 StyleAppearance aAppearance) {
582 return IsWidgetScrollbarPart(aAppearance) ||
583 aAppearance == StyleAppearance::FocusOutline ||
584 (aFrame && aFrame->StyleUI()->mMozTheme == StyleMozTheme::NonNative);