Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / base / PopoverData.h
blob54248ad65d0071b66859039d08ef76eb0793fa36
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 http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_PopoverData_h
8 #define mozilla_dom_PopoverData_h
10 #include "Element.h"
11 #include "nsINode.h"
12 #include "nsIRunnable.h"
13 #include "nsIWeakReferenceUtils.h"
14 #include "nsStringFwd.h"
15 #include "nsThreadUtils.h"
17 namespace mozilla::dom {
19 class CloseWatcher;
21 // https://html.spec.whatwg.org/#attr-popover
22 enum class PopoverAttributeState : uint8_t {
23 None,
24 Auto, ///< https://html.spec.whatwg.org/#attr-popover-auto-state
25 Manual, ///< https://html.spec.whatwg.org/#attr-popover-manual-state
28 enum class PopoverVisibilityState : uint8_t {
29 Hidden,
30 Showing,
33 class PopoverToggleEventTask : public Runnable {
34 public:
35 explicit PopoverToggleEventTask(nsWeakPtr aElement,
36 PopoverVisibilityState aOldState);
38 // MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is MOZ_CAN_RUN_SCRIPT. See
39 // bug 1535398.
40 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() override;
42 PopoverVisibilityState GetOldState() const { return mOldState; }
44 private:
45 nsWeakPtr mElement;
46 PopoverVisibilityState mOldState;
49 class PopoverData {
50 public:
51 PopoverData() = default;
52 ~PopoverData() = default;
54 CloseWatcher& EnsureCloseWatcher(nsGenericHTMLElement* aElement);
55 CloseWatcher* GetCloseWatcher();
57 PopoverAttributeState GetPopoverAttributeState() const { return mState; }
58 void SetPopoverAttributeState(PopoverAttributeState aState) {
59 mState = aState;
62 PopoverVisibilityState GetPopoverVisibilityState() const {
63 return mVisibilityState;
65 void SetPopoverVisibilityState(PopoverVisibilityState aVisibilityState) {
66 mVisibilityState = aVisibilityState;
69 nsWeakPtr GetPreviouslyFocusedElement() const {
70 return mPreviouslyFocusedElement;
72 void SetPreviouslyFocusedElement(nsWeakPtr aPreviouslyFocusedElement) {
73 mPreviouslyFocusedElement = aPreviouslyFocusedElement;
76 RefPtr<Element> GetInvoker() const {
77 return do_QueryReferent(mInvokerElement);
79 void SetInvoker(Element* aInvokerElement) {
80 mInvokerElement =
81 do_GetWeakReference(static_cast<nsINode*>(aInvokerElement));
84 PopoverToggleEventTask* GetToggleEventTask() const { return mTask; }
85 void SetToggleEventTask(PopoverToggleEventTask* aTask) { mTask = aTask; }
86 void ClearToggleEventTask() { mTask = nullptr; }
88 bool IsShowingOrHiding() const { return mIsShowingOrHiding; }
89 void SetIsShowingOrHiding(bool aIsShowingOrHiding) {
90 mIsShowingOrHiding = aIsShowingOrHiding;
93 private:
94 PopoverVisibilityState mVisibilityState = PopoverVisibilityState::Hidden;
95 PopoverAttributeState mState = PopoverAttributeState::None;
96 // Popover and dialog don't share mPreviouslyFocusedElement for there are
97 // chances to lose the previously focused element.
98 // See, https://github.com/whatwg/html/issues/9063
99 nsWeakPtr mPreviouslyFocusedElement = nullptr;
101 // https://html.spec.whatwg.org/#popover-invoker
102 // Since having a popover invoker only makes a difference if the invoker
103 // is in the document (in another open popover to be precise) we can make
104 // this a weak reference, as if the element goes away it's necessarily not
105 // connected to our document.
106 nsWeakPtr mInvokerElement;
107 bool mIsShowingOrHiding = false;
108 RefPtr<PopoverToggleEventTask> mTask;
110 // This won't need to be cycle collected as CloseWatcher only has strong
111 // references to event listeners, which themselves have Weak References back
112 // to the Node.
113 RefPtr<CloseWatcher> mCloseWatcher;
115 } // namespace mozilla::dom
117 #endif