Backed out changeset 7b83373f7a9e (bug 1883860) for causing build bustages @ caps...
[gecko.git] / dom / xul / ChromeObserver.cpp
blob73f712e30cc8efbcd3c4337362a8f37925603a86
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 #include "ChromeObserver.h"
9 #include "nsIBaseWindow.h"
10 #include "nsIWidget.h"
11 #include "nsIFrame.h"
13 #include "nsContentUtils.h"
14 #include "nsView.h"
15 #include "nsPresContext.h"
16 #include "mozilla/dom/Document.h"
17 #include "mozilla/dom/DocumentInlines.h"
18 #include "mozilla/dom/Element.h"
19 #include "mozilla/dom/MutationEventBinding.h"
20 #include "nsXULElement.h"
22 namespace mozilla::dom {
24 NS_IMPL_ISUPPORTS(ChromeObserver, nsIMutationObserver)
26 ChromeObserver::ChromeObserver(Document* aDocument)
27 : nsStubMutationObserver(), mDocument(aDocument) {}
29 ChromeObserver::~ChromeObserver() = default;
31 void ChromeObserver::Init() {
32 mDocument->AddMutationObserver(this);
33 Element* rootElement = mDocument->GetRootElement();
34 if (!rootElement) {
35 return;
37 nsAutoScriptBlocker scriptBlocker;
38 uint32_t attributeCount = rootElement->GetAttrCount();
39 for (uint32_t i = 0; i < attributeCount; i++) {
40 BorrowedAttrInfo info = rootElement->GetAttrInfoAt(i);
41 const nsAttrName* name = info.mName;
42 if (name->LocalName() == nsGkAtoms::chromemargin) {
43 // Some linux windows managers have an issue when the chrome margin is
44 // applied while the browser is loading (bug 1598848). For now, skip
45 // applying this attribute when initializing.
46 continue;
48 AttributeChanged(rootElement, name->NamespaceID(), name->LocalName(),
49 MutationEvent_Binding::ADDITION, nullptr);
53 nsIWidget* ChromeObserver::GetWindowWidget() {
54 // only top level chrome documents can set the titlebar color
55 if (mDocument && mDocument->IsRootDisplayDocument()) {
56 nsCOMPtr<nsISupports> container = mDocument->GetContainer();
57 nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(container);
58 if (baseWindow) {
59 nsCOMPtr<nsIWidget> mainWidget;
60 baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
61 return mainWidget;
64 return nullptr;
67 void ChromeObserver::SetDrawsTitle(bool aState) {
68 nsIWidget* mainWidget = GetWindowWidget();
69 if (mainWidget) {
70 // We can do this synchronously because SetDrawsTitle doesn't have any
71 // synchronous effects apart from a harmless invalidation.
72 mainWidget->SetDrawsTitle(aState);
76 class MarginSetter : public Runnable {
77 public:
78 explicit MarginSetter(nsIWidget* aWidget)
79 : mozilla::Runnable("MarginSetter"),
80 mWidget(aWidget),
81 mMargin(-1, -1, -1, -1) {}
82 MarginSetter(nsIWidget* aWidget, const LayoutDeviceIntMargin& aMargin)
83 : mozilla::Runnable("MarginSetter"), mWidget(aWidget), mMargin(aMargin) {}
85 NS_IMETHOD Run() override {
86 // SetNonClientMargins can dispatch native events, hence doing
87 // it off a script runner.
88 mWidget->SetNonClientMargins(mMargin);
89 return NS_OK;
92 private:
93 nsCOMPtr<nsIWidget> mWidget;
94 LayoutDeviceIntMargin mMargin;
97 void ChromeObserver::SetChromeMargins(const nsAttrValue* aValue) {
98 if (!aValue) return;
100 nsIWidget* mainWidget = GetWindowWidget();
101 if (!mainWidget) return;
103 // top, right, bottom, left - see nsAttrValue
104 nsAutoString tmp;
105 aValue->ToString(tmp);
106 nsIntMargin margins;
107 if (nsContentUtils::ParseIntMarginValue(tmp, margins)) {
108 nsContentUtils::AddScriptRunner(new MarginSetter(
109 mainWidget, LayoutDeviceIntMargin::FromUnknownMargin(margins)));
113 void ChromeObserver::AttributeChanged(dom::Element* aElement,
114 int32_t aNamespaceID, nsAtom* aName,
115 int32_t aModType,
116 const nsAttrValue* aOldValue) {
117 // We only care about changes to the root element.
118 if (!mDocument || aElement != mDocument->GetRootElement()) {
119 return;
122 const nsAttrValue* value = aElement->GetParsedAttr(aName, aNamespaceID);
123 if (value) {
124 // Hide chrome if needed
125 if (aName == nsGkAtoms::hidechrome) {
126 HideWindowChrome(value->Equals(u"true"_ns, eCaseMatters));
127 } else if (aName == nsGkAtoms::chromemargin) {
128 SetChromeMargins(value);
130 // title and drawintitlebar are settable on
131 // any root node (windows, dialogs, etc)
132 else if (aName == nsGkAtoms::title) {
133 mDocument->NotifyPossibleTitleChange(false);
134 } else if (aName == nsGkAtoms::drawtitle) {
135 SetDrawsTitle(value->Equals(u"true"_ns, eCaseMatters));
136 } else if (aName == nsGkAtoms::localedir) {
137 // if the localedir changed on the root element, reset the document
138 // direction
139 mDocument->ResetDocumentDirection();
141 } else {
142 if (aName == nsGkAtoms::hidechrome) {
143 HideWindowChrome(false);
144 } else if (aName == nsGkAtoms::chromemargin) {
145 ResetChromeMargins();
146 } else if (aName == nsGkAtoms::localedir) {
147 // if the localedir changed on the root element, reset the document
148 // direction
149 mDocument->ResetDocumentDirection();
150 } else if (aName == nsGkAtoms::drawtitle) {
151 SetDrawsTitle(false);
156 void ChromeObserver::NodeWillBeDestroyed(nsINode* aNode) {
157 mDocument = nullptr;
160 void ChromeObserver::ResetChromeMargins() {
161 nsIWidget* mainWidget = GetWindowWidget();
162 if (!mainWidget) return;
163 // See nsIWidget
164 nsContentUtils::AddScriptRunner(new MarginSetter(mainWidget));
167 nsresult ChromeObserver::HideWindowChrome(bool aShouldHide) {
168 // only top level chrome documents can hide the window chrome
169 if (!mDocument->IsRootDisplayDocument()) return NS_OK;
171 nsPresContext* presContext = mDocument->GetPresContext();
173 if (presContext && presContext->IsChrome()) {
174 nsIFrame* frame = mDocument->GetDocumentElement()->GetPrimaryFrame();
176 if (frame) {
177 nsView* view = frame->GetClosestView();
179 if (view) {
180 nsIWidget* w = view->GetWidget();
181 NS_ENSURE_STATE(w);
182 w->HideWindowChrome(aShouldHide);
187 return NS_OK;
190 } // namespace mozilla::dom