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
12 #include "nsIRunnable.h"
13 #include "nsIWeakReferenceUtils.h"
14 #include "nsStringFwd.h"
15 #include "nsThreadUtils.h"
17 namespace mozilla::dom
{
21 // https://html.spec.whatwg.org/#attr-popover
22 enum class PopoverAttributeState
: uint8_t {
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 {
33 class PopoverToggleEventTask
: public Runnable
{
35 explicit PopoverToggleEventTask(nsWeakPtr aElement
,
36 PopoverVisibilityState aOldState
);
38 // MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is MOZ_CAN_RUN_SCRIPT. See
40 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD
Run() override
;
42 PopoverVisibilityState
GetOldState() const { return mOldState
; }
46 PopoverVisibilityState mOldState
;
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
) {
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
) {
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
;
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
113 RefPtr
<CloseWatcher
> mCloseWatcher
;
115 } // namespace mozilla::dom