-
Notifications
You must be signed in to change notification settings - Fork 149
/
tooltip.c
188 lines (139 loc) · 3.29 KB
/
tooltip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "main.h"
static TOOLTIP tooltip;
#define TOOLTIP_WIDTH (SCALE * 12)
#define TOOLTIP_HEIGHT (SCALE * 12)
#define TOOLTIP_YOFFSET 12
static void calculate_pos_and_width(TOOLTIP *b, int *x, int *w) {
*x = b->x;
*w = b->width;
// Increase width if needed, so that tooltip text fits.
if(maybe_i18nal_string_is_valid(b->tt_text)) {
STRING* s = maybe_i18nal_string_get(b->tt_text);
int needed_w = textwidth(s->str, s->length) + 4 * SCALE;
if(*w < needed_w) {
*w = needed_w;
}
}
// Push away from the right border to fit.
if(*x + *w >= utox_window_width) {
*x -= *w;
}
}
volatile _Bool kill_thread;
void tooltip_reset(void)
{
TOOLTIP *b = &tooltip;
b->visible = 0;
b->can_show = 0;
if (b->thread) {
kill_thread = 1;
b->thread = 0;
}
}
void tooltip_draw(void)
{
TOOLTIP *b = &tooltip;
if(!b->visible) {
return;
}
// Ensure that font is set before calculating position and width.
setfont(FONT_TEXT);
setcolor(COLOR_MAIN_TEXT);
int x, w;
calculate_pos_and_width(b, &x, &w);
drawrectw(x, b->y, w, b->height, COLOR_MAIN_BACKGROUND);
STRING* s = maybe_i18nal_string_get(b->tt_text);
drawtext(x + SCALE * 2, b->y + SCALE * 2, s->str, s->length);
framerect(x, b->y, x + w, b->y + b->height, COLOR_EDGE_NORMAL);
}
_Bool tooltip_mmove(void)
{
TOOLTIP *b = &tooltip;
b->can_show = 0;
if(!b->visible) {
return 0;
}
b->visible = 0;
if (b->thread) {
kill_thread = 1;
b->thread = 0;
}
return 1;
}
_Bool tooltip_mdown(void)
{
TOOLTIP *b = &tooltip;
b->can_show = 0;
b->mouse_down = 1;
b->visible = 0;
if (b->thread) {
kill_thread = 1;
b->thread = 0;
}
return 0;
}
_Bool tooltip_mup(void)
{
TOOLTIP *b = &tooltip;
b->can_show = 0;
b->mouse_down = 0;
if (b->thread) {
kill_thread = 1;
b->thread = 0;
}
return 0;
}
void tooltip_show(void)
{
TOOLTIP *b = &tooltip;
if (!b->can_show)
return;
b->y = mouse.y + TOOLTIP_YOFFSET;
b->height = TOOLTIP_HEIGHT;
if(b->y + b->height >= utox_window_height) {
b->y -= (b->height + TOOLTIP_YOFFSET);
}
b->x = mouse.x;
b->width = TOOLTIP_WIDTH;
b->visible = 1;
if (b->thread) {
kill_thread = 1;
b->thread = 0;
}
}
volatile _Bool reset_time;
static void tooltip_thread(void *UNUSED(args))
{
uint64_t last_move_time = ~0;
while (1) {
if (kill_thread) {
break;
}
TOOLTIP *b = &tooltip;
if (reset_time) {
last_move_time = get_time() + 500 * 1000 * 1000;
reset_time = 0;
}
if (get_time() > last_move_time) {
postmessage(TOOLTIP_SHOW, 0, 0, NULL);
last_move_time = ~0;
}
yieldcpu(100);
}
kill_thread = 0;
}
// This is being called every time the mouse is moving above a button
void tooltip_new(MAYBE_I18NAL_STRING* text)
{
TOOLTIP *b = &tooltip;
b->can_show = 1;
b->tt_text = text;
if(b->visible || b->mouse_down) {
return;
}
if (!b->thread && !kill_thread) {
thread(tooltip_thread, NULL);
b->thread = 1;
}
reset_time = 1;
}