no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / widget / nsNativeTheme.cpp
blob55393736ba3143b3fca07578a70f1c44d47eb90a
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::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;
77 if (!isXULElement) {
78 return flags;
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;
99 break;
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;
107 break;
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;
118 break;
120 default:
121 break;
124 return flags;
127 /* static */
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,
140 eCaseMatters);
143 /* static */
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;
151 nsAutoString attr;
152 content->AsElement()->GetAttr(aAtom, attr);
153 nsresult err;
154 int32_t value = attr.ToInteger(&err);
155 if (attr.IsEmpty() || NS_FAILED(err)) return defaultValue;
157 return value;
160 /* static */
161 double nsNativeTheme::GetProgressValue(nsIFrame* aFrame) {
162 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
163 return 0;
166 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Value();
169 /* static */
170 double nsNativeTheme::GetProgressMaxValue(nsIFrame* aFrame) {
171 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
172 return 100;
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)) {
190 return false;
193 return IsOpenButton(aFrame) ||
194 state.HasAllStates(ElementState::ACTIVE | ElementState::HOVER);
197 bool nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext,
198 nsIFrame* aFrame,
199 StyleAppearance aAppearance) {
200 // Check for specific widgets to see if HTML has overridden the style.
201 if (!aFrame) {
202 return false;
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()
213 : aFrame);
214 if (progressFrame) {
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()
227 : aFrame);
228 if (meterFrame) {
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()
242 : aFrame);
243 if (rangeFrame) {
244 return !rangeFrame->ShouldUseNativeStyle();
248 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
249 aAppearance) &&
250 aFrame->GetContent()->IsHTMLElement() &&
251 aFrame->Style()->HasAuthorSpecifiedBorderOrBackground();
254 /* static */
255 bool nsNativeTheme::IsFrameRTL(nsIFrame* aFrame) {
256 if (!aFrame) {
257 return false;
259 return aFrame->GetWritingMode().IsPhysicalRTL();
262 /* static */
263 bool nsNativeTheme::IsHTMLContent(nsIFrame* aFrame) {
264 if (!aFrame) {
265 return false;
267 nsIContent* content = aFrame->GetContent();
268 return content && content->IsHTMLElement();
271 // treeheadercell:
272 nsNativeTheme::TreeSortDirection nsNativeTheme::GetTreeSortDirection(
273 nsIFrame* aFrame) {
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)) {
283 case 0:
284 return eTreeSortDirection_Descending;
285 case 1:
286 return eTreeSortDirection_Ascending;
290 return eTreeSortDirection_Natural;
293 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame* aFrame) {
294 if (!aFrame) {
295 return false;
298 // A tree column picker button is always the last header cell.
299 if (aFrame->GetContent()->IsXULElement(nsGkAtoms::button)) {
300 return true;
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))
313 return false;
315 while ((aFrame = aFrame->GetNextSibling())) {
316 if (aFrame->GetRect().Width() > 0) return false;
318 return true;
321 // tab:
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);
341 return false;
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;
365 ++i;
368 if (thisTabIndex == -1 || selectedTabIndex == -1) return false;
370 return (thisTabIndex - selectedTabIndex == aOffset);
373 bool nsNativeTheme::IsVerticalProgress(nsIFrame* aFrame) {
374 if (!aFrame) {
375 return false;
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:
384 return false;
385 case StyleOrient::Vertical:
386 return true;
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");
393 return false;
396 // menupopup:
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))
402 return false;
404 nsIFrame* parent = aFrame;
405 while ((parent = parent->GetParent())) {
406 if (parent->GetContent() == parentContent) {
407 if (aLeftOfParent) {
408 LayoutDeviceIntRect selfBounds, parentBounds;
409 selfBounds = aFrame->GetNearestWidget()->GetScreenBounds();
410 parentBounds = parent->GetNearestWidget()->GetScreenBounds();
411 *aLeftOfParent = selfBounds.X() < parentBounds.X();
413 return true;
417 return false;
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) {
436 nsresult rv;
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);
457 return true;
460 NS_IMETHODIMP
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();
470 if (frame) {
471 frame->InvalidateFrame();
475 mAnimatedContentList.Clear();
476 mAnimatedContentTimeout = UINT32_MAX;
477 return NS_OK;
480 NS_IMETHODIMP
481 nsNativeTheme::GetName(nsACString& aName) {
482 aName.AssignLiteral("nsNativeTheme");
483 return NS_OK;
486 nsIFrame* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
487 nsIFrame* aFrame, bool aNextSibling) {
488 if (!aFrame) return nullptr;
490 // Find the next visible sibling.
491 nsIFrame* sibling = aFrame;
492 do {
493 sibling =
494 aNextSibling ? sibling->GetNextSibling() : sibling->GetPrevSibling();
495 } while (sibling && sibling->GetRect().Width() == 0);
497 // Check same appearance and adjacency.
498 if (!sibling ||
499 sibling->StyleDisplay()->EffectiveAppearance() !=
500 aFrame->StyleDisplay()->EffectiveAppearance() ||
501 (sibling->GetRect().XMost() != aFrame->GetRect().X() &&
502 aFrame->GetRect().XMost() != sibling->GetRect().X()))
503 return nullptr;
504 return sibling;
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
517 // decide:
518 return aFrame->GetSize().width >= aFrame->GetSize().height;
521 /* static */
522 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame* aFrame) {
523 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
524 // might be none.
526 nsIFrame* frame = aFrame;
527 nsIScrollableFrame* scrollFrame = nullptr;
528 while (!scrollFrame && frame) {
529 scrollFrame = frame->GetScrollTargetFrame();
530 frame = frame->GetParent();
532 if (scrollFrame) {
533 aFrame = scrollFrame->GetScrolledFrame();
534 } else {
535 // Leave aFrame untouched.
539 return IsDarkBackground(aFrame);
542 /* static */
543 bool nsNativeTheme::IsDarkBackground(nsIFrame* aFrame) {
544 auto color =
545 nsCSSRendering::FindEffectiveBackgroundColor(
546 aFrame, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
547 .mColor;
548 return LookAndFeel::IsDarkColor(color);
551 /*static*/
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:
565 return true;
566 default:
567 return false;
571 /*static*/
572 bool nsNativeTheme::IsWidgetAlwaysNonNative(nsIFrame* aFrame,
573 StyleAppearance aAppearance) {
574 return IsWidgetScrollbarPart(aAppearance) ||
575 aAppearance == StyleAppearance::FocusOutline ||
576 (aFrame && aFrame->StyleUI()->mMozTheme == StyleMozTheme::NonNative);