Bug 1794292 - [ANGLE] cherry-pick init-gl-point-size. r=gfx-reviewers,bradwerth
[gecko.git] / widget / nsNativeTheme.cpp
blob0b239d09d8d742e5f5a6d02d033219c70ed0f067
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::CheckboxLabel ||
55 aAppearance == StyleAppearance::RadioLabel) {
56 aFrame = aFrame->GetParent()->GetParent();
57 frameContent = aFrame->GetContent();
58 } else if (aAppearance == StyleAppearance::Checkbox ||
59 aAppearance == StyleAppearance::Radio ||
60 aAppearance == StyleAppearance::ToolbarbuttonDropdown ||
61 aAppearance == StyleAppearance::Treeheadersortarrow ||
62 aAppearance == StyleAppearance::ButtonArrowPrevious ||
63 aAppearance == StyleAppearance::ButtonArrowNext ||
64 aAppearance == StyleAppearance::ButtonArrowUp ||
65 aAppearance == StyleAppearance::ButtonArrowDown) {
66 aFrame = aFrame->GetParent();
67 frameContent = aFrame->GetContent();
69 MOZ_ASSERT(frameContent && frameContent->IsElement());
72 ElementState flags = frameContent->AsElement()->StyleState();
73 nsNumberControlFrame* numberControlFrame =
74 nsNumberControlFrame::GetNumberControlFrameForSpinButton(aFrame);
75 if (numberControlFrame &&
76 numberControlFrame->GetContent()->AsElement()->StyleState().HasState(
77 ElementState::DISABLED)) {
78 flags |= ElementState::DISABLED;
81 if (!isXULElement) {
82 return flags;
85 if (CheckBooleanAttr(aFrame, nsGkAtoms::disabled)) {
86 flags |= ElementState::DISABLED;
89 switch (aAppearance) {
90 case StyleAppearance::RadioLabel:
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 flags |= ElementState::CHECKED;
103 break;
105 case StyleAppearance::CheckboxLabel:
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::MenulistButton:
115 case StyleAppearance::Menulist:
116 case StyleAppearance::NumberInput:
117 case StyleAppearance::Textfield:
118 case StyleAppearance::Searchfield:
119 case StyleAppearance::Textarea: {
120 if (CheckBooleanAttr(aFrame, nsGkAtoms::focused)) {
121 flags |= ElementState::FOCUS | ElementState::FOCUSRING;
123 break;
125 default:
126 break;
129 return flags;
132 /* static */
133 bool nsNativeTheme::CheckBooleanAttr(nsIFrame* aFrame, nsAtom* aAtom) {
134 if (!aFrame) return false;
136 nsIContent* content = aFrame->GetContent();
137 if (!content || !content->IsElement()) return false;
139 if (content->IsHTMLElement())
140 return content->AsElement()->HasAttr(kNameSpaceID_None, aAtom);
142 // For XML/XUL elements, an attribute must be equal to the literal
143 // string "true" to be counted as true. An empty string should _not_
144 // be counted as true.
145 return content->AsElement()->AttrValueIs(kNameSpaceID_None, aAtom, u"true"_ns,
146 eCaseMatters);
149 /* static */
150 int32_t nsNativeTheme::CheckIntAttr(nsIFrame* aFrame, nsAtom* aAtom,
151 int32_t defaultValue) {
152 if (!aFrame) return defaultValue;
154 nsIContent* content = aFrame->GetContent();
155 if (!content || !content->IsElement()) return defaultValue;
157 nsAutoString attr;
158 content->AsElement()->GetAttr(kNameSpaceID_None, aAtom, attr);
159 nsresult err;
160 int32_t value = attr.ToInteger(&err);
161 if (attr.IsEmpty() || NS_FAILED(err)) return defaultValue;
163 return value;
166 /* static */
167 double nsNativeTheme::GetProgressValue(nsIFrame* aFrame) {
168 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
169 return 0;
172 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Value();
175 /* static */
176 double nsNativeTheme::GetProgressMaxValue(nsIFrame* aFrame) {
177 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
178 return 100;
181 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Max();
184 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame* aFrame) {
185 if (!aFrame) return false;
187 nsIContent* content = aFrame->GetContent();
188 return content->IsXULElement(nsGkAtoms::button) &&
189 content->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
190 u"menu"_ns, eCaseMatters);
193 bool nsNativeTheme::IsPressedButton(nsIFrame* aFrame) {
194 ElementState state = GetContentState(aFrame, StyleAppearance::Toolbarbutton);
195 if (state.HasState(ElementState::DISABLED)) {
196 return false;
199 return IsOpenButton(aFrame) ||
200 state.HasAllStates(ElementState::ACTIVE | ElementState::HOVER);
203 bool nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext,
204 nsIFrame* aFrame,
205 StyleAppearance aAppearance) {
206 // Check for specific widgets to see if HTML has overridden the style.
207 if (!aFrame) {
208 return false;
211 // Resizers have some special handling, dependent on whether in a scrollable
212 // container or not. If so, use the scrollable container's to determine
213 // whether the style is overriden instead of the resizer. This allows a
214 // non-native transparent resizer to be used instead. Otherwise, we just
215 // fall through and return false.
216 if (aAppearance == StyleAppearance::Resizer) {
217 nsIFrame* parentFrame = aFrame->GetParent();
218 if (parentFrame && parentFrame->IsScrollFrame()) {
219 // if the parent is a scrollframe, the resizer should be native themed
220 // only if the scrollable area doesn't override the widget style.
222 // note that the condition below looks a bit suspect but it's the right
223 // one. If there's no valid appearance, then we should return true, it's
224 // effectively the same as if it had overridden the appearance.
225 parentFrame = parentFrame->GetParent();
226 if (!parentFrame) {
227 return false;
229 auto parentAppearance =
230 parentFrame->StyleDisplay()->EffectiveAppearance();
231 return parentAppearance == StyleAppearance::None ||
232 IsWidgetStyled(aPresContext, parentFrame, parentAppearance);
237 * Progress bar appearance should be the same for the bar and the container
238 * frame. nsProgressFrame owns the logic and will tell us what we should do.
240 if (aAppearance == StyleAppearance::Progresschunk ||
241 aAppearance == StyleAppearance::ProgressBar) {
242 nsProgressFrame* progressFrame = do_QueryFrame(
243 aAppearance == StyleAppearance::Progresschunk ? aFrame->GetParent()
244 : aFrame);
245 if (progressFrame) {
246 return !progressFrame->ShouldUseNativeStyle();
251 * Meter bar appearance should be the same for the bar and the container
252 * frame. nsMeterFrame owns the logic and will tell us what we should do.
254 if (aAppearance == StyleAppearance::Meterchunk ||
255 aAppearance == StyleAppearance::Meter) {
256 nsMeterFrame* meterFrame = do_QueryFrame(
257 aAppearance == StyleAppearance::Meterchunk ? aFrame->GetParent()
258 : aFrame);
259 if (meterFrame) {
260 return !meterFrame->ShouldUseNativeStyle();
265 * An nsRangeFrame and its children are treated atomically when it
266 * comes to native theming (either all parts, or no parts, are themed).
267 * nsRangeFrame owns the logic and will tell us what we should do.
269 if (aAppearance == StyleAppearance::Range ||
270 aAppearance == StyleAppearance::RangeThumb) {
271 nsRangeFrame* rangeFrame = do_QueryFrame(
272 aAppearance == StyleAppearance::RangeThumb ? aFrame->GetParent()
273 : aFrame);
274 if (rangeFrame) {
275 return !rangeFrame->ShouldUseNativeStyle();
279 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
280 aAppearance) &&
281 aFrame->GetContent()->IsHTMLElement() &&
282 aFrame->Style()->HasAuthorSpecifiedBorderOrBackground();
285 /* static */
286 bool nsNativeTheme::IsFrameRTL(nsIFrame* aFrame) {
287 if (!aFrame) {
288 return false;
290 return aFrame->GetWritingMode().IsPhysicalRTL();
293 /* static */
294 bool nsNativeTheme::IsHTMLContent(nsIFrame* aFrame) {
295 if (!aFrame) {
296 return false;
298 nsIContent* content = aFrame->GetContent();
299 return content && content->IsHTMLElement();
302 // treeheadercell:
303 nsNativeTheme::TreeSortDirection nsNativeTheme::GetTreeSortDirection(
304 nsIFrame* aFrame) {
305 if (!aFrame || !aFrame->GetContent()) return eTreeSortDirection_Natural;
307 static Element::AttrValuesArray strings[] = {nsGkAtoms::descending,
308 nsGkAtoms::ascending, nullptr};
310 nsIContent* content = aFrame->GetContent();
311 if (content->IsElement()) {
312 switch (content->AsElement()->FindAttrValueIn(
313 kNameSpaceID_None, nsGkAtoms::sortDirection, strings, eCaseMatters)) {
314 case 0:
315 return eTreeSortDirection_Descending;
316 case 1:
317 return eTreeSortDirection_Ascending;
321 return eTreeSortDirection_Natural;
324 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame* aFrame) {
325 if (!aFrame) {
326 return false;
329 // A tree column picker button is always the last header cell.
330 if (aFrame->GetContent()->IsXULElement(nsGkAtoms::button)) {
331 return true;
334 // Find the parent tree.
335 nsIContent* parent = aFrame->GetContent()->GetParent();
336 while (parent && !parent->IsXULElement(nsGkAtoms::tree)) {
337 parent = parent->GetParent();
340 // If the column picker is visible, this can't be the last column.
341 if (parent && !parent->AsElement()->AttrValueIs(kNameSpaceID_None,
342 nsGkAtoms::hidecolumnpicker,
343 u"true"_ns, eCaseMatters))
344 return false;
346 while ((aFrame = aFrame->GetNextSibling())) {
347 if (aFrame->GetRect().Width() > 0) return false;
349 return true;
352 // tab:
353 bool nsNativeTheme::IsBottomTab(nsIFrame* aFrame) {
354 if (!aFrame) return false;
356 nsAutoString classStr;
357 if (aFrame->GetContent()->IsElement()) {
358 aFrame->GetContent()->AsElement()->GetAttr(kNameSpaceID_None,
359 nsGkAtoms::_class, classStr);
361 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
362 return !classStr.IsEmpty() && classStr.Find(u"tab-bottom") != kNotFound;
365 bool nsNativeTheme::IsFirstTab(nsIFrame* aFrame) {
366 if (!aFrame) return false;
368 for (nsIFrame* first : aFrame->GetParent()->PrincipalChildList()) {
369 if (first->GetRect().Width() > 0 &&
370 first->GetContent()->IsXULElement(nsGkAtoms::tab))
371 return (first == aFrame);
373 return false;
376 bool nsNativeTheme::IsHorizontal(nsIFrame* aFrame) {
377 if (!aFrame) return false;
379 if (!aFrame->GetContent()->IsElement()) return true;
381 return !aFrame->GetContent()->AsElement()->AttrValueIs(
382 kNameSpaceID_None, nsGkAtoms::orient, nsGkAtoms::vertical, eCaseMatters);
385 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame* aFrame, int32_t aOffset) {
386 if (!aFrame) return false;
388 if (aOffset == 0) return IsSelectedTab(aFrame);
390 int32_t thisTabIndex = -1, selectedTabIndex = -1;
392 nsIFrame* currentTab = aFrame->GetParent()->PrincipalChildList().FirstChild();
393 for (int32_t i = 0; currentTab; currentTab = currentTab->GetNextSibling()) {
394 if (currentTab->GetRect().Width() == 0) continue;
395 if (aFrame == currentTab) thisTabIndex = i;
396 if (IsSelectedTab(currentTab)) selectedTabIndex = i;
397 ++i;
400 if (thisTabIndex == -1 || selectedTabIndex == -1) return false;
402 return (thisTabIndex - selectedTabIndex == aOffset);
405 bool nsNativeTheme::IsVerticalProgress(nsIFrame* aFrame) {
406 if (!aFrame) {
407 return false;
409 return IsVerticalMeter(aFrame);
412 bool nsNativeTheme::IsVerticalMeter(nsIFrame* aFrame) {
413 MOZ_ASSERT(aFrame, "You have to pass a non-null aFrame");
414 switch (aFrame->StyleDisplay()->mOrient) {
415 case StyleOrient::Horizontal:
416 return false;
417 case StyleOrient::Vertical:
418 return true;
419 case StyleOrient::Inline:
420 return aFrame->GetWritingMode().IsVertical();
421 case StyleOrient::Block:
422 return !aFrame->GetWritingMode().IsVertical();
424 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
425 return false;
428 // menupopup:
429 bool nsNativeTheme::IsSubmenu(nsIFrame* aFrame, bool* aLeftOfParent) {
430 if (!aFrame) return false;
432 nsIContent* parentContent = aFrame->GetContent()->GetParent();
433 if (!parentContent || !parentContent->IsXULElement(nsGkAtoms::menu))
434 return false;
436 nsIFrame* parent = aFrame;
437 while ((parent = parent->GetParent())) {
438 if (parent->GetContent() == parentContent) {
439 if (aLeftOfParent) {
440 LayoutDeviceIntRect selfBounds, parentBounds;
441 selfBounds = aFrame->GetNearestWidget()->GetScreenBounds();
442 parentBounds = parent->GetNearestWidget()->GetScreenBounds();
443 *aLeftOfParent = selfBounds.X() < parentBounds.X();
445 return true;
449 return false;
452 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent* aContent,
453 uint32_t aMinimumFrameRate) {
454 NS_ASSERTION(aContent, "Null pointer!");
455 NS_ASSERTION(aMinimumFrameRate, "aMinimumFrameRate must be non-zero!");
456 NS_ASSERTION(aMinimumFrameRate <= 1000,
457 "aMinimumFrameRate must be less than 1000!");
459 uint32_t timeout = 1000 / aMinimumFrameRate;
460 timeout = std::min(mAnimatedContentTimeout, timeout);
462 if (!mAnimatedContentTimer) {
463 mAnimatedContentTimer = NS_NewTimer();
464 NS_ENSURE_TRUE(mAnimatedContentTimer, false);
467 if (mAnimatedContentList.IsEmpty() || timeout != mAnimatedContentTimeout) {
468 nsresult rv;
469 if (!mAnimatedContentList.IsEmpty()) {
470 rv = mAnimatedContentTimer->Cancel();
471 NS_ENSURE_SUCCESS(rv, false);
474 if (XRE_IsContentProcess() && NS_IsMainThread()) {
475 mAnimatedContentTimer->SetTarget(
476 aContent->OwnerDoc()->EventTargetFor(TaskCategory::Other));
478 rv = mAnimatedContentTimer->InitWithCallback(this, timeout,
479 nsITimer::TYPE_ONE_SHOT);
480 NS_ENSURE_SUCCESS(rv, false);
482 mAnimatedContentTimeout = timeout;
485 // XXX(Bug 1631371) Check if this should use a fallible operation as it
486 // pretended earlier.
487 mAnimatedContentList.AppendElement(aContent);
489 return true;
492 NS_IMETHODIMP
493 nsNativeTheme::Notify(nsITimer* aTimer) {
494 NS_ASSERTION(aTimer == mAnimatedContentTimer, "Wrong timer!");
496 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
497 // QueueAnimatedContentForRefresh.
499 uint32_t count = mAnimatedContentList.Length();
500 for (uint32_t index = 0; index < count; index++) {
501 nsIFrame* frame = mAnimatedContentList[index]->GetPrimaryFrame();
502 if (frame) {
503 frame->InvalidateFrame();
507 mAnimatedContentList.Clear();
508 mAnimatedContentTimeout = UINT32_MAX;
509 return NS_OK;
512 NS_IMETHODIMP
513 nsNativeTheme::GetName(nsACString& aName) {
514 aName.AssignLiteral("nsNativeTheme");
515 return NS_OK;
518 nsIFrame* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
519 nsIFrame* aFrame, bool aNextSibling) {
520 if (!aFrame) return nullptr;
522 // Find the next visible sibling.
523 nsIFrame* sibling = aFrame;
524 do {
525 sibling =
526 aNextSibling ? sibling->GetNextSibling() : sibling->GetPrevSibling();
527 } while (sibling && sibling->GetRect().Width() == 0);
529 // Check same appearance and adjacency.
530 if (!sibling ||
531 sibling->StyleDisplay()->EffectiveAppearance() !=
532 aFrame->StyleDisplay()->EffectiveAppearance() ||
533 (sibling->GetRect().XMost() != aFrame->GetRect().X() &&
534 aFrame->GetRect().XMost() != sibling->GetRect().X()))
535 return nullptr;
536 return sibling;
539 bool nsNativeTheme::IsRangeHorizontal(nsIFrame* aFrame) {
540 nsIFrame* rangeFrame = aFrame;
541 if (!rangeFrame->IsRangeFrame()) {
542 // If the thumb's frame is passed in, get its range parent:
543 rangeFrame = aFrame->GetParent();
545 if (rangeFrame->IsRangeFrame()) {
546 return static_cast<nsRangeFrame*>(rangeFrame)->IsHorizontal();
548 // Not actually a range frame - just use the ratio of the frame's size to
549 // decide:
550 return aFrame->GetSize().width >= aFrame->GetSize().height;
553 /* static */
554 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame* aFrame) {
555 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
556 // might be none.
558 nsIFrame* frame = aFrame;
559 nsIScrollableFrame* scrollFrame = nullptr;
560 while (!scrollFrame && frame) {
561 scrollFrame = frame->GetScrollTargetFrame();
562 frame = frame->GetParent();
564 if (scrollFrame) {
565 aFrame = scrollFrame->GetScrolledFrame();
566 } else {
567 // Leave aFrame untouched.
571 return IsDarkBackground(aFrame);
574 /* static */
575 bool nsNativeTheme::IsDarkBackground(nsIFrame* aFrame) {
576 auto color =
577 nsCSSRendering::FindEffectiveBackgroundColor(
578 aFrame, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
579 .mColor;
580 return LookAndFeel::IsDarkColor(color);
583 /*static*/
584 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance) {
585 switch (aAppearance) {
586 case StyleAppearance::ScrollbarVertical:
587 case StyleAppearance::ScrollbarHorizontal:
588 case StyleAppearance::ScrollbarbuttonUp:
589 case StyleAppearance::ScrollbarbuttonDown:
590 case StyleAppearance::ScrollbarbuttonLeft:
591 case StyleAppearance::ScrollbarbuttonRight:
592 case StyleAppearance::ScrollbarthumbVertical:
593 case StyleAppearance::ScrollbarthumbHorizontal:
594 case StyleAppearance::ScrollbartrackHorizontal:
595 case StyleAppearance::ScrollbartrackVertical:
596 case StyleAppearance::Scrollcorner:
597 return true;
598 default:
599 return false;