Backed out changeset 85b802206e37 (bug 1883618) for causing bustage on nsHTTPCompress...
[gecko.git] / widget / nsNativeTheme.cpp
blob6900414ff3d3c26c18e723feb4ec2fdf9e06cbc7
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 #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::Searchfield:
124 case StyleAppearance::Textarea: {
125 if (CheckBooleanAttr(aFrame, nsGkAtoms::focused)) {
126 flags |= ElementState::FOCUS | ElementState::FOCUSRING;
128 break;
130 default:
131 break;
134 return flags;
137 /* static */
138 bool nsNativeTheme::CheckBooleanAttr(nsIFrame* aFrame, nsAtom* aAtom) {
139 if (!aFrame) return false;
141 nsIContent* content = aFrame->GetContent();
142 if (!content || !content->IsElement()) return false;
144 if (content->IsHTMLElement()) return content->AsElement()->HasAttr(aAtom);
146 // For XML/XUL elements, an attribute must be equal to the literal
147 // string "true" to be counted as true. An empty string should _not_
148 // be counted as true.
149 return content->AsElement()->AttrValueIs(kNameSpaceID_None, aAtom, u"true"_ns,
150 eCaseMatters);
153 /* static */
154 int32_t nsNativeTheme::CheckIntAttr(nsIFrame* aFrame, nsAtom* aAtom,
155 int32_t defaultValue) {
156 if (!aFrame) return defaultValue;
158 nsIContent* content = aFrame->GetContent();
159 if (!content || !content->IsElement()) return defaultValue;
161 nsAutoString attr;
162 content->AsElement()->GetAttr(aAtom, attr);
163 nsresult err;
164 int32_t value = attr.ToInteger(&err);
165 if (attr.IsEmpty() || NS_FAILED(err)) return defaultValue;
167 return value;
170 /* static */
171 double nsNativeTheme::GetProgressValue(nsIFrame* aFrame) {
172 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
173 return 0;
176 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Value();
179 /* static */
180 double nsNativeTheme::GetProgressMaxValue(nsIFrame* aFrame) {
181 if (!aFrame || !aFrame->GetContent()->IsHTMLElement(nsGkAtoms::progress)) {
182 return 100;
185 return static_cast<HTMLProgressElement*>(aFrame->GetContent())->Max();
188 bool nsNativeTheme::IsButtonTypeMenu(nsIFrame* aFrame) {
189 if (!aFrame) return false;
191 nsIContent* content = aFrame->GetContent();
192 return content->IsXULElement(nsGkAtoms::button) &&
193 content->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
194 u"menu"_ns, eCaseMatters);
197 bool nsNativeTheme::IsPressedButton(nsIFrame* aFrame) {
198 ElementState state = GetContentState(aFrame, StyleAppearance::Toolbarbutton);
199 if (state.HasState(ElementState::DISABLED)) {
200 return false;
203 return IsOpenButton(aFrame) ||
204 state.HasAllStates(ElementState::ACTIVE | ElementState::HOVER);
207 bool nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext,
208 nsIFrame* aFrame,
209 StyleAppearance aAppearance) {
210 // Check for specific widgets to see if HTML has overridden the style.
211 if (!aFrame) {
212 return false;
216 * Progress bar appearance should be the same for the bar and the container
217 * frame. nsProgressFrame owns the logic and will tell us what we should do.
219 if (aAppearance == StyleAppearance::Progresschunk ||
220 aAppearance == StyleAppearance::ProgressBar) {
221 nsProgressFrame* progressFrame = do_QueryFrame(
222 aAppearance == StyleAppearance::Progresschunk ? aFrame->GetParent()
223 : aFrame);
224 if (progressFrame) {
225 return !progressFrame->ShouldUseNativeStyle();
230 * Meter bar appearance should be the same for the bar and the container
231 * frame. nsMeterFrame owns the logic and will tell us what we should do.
233 if (aAppearance == StyleAppearance::Meterchunk ||
234 aAppearance == StyleAppearance::Meter) {
235 nsMeterFrame* meterFrame = do_QueryFrame(
236 aAppearance == StyleAppearance::Meterchunk ? aFrame->GetParent()
237 : aFrame);
238 if (meterFrame) {
239 return !meterFrame->ShouldUseNativeStyle();
244 * An nsRangeFrame and its children are treated atomically when it
245 * comes to native theming (either all parts, or no parts, are themed).
246 * nsRangeFrame owns the logic and will tell us what we should do.
248 if (aAppearance == StyleAppearance::Range ||
249 aAppearance == StyleAppearance::RangeThumb) {
250 nsRangeFrame* rangeFrame = do_QueryFrame(
251 aAppearance == StyleAppearance::RangeThumb ? aFrame->GetParent()
252 : aFrame);
253 if (rangeFrame) {
254 return !rangeFrame->ShouldUseNativeStyle();
258 return nsLayoutUtils::AuthorSpecifiedBorderBackgroundDisablesTheming(
259 aAppearance) &&
260 aFrame->GetContent()->IsHTMLElement() &&
261 aFrame->Style()->HasAuthorSpecifiedBorderOrBackground();
264 /* static */
265 bool nsNativeTheme::IsFrameRTL(nsIFrame* aFrame) {
266 if (!aFrame) {
267 return false;
269 return aFrame->GetWritingMode().IsPhysicalRTL();
272 /* static */
273 bool nsNativeTheme::IsHTMLContent(nsIFrame* aFrame) {
274 if (!aFrame) {
275 return false;
277 nsIContent* content = aFrame->GetContent();
278 return content && content->IsHTMLElement();
281 // treeheadercell:
282 nsNativeTheme::TreeSortDirection nsNativeTheme::GetTreeSortDirection(
283 nsIFrame* aFrame) {
284 if (!aFrame || !aFrame->GetContent()) return eTreeSortDirection_Natural;
286 static Element::AttrValuesArray strings[] = {nsGkAtoms::descending,
287 nsGkAtoms::ascending, nullptr};
289 nsIContent* content = aFrame->GetContent();
290 if (content->IsElement()) {
291 switch (content->AsElement()->FindAttrValueIn(
292 kNameSpaceID_None, nsGkAtoms::sortDirection, strings, eCaseMatters)) {
293 case 0:
294 return eTreeSortDirection_Descending;
295 case 1:
296 return eTreeSortDirection_Ascending;
300 return eTreeSortDirection_Natural;
303 bool nsNativeTheme::IsLastTreeHeaderCell(nsIFrame* aFrame) {
304 if (!aFrame) {
305 return false;
308 // A tree column picker button is always the last header cell.
309 if (aFrame->GetContent()->IsXULElement(nsGkAtoms::button)) {
310 return true;
313 // Find the parent tree.
314 nsIContent* parent = aFrame->GetContent()->GetParent();
315 while (parent && !parent->IsXULElement(nsGkAtoms::tree)) {
316 parent = parent->GetParent();
319 // If the column picker is visible, this can't be the last column.
320 if (parent && !parent->AsElement()->AttrValueIs(kNameSpaceID_None,
321 nsGkAtoms::hidecolumnpicker,
322 u"true"_ns, eCaseMatters))
323 return false;
325 while ((aFrame = aFrame->GetNextSibling())) {
326 if (aFrame->GetRect().Width() > 0) return false;
328 return true;
331 // tab:
332 bool nsNativeTheme::IsBottomTab(nsIFrame* aFrame) {
333 if (!aFrame) return false;
335 nsAutoString classStr;
336 if (aFrame->GetContent()->IsElement()) {
337 aFrame->GetContent()->AsElement()->GetAttr(nsGkAtoms::_class, classStr);
339 // FIXME: This looks bogus, shouldn't this be looking at GetClasses()?
340 return !classStr.IsEmpty() && classStr.Find(u"tab-bottom") != kNotFound;
343 bool nsNativeTheme::IsFirstTab(nsIFrame* aFrame) {
344 if (!aFrame) return false;
346 for (nsIFrame* first : aFrame->GetParent()->PrincipalChildList()) {
347 if (first->GetRect().Width() > 0 &&
348 first->GetContent()->IsXULElement(nsGkAtoms::tab))
349 return (first == aFrame);
351 return false;
354 bool nsNativeTheme::IsHorizontal(nsIFrame* aFrame) {
355 if (!aFrame) return false;
357 if (!aFrame->GetContent()->IsElement()) return true;
359 return !aFrame->GetContent()->AsElement()->AttrValueIs(
360 kNameSpaceID_None, nsGkAtoms::orient, nsGkAtoms::vertical, eCaseMatters);
363 bool nsNativeTheme::IsNextToSelectedTab(nsIFrame* aFrame, int32_t aOffset) {
364 if (!aFrame) return false;
366 if (aOffset == 0) return IsSelectedTab(aFrame);
368 int32_t thisTabIndex = -1, selectedTabIndex = -1;
370 nsIFrame* currentTab = aFrame->GetParent()->PrincipalChildList().FirstChild();
371 for (int32_t i = 0; currentTab; currentTab = currentTab->GetNextSibling()) {
372 if (currentTab->GetRect().Width() == 0) continue;
373 if (aFrame == currentTab) thisTabIndex = i;
374 if (IsSelectedTab(currentTab)) selectedTabIndex = i;
375 ++i;
378 if (thisTabIndex == -1 || selectedTabIndex == -1) return false;
380 return (thisTabIndex - selectedTabIndex == aOffset);
383 bool nsNativeTheme::IsVerticalProgress(nsIFrame* aFrame) {
384 if (!aFrame) {
385 return false;
387 return IsVerticalMeter(aFrame);
390 bool nsNativeTheme::IsVerticalMeter(nsIFrame* aFrame) {
391 MOZ_ASSERT(aFrame, "You have to pass a non-null aFrame");
392 switch (aFrame->StyleDisplay()->mOrient) {
393 case StyleOrient::Horizontal:
394 return false;
395 case StyleOrient::Vertical:
396 return true;
397 case StyleOrient::Inline:
398 return aFrame->GetWritingMode().IsVertical();
399 case StyleOrient::Block:
400 return !aFrame->GetWritingMode().IsVertical();
402 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
403 return false;
406 // menupopup:
407 bool nsNativeTheme::IsSubmenu(nsIFrame* aFrame, bool* aLeftOfParent) {
408 if (!aFrame) return false;
410 nsIContent* parentContent = aFrame->GetContent()->GetParent();
411 if (!parentContent || !parentContent->IsXULElement(nsGkAtoms::menu))
412 return false;
414 nsIFrame* parent = aFrame;
415 while ((parent = parent->GetParent())) {
416 if (parent->GetContent() == parentContent) {
417 if (aLeftOfParent) {
418 LayoutDeviceIntRect selfBounds, parentBounds;
419 selfBounds = aFrame->GetNearestWidget()->GetScreenBounds();
420 parentBounds = parent->GetNearestWidget()->GetScreenBounds();
421 *aLeftOfParent = selfBounds.X() < parentBounds.X();
423 return true;
427 return false;
430 bool nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent* aContent,
431 uint32_t aMinimumFrameRate) {
432 NS_ASSERTION(aContent, "Null pointer!");
433 NS_ASSERTION(aMinimumFrameRate, "aMinimumFrameRate must be non-zero!");
434 NS_ASSERTION(aMinimumFrameRate <= 1000,
435 "aMinimumFrameRate must be less than 1000!");
437 uint32_t timeout = 1000 / aMinimumFrameRate;
438 timeout = std::min(mAnimatedContentTimeout, timeout);
440 if (!mAnimatedContentTimer) {
441 mAnimatedContentTimer = NS_NewTimer();
442 NS_ENSURE_TRUE(mAnimatedContentTimer, false);
445 if (mAnimatedContentList.IsEmpty() || timeout != mAnimatedContentTimeout) {
446 nsresult rv;
447 if (!mAnimatedContentList.IsEmpty()) {
448 rv = mAnimatedContentTimer->Cancel();
449 NS_ENSURE_SUCCESS(rv, false);
452 if (XRE_IsContentProcess() && NS_IsMainThread()) {
453 mAnimatedContentTimer->SetTarget(GetMainThreadSerialEventTarget());
455 rv = mAnimatedContentTimer->InitWithCallback(this, timeout,
456 nsITimer::TYPE_ONE_SHOT);
457 NS_ENSURE_SUCCESS(rv, false);
459 mAnimatedContentTimeout = timeout;
462 // XXX(Bug 1631371) Check if this should use a fallible operation as it
463 // pretended earlier.
464 mAnimatedContentList.AppendElement(aContent);
466 return true;
469 NS_IMETHODIMP
470 nsNativeTheme::Notify(nsITimer* aTimer) {
471 NS_ASSERTION(aTimer == mAnimatedContentTimer, "Wrong timer!");
473 // XXX Assumes that calling nsIFrame::Invalidate won't reenter
474 // QueueAnimatedContentForRefresh.
476 uint32_t count = mAnimatedContentList.Length();
477 for (uint32_t index = 0; index < count; index++) {
478 nsIFrame* frame = mAnimatedContentList[index]->GetPrimaryFrame();
479 if (frame) {
480 frame->InvalidateFrame();
484 mAnimatedContentList.Clear();
485 mAnimatedContentTimeout = UINT32_MAX;
486 return NS_OK;
489 NS_IMETHODIMP
490 nsNativeTheme::GetName(nsACString& aName) {
491 aName.AssignLiteral("nsNativeTheme");
492 return NS_OK;
495 nsIFrame* nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(
496 nsIFrame* aFrame, bool aNextSibling) {
497 if (!aFrame) return nullptr;
499 // Find the next visible sibling.
500 nsIFrame* sibling = aFrame;
501 do {
502 sibling =
503 aNextSibling ? sibling->GetNextSibling() : sibling->GetPrevSibling();
504 } while (sibling && sibling->GetRect().Width() == 0);
506 // Check same appearance and adjacency.
507 if (!sibling ||
508 sibling->StyleDisplay()->EffectiveAppearance() !=
509 aFrame->StyleDisplay()->EffectiveAppearance() ||
510 (sibling->GetRect().XMost() != aFrame->GetRect().X() &&
511 aFrame->GetRect().XMost() != sibling->GetRect().X()))
512 return nullptr;
513 return sibling;
516 bool nsNativeTheme::IsRangeHorizontal(nsIFrame* aFrame) {
517 nsIFrame* rangeFrame = aFrame;
518 if (!rangeFrame->IsRangeFrame()) {
519 // If the thumb's frame is passed in, get its range parent:
520 rangeFrame = aFrame->GetParent();
522 if (rangeFrame->IsRangeFrame()) {
523 return static_cast<nsRangeFrame*>(rangeFrame)->IsHorizontal();
525 // Not actually a range frame - just use the ratio of the frame's size to
526 // decide:
527 return aFrame->GetSize().width >= aFrame->GetSize().height;
530 /* static */
531 bool nsNativeTheme::IsDarkBackgroundForScrollbar(nsIFrame* aFrame) {
532 // Try to find the scrolled frame. Note that for stuff like xul <tree> there
533 // might be none.
535 nsIFrame* frame = aFrame;
536 nsIScrollableFrame* scrollFrame = nullptr;
537 while (!scrollFrame && frame) {
538 scrollFrame = frame->GetScrollTargetFrame();
539 frame = frame->GetParent();
541 if (scrollFrame) {
542 aFrame = scrollFrame->GetScrolledFrame();
543 } else {
544 // Leave aFrame untouched.
548 return IsDarkBackground(aFrame);
551 /* static */
552 bool nsNativeTheme::IsDarkBackground(nsIFrame* aFrame) {
553 auto color =
554 nsCSSRendering::FindEffectiveBackgroundColor(
555 aFrame, /* aStopAtThemed = */ false, /* aPreferBodyToCanvas = */ true)
556 .mColor;
557 return LookAndFeel::IsDarkColor(color);
560 /*static*/
561 bool nsNativeTheme::IsWidgetScrollbarPart(StyleAppearance aAppearance) {
562 switch (aAppearance) {
563 case StyleAppearance::ScrollbarVertical:
564 case StyleAppearance::ScrollbarHorizontal:
565 case StyleAppearance::ScrollbarbuttonUp:
566 case StyleAppearance::ScrollbarbuttonDown:
567 case StyleAppearance::ScrollbarbuttonLeft:
568 case StyleAppearance::ScrollbarbuttonRight:
569 case StyleAppearance::ScrollbarthumbVertical:
570 case StyleAppearance::ScrollbarthumbHorizontal:
571 case StyleAppearance::ScrollbartrackHorizontal:
572 case StyleAppearance::ScrollbartrackVertical:
573 case StyleAppearance::Scrollcorner:
574 return true;
575 default:
576 return false;
580 /*static*/
581 bool nsNativeTheme::IsWidgetAlwaysNonNative(nsIFrame* aFrame,
582 StyleAppearance aAppearance) {
583 return IsWidgetScrollbarPart(aAppearance) ||
584 aAppearance == StyleAppearance::FocusOutline ||
585 (aFrame && aFrame->StyleUI()->mMozTheme == StyleMozTheme::NonNative);