1 | /* |
2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
3 | * Copyright (C) 2013-2017 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
7 | * met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Neither the name of Google Inc. nor the names of its |
12 | * contributors may be used to endorse or promote products derived from |
13 | * this software without specific prior written permission. |
14 | * |
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
18 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
19 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
21 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | #pragma once |
29 | |
30 | #include <wtf/Forward.h> |
31 | #include <wtf/HashMap.h> |
32 | #include <wtf/OptionSet.h> |
33 | #include <wtf/Ref.h> |
34 | |
35 | namespace WebCore { |
36 | |
37 | class Element; |
38 | |
39 | class UserActionElementSet { |
40 | public: |
41 | bool isActive(const Element& element) { return hasFlag(element, Flag::IsActive); } |
42 | bool isFocused(const Element& element) { return hasFlag(element, Flag::IsFocused); } |
43 | bool isHovered(const Element& element) { return hasFlag(element, Flag::IsHovered); } |
44 | bool isInActiveChain(const Element& element) { return hasFlag(element, Flag::InActiveChain); } |
45 | |
46 | void setActive(Element& element, bool enable) { setFlags(element, enable, Flag::IsActive); } |
47 | void setFocused(Element& element, bool enable) { setFlags(element, enable, Flag::IsFocused); } |
48 | void setHovered(Element& element, bool enable) { setFlags(element, enable, Flag::IsHovered); } |
49 | void setInActiveChain(Element& element, bool enable) { setFlags(element, enable, Flag::InActiveChain); } |
50 | |
51 | void clearActiveAndHovered(Element& element) { clearFlags(element, { Flag::IsActive, Flag::InActiveChain, Flag::IsHovered }); } |
52 | |
53 | void clear(); |
54 | |
55 | private: |
56 | enum class Flag { |
57 | IsActive = 1 << 0, |
58 | InActiveChain = 1 << 1, |
59 | IsHovered = 1 << 2, |
60 | IsFocused = 1 << 3 |
61 | }; |
62 | |
63 | void setFlags(Element& element, bool enable, OptionSet<Flag> flags) { enable ? setFlags(element, flags) : clearFlags(element, flags); } |
64 | void setFlags(Element&, OptionSet<Flag>); |
65 | void clearFlags(Element&, OptionSet<Flag>); |
66 | bool hasFlag(const Element&, Flag) const; |
67 | |
68 | HashMap<RefPtr<Element>, OptionSet<Flag>> m_elements; |
69 | }; |
70 | |
71 | } // namespace WebCore |
72 | |