Bug 1769547 - Do not MOZ_CRASH() on missing process r=nika
[gecko.git] / widget / nsNativeTheme.cpp
blob1217277e72dfd6b75269baa4804d75bdf630bf5a
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 "nsMenuFrame.h"
22 #include "nsRangeFrame.h"
23 #include "nsCSSRendering.h"
24 #include "ImageContainer.h"
25 #include "mozilla/ComputedStyle.h"
26 #include "mozilla/EventStates.h"
27 #include "mozilla/dom/Element.h"
28 #include "mozilla/dom/HTMLBodyElement.h"
29 #include "mozilla/dom/HTMLInputElement.h"
30 #include "mozilla/dom/HTMLProgressElement.h"
31 #include "mozilla/PresShell.h"
32 #include "mozilla/StaticPrefs_layout.h"
33 #include "mozilla/dom/DocumentInlines.h"
34 #include <algorithm>
36 using namespace mozilla;
37 using namespace mozilla::dom;
39 nsNativeTheme::nsNativeTheme() : mAnimatedContentTimeout(UINT32_MAX) {}
41 NS_IMPL_ISUPPORTS(nsNativeTheme, nsITimerCallback, nsINamed)
43 /* static */ EventStates nsNativeTheme::GetContentState(
44 nsIFrame* aFrame, StyleAppearance aAppearance) {
45 if (!aFrame) {
46 return EventStates();
49 nsIContent* frameContent = aFrame->GetContent();
50 if (!frameContent || !frameContent->IsElement()) {
51 return EventStates();
54 const bool isXULElement = frameContent->IsXULElement();
55 if (isXULElement) {
56 if (aAppearance == StyleAppearance::CheckboxLabel ||
57 aAppearance == StyleAppearance::RadioLabel) {
58 aFrame = aFrame->GetParent()->GetParent();
59 frameContent = aFrame->GetContent();
60 } else if (aAppearance == StyleAppearance::Checkbox ||
61 aAppearance == StyleAppearance::Radio ||
62 aAppearance == StyleAppearance::ToolbarbuttonDropdown ||
63 aAppearance == StyleAppearance::Treeheadersortarrow ||
64 aAppearance == StyleAppearance::ButtonArrowPrevious ||
65 aAppearance == StyleAppearance::ButtonArrowNext ||
66 aAppearance == StyleAppearance::ButtonArrowUp ||
67 aAppearance == StyleAppearance::ButtonArrowDown) {
68 aFrame = aFrame->GetParent();
69 frameContent = aFrame->GetContent();
71 MOZ_ASSERT(frameContent && frameContent->IsElement());
74 EventStates flags = frameContent->AsElement()->StyleState();
75 nsNumberControlFrame* numberControlFrame =
76 nsNumberControlFrame::GetNumberControlFrameForSpinButton(aFrame);
77 if (numberControlFrame &&
78 numberControlFrame->GetContent()->AsElement()->StyleState().HasState(
79 NS_EVENT_STATE_DISABLED)) {
80 flags |= NS_EVENT_STATE_DISABLED;
83 if (!isXULElement) {
84 return flags;
87 if (CheckBooleanAttr(aFrame, nsGkAtoms::disabled)) {
88 flags |= NS_EVENT_STATE_DISABLED;
91 switch (aAppearance) {
92 case StyleAppearance::RadioLabel:
93 case StyleAppearance::Radio: {
94 if (CheckBooleanAttr(aFrame, nsGkAtoms::focused)) {
95 flags |= NS_EVENT_STATE_FOCUS;
96 nsPIDOMWindowOuter* window =
97 aFrame->GetContent()->OwnerDoc()->GetWindow();
98 if (window && window->ShouldShowFocusRing()) {
99 flags |= NS_EVENT_STATE_FOCUSRING;
102 if (CheckBooleanAttr(aFrame, nsGkAtoms::selected)) {
103 flags |= NS_EVENT_STATE_CHECKED;
105 break;
107 case StyleAppearance::CheckboxLabel:
108 case StyleAppearance::Checkbox: {
109 if (CheckBooleanAttr(aFrame, nsGkAtoms::checked)) {
110 flags |= NS_EVENT_STATE_CHECKED;
111 } else if (CheckBooleanAttr(aFrame, nsGkAtoms::indeterminate)) {
112 flags |= NS_EVENT_STATE_INDETERMINATE;
114 break;
116 case StyleAppearance::MenulistButton:
117 case StyleAppearance::Menulist:
118 case StyleAppearance::NumberInput:
119 case StyleAppearance::Textfield:
120 case StyleAppearance::Searchfield:
121 case StyleAppearance::Textarea: {
122 if (CheckBooleanAttr(aFrame, nsGkAtoms::focused)) {
123 flags |= NS_EVENT_STATE_FOCUS | NS_EVENT_STATE_FOCUSRING;
125 break;
127 default:
128 break;
131 return flags;
134 /* static */
135 bool nsNativeTheme::CheckBooleanAttr(nsIFrame* aFrame, nsAtom* aAtom) {
136 if (!aFrame) return false;
138 nsIContent* content = aFrame->GetContent();
139 if (!content || !content->IsElement()) return false;
141 if (content->IsHTMLElement())
142 return content->AsElement()->HasAttr(kNameSpaceID_None, aAtom);
144 // For XML/XUL elements, an attribute must be equal to the literal
145 // string "true" to be counted as true. An empty string should _not_
146 // be counted as true.
147 return content->AsElement()->AttrValueIs(kNameSpaceID_None, aAtom, u"true"_ns,
148 eCaseMatters);
151 /* static */
152 int32_t nsNativeTheme::CheckIntAttr(nsIFrame* aFrame, nsAtom* aAtom,
153 int32_t defaultValue) {
154 if (!aFrame) return defaultValue;
156 nsIContent* content = aFrame->GetContent();
157 if (!content || !content->IsElement()) return defaultValue;
159 nsAutoString attr;
160 content->AsElement()->GetAttr(kNameSpaceID_None, aAtom, attr);
161 nsresult err;
162 int32_t value = attr.ToInteger(&err);
163 if (attr.IsEmpty() || NS_FAILED(err)) return defaultValue;
165 return value;
168 /* static */
169 double nsNativeTheme::GetProgressValue(nsIFrame* aFrame) {
170 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
171 return 0;
174 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Value();
177 /* static */
178 double nsNativeTheme::GetProgressMaxValue(nsIFrame* aFrame) {
179 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
180 return 100;
183 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Max();
186 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame* aFrame) {
187 if (!aFrame) return false;
189 nsIContent* content = aFrame->GetContent();
190 return content->IsXULElement(nsGkAtoms::button) &&
191 content->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
192 u"menu"_ns, eCaseMatters);
195 bool nsNativeTheme::IsPressedButton(nsIFrame* aFrame) {
196 EventStates eventState =
197 GetContentState(aFrame, StyleAppearance::Toolbarbutton);
198 if (eventState.HasState(NS_EVENT_STATE_DISABLED)) return false;
200 return IsOpenButton(aFrame) ||
201 eventState.HasAllStates(NS_EVENT_STATE_ACTIVE | NS_EVENT_STATE_HOVER);
204 bool nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext,
205 nsIFrame* aFrame,
206 StyleAppearance aAppearance) {
207 // Check for specific widgets to see if HTML has overridden the style.
208 if (!aFrame) {
209 return false;
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();
227 if (!parentFrame) {
228 return false;
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()
245 : aFrame);
246 if (progressFrame) {
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()
259 : aFrame);
260 if (meterFrame) {
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()
274 : aFrame);
275 if (rangeFrame) {
276 return !rangeFrame->ShouldUseNativeStyle();
280 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
281 aAppearance) &&
282 aFrame->GetContent()->IsHTMLElement() &&
283 aFrame->Style()->HasAuthorSpecifiedBorderOrBackground();
286 /* static */
287 bool nsNativeTheme::IsFrameRTL(nsIFrame* aFrame) {
288 if (!aFrame) {
289 return false;
291 return aFrame->GetWritingMode().IsPhysicalRTL();
294 /* static */
295 bool nsNativeTheme::IsHTMLContent(nsIFrame* aFrame) {
296 if (!aFrame) {
297 return false;
299 nsIContent* content = aFrame->GetContent();
300 return content && content->IsHTMLElement();
303 // treeheadercell:
304 nsNativeTheme::TreeSortDirection nsNativeTheme::GetTreeSortDirection(
305 nsIFrame* aFrame) {
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)) {
315 case 0:
316 return eTreeSortDirection_Descending;
317 case 1:
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))
341 return false;
343 while ((aFrame = aFrame->GetNextSibling())) {
344 if (aFrame->GetRect().Width() > 0) return false;
346 return true;
349 // tab:
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("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);
370 return false;
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;
394 ++i;
397 if (thisTabIndex == -1 || selectedTabIndex == -1) return false;
399 return (thisTabIndex - selectedTabIndex == aOffset);
402 bool nsNativeTheme::IsVerticalProgress(nsIFrame* aFrame) {
403 if (!aFrame) {
404 return false;
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:
413 return false;
414 case StyleOrient::Vertical:
415 return true;
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");
422 return false;
425 // menupopup:
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))
431 return false;
433 nsIFrame* parent = aFrame;
434 while ((parent = parent->GetParent())) {
435 if (parent->GetContent() == parentContent) {
436 if (aLeftOfParent) {
437 LayoutDeviceIntRect selfBounds, parentBounds;
438 selfBounds = aFrame->GetNearestWidget()->GetScreenBounds();
439 parentBounds = parent->GetNearestWidget()->GetScreenBounds();
440 *aLeftOfParent = selfBounds.X() < parentBounds.X();
442 return true;
446 return false;
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) {
465 nsresult rv;
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);
486 return true;
489 NS_IMETHODIMP
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();
499 if (frame) {
500 frame->InvalidateFrame();
504 mAnimatedContentList.Clear();
505 mAnimatedContentTimeout = UINT32_MAX;
506 return NS_OK;
509 NS_IMETHODIMP
510 nsNativeTheme::GetName(nsACString& aName) {
511 aName.AssignLiteral("nsNativeTheme");
512 return NS_OK;
515 nsIFrame* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
516 nsIFrame* aFrame, bool aNextSibling) {
517 if (!aFrame) return nullptr;
519 // Find the next visible sibling.
520 nsIFrame* sibling = aFrame;
521 do {
522 sibling =
523 aNextSibling ? sibling->GetNextSibling() : sibling->GetPrevSibling();
524 } while (sibling && sibling->GetRect().Width() == 0);
526 // Check same appearance and adjacency.
527 if (!sibling ||
528 sibling->StyleDisplay()->EffectiveAppearance() !=
529 aFrame->StyleDisplay()->EffectiveAppearance() ||
530 (sibling->GetRect().XMost() != aFrame->GetRect().X() &&
531 aFrame->GetRect().XMost() != sibling->GetRect().X()))
532 return nullptr;
533 return sibling;
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
546 // decide:
547 return aFrame->GetSize().width >= aFrame->GetSize().height;
550 static nsIFrame* GetBodyFrame(nsIFrame* aCanvasFrame) {
551 nsIContent* body = aCanvasFrame->PresContext()->Document()->GetBodyElement();
552 if (!body) {
553 return nullptr;
555 return body->GetPrimaryFrame();
558 /* static */
559 bool nsNativeTheme::IsDarkBackground(nsIFrame* aFrame) {
560 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
561 // might be none.
563 nsIFrame* frame = aFrame;
564 nsIScrollableFrame* scrollFrame = nullptr;
565 while (!scrollFrame && frame) {
566 scrollFrame = frame->GetScrollTargetFrame();
567 frame = frame->GetParent();
569 if (scrollFrame) {
570 aFrame = scrollFrame->GetScrolledFrame();
571 } else {
572 // Leave aFrame untouched.
576 auto backgroundFrame = nsCSSRendering::FindNonTransparentBackgroundFrame(
577 aFrame, /* aStopAtThemed = */ false);
578 if (!backgroundFrame.mFrame) {
579 return false;
582 nscolor color = backgroundFrame.mFrame->StyleBackground()->BackgroundColor(
583 backgroundFrame.mFrame);
585 if (backgroundFrame.mIsForCanvas) {
586 // For canvas frames, prefer to look at the body first, because the body
587 // background color is most likely what will be visible as the background
588 // color of the page, even if the html element has a different background
589 // color which prevents that of the body frame to propagate to the viewport.
590 if (nsIFrame* bodyFrame = GetBodyFrame(aFrame)) {
591 nscolor bodyColor =
592 bodyFrame->StyleBackground()->BackgroundColor(bodyFrame);
593 if (NS_GET_A(bodyColor)) {
594 color = bodyColor;
599 return LookAndFeel::IsDarkColor(color);
602 /*static*/
603 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance) {
604 switch (aAppearance) {
605 case StyleAppearance::ScrollbarVertical:
606 case StyleAppearance::ScrollbarHorizontal:
607 case StyleAppearance::ScrollbarbuttonUp:
608 case StyleAppearance::ScrollbarbuttonDown:
609 case StyleAppearance::ScrollbarbuttonLeft:
610 case StyleAppearance::ScrollbarbuttonRight:
611 case StyleAppearance::ScrollbarthumbVertical:
612 case StyleAppearance::ScrollbarthumbHorizontal:
613 case StyleAppearance::ScrollbartrackHorizontal:
614 case StyleAppearance::ScrollbartrackVertical:
615 case StyleAppearance::Scrollcorner:
616 return true;
617 default:
618 return false;