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/. */
5 #ifndef nsAutoRollup_h__
6 #define nsAutoRollup_h__
7 
8 #include "mozilla/Attributes.h"  // for MOZ_RAII
9 #include "mozilla/StaticPtr.h"   // for StaticRefPtr
10 #include "nsIContent.h"
11 
12 namespace mozilla {
13 namespace widget {
14 
15 // A situation can occur when a mouse event occurs over a menu label while the
16 // menu popup is already open. The expected behaviour is to close the popup.
17 // This happens by calling nsIRollupListener::Rollup before the mouse event is
18 // processed. However, in cases where the mouse event is not consumed, this
19 // event will then get targeted at the menu label causing the menu to open
20 // again. To prevent this, we store in sLastRollup a reference to the popup
21 // that was closed during the Rollup call, and prevent this popup from
22 // reopening while processing the mouse event.
23 // sLastRollup can only be set while an nsAutoRollup is in scope;
24 // when it goes out of scope sLastRollup is cleared automatically.
25 // As sLastRollup is static, it can be retrieved by calling
26 // nsAutoRollup::GetLastRollup.
27 class MOZ_RAII nsAutoRollup {
28  public:
29   nsAutoRollup();
30   ~nsAutoRollup();
31 
32   // Convenience constructor that creates a nsAutoRollup and also sets
33   // the last rollup.
34   explicit nsAutoRollup(nsIContent* aRollup);
35 
36   static void SetLastRollup(nsIContent* aLastRollup);
37   // Return the popup that was last rolled up, or null if there isn't one.
38   static nsIContent* GetLastRollup();
39 
40  private:
41   // Whether sLastRollup was clear when this nsAutoRollup
42   // was created.
43   bool mWasClear;
44 
45   // The number of nsAutoRollup instances active.
46   static uint32_t sCount;
47   // The last rolled up popup.
48   static StaticRefPtr<nsIContent> sLastRollup;
49 };
50 
51 }  // namespace widget
52 }  // namespace mozilla
53 
54 #endif  // nsAutoRollup_h__
55