Services overlay flashes a lot; should have some hysteresis before showing overlay
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / ServicesOverlayController.h
1 /*
2  * Copyright (C) 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef ServicesOverlayController_h
27 #define ServicesOverlayController_h
28
29 #if ENABLE(SERVICE_CONTROLS) || ENABLE(TELEPHONE_NUMBER_DETECTION)
30
31 #include "PageOverlay.h"
32 #include <WebCore/Range.h>
33 #include <WebCore/Timer.h>
34
35 typedef void* DDHighlightRef;
36
37 namespace WebCore {
38 class LayoutRect;
39
40 struct GapRects;
41 }
42
43 namespace WebKit {
44
45 class WebPage;
46
47 typedef void* DDHighlightRef;
48
49 struct TelephoneNumberData {
50     TelephoneNumberData(RetainPtr<DDHighlightRef> highlight, PassRefPtr<WebCore::Range> range)
51         : highlight(highlight)
52         , range(range)
53     {
54     }
55
56     RetainPtr<DDHighlightRef> highlight;
57     RefPtr<WebCore::Range> range;
58 };
59
60 class ServicesOverlayController : private PageOverlay::Client {
61 public:
62     ServicesOverlayController(WebPage&);
63     ~ServicesOverlayController();
64
65     void selectedTelephoneNumberRangesChanged(const Vector<RefPtr<WebCore::Range>>&);
66     void selectionRectsDidChange(const Vector<WebCore::LayoutRect>&, const Vector<WebCore::GapRects>&);
67
68 private:
69     void createOverlayIfNeeded();
70     void handleClick(const WebCore::IntPoint&, DDHighlightRef);
71     void clearHighlightState();
72
73     virtual void pageOverlayDestroyed(PageOverlay*) override;
74     virtual void willMoveToWebPage(PageOverlay*, WebPage*) override;
75     virtual void didMoveToWebPage(PageOverlay*, WebPage*) override;
76     virtual void drawRect(PageOverlay*, WebCore::GraphicsContext&, const WebCore::IntRect& dirtyRect) override;
77     virtual bool mouseEvent(PageOverlay*, const WebMouseEvent&) override;
78
79     bool drawTelephoneNumberHighlightIfVisible(WebCore::GraphicsContext&, const WebCore::IntRect& dirtyRect);
80     void drawSelectionHighlight(WebCore::GraphicsContext&, const WebCore::IntRect& dirtyRect);
81     void drawHighlight(DDHighlightRef, WebCore::GraphicsContext&);
82
83     void establishHoveredTelephoneHighlight(bool& mouseIsOverButton);
84     void maybeCreateSelectionHighlight();
85
86     void clearSelectionHighlight();
87     void clearHoveredTelephoneNumberHighlight();
88
89     bool mouseIsOverHighlight(DDHighlightRef, bool& mouseIsOverButton) const;
90     std::chrono::milliseconds remainingTimeUntilHighlightShouldBeShown() const;
91     void repaintHighlightTimerFired(WebCore::Timer<ServicesOverlayController>&);
92
93     WebPage* m_webPage;
94     PageOverlay* m_servicesOverlay;
95     
96     Vector<WebCore::LayoutRect> m_currentSelectionRects;
97     RetainPtr<DDHighlightRef> m_selectionHighlight;
98     std::chrono::steady_clock::time_point m_lastSelectionChangeTime;
99     std::chrono::steady_clock::time_point m_lastHoveredHighlightChangeTime;
100
101     Vector<RefPtr<WebCore::Range>> m_currentTelephoneNumberRanges;
102     Vector<RetainPtr<DDHighlightRef>> m_telephoneNumberHighlights;
103     std::unique_ptr<TelephoneNumberData> m_hoveredTelephoneNumberData;
104
105     RetainPtr<DDHighlightRef> m_currentHoveredHighlight;
106     RetainPtr<DDHighlightRef> m_currentMouseDownOnButtonHighlight;
107
108     WebCore::Timer<ServicesOverlayController> m_repaintHighlightTimer;
109
110     WebCore::IntPoint m_mousePosition;
111 };
112
113 } // namespace WebKit
114
115 #endif // ENABLE(SERVICE_CONTROLS) && ENABLE(TELEPHONE_NUMBER_DETECTION)
116 #endif // ServicesOverlayController_h