7
7
#define ELEMENTS_TOOLTIP_AUGUST_27_2020
8
8
9
9
#include < elements/element/proxy.hpp>
10
+ #include < elements/element/popup.hpp>
10
11
#include < infra/support.hpp>
11
12
#include < functional>
12
13
@@ -15,94 +16,40 @@ namespace cycfi { namespace elements
15
16
// //////////////////////////////////////////////////////////////////////////
16
17
// Tooltip elements
17
18
// //////////////////////////////////////////////////////////////////////////
18
- template <typename Subject, typename Tip>
19
- class tooltip_element : public proxy <Subject>
19
+ class tooltip_element : public proxy_base
20
20
{
21
21
public:
22
22
23
- using base_type = proxy<Subject>;
23
+ using base_type = proxy_base;
24
+ using on_hover_function = std::function<void (bool visible)>;
24
25
25
- tooltip_element (Subject subject, Tip tip, duration delay)
26
- : base_type(std::move(subject) )
27
- , _tip(std::move (tip))
26
+ template < typename Tip>
27
+ tooltip_element (Tip&& tip, duration delay )
28
+ : _tip(share(basic_popup( std::forward<Tip> (tip)) ))
28
29
, _delay(delay)
29
30
{}
30
31
31
- void draw (context const & ctx) override ;
32
32
bool cursor (context const & ctx, point p, cursor_tracking status) override ;
33
-
34
- Subject const & tip () const { return _tip; }
35
- Subject& tip () { return _tip; }
36
-
37
- using on_hover_function = std::function<void (bool visible)>;
33
+ bool key (context const & ctx, key_info k) override ;
38
34
39
35
on_hover_function on_hover = [](bool ){};
40
36
41
37
private:
42
38
43
- rect tip_bounds (context const & ctx) const ;
44
-
39
+ using popup_ptr = std::shared_ptr<basic_popup_element>;
45
40
enum status { tip_hidden, tip_delayed, tip_visible };
46
41
47
- Tip _tip;
42
+ rect tip_bounds (context const & ctx) const ;
43
+ void close_tip (view& view_);
44
+
45
+ popup_ptr _tip;
48
46
status _tip_status = tip_hidden;
49
47
duration _delay;
48
+ bool _cursor_in_tip = false ;
50
49
};
51
50
52
51
template <typename Subject, typename Tip>
53
- inline rect tooltip_element<Subject, Tip>::tip_bounds(context const & ctx) const
54
- {
55
- auto limits_ = _tip.limits (ctx);
56
- auto w = limits_.min .x ;
57
- auto h = limits_.min .y ;
58
- return rect{ 0 , 0 , w, h }.move_to (ctx.bounds .left , ctx.bounds .top -h);
59
- }
60
-
61
- template <typename Subject, typename Tip>
62
- inline void tooltip_element<Subject, Tip>::draw(context const & ctx)
63
- {
64
- base_type::draw (ctx);
65
- if (_tip_status == tip_visible)
66
- {
67
- context tctx { ctx, &_tip, tip_bounds (ctx) };
68
- _tip.draw (tctx);
69
- }
70
- }
71
-
72
- template <typename Subject, typename Tip>
73
- inline bool tooltip_element<Subject, Tip>::cursor(context const & ctx, point p, cursor_tracking status)
74
- {
75
- if (status != cursor_tracking::leaving)
76
- {
77
- if (_tip_status != tip_visible)
78
- {
79
- _tip_status = tip_delayed;
80
- auto refresh_rect = max (ctx.bounds , tip_bounds (ctx));
81
- ctx.view .post (std::chrono::duration_cast<std::chrono::milliseconds>(_delay),
82
- [this , &view = ctx.view , refresh_rect]()
83
- {
84
- if (_tip_status == tip_delayed)
85
- {
86
- on_hover (true );
87
- _tip_status = tip_visible;
88
- view.refresh (refresh_rect);
89
- }
90
- }
91
- );
92
- }
93
- }
94
- else
95
- {
96
- _tip_status = tip_hidden;
97
- on_hover (false );
98
- ctx.view .refresh (max (ctx.bounds , tip_bounds (ctx)));
99
- }
100
-
101
- return base_type::cursor (ctx, p, status);
102
- }
103
-
104
- template <typename Subject, typename Tip>
105
- inline tooltip_element<remove_cvref_t <Subject>, remove_cvref_t <Tip>>
52
+ inline proxy<remove_cvref_t <Subject>, tooltip_element>
106
53
tooltip (Subject&& subject, Tip&& tip, duration delay = milliseconds{ 500 })
107
54
{
108
55
return { std::forward<Subject>(subject), std::forward<Tip>(tip), delay };
0 commit comments