forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tooltip.c
152 lines (120 loc) · 3.07 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
#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;
}
}
void tooltip_reset(void)
{
TOOLTIP *b = &tooltip;
b->visible = 0;
b->can_show = 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;
return 1;
}
_Bool tooltip_mdown(void)
{
TOOLTIP *b = &tooltip;
b->can_show = 0;
b->mouse_down = 1;
b->visible = 0;
return 0;
}
_Bool tooltip_mup(void)
{
TOOLTIP *b = &tooltip;
b->can_show = 0;
b->mouse_down = 0;
return 0;
}
void tooltip_show(void)
{
TOOLTIP *b = &tooltip;
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;
// Only needed for Xlib to unblock XNextEvent
force_redraw();
}
// This is being called everytime 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->timer_running || b->visible || b->mouse_down) {
return;
}
b->timer_running = 1;
thread(mouse_pos_check,NULL);
}
void mouse_pos_check(void *UNUSED(args))
{
TOOLTIP *b = &tooltip;
int old_x = 0, old_y = 0, tick = 0;
while(1) {
if(mouse.x - old_x == 0 && mouse.y - old_y == 0) {
if(tick >= 5) {
// ~500ms of not moving, show tooltip
if(b->can_show && !b->mouse_down) {
tooltip_show();
}
break;
} else {
tick++;
}
//debug("Mouse stopped. X: %d, Y: %d, OX: %d, OY: %d\n",mouse.x,mouse.y,old_x,old_y);
} else {
old_x = mouse.x;
old_y = mouse.y;
tick = 0;
}
yieldcpu(100);
}
tick = 0;
b->timer_running = 0;
}